Skip to content

Commit 0334cf3

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-59726' into 2.1-develop-pr45
2 parents b72d6f4 + c03a29a commit 0334cf3

File tree

3 files changed

+111
-75
lines changed

3 files changed

+111
-75
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
401401
$linksToInitialize = [];
402402
foreach ($linksByType as $link) {
403403
$linkDataArray = $this->extensibleDataObjectConverter
404-
->toNestedArray($link, [], 'Magento\Catalog\Api\Data\ProductLinkInterface');
404+
->toNestedArray($link, [], \Magento\Catalog\Api\Data\ProductLinkInterface::class);
405405
$linkedSku = $link->getLinkedProductSku();
406406
if (!isset($linkedProductIds[$linkedSku])) {
407407
throw new NoSuchEntityException(
@@ -518,7 +518,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
518518
}
519519

520520
$productDataArray = $this->extensibleDataObjectConverter
521-
->toNestedArray($product, [], 'Magento\Catalog\Api\Data\ProductInterface');
521+
->toNestedArray($product, [], \Magento\Catalog\Api\Data\ProductInterface::class);
522522
$productDataArray = array_replace($productDataArray, $product->getData());
523523
$ignoreLinksFlag = $product->getData('ignore_links_flag');
524524
$productLinks = null;
@@ -631,6 +631,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
631631
$collection->setCurPage($searchCriteria->getCurrentPage());
632632
$collection->setPageSize($searchCriteria->getPageSize());
633633
$collection->load();
634+
$collection->addCategoryIds();
634635

635636
$searchResult = $this->searchResultsFactory->create();
636637
$searchResult->setSearchCriteria($searchCriteria);
@@ -717,7 +718,7 @@ private function getMediaGalleryProcessor()
717718
{
718719
if (null === $this->mediaGalleryProcessor) {
719720
$this->mediaGalleryProcessor = \Magento\Framework\App\ObjectManager::getInstance()
720-
->get('Magento\Catalog\Model\Product\Gallery\Processor');
721+
->get(\Magento\Catalog\Model\Product\Gallery\Processor::class);
721722
}
722723
return $this->mediaGalleryProcessor;
723724
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 95 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
namespace Magento\Catalog\Test\Unit\Model;
1111

12+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
13+
use Magento\Catalog\Api\Data\ProductAttributeSearchResultsInterface;
14+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1215
use Magento\Framework\Api\Data\ImageContentInterface;
16+
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1317
use Magento\Framework\Api\SortOrder;
1418
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1519

@@ -153,6 +157,11 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
153157
*/
154158
private $optionConverterMock;
155159

160+
/**
161+
* @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
162+
*/
163+
private $extensionAttributesJoinProcessor;
164+
156165
/**
157166
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
158167
*/
@@ -322,6 +331,9 @@ protected function setUp()
322331
$this->optionConverterMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option\Converter::class)
323332
->disableOriginalConstructor()
324333
->getMock();
334+
$this->extensionAttributesJoinProcessor = $this->getMockBuilder(JoinProcessorInterface::class)
335+
->disableOriginalConstructor()
336+
->getMock();
325337

326338
$this->model = $this->objectManager->getObject(
327339
\Magento\Catalog\Model\ProductRepository::class,
@@ -347,6 +359,7 @@ protected function setUp()
347359
'imageProcessor' => $this->imageProcessorMock,
348360
'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
349361
'mediaGalleryProcessor' => $this->mediaGalleryProcessor,
362+
'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessor,
350363
]
351364
);
352365
}
@@ -671,85 +684,95 @@ public function testDeleteById()
671684
*/
672685
public function testGetList($fieldName)
673686
{
674-
$searchCriteriaMock = $this->getMock(\Magento\Framework\Api\SearchCriteriaInterface::class, [], [], '', false);
675687
$attributeCode = 'attribute_code';
676-
$collectionMock = $this->getMock(
677-
\Magento\Catalog\Model\ResourceModel\Product\Collection::class,
678-
[],
679-
[],
680-
'',
681-
false
682-
);
683-
$extendedSearchCriteriaMock = $this->getMock(\Magento\Framework\Api\SearchCriteria::class, [], [], '', false);
684-
$productAttributeSearchResultsMock = $this->getMock(
685-
\Magento\Framework\Api\SearchResults::class,
686-
[],
687-
[],
688-
'',
689-
false
690-
);
691-
$productAttributeMock = $this->getMock(
692-
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
693-
[],
694-
[],
695-
'',
696-
false
697-
);
698-
$filterGroupMock = $this->getMock(\Magento\Framework\Api\Search\FilterGroup::class, [], [], '', false);
699-
$filterGroupFilterMock = $this->getMock(\Magento\Framework\Api\Filter::class, [], [], '', false);
700-
$sortOrderMock = $this->getMock(\Magento\Framework\Api\SortOrder::class, [], [], '', false);
701-
$itemsMock = $this->getMock(\Magento\Framework\DataObject::class, [], [], '', false);
702688

703-
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
704-
$this->searchCriteriaBuilderMock
705-
->expects($this->once())
706-
->method('create')
707-
->willReturn($extendedSearchCriteriaMock);
708-
$this->metadataServiceMock
709-
->expects($this->once())
710-
->method('getList')
711-
->with($extendedSearchCriteriaMock)
712-
->willReturn($productAttributeSearchResultsMock);
713-
$productAttributeSearchResultsMock->expects($this->once())
714-
->method('getItems')
715-
->willReturn([$productAttributeMock]);
716-
$productAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
717-
$collectionMock->expects($this->once())->method('addAttributeToSelect')->with($attributeCode);
718-
$collectionMock->expects($this->exactly(2))
719-
->method('joinAttribute')
689+
$filterGroupFilterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
690+
->disableOriginalConstructor()
691+
->getMock();
692+
$filterGroupFilterMock->expects(self::exactly(2))->method('getConditionType')->willReturn('eq');
693+
$filterGroupFilterMock->expects(self::atLeastOnce())->method('getField')->willReturn($fieldName);
694+
$filterGroupFilterMock->expects(self::once())->method('getValue')->willReturn('value');
695+
696+
$filterGroupMock = $this->getMockBuilder(\Magento\Framework\Api\Search\FilterGroup::class)
697+
->disableOriginalConstructor()
698+
->getMock();
699+
$filterGroupMock->expects(self::once())->method('getFilters')->willReturn([$filterGroupFilterMock]);
700+
701+
$sortOrderMock = $this->getMockBuilder(\Magento\Framework\Api\SortOrder::class)
702+
->disableOriginalConstructor()
703+
->getMock();
704+
$sortOrderMock->expects(self::atLeastOnce())->method('getField')->willReturn($fieldName);
705+
$sortOrderMock->expects(self::once())->method('getDirection')->willReturn(SortOrder::SORT_ASC);
706+
707+
/** @var \Magento\Framework\Api\SearchCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject $searchCriteriaMock */
708+
$searchCriteriaMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
709+
->disableOriginalConstructor()
710+
->getMock();
711+
$searchCriteriaMock->expects(self::once())->method('getFilterGroups')->willReturn([$filterGroupMock]);
712+
$searchCriteriaMock->expects(self::once())->method('getCurrentPage')->willReturn(4);
713+
$searchCriteriaMock->expects(self::once())->method('getPageSize')->willReturn(42);
714+
$searchCriteriaMock->expects(self::once())->method('getSortOrders')->willReturn([$sortOrderMock]);
715+
716+
$itemsMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
717+
->disableOriginalConstructor()
718+
->getMock();
719+
720+
$collectionMock = $this->getMockBuilder(Collection::class)
721+
->disableOriginalConstructor()
722+
->getMock();
723+
$collectionMock->expects(self::once())->method('addAttributeToSelect')->with($attributeCode);
724+
$collectionMock->expects(self::once())->method('setPageSize')->with(42);
725+
$collectionMock->expects(self::once())->method('load');
726+
$collectionMock->expects(self::once())->method('addCategoryIds');
727+
$collectionMock->expects(self::once())->method('getItems')->willReturn([$itemsMock]);
728+
$collectionMock->expects(self::once())->method('getSize')->willReturn(128);
729+
$collectionMock->expects(self::once())->method('addOrder')->with($fieldName, 'ASC');
730+
$collectionMock->expects(self::once())->method('setCurPage')->with(4);
731+
$collectionMock->expects(self::exactly(2))->method('joinAttribute')
720732
->withConsecutive(
721733
['status', 'catalog_product/status', 'entity_id', null, 'inner'],
722734
['visibility', 'catalog_product/visibility', 'entity_id', null, 'inner']
723-
);
724-
$searchCriteriaMock->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroupMock]);
725-
$filterGroupMock->expects($this->once())->method('getFilters')->willReturn([$filterGroupFilterMock]);
726-
$filterGroupFilterMock->expects($this->exactly(2))->method('getConditionType')->willReturn('eq');
727-
$filterGroupFilterMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
728-
$filterGroupFilterMock->expects($this->once())->method('getValue')->willReturn('value');
735+
);
736+
737+
$productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
738+
->disableOriginalConstructor()
739+
->getMock();
740+
$productAttributeMock->expects(self::once())->method('getAttributeCode')->willReturn($attributeCode);
741+
742+
$extendedSearchCriteriaMock = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
743+
->disableOriginalConstructor()
744+
->getMock();
745+
746+
$productAttributeSearchResultsMock = $this->getMockBuilder(ProductAttributeSearchResultsInterface::class)
747+
->disableOriginalConstructor()
748+
->setMethods(['getItems'])
749+
->getMockForAbstractClass();
750+
$productAttributeSearchResultsMock->expects(self::once())->method('getItems')
751+
->willReturn([$productAttributeMock]);
752+
753+
$this->collectionFactoryMock->expects(self::once())
754+
->method('create')
755+
->willReturn($collectionMock);
756+
757+
$this->extensionAttributesJoinProcessor->expects(self::once())
758+
->method('process')
759+
->with($collectionMock);
760+
761+
$this->metadataServiceMock->expects(self::once())->method('getList')->with($extendedSearchCriteriaMock)
762+
->willReturn($productAttributeSearchResultsMock);
763+
764+
$this->searchCriteriaBuilderMock->expects(self::once())->method('create')
765+
->willReturn($extendedSearchCriteriaMock);
766+
729767
$this->expectAddToFilter($fieldName, $collectionMock);
730-
$searchCriteriaMock->expects($this->once())->method('getSortOrders')->willReturn([$sortOrderMock]);
731-
$sortOrderMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
732-
$sortOrderMock->expects($this->once())->method('getDirection')->willReturn(SortOrder::SORT_ASC);
733-
$collectionMock->expects($this->once())->method('addOrder')->with($fieldName, 'ASC');
734-
$searchCriteriaMock->expects($this->once())->method('getCurrentPage')->willReturn(4);
735-
$collectionMock->expects($this->once())->method('setCurPage')->with(4);
736-
$searchCriteriaMock->expects($this->once())->method('getPageSize')->willReturn(42);
737-
$collectionMock->expects($this->once())->method('setPageSize')->with(42);
738-
$collectionMock->expects($this->once())->method('load');
739-
$collectionMock->expects($this->once())->method('getItems')->willReturn([$itemsMock]);
740-
$collectionMock->expects($this->once())->method('getSize')->willReturn(128);
741-
$searchResultsMock = $this->getMock(
742-
\Magento\Catalog\Api\Data\ProductSearchResultsInterface::class,
743-
[],
744-
[],
745-
'',
746-
false
747-
);
748-
$searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);
749-
$searchResultsMock->expects($this->once())->method('setItems')->with([$itemsMock]);
750-
$searchResultsMock->expects($this->once())->method('setTotalCount')->with(128);
751-
$this->searchResultsFactoryMock->expects($this->once())->method('create')->willReturn($searchResultsMock);
752768

