Skip to content

Commit 944abbd

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-64295' into develop-team-pr1
2 parents 87b72ac + f6af63a commit 944abbd

File tree

6 files changed

+240
-47
lines changed

6 files changed

+240
-47
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/AfterImportDataObserver.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
namespace Magento\CatalogUrlRewrite\Observer;
77

88
use Magento\Catalog\Model\Category;
9-
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Visibility;
10+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1012
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
1113
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
14+
use Magento\Framework\App\ObjectManager;
1215
use Magento\Framework\Event\Observer;
13-
use Magento\Framework\App\ResourceConnection;
16+
use Magento\Framework\Event\ObserverInterface;
1417
use Magento\ImportExport\Model\Import as ImportExport;
1518
use Magento\Store\Model\Store;
19+
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
20+
use Magento\UrlRewrite\Model\OptionProvider;
21+
use Magento\UrlRewrite\Model\UrlFinderInterface;
1622
use Magento\UrlRewrite\Model\UrlPersistInterface;
1723
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1824
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
19-
use Magento\UrlRewrite\Model\OptionProvider;
20-
use Magento\UrlRewrite\Model\UrlFinderInterface;
21-
use Magento\Framework\Event\ObserverInterface;
22-
use Magento\Catalog\Model\Product\Visibility;
23-
use Magento\Framework\App\ObjectManager;
24-
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
2525

2626
/**
2727
* Class AfterImportDataObserver
@@ -102,6 +102,20 @@ class AfterImportDataObserver implements ObserverInterface
102102
/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
103103
private $mergeDataProviderPrototype;
104104

105+
/**
106+
* Factory for creating category collection.
107+
*
108+
* @var CategoryCollectionFactory
109+
*/
110+
private $categoryCollectionFactory;
111+
112+
/**
113+
* Array of invoked categories during url rewrites generation.
114+
*
115+
* @var array
116+
*/
117+
private $categoriesCache = [];
118+
105119
/**
106120
* @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory
107121
* @param \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory $objectRegistryFactory
@@ -112,7 +126,7 @@ class AfterImportDataObserver implements ObserverInterface
112126
* @param UrlRewriteFactory $urlRewriteFactory
113127
* @param UrlFinderInterface $urlFinder
114128
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
115-
* @throws \InvalidArgumentException
129+
* @param CategoryCollectionFactory|null $categoryCollectionFactory
116130
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117131
*/
118132
public function __construct(
@@ -124,7 +138,8 @@ public function __construct(
124138
UrlPersistInterface $urlPersist,
125139
UrlRewriteFactory $urlRewriteFactory,
126140
UrlFinderInterface $urlFinder,
127-
MergeDataProviderFactory $mergeDataProviderFactory = null
141+
MergeDataProviderFactory $mergeDataProviderFactory = null,
142+
CategoryCollectionFactory $categoryCollectionFactory = null
128143
) {
129144
$this->urlPersist = $urlPersist;
130145
$this->catalogProductFactory = $catalogProductFactory;
@@ -138,6 +153,8 @@ public function __construct(
138153
$mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class);
139154
}
140155
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
156+
$this->categoryCollectionFactory = $categoryCollectionFactory ?:
157+
ObjectManager::getInstance()->get(CategoryCollectionFactory::class);
141158
}
142159

