Skip to content

Commit 3751594

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-67438-Widget-Conditions' into Okapis-develop-pr
2 parents d2925dc + 2431128 commit 3751594

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

app/code/Magento/CatalogWidget/Block/Product/ProductsList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ public function createCollection()
237237
$conditions->collectValidatedAttributes($collection);
238238
$this->sqlBuilder->attachConditionToCollection($collection, $conditions);
239239

240+
/**
241+
* Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches
242+
* several allowed values from condition simultaneously
243+
*/
244+
$collection->distinct(true);
245+
240246
return $collection;
241247
}
242248

app/code/Magento/CatalogWidget/Test/Unit/Block/Product/ProductsListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
269269
'addStoreFilter',
270270
'setPageSize',
271271
'setCurPage',
272+
'distinct'
272273
])->disableOriginalConstructor()
273274
->getMock();
274275
$collection->expects($this->once())->method('setVisibility')
@@ -282,6 +283,7 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
282283
$collection->expects($this->once())->method('addStoreFilter')->willReturnSelf();
283284
$collection->expects($this->once())->method('setPageSize')->with($expectedPageSize)->willReturnSelf();
284285
$collection->expects($this->once())->method('setCurPage')->willReturnSelf();
286+
$collection->expects($this->once())->method('distinct')->willReturnSelf();
285287

286288
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
287289
$this->productsList->setData('conditions_encoded', 'some_serialized_conditions');

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/SourceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public function testReindexMultiselectAttribute()
114114
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/
115115
$attr = $objectManager->get(\Magento\Eav\Model\Config::class)
116116
->getAttribute('catalog_product', 'multiselect_attribute');
117-
$attr->setIsFilterable(1)->save();
118117

119118
/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
120119
$options = $objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class);

dev/tests/integration/testsuite/Magento/Catalog/_files/multiselect_attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'is_searchable' => 0,
2525
'is_visible_in_advanced_search' => 0,
2626
'is_comparable' => 0,
27-
'is_filterable' => 0,
27+
'is_filterable' => 1,
2828
'is_filterable_in_search' => 0,
2929
'is_used_for_promo_rules' => 0,
3030
'is_html_allowed_on_front' => 1,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogWidget\Block\Product;
8+
9+
/**
10+
* Tests for @see \Magento\CatalogWidget\Block\Product\ProductsList
11+
*/
12+
class ProductListTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\CatalogWidget\Block\Product\ProductsList
16+
*/
17+
protected $block;
18+
19+
/**
20+
* @var \Magento\Framework\ObjectManagerInterface
21+
*/
22+
protected $objectManager;
23+
24+
protected function setUp()
25+
{
26+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
27+
$this->block = $this->objectManager->create(
28+
\Magento\CatalogWidget\Block\Product\ProductsList::class
29+
);
30+
}
31+
32+
/**
33+
* Make sure that widget conditions are applied to product collection correctly
34+
*
35+
* 1. Create new multiselect attribute with several options
36+
* 2. Create 2 new products and select at least 2 multiselect options for one of these products
37+
* 3. Create product list widget condition based on the new multiselect attribute
38+
* 4. Set at least 2 options of multiselect attribute to match products for the product list widget
39+
* 5. Load collection for product list widget and make sure that number of loaded products is correct
40+
*
41+
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
42+
*/
43+
public function testCreateCollection()
44+
{
45+
// Reindex EAV attributes to enable products filtration by created multiselect attribute
46+
/** @var \Magento\Catalog\Model\Indexer\Product\Eav\Processor $eavIndexerProcessor */
47+
$eavIndexerProcessor = $this->objectManager->get(
48+
\Magento\Catalog\Model\Indexer\Product\Eav\Processor::class
49+
);
50+
$eavIndexerProcessor->reindexAll();
51+
52+
// Prepare conditions
53+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
54+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
55+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
56+
);
57+
$attribute->load('multiselect_attribute', 'attribute_code');
58+
$multiselectAttributeOptionIds = [];
59+
foreach ($attribute->getOptions() as $option) {
60+
if ($option->getValue()) {
61+
$multiselectAttributeOptionIds[] = $option->getValue();
62+
}
63+
}
64+
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,'
65+
. '`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:'
66+
. '^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,'
67+
. '`attribute`:`multiselect_attribute`,`operator`:`^[^]`,'
68+
. '`value`:[`' . implode(',', $multiselectAttributeOptionIds) . '`]^]^]';
69+
$this->block->setData('conditions_encoded', $encodedConditions);
70+
71+
// Load products collection filtered using specified conditions and perform assesrions
72+
$productCollection = $this->block->createCollection();
73+
$productCollection->load();
74+
$this->assertEquals(
75+
1,
76+
$productCollection->count(),
77+
"Product collection was not filtered according to the widget condition."
78+
);
79+
}
80+
}

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/Widget/ConditionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ConditionsTest extends \PHPUnit_Framework_TestCase
1717
protected $block;
1818

1919
/**
20-
* @var \Magento\Framework\ObjectManagerInt
20+
* @var \Magento\Framework\ObjectManagerInterface
2121
*/
2222
protected $objectManager;
2323

0 commit comments

Comments
 (0)