Skip to content

Commit

Permalink
Merge pull request #279 from magento-fearless-kiwis/develop
Browse files Browse the repository at this point in the history
[FearlessKiwis] Support for Different Product Types in Catalog Product Data Object
  • Loading branch information
Tang, Yu(ytang1) committed May 7, 2015
2 parents a7cb0c2 + 428d6f7 commit 4f25bdd
Show file tree
Hide file tree
Showing 183 changed files with 7,891 additions and 2,563 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Backend/Model/Session/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getQuote()
$this->_quote->setStoreId($this->getStoreId());
}

if ($this->getCustomerId()) {
if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
$customer = $this->customerRepository->getById($this->getCustomerId());
$this->_quote->assignCustomer($customer);
}
Expand Down
157 changes: 88 additions & 69 deletions app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,42 @@ protected function setUp()
*
* @return void
*/
public function testGetQuote()
public function testGetQuoteWithoutQuoteId()
{
$storeId = 10;
$quoteId = 22;
$customerGroupId = 77;
$storeId = 10;
$customerId = 66;
$customerGroupId = 77;

$this->quote->expects($this->any())
->method('getQuoteId')
->will($this->returnValue(null));
$this->quote->expects($this->any())
->method('setQuoteId')
->with($quoteId);
$this->quote->expects($this->any())
->method('getStoreId')
->will($this->returnValue($storeId));
$this->quote->expects($this->any())
->method('getCustomerId')
->will($this->returnValue($customerId));

$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
->getMock();
$defaultGroup->expects($this->any())
->method('getId')
->will($this->returnValue($customerGroupId));
$this->groupManagementMock->expects($this->any())
->method('getDefaultGroup')
->will($this->returnValue($defaultGroup));

$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId)
->willReturn($dataCustomerMock);

$quoteMock = $this->getMock(
'Magento\Quote\Model\Quote',
Expand All @@ -248,28 +278,9 @@ public function testGetQuote()
'',
false
);

$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
->getMock();
$defaultGroup->expects($this->any())
->method('getId')
->will($this->returnValue($customerGroupId));
$this->groupManagementMock->expects($this->any())
->method('getDefaultGroup')
->will($this->returnValue($defaultGroup));

$this->quoteRepositoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quote->expects($this->any())
->method('getStoreId')
->will($this->returnValue($storeId));
$quoteMock->expects($this->once())
->method('setStoreId')
->with($storeId);
$this->quote->expects($this->any())
->method('getQuoteId')
->will($this->returnValue(null));
$quoteMock->expects($this->once())
->method('setCustomerGroupId')
->with($customerGroupId)
Expand All @@ -278,25 +289,9 @@ public function testGetQuote()
->method('setIsActive')
->with(false)
->will($this->returnSelf());
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($quoteMock);
$quoteMock->expects($this->once())
->method('getId')
->will($this->returnValue($quoteId));
$this->quote->expects($this->any())
->method('setQuoteId')
->with($quoteId);
$this->quote->expects($this->any())
->method('getCustomerId')
->will($this->returnValue($customerId));
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId)
->willReturn($dataCustomerMock);
$quoteMock->expects($this->once())
->method('assignCustomer')
->with($dataCustomerMock);
Expand All @@ -307,19 +302,47 @@ public function testGetQuote()
->method('setIsSuperMode')
->with(true);

$this->quoteRepositoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($quoteMock);

$this->assertEquals($quoteMock, $this->quote->getQuote());
}

/**
* Run test getQuote method
*
* @return void
* @dataProvider getQuoteDataProvider
*/
public function testGetQuoteGet()
public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes)
{
$storeId = 10;
$quoteId = 22;
$customerId = 66;
$storeId = 10;

$this->quote->expects($this->any())
->method('getQuoteId')
->will($this->returnValue($quoteId));
$this->quote->expects($this->any())
->method('setQuoteId')
->with($quoteId);
$this->quote->expects($this->any())
->method('getStoreId')
->will($this->returnValue($storeId));
$this->quote->expects($this->any())
->method('getCustomerId')
->will($this->returnValue($customerId));

$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock->expects($this->$expectedNumberOfInvokes())
->method('getById')
->with($customerId)
->willReturn($dataCustomerMock);

$quoteMock = $this->getMock(
'Magento\Quote\Model\Quote',
Expand All @@ -331,43 +354,17 @@ public function testGetQuoteGet()
'assignCustomer',
'setIgnoreOldQty',
'setIsSuperMode',
'getCustomerId',
'__wakeup'
],
[],
'',
false
);

$this->quoteRepositoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quote->expects($this->any())
->method('getStoreId')
->will($this->returnValue($storeId));
$quoteMock->expects($this->once())
->method('setStoreId')
->with($storeId);
$this->quote->expects($this->any())
->method('getQuoteId')
->will($this->returnValue($quoteId));
$this->quoteRepositoryMock->expects($this->once())
->method('get')
->with($quoteId)
->willReturn($quoteMock);
$this->quote->expects($this->any())
->method('setQuoteId')
->with($quoteId);
$this->quote->expects($this->any())
->method('getCustomerId')
->will($this->returnValue($customerId));
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
->disableOriginalConstructor()
->getMock();
$this->customerRepositoryMock->expects($this->once())
->method('getById')
->with($customerId)
->willReturn($dataCustomerMock);
$quoteMock->expects($this->once())
$quoteMock->expects($this->$expectedNumberOfInvokes())
->method('assignCustomer')
->with($dataCustomerMock);
$quoteMock->expects($this->once())
Expand All @@ -376,7 +373,29 @@ public function testGetQuoteGet()
$quoteMock->expects($this->once())
->method('setIsSuperMode')
->with(true);
$quoteMock->expects($this->once())
->method('getCustomerId')
->will($this->returnValue($quoteCustomerId));

