Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG - Prevent Magento from Creating incorrect URLs for category/products when Store View has modified URL keys #28676

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

/**
* Class ProductScopeRewriteGenerator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the original code but I've removed it anyway.

*
* Generates Product/Category URLs for different scopes
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ProductScopeRewriteGenerator
Expand Down Expand Up @@ -174,7 +177,6 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
continue;
}

// category should be loaded per appropriate store if category's URL key has been changed
$categories[] = $this->getCategoryWithOverriddenUrlKey($storeId, $category);
}

Expand Down Expand Up @@ -252,9 +254,14 @@ private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
Category::ENTITY
);

if (!$isUrlKeyOverridden) {
// Category should be loaded per appropriate store at all times. This is because whilst the URL key on the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use PHP Doc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// category in focus might be unchanged, parent category URL keys might be. If the category store ID
// and passed store ID are the same then return current category as it is correct but may have changed in memory

if (!$isUrlKeyOverridden && $storeId == $category->getStoreId()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '==='

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return $category;
}

return $this->categoryRepository->get($category->getEntityId(), $storeId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogUrlRewrite\Test\Unit\Model;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\ObjectRegistry;
Expand Down Expand Up @@ -69,6 +70,9 @@ class ProductScopeRewriteGeneratorTest extends TestCase
/** @var ScopeConfigInterface|MockObject */
private $configMock;

/** @var CategoryRepositoryInterface|MockObject */
private $categoryRepository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suffix with 'Mock'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


protected function setUp(): void
{
$this->serializer = $this->createMock(Json::class);
Expand Down Expand Up @@ -126,6 +130,8 @@ function ($value) {
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();

$this->categoryRepository = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);

$this->productScopeGenerator = (new ObjectManager($this))->getObject(
ProductScopeRewriteGenerator::class,
[
Expand All @@ -137,7 +143,8 @@ function ($value) {
'storeViewService' => $this->storeViewService,
'storeManager' => $this->storeManager,
'mergeDataProviderFactory' => $mergeDataProviderFactory,
'config' => $this->configMock
'config' => $this->configMock,
'categoryRepository' => $this->categoryRepository
]
);
$this->categoryMock = $this->getMockBuilder(Category::class)
Expand Down Expand Up @@ -215,6 +222,8 @@ public function testGenerationForSpecificStore()
$this->anchorUrlRewriteGenerator->expects($this->any())->method('generate')
->willReturn([]);

$this->categoryRepository->expects($this->once())->method('get')->willReturn($this->categoryMock);

$this->assertEquals(
['category-1_1' => $canonical],
$this->productScopeGenerator->generateForSpecificStoreView(1, [$this->categoryMock], $product, 1)
Expand Down