Skip to content

Commit 2813087

Browse files
committed
Merge remote-tracking branch 'github-magento/MAGETWO-91540' into EPAM-PR-7
2 parents 0eb8677 + 0d6e11b commit 2813087

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Framework\DB\Adapter\ConnectionException;
2121
use Magento\Framework\DB\Adapter\DeadlockException;
2222
use Magento\Framework\DB\Adapter\LockWaitException;
23+
use Magento\Framework\EntityManager\Operation\Read\ReadExtensions;
2324
use Magento\Framework\Exception\CouldNotSaveException;
2425
use Magento\Framework\Exception\InputException;
2526
use Magento\Framework\Exception\LocalizedException;
@@ -154,6 +155,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
154155
*/
155156
private $serializer;
156157

158+
/**
159+
* @var ReadExtensions
160+
*/
161+
private $readExtensions;
162+
157163
/**
158164
* ProductRepository constructor.
159165
* @param ProductFactory $productFactory
@@ -179,6 +185,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
179185
* @param CollectionProcessorInterface $collectionProcessor [optional]
180186
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
181187
* @param int $cacheLimit [optional]
188+
* @param ReadExtensions|null $readExtensions
182189
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
183190
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
184191
*/
@@ -205,7 +212,8 @@ public function __construct(
205212
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
206213
CollectionProcessorInterface $collectionProcessor = null,
207214
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
208-
$cacheLimit = 1000
215+
$cacheLimit = 1000,
216+
ReadExtensions $readExtensions = null
209217
) {
210218
$this->productFactory = $productFactory;
211219
$this->collectionFactory = $collectionFactory;
@@ -228,6 +236,8 @@ public function __construct(
228236
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
229237
->get(\Magento\Framework\Serialize\Serializer\Json::class);
230238
$this->cacheLimit = (int)$cacheLimit;
239+
$this->readExtensions = $readExtensions ?: \Magento\Framework\App\ObjectManager::getInstance()
240+
->get(ReadExtensions::class);
231241
}
232242

233243
/**
@@ -668,6 +678,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
668678
$collection->load();
669679

670680
$collection->addCategoryIds();
681+
$this->addExtensionAttributes($collection);
671682
$searchResult = $this->searchResultsFactory->create();
672683
$searchResult->setSearchCriteria($searchCriteria);
673684
$searchResult->setItems($collection->getItems());
@@ -678,7 +689,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
678689
$this->getCacheKey(
679690
[
680691
false,
681-
$product->hasData(\Magento\Catalog\Model\Product::STORE_ID) ? $product->getStoreId() : null
692+
$product->getStoreId()
682693
]
683694
),
684695
$product
@@ -688,6 +699,20 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
688699
return $searchResult;
689700
}
690701

702+
/**
703+
* Add extension attributes to loaded items.
704+
*
705+
* @param Collection $collection
706+
* @return Collection
707+
*/
708+
private function addExtensionAttributes(Collection $collection) : Collection
709+
{
710+
foreach ($collection->getItems() as $item) {
711+
$this->readExtensions->execute($item);
712+
}
713+
return $collection;
714+
}
715+
691716
/**
692717
* Helper function that adds a FilterGroup to the collection.
693718
*

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
2929
use Magento\Framework\Api\SearchCriteriaBuilder;
3030
use Magento\Framework\DB\Adapter\ConnectionException;
31+
use Magento\Framework\EntityManager\Operation\Read\ReadExtensions;
3132
use Magento\Framework\Filesystem;
3233
use Magento\Framework\Serialize\Serializer\Json;
3334
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -179,6 +180,11 @@ class ProductRepositoryTest extends \PHPUnit\Framework\TestCase
179180
*/
180181
private $cacheLimit = 2;
181182

183+
/**
184+
* @var ReadExtensions|\PHPUnit_Framework_MockObject_MockObject
185+
*/
186+
private $readExtensionsMock;
187+
182188
/**
183189
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
184190
*/
@@ -292,6 +298,8 @@ function ($value) {
292298
}
293299
)
294300
);
301+
$this->readExtensionsMock = $this->getMockBuilder(ReadExtensions::class)
302+
->disableOriginalConstructor()->getMock();
295303

