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
+ private $ state ;
29
33
30
- /**
31
- * @var Processor
32
- */
33
- protected $ _processor ;
34
+ /** @var Processor */
35
+ private $ 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
+
49
+ /** @var Full */
50
+ private $ action ;
51
+
40
52
/**
41
53
* @inheritdoc
42
54
*/
@@ -58,21 +70,29 @@ public static function setUpBeforeClass(): void
58
70
*/
59
71
protected function setUp (): void
60
72
{
73
+ parent ::setUp ();
74
+
61
75
$ 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);
64
82
}
65
83
66
84
/**
67
85
* @magentoDbIsolation disabled
68
86
* @magentoAppIsolation enabled
69
87
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
70
88
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
89
+ *
90
+ * @return void
71
91
*/
72
- public function testReindexAll ()
92
+ public function testReindexAll (): void
73
93
{
74
- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
75
- $ this ->_processor ->reindexAll ();
94
+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
95
+ $ this ->processor ->reindexAll ();
76
96
77
97
$ categoryFactory = $ this ->objectManager ->get (CategoryFactory::class);
78
98
$ listProduct = $ this ->objectManager ->get (ListProduct::class);
@@ -98,11 +118,13 @@ public function testReindexAll()
98
118
* @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
99
119
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
100
120
* @magentoConfigFixture fixturestore_store catalog/frontend/flat_catalog_product 1
121
+ *
122
+ * @return void
101
123
*/
102
- public function testReindexAllMultipleStores ()
124
+ public function testReindexAllMultipleStores (): void
103
125
{
104
- $ this ->assertTrue ($ this ->_state ->isFlatEnabled ());
105
- $ this ->_processor ->reindexAll ();
126
+ $ this ->assertTrue ($ this ->state ->isFlatEnabled ());
127
+ $ this ->processor ->reindexAll ();
106
128
107
129
/** @var ProductCollectionFactory $productCollectionFactory */
108
130
$ productCollectionFactory = $ this ->objectManager ->create (ProductCollectionFactory::class);
@@ -116,24 +138,76 @@ public function testReindexAllMultipleStores()
116
138
$ store ->getId () => 'StoreTitle ' ,
117
139
];
118
140
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
+ }
127
163
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
+ }
129
182
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
+ }
136
198
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 );
138
212
}
139
213
}
0 commit comments