Skip to content

Commit 872e0fb

Browse files
committed
Merge remote-tracking branch 'tango/MAGETWO-58329' into PR-2008-2.3-tmp
2 parents d53efca + d7784c5 commit 872e0fb

File tree

5 files changed

+149
-49
lines changed

5 files changed

+149
-49
lines changed

app/code/Magento/Rule/Model/Condition/Sql/Builder.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ protected function _joinTablesToCollection(
128128
*
129129
* @param AbstractCondition $condition
130130
* @param string $value
131-
* @param bool $isDefaultStoreUsed
131+
* @param bool $isDefaultStoreUsed no longer used because caused an issue about not existing table alias
132132
* @return string
133133
* @throws \Magento\Framework\Exception\LocalizedException
134+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
134135
*/
135136
protected function _getMappedSqlCondition(
136137
AbstractCondition $condition,
@@ -151,12 +152,6 @@ protected function _getMappedSqlCondition(
151152
}
152153

153154
$defaultValue = 0;
154-
// Check if attribute has a table with default value and add it to the query
155-
if ($this->canAttributeHaveDefaultValue($condition->getAttribute(), $isDefaultStoreUsed)) {
156-
$defaultField = 'at_' . $condition->getAttribute() . '_default.value';
157-
$defaultValue = $this->_connection->quoteIdentifier($defaultField);
158-
}
159-
160155
$sql = str_replace(
161156
':field',
162157
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
@@ -227,45 +222,10 @@ public function attachConditionToCollection(
227222
): void {
228223
$this->_connection = $collection->getResource()->getConnection();
229224
$this->_joinTablesToCollection($collection, $combine);
230-
$isDefaultStoreUsed = $this->checkIsDefaultStoreUsed($collection);
231-
$whereExpression = (string)$this->_getMappedSqlCombination($combine, '', $isDefaultStoreUsed);
225+
$whereExpression = (string)$this->_getMappedSqlCombination($combine);
232226
if (!empty($whereExpression)) {
233227
// Select ::where method adds braces even on empty expression
234228
$collection->getSelect()->where($whereExpression);
235229
}
236230
}
237-
238-
/**
239-
* Check is default store used.
240-
*
241-
* @param AbstractCollection $collection
242-
* @return bool
243-
*/
244-
private function checkIsDefaultStoreUsed(AbstractCollection $collection): bool
245-
{
246-
return (int)$collection->getStoreId() === (int)$collection->getDefaultStoreId();
247-
}
248-
249-
/**
250-
* Check if attribute can have default value.
251-
*
252-
* @param string $attributeCode
253-
* @param bool $isDefaultStoreUsed
254-
* @return bool
255-
*/
256-
private function canAttributeHaveDefaultValue(string $attributeCode, bool $isDefaultStoreUsed): bool
257-
{
258-
if ($isDefaultStoreUsed) {
259-
return false;
260-
}
261-
262-
try {
263-
$attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode);
264-
} catch (NoSuchEntityException $e) {
265-
// It's not exceptional case as we want to check if we have such attribute or not
266-
return false;
267-
}
268-
269-
return !$attribute->isScopeGlobal();
270-
}
271231
}

app/code/Magento/Rule/Test/Unit/Model/Condition/Sql/BuilderTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ public function testAttachConditionToCollection()
6161
$collection->expects($this->any())
6262
->method('getSelect')
6363
->will($this->returnValue($select));
64-
$collection->expects($this->once())
65-
->method('getStoreId')
66-
->willReturn(1);
67-
$collection->expects($this->once())
68-
->method('getDefaultStoreId')
69-
->willReturn(1);
7064

7165
$resource->expects($this->once())
7266
->method('getConnection')
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/**
9+
* Create multiselect attribute
10+
*/
11+
require __DIR__ . '/dropdown_attribute.php';
12+
13+
/** Create products with attribute option of dropdown type */
14+
15+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
16+
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
17+
\Magento\Catalog\Setup\CategorySetup::class
18+
);
19+
20+
/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
21+
$options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
22+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
23+
);
24+
$options->setAttributeFilter($attribute->getId());
25+
$optionIds = $options->getAllIds();
26+
27+
/** @var $product \Magento\Catalog\Model\Product */
28+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
29+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
30+
->setId($optionIds[0] * 10)
31+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
32+
->setWebsiteIds([1])
33+
->setName('With Option 1')
34+
->setSku('simple_op_1')
35+
->setPrice(10)
36+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
37+
->setDropdownAttribute($optionIds[0])
38+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
39+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
40+
->save();
41+
42+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
43+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
44+
->setId($optionIds[1] * 10)
45+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
46+
->setWebsiteIds([1])
47+
->setName('With Option 2')
48+
->setSku('simple_op_2')
49+
->setPrice(10)
50+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
51+
->setDropdownAttribute($optionIds[1])
52+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
53+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
54+
->save();
55+
56+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
57+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
58+
->setId($optionIds[2] * 10)
59+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
60+
->setWebsiteIds([1])
61+
->setName('With Option 3')
62+
->setSku('simple_op_3')
63+
->setPrice(10)
64+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
65+
->setDropdownAttribute($optionIds[2])
66+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
67+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
68+
->save();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
/**
9+
* Remove all products as strategy of isolation process
10+
*/
11+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\Framework\Registry');
12+
$registry->unregister('isSecureArea');
13+
$registry->register('isSecureArea', true);
14+
15+
/** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product */
16+
$productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
17+
->create('Magento\Catalog\Model\Product')
18+
->getCollection();
19+
20+
foreach ($productCollection as $product) {
21+
$product->delete();
22+
}
23+
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,57 @@ public function testCreateCollection()
7878
"Product collection was not filtered according to the widget condition."
7979
);
8080
}
81+
82+
/**
83+
* Test product list widget can process condition with dropdown type of attribute
84+
*
85+
* @magentoDbIsolation disabled
86+
* @magentoDataFixture Magento/Catalog/_files/products_with_dropdown_attribute.php
87+
*/
88+
public function testCreateCollectionWithDropdownAttribute()
89+
{
90+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
91+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
92+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
93+
);
94+
$attribute->load('dropdown_attribute', 'attribute_code');
95+
$dropdownAttributeOptionIds = [];
96+
foreach ($attribute->getOptions() as $option) {
97+
if ($option->getValue()) {
98+
$dropdownAttributeOptionIds[] = $option->getValue();
99+
}
100+
}
101+
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,' .
102+
'`aggregator`:`any`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
103+
'|Condition||Product`,`attribute`:`dropdown_attribute`,`operator`:`==`,`value`:`'
104+
. $dropdownAttributeOptionIds[0] . '`^],`1--2`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
105+
'|Condition||Product`,`attribute`:`dropdown_attribute`,`operator`:`==`,`value`:`'
106+
. $dropdownAttributeOptionIds[1] . '`^]^]';
107+
$this->block->setData('conditions_encoded', $encodedConditions);
108+
$this->performAssertions(2);
109+
$attribute->setUsedInProductListing(0);
110+
$attribute->save();
111+
$this->performAssertions(2);
112+
$attribute->setIsGlobal(1);
113+
$attribute->save();
114+
$this->performAssertions(2);
115+
}
116+
117+
/**
118+
* Check product collection includes correct amount of products.
119+
*
120+
* @param int $count
121+
* @return void
122+
*/
123+
private function performAssertions(int $count)
124+
{
125+
// Load products collection filtered using specified conditions and perform assertions.
126+
$productCollection = $this->block->createCollection();
127+
$productCollection->load();
128+
$this->assertEquals(
129+
$count,
130+
$productCollection->count(),
131+
"Product collection was not filtered according to the widget condition."
132+
);
133+
}
81134
}

0 commit comments

Comments
 (0)