Skip to content

Commit 0a88775

Browse files
Merge remote-tracking branch '35053/32910-fix-addinig-url-rewrite-for-one-store-group' into comm_voted_v1
2 parents fb564df + e2a0e34 commit 0a88775

File tree

5 files changed

+226
-135
lines changed

5 files changed

+226
-135
lines changed

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,18 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
161161
Product::ENTITY
162162
)) {
163163
$mergeDataProvider->merge(
164-
$this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId)
164+
$this->generateForSpecificStoreView($id, $productCategories, $product, $rootCategoryId, true)
165165
);
166166
} else {
167167
$scopedProduct = $this->productRepository->getById($productId, false, $id);
168168
$mergeDataProvider->merge(
169-
$this->generateForSpecificStoreView($id, $productCategories, $scopedProduct, $rootCategoryId)
169+
$this->generateForSpecificStoreView(
170+
$id,
171+
$productCategories,
172+
$scopedProduct,
173+
$rootCategoryId,
174+
true
175+
)
170176
);
171177
}
172178
}
@@ -182,12 +188,20 @@ public function generateForGlobalScope($productCategories, Product $product, $ro
182188
* @param \Magento\Framework\Data\Collection|Category[] $productCategories
183189
* @param \Magento\Catalog\Model\Product $product
184190
* @param int|null $rootCategoryId
191+
* @param bool $isGlobalScope
185192
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
193+
* @throws NoSuchEntityException
186194
*/
187-
public function generateForSpecificStoreView($storeId, $productCategories, Product $product, $rootCategoryId = null)
188-
{
195+
public function generateForSpecificStoreView(
196+
$storeId,
197+
$productCategories,
198+
Product $product,
199+
$rootCategoryId = null,
200+
bool $isGlobalScope = false
201+
) {
189202
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
190203
$categories = [];
204+
191205
foreach ($productCategories as $category) {
192206
if (!$this->isCategoryProperForGenerating($category, $storeId)) {
193207
continue;
@@ -196,35 +210,29 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
196210
$categories[] = $this->getCategoryWithOverriddenUrlKey($storeId, $category);
197211
}
198212

199-
$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
200-
201213
$mergeDataProvider->merge(
202214
$this->canonicalUrlRewriteGenerator->generate($storeId, $product)
203215
);
204216

205-
if ($this->isCategoryRewritesEnabled()) {
206-
$mergeDataProvider->merge(
207-
$this->categoriesUrlRewriteGenerator->generate($storeId, $product, $productCategories)
208-
);
217+
$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
218+
219+
if ($isGlobalScope) {
220+
$generatedUrls = $this->generateCategoryUrls((int) $storeId, $product, $productCategories);
221+
} else {
222+
$generatedUrls = $this->generateCategoryUrlsInStoreGroup((int) $storeId, $product, $productCategories);
209223
}
210224

225+
$mergeDataProvider->merge(array_merge(...$generatedUrls));
211226
$mergeDataProvider->merge(
212-
$this->currentUrlRewritesRegenerator->generate(
227+
$this->currentUrlRewritesRegenerator->generateAnchor(
213228
$storeId,
214229
$product,
215230
$productCategories,
216231
$rootCategoryId
217232
)
218233
);
219-
220-
if ($this->isCategoryRewritesEnabled()) {
221-
$mergeDataProvider->merge(
222-
$this->anchorUrlRewriteGenerator->generate($storeId, $product, $productCategories)
223-
);
224-
}
225-
226234
$mergeDataProvider->merge(
227-
$this->currentUrlRewritesRegenerator->generateAnchor(
235+
$this->currentUrlRewritesRegenerator->generate(
228236
$storeId,
229237
$product,
230238
$productCategories,
@@ -252,6 +260,65 @@ public function isCategoryProperForGenerating(Category $category, $storeId)
252260
return false;
253261
}
254262

263+
/**
264+
* Generate category URLs for the whole store group.
265+
*
266+
* @param int $storeId
267+
* @param Product $product
268+
* @param ObjectRegistry $productCategories
269+
*
270+
* @return array
271+
* @throws NoSuchEntityException
272+
*/
273+
private function generateCategoryUrlsInStoreGroup(
274+
int $storeId,
275+
Product $product,
276+
ObjectRegistry $productCategories
277+
): array {
278+
$currentStore = $this->storeManager->getStore($storeId);
279+
$currentGroupId = $currentStore->getStoreGroupId();
280+
$storeList = $this->storeManager->getStores();
281+
$generatedUrls = [];
282+
283+
foreach ($storeList as $store) {
284+
if ($store->getStoreGroupId() === $currentGroupId && $this->isCategoryRewritesEnabled()) {
285+
$groupStoreId = (int) $store->getId();
286+
$generatedUrls[] = $this->generateCategoryUrls(
287+
$groupStoreId,
288+
$product,
289+
$productCategories
290+
);
291+
}
292+
}
293+
294+
return array_merge(...$generatedUrls);
295+
}
296+
297+
/**
298+
* Generate category URLs.
299+
*
300+
* @param int $storeId
301+
* @param Product $product
302+
* @param ObjectRegistry $categories
303+
*
304+
* @return array
305+
*/
306+
private function generateCategoryUrls(int $storeId, Product $product, ObjectRegistry $categories): array
307+
{
308+
$generatedUrls[] = $this->categoriesUrlRewriteGenerator->generate(
309+
$storeId,
310+
$product,
311+
$categories
312+
);
313+
$generatedUrls[] = $this->anchorUrlRewriteGenerator->generate(
314+
$storeId,
315+
$product,
316+
$categories
317+
);
318+
319+
return $generatedUrls;
320+
}
321+
255322
/**
256323
* Check if URL key has been changed
257324
*

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ public function testGenerationForGlobalScope()
166166
$product = $this->createMock(Product::class);
167167
$product->expects($this->any())->method('getStoreId')->willReturn(null);
168168
$product->expects($this->any())->method('getStoreIds')->willReturn([1]);
169+
$store = $this->getMockBuilder(Store::class)
170+
->disableOriginalConstructor()
171+
->getMock();
172+
$store->expects($this->any())->method('getStoreGroupId')->willReturn(1);
173+
$this->storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
169174
$this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
170175
->willReturn(true);
171176
$this->initObjectRegistryFactory([]);
@@ -211,6 +216,11 @@ public function testGenerationForSpecificStore()
211216
$product = $this->createMock(Product::class);
212217
$product->expects($this->any())->method('getStoreId')->willReturn(1);
213218
$product->expects($this->never())->method('getStoreIds');
219+
$store = $this->getMockBuilder(Store::class)
220+
->disableOriginalConstructor()
221+
->getMock();
222+
$store->expects($this->any())->method('getStoreGroupId')->willReturn(1);
223+
$this->storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
214224
$this->categoryMock->expects($this->any())->method('getParentIds')
215225
->willReturn(['root-id', $storeRootCategoryId]);
216226
$this->categoryMock->expects($this->any())->method('getId')->willReturn($category_id);

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/UrlTest.php

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
/**
1212
* Test class for \Magento\Catalog\Model\Product\Url.
1313
*
14-
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
1514
* @magentoAppArea frontend
1615
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1716
*/
@@ -37,6 +36,9 @@ protected function setUp(): void
3736
);
3837
}
3938

39+
/**
40+
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
41+
*/
4042
public function testGetUrlInStore()
4143
{
4244
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -89,6 +91,7 @@ public function getUrlsWithSecondStoreProvider()
8991

9092
/**
9193
* @magentoDbIsolation disabled
94+
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
9295
*/
9396
public function testGetProductUrl()
9497
{
@@ -99,52 +102,10 @@ public function testGetProductUrl()
99102
$this->assertStringEndsWith('simple-product.html', $this->_model->getProductUrl($product));
100103
}
101104

102-
public function testFormatUrlKey()
103-
{
104-
$this->assertEquals('abc-test', $this->_model->formatUrlKey('AbC#-$^test'));
105-
}
106-
107-
public function testGetUrlPath()
108-
{
109-
/** @var $product \Magento\Catalog\Model\Product */
110-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
111-
\Magento\Catalog\Model\Product::class
112-
);
113-
$product->setUrlPath('product.html');
114-
115-
/** @var $category \Magento\Catalog\Model\Category */
116-
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
117-
\Magento\Catalog\Model\Category::class,
118-
['data' => ['url_path' => 'category', 'entity_id' => 5, 'path_ids' => [2, 3, 5]]]
119-
);
120-
$category->setOrigData();
121-
122-
$this->assertEquals('product.html', $this->urlPathGenerator->getUrlPath($product));
123-
$this->assertEquals('category/product.html', $this->urlPathGenerator->getUrlPath($product, $category));
124-
}
125-
126-
/**
127-
* @magentoDbIsolation disabled
128-
* @magentoAppArea frontend
129-
*/
130-
public function testGetUrl()
131-
{
132-
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
133-
\Magento\Catalog\Model\ProductRepository::class
134-
);
135-
$product = $repository->get('simple');
136-
$this->assertStringEndsWith('simple-product.html', $this->_model->getUrl($product));
137-
138-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
139-
\Magento\Catalog\Model\Product::class
140-
);
141-
$product->setId(100);
142-
$this->assertStringContainsString('catalog/product/view/id/100/', $this->_model->getUrl($product));
143-
}
144-
145105
/**
146106
* Check that rearranging product url rewrites do not influence on whether to use category in product links
147107
*
108+
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
148109
* @magentoConfigFixture current_store catalog/seo/product_use_categories 0
149110
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
150111
* @magentoDbIsolation disabled
@@ -187,4 +148,52 @@ public function testGetProductUrlWithRearrangedUrlRewrites()
187148
$urlPersist->replace($rewrites);
188149
$this->assertStringNotContainsString($category->getUrlPath(), $this->_model->getProductUrl($product));
189150
}
151+
152+
/**
153+
* @magentoDbIsolation disabled
154+
*/
155+
public function testFormatUrlKey()
156+
{
157+
$this->assertEquals('abc-test', $this->_model->formatUrlKey('AbC#-$^test'));
158+
}
159+
160+
/**
161+
* @magentoDbIsolation disabled
162+
* @magentoDataFixture Magento/Catalog/_files/url_rewrites.php
163+
* @magentoConfigFixture current_store catalog/seo/product_use_categories 0
164+
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
165+
*/
166+
public function testGetUrl()
167+
{
168+
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
169+
\Magento\Catalog\Model\ProductRepository::class
170+
);
171+
$product = $repository->get('simple');
172+
$this->assertStringEndsWith('simple-product.html', $this->_model->getProductUrl($product));
173+
174+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
175+
\Magento\Catalog\Model\Product::class
176+
);
177+
$product->setId(100);
178+
$this->assertStringContainsString('catalog/product/view/id/100/', $this->_model->getUrl($product));
179+
}
180+
181+
public function testGetUrlPath()
182+
{
183+
/** @var $product \Magento\Catalog\Model\Product */
184+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
185+
\Magento\Catalog\Model\Product::class
186+
);
187+
$product->setUrlPath('product.html');
188+
189+
/** @var $category \Magento\Catalog\Model\Category */
190+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
191+
\Magento\Catalog\Model\Category::class,
192+
['data' => ['url_path' => 'category', 'entity_id' => 5, 'path_ids' => [2, 3, 5]]]
193+
);
194+
$category->setOrigData();
195+
196+
$this->assertEquals('product.html', $this->urlPathGenerator->getUrlPath($product));
197+
$this->assertEquals('category/product.html', $this->urlPathGenerator->getUrlPath($product, $category));
198+
}
190199
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandlerTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ function (UrlRewrite $urlRewrite) {
7777

7878
$expected = [
7979
'store-1-key.html', // the Default store
80-
'cat-1/store-1-key.html', // the Default store with Category URL key
81-
'/store-1-key.html', // an anchor URL the Default store
80+
'cat-1/store-1-key.html', // the Default store with Category URL key, first store view
81+
'/store-1-key.html', // an anchor URL the Default store, first store view
82+
'cat-1/store-1-key.html', // the Default store with Category URL key, second store view
83+
'/store-1-key.html', // an anchor URL the Default store, second store view
8284
'p002.html', // the Secondary store
83-
'cat-1-2/p002.html', // the Secondary store with Category URL key
84-
'/p002.html', // an anchor URL the Secondary store
85+
'cat-1-2/p002.html', // the Secondary store with Category URL key, first store view
86+
'/p002.html', // an anchor URL the Secondary store, first store view
87+
'cat-1-2/p002.html', // the Secondary store with Category URL key, second store view
88+
'/p002.html', // an anchor URL the Secondary store, second store view
8589
];
8690
self::assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
8791
}

0 commit comments

Comments
 (0)