296304
$this->model = $this->objectManager->getObject(
297305
ProductRepository::class,
@@ -315,7 +323,8 @@ function ($value) {
315323
'mediaGalleryProcessor' => $this->mediaGalleryProcessor,
316324
'collectionProcessor' => $this->collectionProcessor,
317325
'serializer' => $this->serializerMock,
318-
'cacheLimit' => $this->cacheLimit
326+
'cacheLimit' => $this->cacheLimit,
327+
'readExtensions' => $this->readExtensionsMock,
319328
]
320329
);
321330
}
@@ -768,6 +777,8 @@ public function testGetList()
768777
$collectionMock->expects($this->once())->method('load');
769778
$collectionMock->expects($this->once())->method('addCategoryIds');
770779
$collectionMock->expects($this->atLeastOnce())->method('getItems')->willReturn([$this->product]);
780+
$this->readExtensionsMock->expects($this->once())->method('execute')->with($this->product);
781+
$collectionMock->expects($this->atLeastOnce())->method('getItems')->willReturn([$this->product]);
771782
$collectionMock->expects($this->once())->method('getSize')->willReturn(128);
772783
$searchResultsMock = $this->createMock(\Magento\Catalog\Api\Data\ProductSearchResultsInterface::class);
773784
$searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace Magento\Catalog\Api;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1112
use Magento\Downloadable\Model\Link;
1213
use Magento\Store\Model\Store;
13-
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1414
use Magento\Store\Model\Website;
1515
use Magento\Store\Model\WebsiteRepository;
1616
use Magento\TestFramework\Helper\Bootstrap;
@@ -20,8 +20,8 @@
2020
use Magento\Framework\Api\SearchCriteriaBuilder;
2121
use Magento\Framework\Api\SortOrder;
2222
use Magento\Framework\Api\SortOrderBuilder;
23-
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
2423
use Magento\Framework\Exception\NoSuchEntityException;
24+
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
2525

2626
/**
2727
* @magentoAppIsolation enabled
@@ -815,6 +815,7 @@ public function testGetList()
815815
$this->assertTrue(count($response['items']) > 0);
816816

817817
$this->assertNotNull($response['items'][0]['sku']);
818+
$this->assertNotNull($response['items'][0][ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['website_ids']);
818819
$this->assertEquals('simple', $response['items'][0]['sku']);
819820

820821
$index = null;
@@ -912,6 +913,7 @@ public function testGetListWithFilteringByWebsite()
912913
$this->assertTrue(count($response['items']) == 1);
913914
$this->assertTrue(isset($response['items'][0]['sku']));
914915
$this->assertEquals('simple-2', $response['items'][0]['sku']);
916+
$this->assertNotNull($response['items'][0][ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['website_ids']);
915917
}
916918

917919
/**
@@ -1058,6 +1060,9 @@ public function testGetListWithMultipleFilterGroupsAndSortingAndPagination()
10581060
$this->assertEquals(3, $searchResult['total_count']);
10591061
$this->assertEquals(1, count($searchResult['items']));
10601062
$this->assertEquals('search_product_4', $searchResult['items'][0][ProductInterface::SKU]);
1063+
$this->assertNotNull(
1064+
$searchResult['items'][0][ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['website_ids']
1065+
);
10611066
}
10621067

10631068
/**

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
8+
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
89

910
\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
1011

@@ -19,10 +20,15 @@
1920
$tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
2021
/** @var $tpExtensionAttributes */
2122
$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class);
23+
/** @var $productExtensionAttributes */
24+
$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class);
2225

2326
$adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin');
2427
$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create()
2528
->setWebsiteId($adminWebsite->getId());
29+
$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create(
30+
['website_ids' => $adminWebsite->getId()]
31+
);
2632

2733
$tierPrices[] = $tierPriceFactory->create(
2834
[
@@ -82,6 +88,7 @@
8288
->setTaxClassId(0)
8389
->setTierPrices($tierPrices)
8490
->setDescription('Description with <b>html tag</b>')
91+
->setExtensionAttributes($productExtensionAttributesWebsiteIds)
8592
->setMetaTitle('meta title')
8693
->setMetaKeyword('meta keyword')
8794
->setMetaDescription('meta description')

0 commit comments

Comments
 (0)