Skip to content

Commit 8059762

Browse files
committed
MC-39722: Create automated test for: "Dropdown attribute in flat product"
1 parent 5ee9e62 commit 8059762

File tree

1 file changed

+107
-37
lines changed
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/Action

1 file changed

+107
-37
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/Action/FullTest.php

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,49 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Model\Indexer\Product\Flat\Action;
79

810
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
912
use Magento\Catalog\Block\Product\ListProduct;
1013
use Magento\Catalog\Model\CategoryFactory;
1114
use Magento\Catalog\Model\Indexer\Product\Flat\Processor;
1215
use Magento\Catalog\Model\Indexer\Product\Flat\State;
1316
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
17+
use Magento\Catalog\Model\ResourceModel\Product\Flat as FlatResource;
1418
use Magento\CatalogSearch\Model\Indexer\Fulltext;
19+
use Magento\Eav\Api\AttributeOptionManagementInterface;
1520
use Magento\Framework\Indexer\IndexerRegistry;
21+
use Magento\Framework\ObjectManagerInterface;
1622
use Magento\Store\Model\StoreManagerInterface;
1723
use Magento\TestFramework\Helper\Bootstrap;
18-
use Magento\TestFramework\ObjectManager;
24+
use Magento\TestFramework\Indexer\TestCase;
1925

2026
/**
2127
* Full reindex Test
2228
*/
23-
class FullTest extends \Magento\TestFramework\Indexer\TestCase
29+
class FullTest extends TestCase
2430
{
25-
/**
26-
* @var State
27-
*/
28-
protected $_state;
31+
/** @var State */
32+
protected $state;
2933

30-
/**
31-
* @var Processor
32-
*/
33-
protected $_processor;
34+
/** @var Processor */
35+
protected $processor;
3436

35-
/**
36-
* @var ObjectManager
37-
*/
37+
/** @var ObjectManagerInterface */
3838
private $objectManager;
3939

40+
/** @var FlatResource */
41+
private $flatResource;
42+
43+
/** @var AttributeOptionManagementInterface */
44+
private $optionManagement;
45+
46+
/** @var ProductRepositoryInterface */
47+
private $productRepository;
48+
4049
/**
4150
* @inheritdoc
4251
*/
@@ -58,21 +67,28 @@ public static function setUpBeforeClass(): void
5867
*/
5968
protected function setUp(): void
6069
{
70+
parent::setUp();
71+
6172
$this->objectManager = Bootstrap::getObjectManager();
62-
$this->_state = $this->objectManager->get(State::class);
63-
$this->_processor = $this->objectManager->get(Processor::class);
73+
$this->state = $this->objectManager->get(State::class);
74+
$this->processor = $this->objectManager->get(Processor::class);
75+
$this->flatResource = $this->objectManager->get(FlatResource::class);
76+
$this->optionManagement = $this->objectManager->get(AttributeOptionManagementInterface::class);
77+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
6478
}
6579

6680
/**
6781
* @magentoDbIsolation disabled
6882
* @magentoAppIsolation enabled
6983
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
7084
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
85+
*
86+
* @return void
7187
*/
72-
public function testReindexAll()
88+
public function testReindexAll(): void
7389
{
74-
$this->assertTrue($this->_state->isFlatEnabled());
75-
$this->_processor->reindexAll();
90+
$this->assertTrue($this->state->isFlatEnabled());
91+
$this->processor->reindexAll();
7692

7793
$categoryFactory = $this->objectManager->get(CategoryFactory::class);
7894
$listProduct = $this->objectManager->get(ListProduct::class);
@@ -98,11 +114,13 @@ public function testReindexAll()
98114
* @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
99115
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
100116
* @magentoConfigFixture fixturestore_store catalog/frontend/flat_catalog_product 1
117+
*
118+
* @return void
101119
*/
102-
public function testReindexAllMultipleStores()
120+
public function testReindexAllMultipleStores(): void
103121
{
104-
$this->assertTrue($this->_state->isFlatEnabled());
105-
$this->_processor->reindexAll();
122+
$this->assertTrue($this->state->isFlatEnabled());
123+
$this->processor->reindexAll();
106124

107125
/** @var ProductCollectionFactory $productCollectionFactory */
108126
$productCollectionFactory = $this->objectManager->create(ProductCollectionFactory::class);
@@ -116,24 +134,76 @@ public function testReindexAllMultipleStores()
116134
$store->getId() => 'StoreTitle',
117135
];
118136

119-
foreach ($expectedData as $storeId => $productName) {
120-
$storeManager->setCurrentStore($storeId);
121-
$productCollection = $productCollectionFactory->create();
122-
123-
$this->assertTrue(
124-
$productCollection->isEnabledFlat(),
125-
'Flat should be enabled for product collection.'
126-
);
137+
try {
138+
foreach ($expectedData as $storeId => $productName) {
139+
$storeManager->setCurrentStore($storeId);
140+
$productCollection = $productCollectionFactory->create();
141+
142+
$this->assertTrue(
143+
$productCollection->isEnabledFlat(),
144+
'Flat should be enabled for product collection.'
145+
);
146+
147+
$productCollection->addIdFilter(1)->addAttributeToSelect(ProductInterface::NAME);
148+
149+
$this->assertEquals(
150+
$productName,
151+
$productCollection->getFirstItem()->getName(),
152+
'Wrong product name specified per store.'
153+
);
154+
}
155+
} finally {
156+
$storeManager->setCurrentStore($currentStore);
157+
}
158+
}
127159

128-
$productCollection->addIdFilter(1)->addAttributeToSelect(ProductInterface::NAME);
160+
/**
161+
* @magentoDbIsolation disabled
162+
*
163+
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
164+
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
165+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
166+
*
167+
* @return void
168+
*/
169+
public function testCheckDropdownAttributeInFlat(): void
170+
{
171+
$attributeCode = 'dropdown_attribute';
172+
$options = $this->optionManagement->getItems($this->flatResource->getTypeId(), $attributeCode);
173+
$attributeValue = $options[1]->getValue();
174+
$this->updateProduct('simple2', $attributeCode, $attributeValue);
175+
$this->processor->reindexAll();
176+
$this->assertFlatColumnValue($attributeCode, $attributeValue);
177+
}
129178

130-
$this->assertEquals(
131-
$productName,
132-
$productCollection->getFirstItem()->getName(),
133-
'Wrong product name specified per store.'
134-
);
135-
}
179+
/**
180+
* Assert if column exist and column value in flat table
181+
*
182+
* @param string $attributeCode
183+
* @param string $value
184+
* @return void
185+
*/
186+
private function assertFlatColumnValue(string $attributeCode, string $value): void
187+
{
188+
$connect = $this->flatResource->getConnection();
189+
$tableName = $this->flatResource->getFlatTableName();
190+
$this->assertTrue($connect->tableColumnExists($tableName, $attributeCode));
191+
$select = $connect->select()->from($tableName, $attributeCode);
192+
$this->assertEquals($value, $connect->fetchOne($select));
193+
}
136194

137-
$storeManager->setCurrentStore($currentStore);
195+
/**
196+
* Update product
197+
*
198+
* @param string $sku
199+
* @param string $attributeCode
200+
* @param string $value
201+
* @return void
202+
*/
203+
private function updateProduct(string $sku, string $attributeCode, string $value): void
204+
{
205+
$product = $this->productRepository->get($sku);
206+
$product->setData($attributeCode, $value);
207+
$this->productRepository->save($product);
138208
}
139209
}

0 commit comments

Comments
 (0)