33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Catalog \Model \Indexer \Product \Flat \Action ;
79
810use Magento \Catalog \Api \Data \ProductInterface ;
11+ use Magento \Catalog \Api \ProductRepositoryInterface ;
912use Magento \Catalog \Block \Product \ListProduct ;
1013use Magento \Catalog \Model \CategoryFactory ;
1114use Magento \Catalog \Model \Indexer \Product \Flat \Processor ;
1215use Magento \Catalog \Model \Indexer \Product \Flat \State ;
1316use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
17+ use Magento \Catalog \Model \ResourceModel \Product \Flat as FlatResource ;
1418use Magento \CatalogSearch \Model \Indexer \Fulltext ;
19+ use Magento \Eav \Api \AttributeOptionManagementInterface ;
1520use Magento \Framework \Indexer \IndexerRegistry ;
21+ use Magento \Framework \ObjectManagerInterface ;
1622use Magento \Store \Model \StoreManagerInterface ;
1723use 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+ private $ state ;
2933
30- /**
31- * @var Processor
32- */
33- protected $ _processor ;
34+ /** @var Processor */
35+ private $ 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+
49+ /** @var Full */
50+ private $ action ;
51+
4052 /**
4153 * @inheritdoc
4254 */
@@ -58,21 +70,29 @@ public static function setUpBeforeClass(): void
5870 */
5971 protected function setUp (): void
6072 {
73+ parent ::setUp ();
74+
6175 $ this ->objectManager = Bootstrap::getObjectManager ();
62- $ this ->_state = $ this ->objectManager ->get (State::class);
63- $ this ->_processor = $ this ->objectManager ->get (Processor::class);
76+ $ this ->state = $ this ->objectManager ->get (State::class);
77+ $ this ->processor = $ this ->objectManager ->get (Processor::class);
78+ $ this ->flatResource = $ this ->objectManager ->get (FlatResource::class);
79+ $ this ->optionManagement = $ this ->objectManager ->get (AttributeOptionManagementInterface::class);
80+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
81+ $ this ->action = $ this ->objectManager ->get (Full::class);
6482 }
6583
6684 /**
6785 * @magentoDbIsolation disabled
6886 * @magentoAppIsolation enabled
6987 * @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
7088 * @magentoDataFixture Magento/Catalog/_files/product_simple.php
89+ *
90+ * @return void
7191 */
72- public function testReindexAll ()
92+ public function testReindexAll (): void
7393 {
74- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
75- $ this ->_processor ->reindexAll ();
94+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
95+ $ this ->processor ->reindexAll ();
7696
7797 $ categoryFactory = $ this ->objectManager ->get (CategoryFactory::class);
7898 $ listProduct = $ this ->objectManager ->get (ListProduct::class);
@@ -98,11 +118,13 @@ public function testReindexAll()
98118 * @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
99119 * @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
100120 * @magentoConfigFixture fixturestore_store catalog/frontend/flat_catalog_product 1
121+ *
122+ * @return void
101123 */
102- public function testReindexAllMultipleStores ()
124+ public function testReindexAllMultipleStores (): void
103125 {
104- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
105- $ this ->_processor ->reindexAll ();
126+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
127+ $ this ->processor ->reindexAll ();
106128
107129 /** @var ProductCollectionFactory $productCollectionFactory */
108130 $ productCollectionFactory = $ this ->objectManager ->create (ProductCollectionFactory::class);
@@ -116,24 +138,76 @@ public function testReindexAllMultipleStores()
116138 $ store ->getId () => 'StoreTitle ' ,
117139 ];
118140
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- );
141+ try {
142+ foreach ($ expectedData as $ storeId => $ productName ) {
143+ $ storeManager ->setCurrentStore ($ storeId );
144+ $ productCollection = $ productCollectionFactory ->create ();
145+
146+ $ this ->assertTrue (
147+ $ productCollection ->isEnabledFlat (),
148+ 'Flat should be enabled for product collection. '
149+ );
150+
151+ $ productCollection ->addIdFilter (1 )->addAttributeToSelect (ProductInterface::NAME );
152+
153+ $ this ->assertEquals (
154+ $ productName ,
155+ $ productCollection ->getFirstItem ()->getName (),
156+ 'Wrong product name specified per store. '
157+ );
158+ }
159+ } finally {
160+ $ storeManager ->setCurrentStore ($ currentStore );
161+ }
162+ }
127163
128- $ productCollection ->addIdFilter (1 )->addAttributeToSelect (ProductInterface::NAME );
164+ /**
165+ * @magentoDbIsolation disabled
166+ *
167+ * @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
168+ * @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
169+ * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
170+ *
171+ * @return void
172+ */
173+ public function testCheckDropdownAttributeInFlat (): void
174+ {
175+ $ attributeCode = 'dropdown_attribute ' ;
176+ $ options = $ this ->optionManagement ->getItems ($ this ->flatResource ->getTypeId (), $ attributeCode );
177+ $ attributeValue = $ options [1 ]->getValue ();
178+ $ this ->updateProduct ('simple2 ' , $ attributeCode , $ attributeValue );
179+ $ this ->action ->execute ();
180+ $ this ->assertFlatColumnValue ($ attributeCode , $ attributeValue );
181+ }
129182
130- $ this ->assertEquals (
131- $ productName ,
132- $ productCollection ->getFirstItem ()->getName (),
133- 'Wrong product name specified per store. '
134- );
135- }
183+ /**
184+ * Assert if column exist and column value in flat table
185+ *
186+ * @param string $attributeCode
187+ * @param string $value
188+ * @return void
189+ */
190+ private function assertFlatColumnValue (string $ attributeCode , string $ value ): void
191+ {
192+ $ connect = $ this ->flatResource ->getConnection ();
193+ $ tableName = $ this ->flatResource ->getFlatTableName ();
194+ $ this ->assertTrue ($ connect ->tableColumnExists ($ tableName , $ attributeCode ));
195+ $ select = $ connect ->select ()->from ($ tableName , $ attributeCode );
196+ $ this ->assertEquals ($ value , $ connect ->fetchOne ($ select ));
197+ }
136198
137- $ storeManager ->setCurrentStore ($ currentStore );
199+ /**
200+ * Update product
201+ *
202+ * @param string $sku
203+ * @param string $attributeCode
204+ * @param string $value
205+ * @return void
206+ */
207+ private function updateProduct (string $ sku , string $ attributeCode , string $ value ): void
208+ {
209+ $ product = $ this ->productRepository ->get ($ sku );
210+ $ product ->setData ($ attributeCode , $ value );
211+ $ this ->productRepository ->save ($ product );
138212 }
139213}
0 commit comments