5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Category ;
7
7
8
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
8
9
use Magento \Catalog \Model \Category ;
10
+ use Magento \Catalog \Model \Indexer \Category \Product as CategoryProductIndexer ;
11
+ use Magento \Catalog \Model \Product as ProductModel ;
12
+ use Magento \Catalog \Model \ResourceModel \Category as CategoryResource ;
13
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
14
+ use Magento \Framework \Indexer \IndexerInterface ;
15
+ use Magento \Framework \Indexer \StateInterface ;
16
+ use Magento \Indexer \Model \Indexer ;
17
+ use Magento \TestFramework \Catalog \Model \GetCategoryByName ;
18
+ use Magento \TestFramework \Helper \Bootstrap ;
19
+ use PHPUnit \Framework \TestCase ;
9
20
10
21
/**
22
+ * Test for catalog_category_product indexer.
23
+ *
11
24
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category.php
12
25
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_products.php
13
26
* @magentoDbIsolation disabled
14
27
* @magentoAppIsolation enabled
28
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
29
*/
16
- class ProductTest extends \ PHPUnit \ Framework \ TestCase
30
+ class ProductTest extends TestCase
17
31
{
18
- const DEFAULT_ROOT_CATEGORY = 2 ;
32
+ private const DEFAULT_ROOT_CATEGORY = 2 ;
19
33
20
34
/**
21
- * @var \Magento\Framework\Indexer\ IndexerInterface
35
+ * @var IndexerInterface
22
36
*/
23
- protected $ indexer ;
37
+ private $ indexer ;
24
38
25
39
/**
26
- * @var \Magento\Catalog\Model\ResourceModel\Product
40
+ * @var ProductResource
27
41
*/
28
- protected $ productResource ;
42
+ private $ productResource ;
29
43
30
44
/**
31
- * @var \Magento\Catalog\Api\ CategoryRepositoryInterface
45
+ * @var CategoryRepositoryInterface
32
46
*/
33
47
private $ categoryRepository ;
34
48
35
- protected function setUp (): void
36
- {
37
- /** @var \Magento\Framework\Indexer\IndexerInterface indexer */
38
- $ this ->indexer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
39
- \Magento \Indexer \Model \Indexer::class
40
- );
41
- $ this ->indexer ->load ('catalog_category_product ' );
49
+ /**
50
+ * @var GetCategoryByName
51
+ */
52
+ private $ getCategoryByName ;
42
53
43
- /** @var \Magento\Catalog\Model\ResourceModel\Product $productResource */
44
- $ this -> productResource = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager ()-> get (
45
- \ Magento \ Catalog \ Model \ ResourceModel \Product::class
46
- ) ;
54
+ /**
55
+ * @var CategoryResource
56
+ */
57
+ private $ categoryResource ;
47
58
48
- $ this ->categoryRepository = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
49
- \Magento \Catalog \Api \CategoryRepositoryInterface::class
50
- );
59
+ /**
60
+ * @inheritdoc
61
+ */
62
+ protected function setUp (): void
63
+ {
64
+ /** @var IndexerInterface indexer */
65
+ $ this ->indexer = Bootstrap::getObjectManager ()->create (Indexer::class);
66
+ $ this ->indexer ->load (CategoryProductIndexer::INDEXER_ID );
67
+
68
+ $ this ->productResource = Bootstrap::getObjectManager ()->get (ProductResource::class);
69
+ $ this ->categoryRepository = Bootstrap::getObjectManager ()->create (CategoryRepositoryInterface::class);
70
+ $ this ->categoryResource = Bootstrap::getObjectManager ()->create (CategoryResource::class);
71
+ $ this ->getCategoryByName = Bootstrap::getObjectManager ()->create (GetCategoryByName::class);
51
72
}
52
73
53
74
/**
@@ -61,7 +82,7 @@ public function testReindexAll()
61
82
/** @var Category $categoryFourth */
62
83
$ categoryFourth = end ($ categories );
63
84
foreach ($ products as $ product ) {
64
- /** @var \Magento\Catalog\Model\Product $product */
85
+ /** @var ProductModel $product */
65
86
$ product ->setCategoryIds ([$ categoryFourth ->getId ()]);
66
87
$ product ->save ();
67
88
}
@@ -77,7 +98,7 @@ public function testReindexAll()
77
98
$ this ->indexer ->reindexAll ();
78
99
79
100
foreach ($ products as $ product ) {
80
- /** @var \Magento\Catalog\Model\Product $product */
101
+ /** @var ProductModel $product */
81
102
foreach ($ categories as $ categoryId ) {
82
103
$ this ->assertTrue ((bool )$ this ->productResource ->canBeShowInCategory ($ product , $ categoryId ));
83
104
}
@@ -99,7 +120,7 @@ public function testCategoryMove()
99
120
/** @var Category $categoryFourth */
100
121
$ categoryFourth = end ($ categories );
101
122
foreach ($ products as $ product ) {
102
- /** @var \Magento\Catalog\Model\Product $product */
123
+ /** @var ProductModel $product */
103
124
$ product ->setCategoryIds ([$ categoryFourth ->getId ()]);
104
125
$ product ->save ();
105
126
}
@@ -125,7 +146,7 @@ public function testCategoryMove()
125
146
$ categories = [self ::DEFAULT_ROOT_CATEGORY , $ categorySecond ->getId (), $ categoryFourth ->getId ()];
126
147
127
148
foreach ($ products as $ product ) {
128
- /** @var \Magento\Catalog\Model\Product $product */
149
+ /** @var ProductModel $product */
129
150
foreach ($ categories as $ categoryId ) {
130
151
$ this ->assertTrue ((bool )$ this ->productResource ->canBeShowInCategory ($ product , $ categoryId ));
131
152
}
@@ -153,7 +174,7 @@ public function testCategoryDelete()
153
174
$ categories = [$ categorySecond ->getId (), $ categoryFourth ->getId ()];
154
175
155
176
foreach ($ products as $ product ) {
156
- /** @var \Magento\Catalog\Model\Product $product */
177
+ /** @var ProductModel $product */
157
178
foreach ($ categories as $ categoryId ) {
158
179
$ this ->assertFalse ((bool )$ this ->productResource ->canBeShowInCategory ($ product , $ categoryId ));
159
180
}
@@ -178,22 +199,15 @@ public function testCategoryCreate()
178
199
$ categoryFourth = end ($ categories );
179
200
180
201
/** @var Category $categorySixth */
181
- $ categorySixth = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
182
- \Magento \Catalog \Model \Category::class
183
- );
184
- $ categorySixth ->setName (
185
- 'Category 6 '
186
- )->setPath (
187
- $ categoryFourth ->getPath ()
188
- )->setAvailableSortBy (
189
- 'name '
190
- )->setDefaultSortBy (
191
- 'name '
192
- )->setIsActive (
193
- true
194
- )->save ();
195
-
196
- /** @var \Magento\Catalog\Model\Product $productThird */
202
+ $ categorySixth = Bootstrap::getObjectManager ()->create (Category::class);
203
+ $ categorySixth ->setName ('Category 6 ' )
204
+ ->setPath ($ categoryFourth ->getPath ())
205
+ ->setAvailableSortBy ('name ' )
206
+ ->setDefaultSortBy ('name ' )
207
+ ->setIsActive (true )
208
+ ->save ();
209
+
210
+ /** @var ProductModel $productThird */
197
211
$ productThird = end ($ products );
198
212
$ productThird ->setCategoryIds ([$ categorySixth ->getId ()]);
199
213
$ productThird ->save ();
@@ -211,33 +225,64 @@ public function testCategoryCreate()
211
225
212
226
/**
213
227
* @magentoAppArea adminhtml
214
- * @depends testReindexAll
228
+ *
229
+ * @magentoDataFixture Magento/Catalog/_files/category_tree.php
230
+ *
231
+ * @return void
215
232
*/
216
- public function testCatalogCategoryProductIndexInvalidateAfterDelete ()
233
+ public function testCatalogCategoryProductIndexInvalidateAfterDelete (): void
217
234
{
218
- $ indexerShouldBeValid = (bool )$ this ->indexer ->isInvalid ();
235
+ $ this ->indexer ->reindexAll ();
236
+ $ indexerShouldBeValid = $ this ->indexer ->isInvalid ();
219
237
220
- $ categories = $ this ->getCategories (1 );
221
- $ this ->categoryRepository ->delete (array_pop ($ categories ));
238
+ $ this ->categoryRepository ->deleteByIdentifier (400 );
222
239
223
240
$ state = $ this ->indexer ->getState ();
224
241
$ state ->loadByIndexer ($ this ->indexer ->getId ());
225
242
$ status = $ state ->getStatus ();
226
243
227
244
$ this ->assertFalse ($ indexerShouldBeValid );
228
- $ this ->assertEquals (\Magento \Framework \Indexer \StateInterface::STATUS_INVALID , $ status );
245
+ $ this ->assertEquals (StateInterface::STATUS_INVALID , $ status );
246
+ }
247
+
248
+ /**
249
+ * Test invalidate reindex after change product position on category
250
+ *
251
+ * @magentoAppArea adminhtml
252
+ * @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
253
+ *
254
+ * @return void
255
+ */
256
+ public function testCatalogCategoryProductIndexInvalidateAfterChangeProductPosition (): void
257
+ {
258
+ $ this ->indexer ->setScheduled (true );
259
+ $ indexerShouldBeValid = $ this ->indexer ->isValid ();
260
+
261
+ $ category = $ this ->getCategoryByName ->execute ('Category 999 ' );
262
+
263
+ $ category ->setPostedProducts ([
264
+ $ this ->productResource ->getIdBySku ('simple1000 ' ) => 1 ,
265
+ $ this ->productResource ->getIdBySku ('simple1001 ' ) => 2
266
+ ]);
267
+
268
+ $ this ->categoryResource ->save ($ category );
269
+
270
+ $ state = $ this ->indexer ->getState ();
271
+ $ state ->loadByIndexer ($ this ->indexer ->getId ());
272
+ $ status = $ state ->getStatus ();
273
+
274
+ $ this ->assertTrue ($ indexerShouldBeValid );
275
+ $ this ->assertEquals (StateInterface::STATUS_INVALID , $ status );
229
276
}
230
277
231
278
/**
232
279
* @param int $count
233
280
* @return Category[]
234
281
*/
235
- protected function getCategories ($ count )
282
+ private function getCategories (int $ count ): array
236
283
{
237
284
/** @var Category $category */
238
- $ category = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
239
- \Magento \Catalog \Model \Category::class
240
- );
285
+ $ category = Bootstrap::getObjectManager ()->create (Category::class);
241
286
242
287
$ result = $ category ->getCollection ()->addAttributeToSelect ('name ' )->getItems ();
243
288
$ result = array_slice ($ result , 2 );
@@ -247,14 +292,12 @@ protected function getCategories($count)
247
292
248
293
/**
249
294
* @param int $count
250
- * @return \Magento\Catalog\Model\Product []
295
+ * @return ProductModel []
251
296
*/
252
- protected function getProducts ($ count )
297
+ private function getProducts (int $ count ): array
253
298
{
254
- /** @var \Magento\Catalog\Model\Product $product */
255
- $ product = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
256
- \Magento \Catalog \Model \Product::class
257
- );
299
+ /** @var ProductModel $product */
300
+ $ product = Bootstrap::getObjectManager ()->create (ProductModel::class);
258
301
259
302
$ result [] = $ product ->load (1 );
260
303
$ result [] = $ product ->load (2 );
@@ -266,7 +309,7 @@ protected function getProducts($count)
266
309
/**
267
310
* Clear index data
268
311
*/
269
- protected function clearIndex ()
312
+ private function clearIndex ()
270
313
{
271
314
$ this ->productResource ->getConnection ()->delete (
272
315
$ this ->productResource ->getTable ('catalog_category_product_index ' )
@@ -280,4 +323,15 @@ protected function clearIndex()
280
323
);
281
324
$ this ->assertFalse ($ actualResult );
282
325
}
326
+
327
+ /**
328
+ * @inheritdoc
329
+ */
330
+ protected function tearDown (): void
331
+ {
332
+ parent ::tearDown ();
333
+
334
+ $ this ->indexer ->reindexAll ();
335
+ $ this ->indexer ->setScheduled (false );
336
+ }
283
337
}
0 commit comments