Skip to content

Commit

Permalink
Changed so that store view category is always used to create product …
Browse files Browse the repository at this point in the history
…URLs
  • Loading branch information
Barny Shergold authored and Barny Shergold committed Jun 10, 2020
1 parent 8513dfd commit 3409af0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ 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);
// Category should be loaded per appropriate store at all times. This is because whilst the URL key on the
// category in focus might be unchanged, parent category URL keys might be
$categories[] = $this->categoryRepository->get($category->getEntityId(), $storeId);
}

$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
Expand Down Expand Up @@ -234,29 +235,6 @@ public function isCategoryProperForGenerating(Category $category, $storeId)
return false;
}

/**
* Check if URL key has been changed
*
* Checks if URL key has been changed for provided category and returns reloaded category,
* in other case - returns provided category.
*
* @param int $storeId
* @param Category $category
* @return Category
*/
private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
{
$isUrlKeyOverridden = $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
$storeId,
$category->getEntityId(),
Category::ENTITY
);

if (!$isUrlKeyOverridden) {
return $category;
}
return $this->categoryRepository->get($category->getEntityId(), $storeId);
}

/**
* Check config value of generate_category_product_rewrites
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;

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

0 comments on commit 3409af0

Please sign in to comment.