Skip to content

Commit bf9d8d7

Browse files
committed
ACP2E-531: [Magento Cloud] Filtering with multiple categories sorting by position not working as expected
1 parent b365145 commit bf9d8d7

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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\Catalog\Test\Unit\Model\ResourceModel\Product\Collection;
9+
10+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\Collection\JoinMinimalPosition;
13+
use Magento\Framework\DB\Adapter\Pdo\Mysql;
14+
use Magento\Framework\DB\Select;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test for JoinMinimalPosition
20+
*/
21+
class JoinMinimalPositionTest extends TestCase
22+
{
23+
/**
24+
* @var TableMaintainer|MockObject
25+
*/
26+
private $tableMaintainer;
27+
28+
/**
29+
* @var JoinMinimalPosition
30+
*/
31+
private $model;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
parent::setUp();
39+
$this->tableMaintainer = $this->createMock(TableMaintainer::class);
40+
$this->model = new JoinMinimalPosition(
41+
$this->tableMaintainer
42+
);
43+
}
44+
45+
/**
46+
* Test that correct SQL is generated
47+
*
48+
* @return void
49+
* @throws \Magento\Framework\Exception\LocalizedException
50+
* @throws \Zend_Db_Select_Exception
51+
*/
52+
public function testExecute(): void
53+
{
54+
$expectedColumns = [
55+
[
56+
'e',
57+
'*',
58+
null
59+
],
60+
[
61+
'at_status',
62+
'value_id',
63+
'status'
64+
],
65+
[
66+
'e',
67+
'visibility',
68+
null
69+
],
70+
[
71+
'e',
72+
new \Zend_Db_Expr('LEAST(IFNULL(cat_index_3.position, ~0), IFNULL(cat_index_5.position, ~0))'),
73+
'cat_index_position'
74+
],
75+
];
76+
$expectedFromParts = [
77+
'e' => [
78+
'joinType' => 'from',
79+
'schema' => null,
80+
'tableName' => 'catalog_product_entity',
81+
'joinCondition' => null,
82+
],
83+
'cat_index_3' => [
84+
'joinType' => 'left join',
85+
'schema' => null,
86+
'tableName' => null,
87+
'joinCondition' => 'cat_index_3.product_id=e.entity_id' .
88+
' AND cat_index_3.store_id=1' .
89+
' AND cat_index_3.category_id=3',
90+
],
91+
'cat_index_5' => [
92+
'joinType' => 'left join',
93+
'schema' => null,
94+
'tableName' => null,
95+
'joinCondition' => 'cat_index_5.product_id=e.entity_id' .
96+
' AND cat_index_5.store_id=1' .
97+
' AND cat_index_5.category_id=5',
98+
]
99+
];
100+
$categoryIds = [3, 5];
101+
$collection = $this->getMockBuilder(Collection::class)
102+
->disableOriginalConstructor()
103+
->onlyMethods(['getConnection', 'getSelect', 'getStoreId'])
104+
->getMockForAbstractClass();
105+
$connection = $this->getMockBuilder(Mysql::class)
106+
->disableOriginalConstructor()
107+
->onlyMethods(['_connect'])
108+
->getMockForAbstractClass();
109+
$select = $this->getMockBuilder(Select::class)
110+
->disableOriginalConstructor()
111+
->getMockForAbstractClass();
112+
113+
$select->reset();
114+
$select->from(['e' => 'catalog_product_entity']);
115+
$select->columns(['cat_index_position' => 'position']);
116+
$select->columns(['status' => 'at_status.value_id']);
117+
$select->columns(['visibility']);
118+
119+
$collection->addStaticField('entity_id');
120+
$collection->method('getConnection')
121+
->willReturn($connection);
122+
$collection->method('getSelect')
123+
->willReturn($select);
124+
$collection->method('getStoreId')
125+
->willReturn(1);
126+
$this->model->execute($collection, $categoryIds);
127+
$this->assertEquals(
128+
$expectedFromParts,
129+
$select->getPart(Select::FROM)
130+
);
131+
$this->assertEquals(
132+
$expectedColumns,
133+
$select->getPart(Select::COLUMNS)
134+
);
135+
}
136+
}

0 commit comments

Comments
 (0)