Skip to content

Commit

Permalink
Don't return categoryId from registry if the product doesn't belong i…
Browse files Browse the repository at this point in the history
…n current category
  • Loading branch information
ErikPel committed Nov 20, 2018
1 parent 072ef37 commit a5dd6dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ public function getIdBySku($sku)
public function getCategoryId()
{
$category = $this->_registry->registry('current_category');
if ($category) {
if ($category && in_array($category->getId(), $this->getCategoryIds())) {
return $category->getId();
}
return false;
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ public function testSetCategoryCollection()

public function testGetCategory()
{
$this->model->setData('category_ids', [10]);
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
$this->categoryRepository->expects($this->any())->method('get')->will($this->returnValue($this->category));
Expand All @@ -557,14 +558,23 @@ public function testGetCategory()

public function testGetCategoryId()
{
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
$this->model->setData('category_ids', [10]);
$this->category->expects($this->any())->method('getId')->will($this->returnValue(10));

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

public function testGetCategoryIdWhenProductNotInCurrentCategory()
{
$this->model->setData('category_ids', [12]);
$this->category->expects($this->once())->method('getId')->will($this->returnValue(10));
$this->registry->expects($this->any())->method('registry')->will($this->returnValue($this->category));
$this->assertFalse($this->model->getCategoryId());
}

public function testGetIdBySku()
{
$this->resource->expects($this->once())->method('getIdBySku')->will($this->returnValue(5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testGetCategoryId()
{
$this->assertFalse($this->_model->getCategoryId());
$category = new \Magento\Framework\DataObject(['id' => 5]);

$this->_model->setCategoryIds([5]);
$this->objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
try {
$this->assertEquals(5, $this->_model->getCategoryId());
Expand All @@ -83,6 +83,7 @@ public function testGetCategoryId()
public function testGetCategory()
{
$this->assertEmpty($this->_model->getCategory());
$this->_model->setCategoryIds([3]);

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

0 comments on commit a5dd6dc

Please sign in to comment.