Skip to content

Commit 7900322

Browse files
author
Oleksii Korshenko
authored
ENGCOM-3859: [Backport] Don't return categoryId from registry if the product doesn't belong in the current category #20286
2 parents a32e880 + 99c69fe commit 7900322

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

app/code/Magento/Catalog/Model/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ public function getIdBySku($sku)
714714
public function getCategoryId()
715715
{
716716
$category = $this->_registry->registry('current_category');
717-
if ($category) {
717+
if ($category && in_array($category->getId(), $this->getCategoryIds())) {
718718
return $category->getId();
719719
}
720720
return false;

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ public function testSetCategoryCollection()
543543

544544
public function testGetCategory()
545545
{
546+
$this->model->setData('category_ids', [10]);
546547
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));
547548
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
548549
$this->categoryRepository->expects($this->any())->method('get')->will($this->returnValue($this->category));
@@ -551,14 +552,23 @@ public function testGetCategory()
551552

552553
public function testGetCategoryId()
553554
{
554-
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
555+
$this->model->setData('category_ids', [10]);
556+
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));
555557

556558
$this->registry->expects($this->at(0))->method('registry');
557559
$this->registry->expects($this->at(1))->method('registry')->will($this->returnValue($this->category));
558560
$this->assertFalse($this->model->getCategoryId());
559561
$this->assertEquals(10, $this->model->getCategoryId());
560562
}
561563

564+
public function testGetCategoryIdWhenProductNotInCurrentCategory()
565+
{
566+
$this->model->setData('category_ids', [12]);
567+
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
568+
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
569+
$this->assertFalse($this->model->getCategoryId());
570+
}
571+
562572
public function testGetIdBySku()
563573
{
564574
$this->resource->expects($this->once())->method('getIdBySku')->will($this->returnValue(5));

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductExternalTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testGetCategoryId()
6969
{
7070
$this->assertFalse($this->_model->getCategoryId());
7171
$category = new \Magento\Framework\DataObject(['id' => 5]);
72-
72+
$this->_model->setCategoryIds([5]);
7373
$this->objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
7474
try {
7575
$this->assertEquals(5, $this->_model->getCategoryId());
@@ -83,6 +83,7 @@ public function testGetCategoryId()
8383
public function testGetCategory()
8484
{
8585
$this->assertEmpty($this->_model->getCategory());
86+
$this->_model->setCategoryIds([3]);
8687

8788
$this->objectManager->get(\Magento\Framework\Registry::class)
8889
->register('current_category', new \Magento\Framework\DataObject(['id' => 3]));

0 commit comments

Comments
 (0)