$this->quoteRepositoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quoteRepositoryMock->expects($this->once())
->method('get')
->with($quoteId)
->willReturn($quoteMock);

$this->assertEquals($quoteMock, $this->quote->getQuote());
}

/**
* @return array
*/
public function getQuoteDataProvider()
{
return [
'customer ids different' => [66, null, 'once'],
'customer ids same' => [66, 66, 'never'],
];
}
}
17 changes: 14 additions & 3 deletions app/code/Magento/Catalog/Block/Product/View/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,30 @@ public function getJsonConfig()
/* @var $option \Magento\Catalog\Model\Product\Option */
$priceValue = 0;
if ($option->getGroupByType() == \Magento\Catalog\Model\Product\Option::OPTION_GROUP_SELECT) {
$_tmpPriceValues = [];
$tmpPriceValues = [];
foreach ($option->getValues() as $value) {
/* @var $value \Magento\Catalog\Model\Product\Option\Value */
$id = $value->getId();
$_tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
$tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
}
$priceValue = $_tmpPriceValues;
$priceValue = $tmpPriceValues;
} else {
$priceValue = $this->_getPriceConfiguration($option);
}
$config[$option->getId()] = $priceValue;
}

$configObj = new \Magento\Framework\Object(
[
'config' => $config,
]
);

//pass the return array encapsulated in an object for the other modules to be able to alter it eg: weee
$this->_eventManager->dispatch('catalog_product_option_price_configuration_after', ['configObj' => $configObj]);

$config=$configObj->getConfig();

return $this->_jsonEncoder->encode($config);
}

Expand Down
67 changes: 47 additions & 20 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,25 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
protected $metadataService;

/*
* @param \Magento\Catalog\Model\ProductLink\ProductLinkManagementInterface
* @param \Magento\Catalog\Model\ProductLink\CollectionProvider
*/
protected $linkManagement;
protected $entityCollectionProvider;

/*
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
* @param \Magento\Catalog\Model\Product\LinkTypeProvider
*/
protected $linkProvider;

/*
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory
*/
protected $productLinkFactory;

/*
* @param \Magento\Catalog\Api\Data\ProductLinkExtensionFactory
*/
protected $productLinkExtensionFactory;

/**
* @var \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory
*/
Expand Down Expand Up @@ -318,8 +328,10 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
* @param Indexer\Product\Eav\Processor $productEavIndexerProcessor
* @param CategoryRepositoryInterface $categoryRepository
* @param Product\Image\CacheFactory $imageCacheFactory
* @param \Magento\Catalog\Model\ProductLink\Management $linkManagement
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
* @param \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
* @param \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
* @param array $data
Expand Down Expand Up @@ -354,8 +366,10 @@ public function __construct(
\Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
CategoryRepositoryInterface $categoryRepository,
Product\Image\CacheFactory $imageCacheFactory,
\Magento\Catalog\Model\ProductLink\Management $linkManagement,
\Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
\Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
array $data = []
Expand All @@ -380,8 +394,10 @@ public function __construct(
$this->_productEavIndexerProcessor = $productEavIndexerProcessor;
$this->categoryRepository = $categoryRepository;
$this->imageCacheFactory = $imageCacheFactory;
$this->linkManagement = $linkManagement;
$this->entityCollectionProvider = $entityCollectionProvider;
$this->linkTypeProvider = $linkTypeProvider;
$this->productLinkFactory = $productLinkFactory;
$this->productLinkExtensionFactory = $productLinkExtensionFactory;
$this->mediaGalleryEntryFactory = $mediaGalleryEntryFactory;
$this->dataObjectHelper = $dataObjectHelper;
parent::__construct(
Expand Down Expand Up @@ -1352,23 +1368,34 @@ public function getCrossSellLinkCollection()
public function getProductLinks()
{
if (empty($this->_links)) {
$productLinks = [];

$productLinks['related'] = $this->getRelatedProducts();
$productLinks['upsell'] = $this->getUpSellProducts();
$productLinks['crosssell'] = $this->getCrossSellProducts();

$output = [];
foreach ($productLinks as $type => $linkTypeArray) {
foreach ($linkTypeArray as $link) {
$linkTypes = $this->linkTypeProvider->getLinkTypes();
foreach (array_keys($linkTypes) as $linkTypeName) {
$collection = $this->entityCollectionProvider->getCollection($this, $linkTypeName);
foreach ($collection as $item) {
/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
$productLink = $this->productLinkFactory->create();
$productLink->setProductSku($this->getSku())
->setLinkType($type)
->setLinkedProductSku($link['sku'])
->setLinkedProductType($link['type_id'])
->setPosition($link['position']);

->setLinkType($linkTypeName)
->setLinkedProductSku($item['sku'])
->setLinkedProductType($item['type'])
->setPosition($item['position']);
if (isset($item['custom_attributes'])) {
$productLinkExtension = $productLink->getExtensionAttributes();
if ($productLinkExtension === null) {
$productLinkExtension = $this->productLinkExtensionFactory->create();
}
foreach ($item['custom_attributes'] as $option) {
$name = $option['attribute_code'];
$value = $option['value'];
$setterName = 'set' . ucfirst($name);
// Check if setter exists
if (method_exists($productLinkExtension, $setterName)) {
call_user_func([$productLinkExtension, $setterName], $value);
}
}
$productLink->setExtensionAttributes($productLinkExtension);
}
$output[] = $productLink;
}
}
Expand Down
Loading

0 comments on commit 4f25bdd

Please sign in to comment.