Skip to content

Commit 20541b0

Browse files
committed
Merge remote-tracking branch 'magento-mpi/MC-21716' into MPI-PR-2019-10-25
2 parents a0c3c03 + c8ba3c8 commit 20541b0

File tree

3 files changed

+146
-12
lines changed

3 files changed

+146
-12
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogUrlRewrite\Model\Product;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Model\Category;
910
use Magento\Catalog\Model\Product;
1011
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
1112
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
@@ -67,6 +68,9 @@ public function generate($storeId, Product $product, ObjectRegistry $productCate
6768
if ($anchorCategoryIds) {
6869
foreach ($anchorCategoryIds as $anchorCategoryId) {
6970
$anchorCategory = $this->categoryRepository->get($anchorCategoryId);
71+
if ((int)$anchorCategory->getParentId() === Category::TREE_ROOT_ID) {
72+
continue;
73+
}
7074
$urls[] = $this->urlRewriteFactory->create()
7175
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
7276
->setEntityId($product->getId())
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Product;
7+
8+
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
11+
class AnchorUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/** @var \Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator */
14+
protected $anchorUrlRewriteGenerator;
15+
16+
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
17+
protected $productUrlPathGenerator;
18+
19+
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $product;
21+
22+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
23+
private $categoryRepositoryInterface;
24+
25+
/** @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $categoryRegistry;
27+
28+
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */
29+
protected $urlRewriteFactory;
30+
31+
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $urlRewrite;
33+
34+
protected function setUp()
35+
{
36+
$this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
37+
->setMethods(['create'])
38+
->disableOriginalConstructor()->getMock();
39+
$this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
40+
->disableOriginalConstructor()->getMock();
41+
$this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
42+
->disableOriginalConstructor()->getMock();
43+
$this->categoryRepositoryInterface = $this->getMockBuilder(
44+
\Magento\Catalog\Api\CategoryRepositoryInterface::class
45+
)->disableOriginalConstructor()->getMock();
46+
$this->categoryRegistry = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\ObjectRegistry::class)
47+
->disableOriginalConstructor()->getMock();
48+
$this->productUrlPathGenerator = $this->getMockBuilder(
49+
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
50+
)->disableOriginalConstructor()->getMock();
51+
$this->anchorUrlRewriteGenerator = (new ObjectManager($this))->getObject(
52+
\Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator::class,
53+
[
54+
'productUrlPathGenerator' => $this->productUrlPathGenerator,
55+
'urlRewriteFactory' => $this->urlRewriteFactory,
56+
'categoryRepository' => $this->categoryRepositoryInterface
57+
]
58+
);
59+
}
60+
61+
public function testGenerateEmpty()
62+
{
63+
$this->categoryRegistry->expects($this->any())->method('getList')->will($this->returnValue([]));
64+
65+
$this->assertEquals(
66+
[],
67+
$this->anchorUrlRewriteGenerator->generate(1, $this->product, $this->categoryRegistry)
68+
);
69+
}
70+
71+
public function testGenerateCategories()
72+
{
73+
$urlPathWithCategory = 'category1/category2/category3/simple-product.html';
74+
$storeId = 10;
75+
$productId = 12;
76+
$canonicalUrlPathWithCategory = 'canonical-path-with-category';
77+
$categoryParentId = '1';
78+
$categoryIds = [$categoryParentId,'2','3','4'];
79+
$urls = ['category1/simple-product.html',
80+
'category1/category2/simple-product.html',
81+
'category1/category2/category3/simple-product.html'];
82+
83+
$this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
84+
$this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')
85+
->will($this->returnValue($urlPathWithCategory));
86+
$this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')
87+
->will($this->returnValue($canonicalUrlPathWithCategory));
88+
$category = $this->createMock(\Magento\Catalog\Model\Category::class);
89+
$category->expects($this->any())->method('getId')->will($this->returnValue($categoryIds));
90+
$category->expects($this->any())->method('getAnchorsAbove')->will($this->returnValue($categoryIds));
91+
$category->expects($this->any())->method('getParentId')->will(
92+
$this->onConsecutiveCalls(
93+
$categoryIds[0],
94+
$categoryIds[1],
95+
$categoryIds[2],
96+
$categoryIds[3]
97+
)
98+
);
99+
$this->categoryRepositoryInterface
100+
->expects($this->any())
101+
->method('get')
102+
->withConsecutive(
103+
[ 'category_id' => $categoryIds[0]],
104+
[ 'category_id' => $categoryIds[1]],
105+
[ 'category_id' => $categoryIds[2]]
106+
)
107+
->will($this->returnValue($category));
108+
$this->categoryRegistry->expects($this->any())->method('getList')
109+
->will($this->returnValue([$category]));
110+
$this->urlRewrite->expects($this->any())->method('setStoreId')
111+
->with($storeId)
112+
->will($this->returnSelf());
113+
$this->urlRewrite->expects($this->any())->method('setEntityId')
114+
->with($productId)
115+
->will($this->returnSelf());
116+
$this->urlRewrite->expects($this->any())->method('setEntityType')
117+
->with(ProductUrlRewriteGenerator::ENTITY_TYPE)
118+
->will($this->returnSelf());
119+
$this->urlRewrite->expects($this->any())->method('setRequestPath')
120+
->will($this->returnSelf());
121+
$this->urlRewrite->expects($this->any())->method('setTargetPath')
122+
->will($this->returnSelf());
123+
$this->urlRewrite->expects($this->any())->method('setMetadata')
124+
->will(
125+
$this->onConsecutiveCalls(
126+
$urls[0],
127+
$urls[1],
128+
$urls[2]
129+
)
130+
);
131+
$this->urlRewriteFactory->expects($this->any())->method('create')->will(
132+
$this->returnValue($this->urlRewrite)
133+
);
134+
135+
$this->assertEquals(
136+
$urls,
137+
$this->anchorUrlRewriteGenerator->generate($storeId, $this->product, $this->categoryRegistry)
138+
);
139+
}
140+
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ public function testGenerateUrlRewritesWithoutSaveHistory()
9898
'catalog/product/view/id/' . $productForTest . '/category/4',
9999
1,
100100
0
101-
],
102-
[
103-
'/simple-product-two.html',
104-
'catalog/product/view/id/' . $productForTest . '/category/2',
105-
1,
106-
0
107101
]
108102
];
109103

@@ -187,12 +181,6 @@ public function testGenerateUrlRewritesWithSaveHistory()
187181
1,
188182
0
189183
],
190-
[
191-
'/simple-product-two.html',
192-
'catalog/product/view/id/' . $productForTest . '/category/2',
193-
1,
194-
0
195-
],
196184
[
197185
'category-1/simple-product-two.html',
198186
'new-url/simple-product-two.html',
@@ -329,6 +317,8 @@ public function testGenerateUrlRewritesWithoutGenerateProductRewrites()
329317
* @magentoAppIsolation enabled
330318
*
331319
* @return void
320+
* @throws NoSuchEntityException
321+
* @throws \Magento\Framework\Exception\StateException
332322
*/
333323
public function testRemoveCatalogUrlRewrites()
334324
{

0 commit comments

Comments
 (0)