Skip to content

Commit 45fe30b

Browse files
committed
AC-1883::[GraphQl] Categories from root categories of other websites are returned.
1 parent 5d9fe40 commit 45fe30b

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/AbstractAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ protected function fillTempCategoryTreeIndex($temporaryName)
717717
}
718718

719719
/**
720-
* Retrieve select for reindex products of non anchor categories
720+
* Retrieve select for reindex products of anchor categories
721721
*
722722
* @param Store $store
723723
* @return Select

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductCategories.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ public function getCategoryIdsByProduct(int $productId, int $storeId)
6868
->joinInner(['store' => $storeTable], $connection->quoteInto('store.store_id = ?', $storeId), [])
6969
->joinInner(
7070
['store_group' => $storeGroupTable],
71-
'store.group_id = store_group.group_id AND cat_index.category_id != store_group.root_category_id',
72-
[]
71+
$connection->quoteInto('store.group_id = store_group.group_id AND NOT EXISTS
72+
(SELECT 1 FROM store_group WHERE cat_index.category_id IN (store_group.root_category_id)
73+
and cat_index.product_id = ?)',
74+
$productId,
75+
\Zend_Db::INT_TYPE
76+
),
7377
)
7478
->where('product_id = ?', $productId);
7579

76-
$categoryIds = $connection->fetchCol($select);
77-
78-
return $categoryIds;
80+
return $connection->fetchCol($select);
7981
}
8082

8183
/**
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver\Product;
9+
10+
use Magento\CatalogGraphQl\Model\Resolver\Product\ProductCategories;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\DB\Select;
14+
use Magento\Framework\Indexer\DimensionFactory;
15+
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @see ProductCategories
21+
*/
22+
class ProductCategoriesTest extends TestCase
23+
{
24+
/**
25+
* @var ProductCategories
26+
*/
27+
private ProductCategories $productCategories;
28+
29+
/**
30+
* @var IndexScopeResolver|MockObject
31+
*/
32+
private IndexScopeResolver $indexScopeResolverMock;
33+
34+
/**
35+
* @var ResourceConnection|MockObject
36+
*/
37+
private ResourceConnection $resourceConnectionMock;
38+
39+
/**
40+
* @var AdapterInterface|MockObject
41+
*/
42+
private AdapterInterface $adapterInterfaceMock;
43+
44+
/**
45+
* @var DimensionFactory|MockObject
46+
*/
47+
private DimensionFactory $dimensionFactoryMock;
48+
49+
/**
50+
* @var Select|MockObject
51+
*/
52+
private Select $selectMock;
53+
54+
protected function setUp(): void
55+
{
56+
$this->indexScopeResolverMock = $this->createMock(IndexScopeResolver::class);
57+
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
58+
$this->dimensionFactoryMock = $this->createMock(DimensionFactory::class);
59+
$this->adapterInterfaceMock = $this->createMock(AdapterInterface::class);
60+
$this->selectMock = $this->createMock(Select::class);
61+
$this->productCategories = new ProductCategories(
62+
$this->indexScopeResolverMock,
63+
$this->resourceConnectionMock,
64+
$this->dimensionFactoryMock
65+
);
66+
}
67+
68+
public function testGetCategoryIdsByProduct(): void
69+
{
70+
$this->selectMock
71+
->expects($this->once())
72+
->method('from')
73+
->willReturnSelf();
74+
$this->selectMock
75+
->expects($this->exactly(2))
76+
->method('joinInner')
77+
->willReturnSelf();
78+
$this->selectMock
79+
->expects($this->once())
80+
->method('where')
81+
->willReturnSelf();
82+
$this->adapterInterfaceMock
83+
->expects($this->once())
84+
->method('select')
85+
->willReturn($this->selectMock);
86+
$this->adapterInterfaceMock
87+
->expects($this->once())
88+
->method('fetchCol')
89+
->willReturn([]);
90+
$this->adapterInterfaceMock
91+
->expects($this->exactly(2))
92+
->method('quoteInto')
93+
->willReturn('');
94+
95+
$this->resourceConnectionMock
96+
->expects($this->once())
97+
->method('getConnection')
98+
->willReturn($this->adapterInterfaceMock);
99+
$this->resourceConnectionMock->expects($this->exactly(2))->method('getTableName')->willReturn('TableName');
100+
101+
$this->productCategories->getCategoryIdsByProduct(1, 1);
102+
}
103+
}

0 commit comments

Comments
 (0)