769+
$searchResultsMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductSearchResultsInterface::class)
770+
->disableOriginalConstructor()
771+
->getMock();
772+
$searchResultsMock->expects(self::once())->method('setSearchCriteria')->with($searchCriteriaMock);
773+
$searchResultsMock->expects(self::once())->method('setItems')->with([$itemsMock]);
774+
775+
$this->searchResultsFactoryMock->expects($this->once())->method('create')->willReturn($searchResultsMock);
753776
$this->assertEquals($searchResultsMock, $this->model->getList($searchCriteriaMock));
754777
}
755778

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ public function testGetList()
589589

590590
$this->assertNotNull($response['items'][0]['sku']);
591591
$this->assertEquals('simple', $response['items'][0]['sku']);
592+
593+
$index = null;
594+
foreach ($response['items'][0]['custom_attributes'] as $key => $customAttribute) {
595+
if ($customAttribute['attribute_code'] == 'category_ids') {
596+
$index = $key;
597+
break;
598+
}
599+
}
600+
$this->assertNotNull($index, 'Category information wasn\'t set');
601+
602+
$expectedResult = (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) ? ['string' => '2'] : ['2'];
603+
$this->assertEquals($expectedResult, $response['items'][0]['custom_attributes'][$index]['value']);
592604
}
593605

594606
/**

0 commit comments

Comments
 (0)