3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Model \Indexer \Product \Flat \Action ;
7
9
8
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
9
12
use Magento \Catalog \Block \Product \ListProduct ;
10
13
use Magento \Catalog \Model \CategoryFactory ;
11
14
use Magento \Catalog \Model \Indexer \Product \Flat \Processor ;
12
15
use Magento \Catalog \Model \Indexer \Product \Flat \State ;
13
16
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
17
+ use Magento \Catalog \Model \ResourceModel \Product \Flat as FlatResource ;
14
18
use Magento \CatalogSearch \Model \Indexer \Fulltext ;
19
+ use Magento \Eav \Api \AttributeOptionManagementInterface ;
15
20
use Magento \Framework \Indexer \IndexerRegistry ;
21
+ use Magento \Framework \ObjectManagerInterface ;
16
22
use Magento \Store \Model \StoreManagerInterface ;
17
23
use Magento \TestFramework \Helper \Bootstrap ;
18
- use Magento \TestFramework \ObjectManager ;
24
+ use Magento \TestFramework \Indexer \ TestCase ;
19
25
20
26
/**
21
27
* Full reindex Test
22
28
*/
23
- class FullTest extends \ Magento \ TestFramework \ Indexer \ TestCase
29
+ class FullTest extends TestCase
24
30
{
25
- /**
26
- * @var State
27
- */
28
- protected $ _state ;
31
+ /** @var State */
32
+ protected $ state ;
29
33
30
- /**
31
- * @var Processor
32
- */
33
- protected $ _processor ;
34
+ /** @var Processor */
35
+ protected $ processor ;
34
36
35
- /**
36
- * @var ObjectManager
37
- */
37
+ /** @var ObjectManagerInterface */
38
38
private $ objectManager ;
39
39
40
+ /** @var FlatResource */
41
+ private $ flatResource ;
42
+
43
+ /** @var AttributeOptionManagementInterface */
44
+ private $ optionManagement ;
45
+
46
+ /** @var ProductRepositoryInterface */
47
+ private $ productRepository ;
48
+
40
49
/**
41
50
* @inheritdoc
42
51
*/
@@ -58,21 +67,28 @@ public static function setUpBeforeClass(): void
58
67
*/
59
68
protected function setUp (): void
60
69
{
70
+ parent ::setUp ();
71
+
61
72
$ 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);
64
78
}
65
79
66
80
/**
67
81
* @magentoDbIsolation disabled
68
82
* @magentoAppIsolation enabled
69
83
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
70
84
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
85
+ *
86
+ * @return void
71
87
*/
72
- public function testReindexAll ()
88
+ public function testReindexAll (): void
73
89
{
74
- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
75
- $ this ->_processor ->reindexAll ();
90
+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
91
+ $ this ->processor ->reindexAll ();
76
92
77
93
$ categoryFactory = $ this ->objectManager ->get (CategoryFactory::class);
78
94
$ listProduct = $ this ->objectManager ->get (ListProduct::class);
@@ -98,11 +114,13 @@ public function testReindexAll()
98
114
* @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
99
115
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
100
116
* @magentoConfigFixture fixturestore_store catalog/frontend/flat_catalog_product 1
117
+ *
118
+ * @return void
101
119
*/
102
- public function testReindexAllMultipleStores ()
120
+ public function testReindexAllMultipleStores (): void
103
121
{
104
- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
105
- $ this ->_processor ->reindexAll ();
122
+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
123
+ $ this ->processor ->reindexAll ();
106
124
107
125
/** @var ProductCollectionFactory $productCollectionFactory */
108
126
$ productCollectionFactory = $ this ->objectManager ->create (ProductCollectionFactory::class);
@@ -116,24 +134,76 @@ public function testReindexAllMultipleStores()
116
134
$ store ->getId () => 'StoreTitle ' ,
117
135
];
118
136
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
+ }
127
159
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
+ }
129
178
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
+ }
136
194
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 );
138
208
}
139
209
}
0 commit comments