|
| 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 | +} |
0 commit comments