7
7
8
8
namespace Magento \Catalog \Block \Product \ListProduct ;
9
9
10
- use Magento \Catalog \Api \CategoryLinkManagementInterface ;
11
10
use Magento \Catalog \Api \CategoryRepositoryInterface ;
12
- use Magento \Catalog \Api \Data \CategoryInterface ;
13
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
14
12
use Magento \Catalog \Block \Product \ListProduct ;
15
- use Magento \Catalog \Model \Layer ;
16
- use Magento \Catalog \Model \Layer \Resolver ;
17
13
use Magento \Catalog \Model \Product \Visibility ;
18
14
use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
19
15
use Magento \Framework \ObjectManagerInterface ;
20
- use Magento \Framework \Registry ;
21
16
use Magento \Framework \View \LayoutInterface ;
22
- use Magento \Store \Api \StoreRepositoryInterface ;
23
17
use Magento \Store \Model \StoreManagerInterface ;
24
18
use Magento \TestFramework \Helper \Bootstrap ;
25
19
use PHPUnit \Framework \TestCase ;
28
22
* Checks products displaying on category page
29
23
*
30
24
* @magentoDbIsolation disabled
31
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
+ * @magentoAppIsolation enabled
32
26
*/
33
- class AbstractTest extends TestCase
27
+ class ProductInCategoriesViewTest extends TestCase
34
28
{
35
29
/** @var ObjectManagerInterface */
36
30
private $ objectManager ;
@@ -41,21 +35,12 @@ class AbstractTest extends TestCase
41
35
/** @var CategoryRepositoryInterface */
42
36
private $ categoryRepository ;
43
37
44
- /** @var Registry */
45
- private $ registry ;
46
-
47
38
/** @var ProductRepositoryInterface */
48
39
private $ productRepository ;
49
40
50
- /** @var CategoryLinkManagementInterface */
51
- private $ categoryLinkManagement ;
52
-
53
41
/** @var StoreManagerInterface */
54
42
private $ storeManager ;
55
43
56
- /** @var StoreRepositoryInterface */
57
- private $ storeRepository ;
58
-
59
44
/** @var LayoutInterface */
60
45
private $ layout ;
61
46
@@ -68,25 +53,22 @@ protected function setUp()
68
53
69
54
$ this ->objectManager = Bootstrap::getObjectManager ();
70
55
$ this ->categoryRepository = $ this ->objectManager ->create (CategoryRepositoryInterface::class);
71
- $ this ->block = $ this ->objectManager ->get (LayoutInterface::class)->createBlock (ListProduct::class);
72
- $ this ->registry = $ this ->objectManager ->get (Registry::class);
73
56
$ this ->productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
74
- $ this ->categoryLinkManagement = $ this ->objectManager ->create (CategoryLinkManagementInterface::class);
75
57
$ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
76
- $ this ->storeRepository = $ this ->objectManager ->create (StoreRepositoryInterface::class);
77
58
$ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
59
+ $ this ->block = $ this ->layout ->createBlock (ListProduct::class);
78
60
}
79
61
80
62
/**
81
63
* @magentoDataFixture Magento/Catalog/_files/category_with_two_products.php
82
- * @magentoAppIsolation enabled
83
64
* @dataProvider productDataProvider
84
65
* @param array $data
85
66
* @return void
86
67
*/
87
68
public function testCategoryProductView (array $ data ): void
88
69
{
89
- $ collection = $ this ->processCategoryViewTest ($ data ['sku ' ], $ data );
70
+ $ this ->updateProduct ($ data ['sku ' ], $ data );
71
+ $ collection = $ this ->getCategoryProductCollection (333 );
90
72
91
73
$ this ->assertEquals (1 , $ collection ->getSize ());
92
74
$ this ->assertEquals ('simple333 ' , $ collection ->getFirstItem ()->getSku ());
@@ -120,14 +102,14 @@ public function productDataProvider(): array
120
102
121
103
/**
122
104
* @magentoDataFixture Magento/Catalog/_files/category_product.php
123
- * @magentoAppIsolation enabled
124
105
* @dataProvider productVisibilityProvider
125
106
* @param array $data
126
107
* @return void
127
108
*/
128
109
public function testCategoryProductVisibility (array $ data ): void
129
110
{
130
- $ collection = $ this ->processCategoryViewTest ($ data ['data ' ]['sku ' ], $ data ['data ' ]);
111
+ $ this ->updateProduct ($ data ['data ' ]['sku ' ], $ data ['data ' ]);
112
+ $ collection = $ this ->getCategoryProductCollection (333 );
131
113
132
114
$ this ->assertEquals ($ data ['expected_count ' ], $ collection ->getSize ());
133
115
}
@@ -180,34 +162,38 @@ public function productVisibilityProvider(): array
180
162
}
181
163
182
164
/**
183
- * @magentoAppIsolation enabled
184
165
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
185
166
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
186
167
* @return void
187
168
*/
188
169
public function testAnchorCategoryProductVisibility (): void
189
170
{
190
- $ collections = $ this ->processAnchorTest (true );
171
+ $ this ->updateCategoryIsAnchor (400 , true );
172
+ $ this ->assignProductCategories (['simple2 ' ], [402 ]);
173
+ $ parentCategoryCollection = $ this ->getCategoryProductCollection (400 );
174
+ $ childCategoryCollection = $ this ->getCategoryProductCollection (402 , true );
191
175
192
- $ this ->assertEquals (1 , $ collections [ ' parent_collection ' ] ->getSize ());
176
+ $ this ->assertEquals (1 , $ parentCategoryCollection ->getSize ());
193
177
$ this ->assertEquals (
194
- $ collections [ ' child_collection ' ] ->getAllIds (),
195
- $ collections [ ' parent_collection ' ] ->getAllIds ()
178
+ $ childCategoryCollection ->getAllIds (),
179
+ $ parentCategoryCollection ->getAllIds ()
196
180
);
197
181
}
198
182
199
183
/**
200
- * @magentoAppIsolation enabled
201
184
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
202
185
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
203
186
* @return void
204
187
*/
205
188
public function testNonAnchorCategoryProductVisibility (): void
206
189
{
207
- $ collections = $ this ->processAnchorTest (false );
190
+ $ this ->updateCategoryIsAnchor (400 , false );
191
+ $ this ->assignProductCategories (['simple2 ' ], [402 ]);
192
+ $ parentCategoryCollection = $ this ->getCategoryProductCollection (400 );
193
+ $ childCategoryCollection = $ this ->getCategoryProductCollection (402 , true );
208
194
209
- $ this ->assertCount (0 , $ collections [ ' parent_collection ' ] );
210
- $ this ->assertCount (1 , $ collections [ ' child_collection ' ] );
195
+ $ this ->assertCount (0 , $ parentCategoryCollection );
196
+ $ this ->assertCount (1 , $ childCategoryCollection );
211
197
}
212
198
213
199
/**
@@ -217,85 +203,94 @@ public function testNonAnchorCategoryProductVisibility(): void
217
203
*/
218
204
public function testCategoryProductViewOnMultiWebsite (): void
219
205
{
220
- $ this ->setCategoriesToProducts (['simple-1 ' , 'simple-2 ' ]);
221
- $ store = $ this ->storeRepository ->get ('fixture_second_store ' );
222
- $ this ->storeManager ->setCurrentStore ($ store ->getId ());
223
- $ category = $ this ->categoryRepository ->get (333 );
224
- $ this ->registerCategory ($ category );
225
- $ collection = $ this ->block ->getLoadedProductCollection ();
226
- $ this ->assertEquals (1 , $ collection ->getSize ());
227
- $ this ->assertEquals ('simple-2 ' , $ collection ->getFirstItem ()->getSku ());
206
+ $ this ->assignProductCategories (['simple-1 ' , 'simple-2 ' ], [3 , 333 ]);
207
+ $ store = $ this ->storeManager ->getStore ('fixture_second_store ' );
208
+ $ currentStore = $ this ->storeManager ->getStore ();
209
+
210
+ try {
211
+ $ this ->storeManager ->setCurrentStore ($ store ->getId ());
212
+ $ collection = $ this ->block ->getLoadedProductCollection ();
213
+ $ collectionSize = $ collection ->getSize ();
214
+ } finally {
215
+ $ this ->storeManager ->setCurrentStore ($ currentStore );
216
+ }
217
+
218
+ $ this ->assertEquals (1 , $ collectionSize );
219
+ $ this ->assertNull ($ collection ->getItemByColumnValue ('sku ' , 'simple-1 ' ));
220
+ $ this ->assertNotNull ($ collection ->getItemByColumnValue ('sku ' , 'simple-2 ' ));
228
221
}
229
222
230
223
/**
231
224
* Set categories to the products
232
225
*
233
226
* @param array $skus
227
+ * @param $categoryIds
234
228
* @return void
235
229
*/
236
- private function setCategoriesToProducts (array $ skus ): void
230
+ private function assignProductCategories (array $ skus, array $ categoryIds ): void
237
231
{
238
232
foreach ($ skus as $ sku ) {
239
233
$ product = $ this ->productRepository ->get ($ sku );
240
- $ product ->setCategoryIds ([ 2 , 333 ] );
234
+ $ product ->setCategoryIds ($ categoryIds );
241
235
$ this ->productRepository ->save ($ product );
242
236
}
243
237
}
244
238
245
239
/**
246
- * Proccess for anchor and non anchor category test
240
+ * Update product
247
241
*
248
- * @param bool $isAnchor
249
- * @return array
242
+ * @param string $sku
243
+ * @param array $data
244
+ * @return void
250
245
*/
251
- private function processAnchorTest ( bool $ isAnchor ): array
246
+ private function updateProduct ( string $ sku , array $ data ): void
252
247
{
253
- $ category = $ this ->categoryRepository ->get (400 );
254
- $ category ->setIsAnchor ($ isAnchor );
255
- $ this ->categoryRepository ->save ($ category );
256
- $ childCategory = $ this ->categoryRepository ->get (402 );
257
- $ this ->categoryLinkManagement ->assignProductToCategories ('simple2 ' , [$ childCategory ->getId ()]);
258
- $ this ->registerCategory ($ category );
259
- $ parentCategoryCollection = $ this ->block ->getLoadedProductCollection ();
260
- $ this ->objectManager ->removeSharedInstance (Resolver::class);
261
- $ this ->objectManager ->removeSharedInstance (Layer::class);
262
- $ this ->registerCategory ($ childCategory );
263
- $ newBlock = $ this ->layout ->createBlock (ListProduct::class);
264
- $ childCategoryCollection = $ newBlock ->getLoadedProductCollection ();
265
-
266
- return [
267
- 'parent_collection ' => $ parentCategoryCollection ,
268
- 'child_collection ' => $ childCategoryCollection ,
269
- ];
248
+ $ product = $ this ->productRepository ->get ($ sku );
249
+ $ product ->addData ($ data );
250
+ $ this ->productRepository ->save ($ product );
270
251
}
271
252
272
253
/**
273
- * Proccess category view test
254
+ * Returns category collection by category id
274
255
*
275
- * @param string $sku
276
- * @param array $data
256
+ * @param int $categoryId
257
+ * @param bool $refreshBlock
277
258
* @return AbstractCollection
278
259
*/
279
- private function processCategoryViewTest ( string $ sku , array $ data ): AbstractCollection
260
+ private function getCategoryProductCollection ( int $ categoryId , bool $ refreshBlock = false ): AbstractCollection
280
261
{
281
- $ product = $ this ->productRepository -> get ( $ sku );
282
- $ product -> addData ( $ data );
283
- $ this -> productRepository -> save ( $ product );
284
- $ category = $ this -> categoryRepository -> get ( 333 );
285
- $ this -> registerCategory ( $ category );
262
+ $ block = $ this ->getListingBlock ( $ refreshBlock );
263
+ $ block -> getLayer ()-> setCurrentCategory ( $ categoryId );
264
+
265
+ return $ block -> getLoadedProductCollection ( );
266
+ }
286
267
287
- return $ this ->block ->getLoadedProductCollection ();
268
+ /**
269
+ * Update is_anchor attribute of the category
270
+ *
271
+ * @param int $categoryId
272
+ * @param bool $isAnchor
273
+ * @return void
274
+ */
275
+ private function updateCategoryIsAnchor (int $ categoryId , bool $ isAnchor ): void
276
+ {
277
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
278
+ $ category ->setIsAnchor ($ isAnchor );
279
+ $ this ->categoryRepository ->save ($ category );
288
280
}
289
281
290
282
/**
291
- * Register current category
283
+ * Get product listing block
292
284
*
293
- * @param CategoryInterface $category
294
- * @retun void
285
+ * @param bool $refresh
286
+ * @return ListProduct
295
287
*/
296
- private function registerCategory ( CategoryInterface $ category ): void
288
+ private function getListingBlock ( bool $ refresh ): ListProduct
297
289
{
298
- $ this ->registry ->unregister ('current_category ' );
299
- $ this ->registry ->register ('current_category ' , $ category );
290
+ if ($ refresh ) {
291
+ $ this ->block = $ this ->layout ->createBlock (ListProduct::class);
292
+ }
293
+
294
+ return $ this ->block ;
300
295
}
301
296
}
0 commit comments