Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return categoryId from registry if the product doesn't belong in the current category #19232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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