9
9
namespace Magento \Catalog \Controller \Adminhtml ;
10
10
11
11
use Magento \Catalog \Api \CategoryRepositoryInterface ;
12
+ use Magento \Catalog \Model \Category as Category ;
13
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
12
14
use Magento \Framework \App \Request \Http as HttpRequest ;
13
- use Magento \Framework \Exception \NoSuchEntityException ;
14
- use Magento \TestFramework \Helper \Bootstrap ;
15
- use Magento \Store \Model \Store ;
15
+ use Magento \Framework \Exception \AuthenticationException ;
16
+ use Magento \Framework \Message \MessageInterface ;
17
+ use Magento \Framework \Serialize \Serializer \Json ;
18
+ use Magento \Store \Api \StoreRepositoryInterface ;
16
19
use Magento \Catalog \Model \ResourceModel \Product ;
20
+ use Magento \TestFramework \TestCase \AbstractBackendController ;
17
21
18
22
/**
19
23
* Test for category backend actions
20
24
*
21
25
* @magentoAppArea adminhtml
22
26
*/
23
- class CategoryTest extends \ Magento \ TestFramework \ TestCase \ AbstractBackendController
27
+ class CategoryTest extends AbstractBackendController
24
28
{
25
29
/**
26
- * @var \Magento\Catalog\Model\ResourceModel\Product
30
+ * @var ProductResource
27
31
*/
28
32
protected $ productResource ;
29
33
34
+ /** @var CategoryRepositoryInterface */
35
+ private $ categoryRepository ;
36
+
37
+ /** @var StoreRepositoryInterface */
38
+ private $ storeRepository ;
39
+
30
40
/**
31
- * @inheritDoc
41
+ * @inheritdoc
32
42
*
33
- * @throws \Magento\Framework\Exception\ AuthenticationException
43
+ * @throws AuthenticationException
34
44
*/
35
45
protected function setUp ()
36
46
{
37
47
parent ::setUp ();
38
48
39
- /** @var Product $productResource */
40
- $ this ->productResource = Bootstrap:: getObjectManager ()-> get (
41
- Product ::class
42
- );
49
+ /** @var ProductResource $productResource */
50
+ $ this ->productResource = $ this -> _objectManager -> get (ProductResource::class);
51
+ $ this -> categoryRepository = $ this -> _objectManager -> get (CategoryRepositoryInterface ::class);
52
+ $ this -> storeRepository = $ this -> _objectManager -> get (StoreRepositoryInterface::class );
43
53
}
44
54
45
55
/**
@@ -52,35 +62,23 @@ protected function setUp()
52
62
* @param array $inputData
53
63
* @param array $defaultAttributes
54
64
* @param array $attributesSaved
55
- * @param bool $isSuccess
65
+ * @return void
56
66
*/
57
- public function testSaveAction ($ inputData , $ defaultAttributes , $ attributesSaved = [], $ isSuccess = true )
67
+ public function testSaveAction (array $ inputData , array $ defaultAttributes , array $ attributesSaved = []): void
58
68
{
59
- /** @var $store \Magento\Store\Model\Store */
60
- $ store = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (\Magento \Store \Model \Store::class);
61
- $ store ->load ('fixturestore ' , 'code ' );
69
+ $ store = $ this ->storeRepository ->get ('fixturestore ' );
62
70
$ storeId = $ store ->getId ();
63
-
64
71
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
65
72
$ this ->getRequest ()->setPostValue ($ inputData );
66
73
$ this ->getRequest ()->setParam ('store ' , $ storeId );
67
74
$ this ->getRequest ()->setParam ('id ' , 2 );
68
75
$ this ->dispatch ('backend/catalog/category/save ' );
69
-
70
- if ($ isSuccess ) {
71
- $ this ->assertSessionMessages (
72
- $ this ->equalTo (['You saved the category. ' ]),
73
- \Magento \Framework \Message \MessageInterface::TYPE_SUCCESS
74
- );
75
- }
76
-
77
- /** @var $category \Magento\Catalog\Model\Category */
78
- $ category = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
79
- \Magento \Catalog \Model \Category::class
76
+ $ this ->assertSessionMessages (
77
+ $ this ->equalTo (['You saved the category. ' ]),
78
+ MessageInterface::TYPE_SUCCESS
80
79
);
81
- $ category ->setStoreId ($ storeId );
82
- $ category ->load (2 );
83
-
80
+ /** @var $category Category */
81
+ $ category = $ this ->categoryRepository ->get (2 , $ storeId );
84
82
$ errors = [];
85
83
foreach ($ attributesSaved as $ attribute => $ value ) {
86
84
$ actualValue = $ category ->getData ($ attribute );
@@ -108,21 +106,20 @@ public function testSaveAction($inputData, $defaultAttributes, $attributesSaved
108
106
*
109
107
* @magentoDbIsolation enabled
110
108
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
111
- * @throws NoSuchEntityException
109
+ * @return void
112
110
*/
113
- public function testDefaultValueForCategoryUrlPath ()
111
+ public function testDefaultValueForCategoryUrlPath (): void
114
112
{
115
- $ repository = $ this ->_objectManager ->get (CategoryRepositoryInterface::class);
116
113
$ categoryId = 3 ;
117
- $ category = $ repository ->get ($ categoryId );
114
+ $ category = $ this -> categoryRepository ->get ($ categoryId );
118
115
$ newUrlPath = 'test_url_path ' ;
119
116
$ defaultUrlPath = $ category ->getData ('url_path ' );
120
117
121
118
// update url_path and check it
122
119
$ category ->setStoreId (1 );
123
120
$ category ->setUrlKey ($ newUrlPath );
124
121
$ category ->setUrlPath ($ newUrlPath );
125
- $ repository ->save ($ category );
122
+ $ this -> categoryRepository ->save ($ category );
126
123
$ this ->assertEquals ($ newUrlPath , $ category ->getUrlPath ());
127
124
128
125
// set default url_path and check it
@@ -135,7 +132,7 @@ public function testDefaultValueForCategoryUrlPath()
135
132
];
136
133
$ this ->getRequest ()->setPostValue ($ postData );
137
134
$ this ->dispatch ('backend/catalog/category/save ' );
138
- $ category = $ repository ->get ($ categoryId );
135
+ $ category = $ this -> categoryRepository ->get ($ categoryId );
139
136
$ this ->assertEquals ($ defaultUrlPath , $ category ->getData ('url_path ' ));
140
137
}
141
138
@@ -145,8 +142,9 @@ public function testDefaultValueForCategoryUrlPath()
145
142
* @param array $postData
146
143
* @dataProvider categoryCreatedFromProductCreationPageDataProvider
147
144
* @magentoDbIsolation enabled
145
+ * @return void
148
146
*/
149
- public function testSaveActionFromProductCreationPage ($ postData )
147
+ public function testSaveActionFromProductCreationPage ($ postData ): void
150
148
{
151
149
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
152
150
$ this ->getRequest ()->setPostValue ($ postData );
@@ -159,11 +157,7 @@ public function testSaveActionFromProductCreationPage($postData)
159
157
$ this ->stringContains ('http://localhost/index.php/backend/catalog/category/edit/ ' )
160
158
);
161
159
} else {
162
- $ result = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
163
- \Magento \Framework \Json \Helper \Data::class
164
- )->jsonDecode (
165
- $ body
166
- );
160
+ $ result = $ this ->_objectManager ->get (Json::class)->unserialize ($ body );
167
161
$ this ->assertArrayHasKey ('messages ' , $ result );
168
162
$ this ->assertFalse ($ result ['error ' ]);
169
163
$ category = $ result ['category ' ];
@@ -182,7 +176,7 @@ public function testSaveActionFromProductCreationPage($postData)
182
176
* @static
183
177
* @return array
184
178
*/
185
- public static function categoryCreatedFromProductCreationPageDataProvider ()
179
+ public static function categoryCreatedFromProductCreationPageDataProvider (): array
186
180
{
187
181
/* Keep in sync with new-category-dialog.js */
188
182
$ postData = [
@@ -201,8 +195,10 @@ public static function categoryCreatedFromProductCreationPageDataProvider()
201
195
202
196
/**
203
197
* Test SuggestCategories finds any categories.
198
+ *
199
+ * @return void
204
200
*/
205
- public function testSuggestCategoriesActionDefaultCategoryFound ()
201
+ public function testSuggestCategoriesActionDefaultCategoryFound (): void
206
202
{
207
203
$ this ->getRequest ()->setParam ('label_part ' , 'Default ' );
208
204
$ this ->dispatch ('backend/catalog/category/suggestCategories ' );
@@ -214,8 +210,10 @@ public function testSuggestCategoriesActionDefaultCategoryFound()
214
210
215
211
/**
216
212
* Test SuggestCategories properly processes search by label.
213
+ *
214
+ * @return void
217
215
*/
218
- public function testSuggestCategoriesActionNoSuggestions ()
216
+ public function testSuggestCategoriesActionNoSuggestions (): void
219
217
{
220
218
$ this ->getRequest ()->setParam ('label_part ' , strrev ('Default ' ));
221
219
$ this ->dispatch ('backend/catalog/category/suggestCategories ' );
@@ -228,7 +226,7 @@ public function testSuggestCategoriesActionNoSuggestions()
228
226
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
229
227
* @return array
230
228
*/
231
- public function saveActionDataProvider ()
229
+ public function saveActionDataProvider (): array
232
230
{
233
231
return [
234
232
'default values ' => [
@@ -352,64 +350,39 @@ public function saveActionDataProvider()
352
350
'filter_price_range ' => null
353
351
],
354
352
],
355
- 'incorrect datefrom ' => [
356
- [
357
- 'id ' => '2 ' ,
358
- 'entity_id ' => '2 ' ,
359
- 'path ' => '1/2 ' ,
360
- 'name ' => 'Custom Name ' ,
361
- 'is_active ' => '0 ' ,
362
- 'description ' => 'Custom Description ' ,
363
- 'meta_title ' => 'Custom Title ' ,
364
- 'meta_keywords ' => 'Custom keywords ' ,
365
- 'meta_description ' => 'Custom meta description ' ,
366
- 'include_in_menu ' => '0 ' ,
367
- 'url_key ' => 'default-category ' ,
368
- 'display_mode ' => 'PRODUCTS ' ,
369
- 'landing_page ' => '1 ' ,
370
- 'is_anchor ' => true ,
371
- 'custom_apply_to_products ' => '0 ' ,
372
- 'custom_design ' => 'Magento/blank ' ,
373
- 'custom_design_from ' => '5/29/2015 ' ,
374
- 'custom_design_to ' => '5/21/2015 ' ,
375
- 'page_layout ' => '' ,
376
- 'custom_layout_update ' => '' ,
377
- 'use_config ' => [
378
- 'available_sort_by ' => 1 ,
379
- 'default_sort_by ' => 1 ,
380
- 'filter_price_range ' => 1 ,
381
- ],
382
- ],
383
- [
384
- 'name ' => false ,
385
- 'default_sort_by ' => false ,
386
- 'display_mode ' => false ,
387
- 'meta_title ' => false ,
388
- 'custom_design ' => false ,
389
- 'page_layout ' => false ,
390
- 'is_active ' => false ,
391
- 'include_in_menu ' => false ,
392
- 'landing_page ' => false ,
393
- 'custom_apply_to_products ' => false ,
394
- 'available_sort_by ' => false ,
395
- 'description ' => false ,
396
- 'meta_keywords ' => false ,
397
- 'meta_description ' => false ,
398
- 'custom_layout_update ' => false ,
399
- 'custom_design_from ' => false ,
400
- 'custom_design_to ' => false ,
401
- 'filter_price_range ' => false
402
- ],
403
- [],
404
- false
405
- ]
406
353
];
407
354
}
408
355
356
+ /**
357
+ * @magentoDbIsolation enabled
358
+ * @return void
359
+ */
360
+ public function testIncorrectDateFrom (): void
361
+ {
362
+ $ data = [
363
+ 'name ' => 'Test Category ' ,
364
+ 'attribute_set_id ' => '3 ' ,
365
+ 'parent_id ' => 2 ,
366
+ 'path ' => '1/2 ' ,
367
+ 'is_active ' => true ,
368
+ 'custom_design_from ' => '5/29/2015 ' ,
369
+ 'custom_design_to ' => '5/21/2015 ' ,
370
+ ];
371
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
372
+ $ this ->getRequest ()->setPostValue ($ data );
373
+ $ this ->dispatch ('backend/catalog/category/save ' );
374
+ $ this ->assertSessionMessages (
375
+ $ this ->equalTo ([(string )__ ('Make sure the To Date is later than or the same as the From Date. ' )]),
376
+ MessageInterface::TYPE_ERROR
377
+ );
378
+ }
379
+
409
380
/**
410
381
* Test validation.
382
+ *
383
+ * @return void
411
384
*/
412
- public function testSaveActionCategoryWithDangerRequest ()
385
+ public function testSaveActionCategoryWithDangerRequest (): void
413
386
{
414
387
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
415
388
$ this ->getRequest ()->setPostValue (
@@ -428,7 +401,7 @@ public function testSaveActionCategoryWithDangerRequest()
428
401
$ this ->dispatch ('backend/catalog/category/save ' );
429
402
$ this ->assertSessionMessages (
430
403
$ this ->equalTo (['The "Name" attribute value is empty. Set the attribute and try again. ' ]),
431
- \ Magento \ Framework \ Message \ MessageInterface::TYPE_ERROR
404
+ MessageInterface::TYPE_ERROR
432
405
);
433
406
}
434
407
@@ -444,18 +417,23 @@ public function testSaveActionCategoryWithDangerRequest()
444
417
* @param int $grandChildId
445
418
* @param string $grandChildUrlKey
446
419
* @param boolean $error
420
+ * @return void
447
421
*/
448
- public function testMoveAction ($ parentId , $ childId , $ childUrlKey , $ grandChildId , $ grandChildUrlKey , $ error )
449
- {
422
+ public function testMoveAction (
423
+ int $ parentId ,
424
+ int $ childId ,
425
+ string $ childUrlKey ,
426
+ int $ grandChildId ,
427
+ string $ grandChildUrlKey ,
428
+ bool $ error
429
+ ): void {
450
430
$ urlKeys = [
451
431
$ childId => $ childUrlKey ,
452
432
$ grandChildId => $ grandChildUrlKey ,
453
433
];
454
434
foreach ($ urlKeys as $ categoryId => $ urlKey ) {
455
- /** @var $category \Magento\Catalog\Model\Category */
456
- $ category = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
457
- \Magento \Catalog \Model \Category::class
458
- );
435
+ /** @var $category Category */
436
+ $ category = $ this ->_objectManager ->create (Category::class);
459
437
if ($ categoryId > 0 ) {
460
438
$ category ->load ($ categoryId )
461
439
->setUrlKey ($ urlKey )
@@ -477,7 +455,7 @@ public function testMoveAction($parentId, $childId, $childUrlKey, $grandChildId,
477
455
*
478
456
* @return array
479
457
*/
480
- public function moveActionDataProvider ()
458
+ public function moveActionDataProvider (): array
481
459
{
482
460
return [
483
461
[400 , 401 , 'first_url_key ' , 402 , 'second_url_key ' , false ],
@@ -496,11 +474,9 @@ public function moveActionDataProvider()
496
474
*
497
475
* @param array $postData
498
476
*/
499
- public function testSaveCategoryWithProductPosition (array $ postData )
477
+ public function testSaveCategoryWithProductPosition (array $ postData ): void
500
478
{
501
- /** @var $store \Magento\Store\Model\Store */
502
- $ store = Bootstrap::getObjectManager ()->create (Store::class);
503
- $ store ->load ('fixturestore ' , 'code ' );
479
+ $ store = $ this ->storeRepository ->get ('fixturestore ' );
504
480
$ storeId = $ store ->getId ();
505
481
$ oldCategoryProductsCount = $ this ->getCategoryProductsCount ();
506
482
$ this ->getRequest ()->setParam ('store ' , $ storeId );
@@ -522,7 +498,7 @@ public function testSaveCategoryWithProductPosition(array $postData)
522
498
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
523
499
* @return array
524
500
*/
525
- public function saveActionWithDifferentWebsitesDataProvider ()
501
+ public function saveActionWithDifferentWebsitesDataProvider (): array
526
502
{
527
503
return [
528
504
'default_values ' => [
0 commit comments