143160
/**
@@ -318,7 +335,7 @@ protected function canonicalUrlRewriteGenerate()
318335
}
319336

320337
/**
321-
* Generate list based on categories
338+
* Generate list based on categories.
322339
*
323340
* @return UrlRewrite[]
324341
*/
@@ -328,7 +345,7 @@ protected function categoriesUrlRewriteGenerate()
328345
foreach ($this->products as $productId => $productsByStores) {
329346
foreach ($productsByStores as $storeId => $product) {
330347
foreach ($this->categoryCache[$productId] as $categoryId) {
331-
$category = $this->import->getCategoryProcessor()->getCategoryById($categoryId);
348+
$category = $this->getCategoryById($categoryId, $storeId);
332349
if ($category->getParentId() == Category::TREE_ROOT_ID) {
333350
continue;
334351
}
@@ -481,4 +498,27 @@ protected function isCategoryProperForGenerating($category, $storeId)
481498
$this->acceptableCategories[$storeId][$category->getId()] = $acceptable;
482499
return $acceptable;
483500
}
501+
502+
/**
503+
* Get category by id considering store scope.
504+
*
505+
* @param int $categoryId
506+
* @param int $storeId
507+
* @return Category|\Magento\Framework\DataObject
508+
*/
509+
private function getCategoryById($categoryId, $storeId)
510+
{
511+
if (!isset($this->categoriesCache[$categoryId][$storeId])) {
512+
/** @var CategoryCollection $categoryCollection */
513+
$categoryCollection = $this->categoryCollectionFactory->create();
514+
$categoryCollection->addIdFilter([$categoryId])
515+
->setStoreId($storeId)
516+
->addAttributeToSelect('name')
517+
->addAttributeToSelect('url_key')
518+
->addAttributeToSelect('url_path');
519+
$this->categoriesCache[$categoryId][$storeId] = $categoryCollection->getFirstItem();
520+
}
521+
522+
return $this->categoriesCache[$categoryId][$storeId];
523+
}
484524
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/AfterImportDataObserverTest.php

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace Magento\CatalogUrlRewrite\Test\Unit\Observer;
99

10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1112
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
12-
use Magento\Store\Model\Store;
1313
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Store\Model\Store;
1416

1517
/**
1618
* Class AfterImportDataObserverTest
@@ -110,9 +112,16 @@ class AfterImportDataObserverTest extends \PHPUnit_Framework_TestCase
110112
*/
111113
private $product;
112114

113-
/** @var \Magento\UrlRewrite\Model\MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject */
115+
/**
116+
* @var \Magento\UrlRewrite\Model\MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject
117+
*/
114118
private $mergeDataProvider;
115119

120+
/**
121+
* @var CategoryCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
122+
*/
123+
private $categoryCollectionFactory;
124+
116125
/**
117126
* Test products returned by getBunch method of event object.
118127
*
@@ -245,37 +254,6 @@ protected function setUp()
245254
->disableOriginalConstructor()
246255
->getMock();
247256

248-
$categoryProcessor = $this->getMock(
249-
\Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor::class,
250-
[
251-
'getCategoryById',
252-
],
253-
[],
254-
'',
255-
false
256-
);
257-
$category = $this->getMock(
258-
\Magento\Catalog\Model\Category::class,
259-
[
260-
'getId',
261-
],
262-
[],
263-
'',
264-
false
265-
);
266-
$category
267-
->expects($this->any())
268-
->method('getId')
269-
->willReturn($this->categoryId);
270-
$categoryProcessor
271-
->expects($this->any())
272-
->method('getCategoryById')
273-
->with($this->categoryId)
274-
->willReturn($category);
275-
$this->importProduct
276-
->expects($this->any())
277-
->method('getCategoryProcessor')
278-
->willReturn($categoryProcessor);
279257
$mergeDataProviderFactory = $this->getMock(
280258
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
281259
['create'],
@@ -286,6 +264,11 @@ protected function setUp()
286264
$this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider;
287265
$mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);
288266

267+
$this->categoryCollectionFactory = $this->getMockBuilder(CategoryCollectionFactory::class)
268+
->setMethods(['create'])
269+
->disableOriginalConstructor()
270+
->getMock();
271+
289272
$this->objectManager = new ObjectManager($this);
290273
$this->import = $this->objectManager->getObject(
291274
\Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::class,
@@ -298,7 +281,8 @@ protected function setUp()
298281
'urlPersist' => $this->urlPersist,
299282
'urlRewriteFactory' => $this->urlRewriteFactory,
300283
'urlFinder' => $this->urlFinder,
301-
'mergeDataProviderFactory' => $mergeDataProviderFactory
284+
'mergeDataProviderFactory' => $mergeDataProviderFactory,
285+
'categoryCollectionFactory' => $this->categoryCollectionFactory
302286
]
303287
);
304288
}
@@ -563,6 +547,7 @@ public function testCanonicalUrlRewriteGenerateWithEmptyUrlPath()
563547

564548
/**
565549
* Cover categoriesUrlRewriteGenerate().
550+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
566551
*/
567552
public function testCategoriesUrlRewriteGenerate()
568553
{
@@ -601,6 +586,33 @@ public function testCategoriesUrlRewriteGenerate()
601586
->expects($this->any())
602587
->method('getId')
603588
->will($this->returnValue($this->categoryId));
589+
590+
$categoryCollection = $this->getMockBuilder(CategoryCollection::class)
591+
->disableOriginalConstructor()
592+
->getMock();
593+
$categoryCollection->expects($this->once())
594+
->method('addIdFilter')
595+
->with([$this->categoryId])
596+
->willReturnSelf();
597+
$categoryCollection->expects($this->once())
598+
->method('setStoreId')
599+
->with($storeId)
600+
->willReturnSelf();
601+
$categoryCollection->expects($this->exactly(3))
602+
->method('addAttributeToSelect')
603+
->withConsecutive(
604+
['name'],
605+
['url_key'],
606+
['url_path']
607+
)->willReturnSelf();
608+
$categoryCollection->expects($this->once())
609+
->method('getFirstItem')
610+
->willReturn($category);
611+
612+
$this->categoryCollectionFactory->expects($this->once())
613+
->method('create')
614+
->willReturn($categoryCollection);
615+
604616
$this->urlRewrite
605617
->expects($this->any())
606618
->method('setStoreId')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\TestFramework\Helper\Bootstrap;
7+
8+
/** @var \Magento\Catalog\Model\Category $category */
9+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
10+
$category->isObjectNew(true);
11+
$category->setId(
12+
555
13+
)->setCreatedAt(
14+
'2017-05-5 09:50:07'
15+
)->setName(
16+
'category-admin'
17+
)->setParentId(
18+
2
19+
)->setPath(
20+
'1/2/555'
21+
)->setLevel(
22+
2
23+
)->setAvailableSortBy(
24+
['position', 'name']
25+
)->setDefaultSortBy(
26+
'name'
27+
)->setIsActive(
28+
true
29+
)->setPosition(
30+
1
31+
)->setUrlKey(
32+
'category-admin'
33+
)->save();
34+
35+
/** @var \Magento\Store\Model\Store $store */
36+
$store = Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
37+
38+
$category->setStoreId($store->load('default')->getId())
39+
->setName('category-defaultstore')
40+
->setUrlKey('category-defaultstore')
41+
->save();
42+
43+
$category->setStoreId($store->load('fixturestore')->getId())
44+
->setName('category-fixturestore')
45+
->setUrlKey('category-fixturestore')
46+
->save();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Registry $registry */
8+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
9+
$registry->unregister('isSecureArea');
10+
$registry->register('isSecureArea', true);
11+
12+
/** @var $category \Magento\Catalog\Model\Category */
13+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
14+
$category->load(555);
15+
if ($category->getId()) {
16+
$category->delete();
17+
}
18+
19+
$registry->unregister('isSecureArea');
20+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)