Skip to content

Commit 667c0ad

Browse files
Merge remote-tracking branch '39681/fix-for-issue-35371' into blprs
2 parents 27217d0 + ca76908 commit 667c0ad

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Storage/DynamicStorage.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
4+
* Copyright 2019 Adobe
5+
* All Rights Reserved.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\CatalogUrlRewrite\Model\Storage;
@@ -170,7 +172,7 @@ private function findProductRewriteByRequestPath(array $data): ?array
170172
}
171173
$categorySuffix = $this->getCategoryUrlSuffix($data[UrlRewrite::STORE_ID]);
172174
$productResource = $this->productFactory->create();
173-
$categoryPath = str_replace('/' . $productUrl, '', $requestPath);
175+
$categoryPath = substr($requestPath, 0, -1 * strlen('/' . $productUrl));
174176
if ($productFromDb[UrlRewrite::REDIRECT_TYPE]) {
175177
$productUrl = $productFromDb[UrlRewrite::TARGET_PATH];
176178
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Storage/DynamicStorageTest.php

+70-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -80,6 +80,11 @@ class DynamicStorageTest extends TestCase
8080
*/
8181
private $logger;
8282

83+
/**
84+
* @var string
85+
*/
86+
private $requestPath;
87+
8388
/**
8489
* @inheritdoc
8590
*/
@@ -169,9 +174,7 @@ public function testFindProductRewriteByRequestPath(
169174
bool $canBeShownInCategory,
170175
?array $expectedProductRewrite
171176
): void {
172-
$this->connectionMock->expects($this->any())
173-
->method('fetchRow')
174-
->will($this->onConsecutiveCalls($productFromDb, $categoryFromDb));
177+
$this->fetchDataMock($productFromDb, $categoryFromDb);
175178

176179
$scopeConfigMap = [
177180
[
@@ -347,6 +350,68 @@ public static function findProductRewriteByRequestPathDataProvider(): array
347350
'redirect_type' => OptionProvider::PERMANENT,
348351
]
349352
],
353+
[
354+
// Category has product url key at the beginning of its url key
355+
[
356+
'request_path' => 'test-category/test-sub-category/test',
357+
'store_id' => 1
358+
],
359+
[
360+
'entity_type' => 'product',
361+
'entity_id' => '1',
362+
'request_path' => 'test',
363+
'target_path' => 'catalog/product/view/id/1',
364+
'redirect_type' => '0',
365+
],
366+
'',
367+
[
368+
'entity_type' => 'category',
369+
'entity_id' => '38',
370+
'request_path' => 'test-category/test-sub-category',
371+
'target_path' => 'catalog/category/view/id/38',
372+
'redirect_type' => '0',
373+
],
374+
true,
375+
[
376+
'entity_type' => 'product',
377+
'entity_id' => '1',
378+
'request_path' => 'test-category/test-sub-category/test',
379+
'target_path' => 'catalog/product/view/id/1/category/38',
380+
'redirect_type' => '0',
381+
]
382+
],
350383
];
351384
}
385+
386+
/**
387+
* @param array|false $productFromDb
388+
* @param array|false $categoryFromDb
389+
*
390+
* @return void
391+
*/
392+
private function fetchDataMock($productFromDb, $categoryFromDb): void
393+
{
394+
$selectMock = $this->selectMock;
395+
$this->selectMock->expects($this->any())
396+
->method('where')
397+
->willReturnCallback(function ($string, $value) use ($selectMock) {
398+
if ($string == 'url_rewrite.request_path IN (?)') {
399+
$this->requestPath = array_shift($value);
400+
}
401+
return $selectMock;
402+
});
403+
$this->connectionMock->expects($this->any())
404+
->method('fetchRow')
405+
->willReturnCallback(function () use ($productFromDb, $categoryFromDb) {
406+
switch (true) {
407+
case $productFromDb && $productFromDb['request_path'] == $this->requestPath:
408+
return $productFromDb;
409+
case $categoryFromDb && $categoryFromDb['request_path'] == $this->requestPath:
410+
return $categoryFromDb;
411+
default:
412+
return false;
413+
}
414+
})
415+
;
416+
}
352417
}

0 commit comments

Comments
 (0)