Skip to content

Commit

Permalink
MAGETWO-82972: GITHUB-8970: Cannot assign products to categories not …
Browse files Browse the repository at this point in the history
…under tree root. #11817
  • Loading branch information
Oleksii Korshenko authored Nov 2, 2017
2 parents 148da00 + 1579a0c commit 97d5dcc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
*/
public function isCategoryProperForGenerating(Category $category, $storeId)
{
if ($category->getParentId() != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
list(, $rootCategoryId) = $category->getParentIds();
$parentIds = $category->getParentIds();
if (count($parentIds) >= 2) {
$rootCategoryId = $parentIds[1];
return $rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId();
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializer;

/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
private $categoryMock;

public function setUp()
{
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
Expand Down Expand Up @@ -83,6 +86,10 @@ function ($value) {
$this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class)
->disableOriginalConstructor()->getMock();
$this->storeManager = $this->createMock(StoreManagerInterface::class);
$storeRootCategoryId = 2;
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
$store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId));
$this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
$mergeDataProviderFactory = $this->createPartialMock(
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
['create']
Expand All @@ -103,6 +110,7 @@ function ($value) {
'mergeDataProviderFactory' => $mergeDataProviderFactory
]
);
$this->categoryMock = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();
}

public function testGenerationForGlobalScope()
Expand All @@ -112,12 +120,6 @@ public function testGenerationForGlobalScope()
$product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1]));
$this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
->will($this->returnValue(false));
$categoryMock = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()
->getMock();
$categoryMock->expects($this->once())
->method('getParentId')
->willReturn(1);
$this->initObjectRegistryFactory([]);
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
$canonical->setRequestPath('category-1')
Expand Down Expand Up @@ -149,25 +151,21 @@ public function testGenerationForGlobalScope()
'category-3_3' => $current,
'category-4_4' => $anchorCategories
],
$this->productScopeGenerator->generateForGlobalScope([$categoryMock], $product, 1)
$this->productScopeGenerator->generateForGlobalScope([$this->categoryMock], $product, 1)
);
}

public function testGenerationForSpecificStore()
{
$storeRootCategoryId = 2;
$category_id = 4;
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
$product->expects($this->any())->method('getStoreId')->will($this->returnValue(1));
$product->expects($this->never())->method('getStoreIds');
$storeRootCategoryId = 'root-for-store-id';
$category = $this->createMock(\Magento\Catalog\Model\Category::class);
$category->expects($this->any())->method('getParentIds')
$this->categoryMock->expects($this->any())->method('getParentIds')
->will($this->returnValue(['root-id', $storeRootCategoryId]));
$category->expects($this->any())->method('getParentId')->will($this->returnValue('parent_id'));
$category->expects($this->any())->method('getId')->will($this->returnValue('category_id'));
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
$store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId));
$this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
$this->initObjectRegistryFactory([$category]);
$this->categoryMock->expects($this->any())->method('getId')->will($this->returnValue($category_id));
$this->initObjectRegistryFactory([$this->categoryMock]);
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
$canonical->setRequestPath('category-1')
->setStoreId(1);
Expand All @@ -184,7 +182,7 @@ public function testGenerationForSpecificStore()

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

Expand Down Expand Up @@ -212,4 +210,40 @@ protected function initObjectRegistryFactory($entities)
->with(['entities' => $entities])
->will($this->returnValue($objectRegistry));
}

/**
* Test the possibility of url rewrite generation.
*
* @param array $parentIds
* @param bool $expectedResult
* @dataProvider isCategoryProperForGeneratingDataProvider
*/
public function testIsCategoryProperForGenerating($parentIds, $expectedResult)
{
$storeId = 1;
$this->categoryMock->expects(self::any())->method('getParentIds')->willReturn($parentIds);
$result = $this->productScopeGenerator->isCategoryProperForGenerating(
$this->categoryMock,
$storeId
);
self::assertEquals(
$expectedResult,
$result
);
}

/**
* Data provider for testIsCategoryProperForGenerating.
*
* @return array
*/
public function isCategoryProperForGeneratingDataProvider()
{
return [
[['0'], false],
[['1'], false],
[['1', '2'], true],
[['1', '3'], false],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@

/** @var CategoryLinkManagementInterface $linkManagement */
$linkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
$linkManagement->assignProductToCategories($product->getSku(), [$category->getEntityId()]);
$linkManagement->assignProductToCategories($product->getSku(), [Category::TREE_ROOT_ID, $category->getEntityId()]);

0 comments on commit 97d5dcc

Please sign in to comment.