Skip to content

Commit 1fc3b76

Browse files
authored
ENGCOM-3476: Don't return categoryId from registry if the product doesn't belong in the current category #19232
2 parents 8a2cee1 + 25bcd17 commit 1fc3b76

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
@@ -727,7 +727,7 @@ public function getIdBySku($sku)
727727
public function getCategoryId()
728728
{
729729
$category = $this->_registry->registry('current_category');
730-
if ($category) {
730+
if ($category && in_array($category->getId(), $this->getCategoryIds())) {
731731
return $category->getId();
732732
}
733733
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
@@ -549,6 +549,7 @@ public function testSetCategoryCollection()
549549

550550
public function testGetCategory()
551551
{
552+
$this->model->setData('category_ids', [10]);
552553
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));
553554
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
554555
$this->categoryRepository->expects($this->any())->method('get')->will($this->returnValue($this->category));
@@ -557,14 +558,23 @@ public function testGetCategory()
557558

558559
public function testGetCategoryId()
559560
{
560-
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
561+
$this->model->setData('category_ids', [10]);
562+
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));
561563

562564
$this->registry->expects($this->at(0))->method('registry');
563565
$this->registry->expects($this->at(1))->method('registry')->will($this->returnValue($this->category));
564566
$this->assertFalse($this->model->getCategoryId());
565567
$this->assertEquals(10, $this->model->getCategoryId());
566568
}
567569

570+
public function testGetCategoryIdWhenProductNotInCurrentCategory()
571+
{
572+
$this->model->setData('category_ids', [12]);
573+
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
574+
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
575+
$this->assertFalse($this->model->getCategoryId());
576+
}
577+
568578
public function testGetIdBySku()
569579
{
570580
$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)