Skip to content

Commit d428884

Browse files
committed
MC-20697: Admin: Create a category
1 parent 68c48d5 commit d428884

File tree

2 files changed

+258
-178
lines changed

2 files changed

+258
-178
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

Lines changed: 88 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,47 @@
99
namespace Magento\Catalog\Controller\Adminhtml;
1010

1111
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\Catalog\Model\Category as Category;
13+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1214
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;
1619
use Magento\Catalog\Model\ResourceModel\Product;
20+
use Magento\TestFramework\TestCase\AbstractBackendController;
1721

1822
/**
1923
* Test for category backend actions
2024
*
2125
* @magentoAppArea adminhtml
2226
*/
23-
class CategoryTest extends \Magento\TestFramework\TestCase\AbstractBackendController
27+
class CategoryTest extends AbstractBackendController
2428
{
2529
/**
26-
* @var \Magento\Catalog\Model\ResourceModel\Product
30+
* @var ProductResource
2731
*/
2832
protected $productResource;
2933

34+
/** @var CategoryRepositoryInterface */
35+
private $categoryRepository;
36+
37+
/** @var StoreRepositoryInterface */
38+
private $storeRepository;
39+
3040
/**
31-
* @inheritDoc
41+
* @inheritdoc
3242
*
33-
* @throws \Magento\Framework\Exception\AuthenticationException
43+
* @throws AuthenticationException
3444
*/
3545
protected function setUp()
3646
{
3747
parent::setUp();
3848

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);
4353
}
4454

4555
/**
@@ -52,35 +62,23 @@ protected function setUp()
5262
* @param array $inputData
5363
* @param array $defaultAttributes
5464
* @param array $attributesSaved
55-
* @param bool $isSuccess
65+
* @return void
5666
*/
57-
public function testSaveAction($inputData, $defaultAttributes, $attributesSaved = [], $isSuccess = true)
67+
public function testSaveAction(array $inputData, array $defaultAttributes, array $attributesSaved = []): void
5868
{
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');
6270
$storeId = $store->getId();
63-
6471
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
6572
$this->getRequest()->setPostValue($inputData);
6673
$this->getRequest()->setParam('store', $storeId);
6774
$this->getRequest()->setParam('id', 2);
6875
$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
8079
);
81-
$category->setStoreId($storeId);
82-
$category->load(2);
83-
80+
/** @var $category Category */
81+
$category = $this->categoryRepository->get(2, $storeId);
8482
$errors = [];
8583
foreach ($attributesSaved as $attribute => $value) {
8684
$actualValue = $category->getData($attribute);
@@ -108,21 +106,20 @@ public function testSaveAction($inputData, $defaultAttributes, $attributesSaved
108106
*
109107
* @magentoDbIsolation enabled
110108
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
111-
* @throws NoSuchEntityException
109+
* @return void
112110
*/
113-
public function testDefaultValueForCategoryUrlPath()
111+
public function testDefaultValueForCategoryUrlPath(): void
114112
{
115-
$repository = $this->_objectManager->get(CategoryRepositoryInterface::class);
116113
$categoryId = 3;
117-
$category = $repository->get($categoryId);
114+
$category = $this->categoryRepository->get($categoryId);
118115
$newUrlPath = 'test_url_path';
119116
$defaultUrlPath = $category->getData('url_path');
120117

121118
// update url_path and check it
122119
$category->setStoreId(1);
123120
$category->setUrlKey($newUrlPath);
124121
$category->setUrlPath($newUrlPath);
125-
$repository->save($category);
122+
$this->categoryRepository->save($category);
126123
$this->assertEquals($newUrlPath, $category->getUrlPath());
127124

128125
// set default url_path and check it
@@ -135,7 +132,7 @@ public function testDefaultValueForCategoryUrlPath()
135132
];
136133
$this->getRequest()->setPostValue($postData);
137134
$this->dispatch('backend/catalog/category/save');
138-
$category = $repository->get($categoryId);
135+
$category = $this->categoryRepository->get($categoryId);
139136
$this->assertEquals($defaultUrlPath, $category->getData('url_path'));
140137
}
141138

@@ -145,8 +142,9 @@ public function testDefaultValueForCategoryUrlPath()
145142
* @param array $postData
146143
* @dataProvider categoryCreatedFromProductCreationPageDataProvider
147144
* @magentoDbIsolation enabled
145+
* @return void
148146
*/
149-
public function testSaveActionFromProductCreationPage($postData)
147+
public function testSaveActionFromProductCreationPage($postData): void
150148
{
151149
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
152150
$this->getRequest()->setPostValue($postData);
@@ -159,11 +157,7 @@ public function testSaveActionFromProductCreationPage($postData)
159157
$this->stringContains('http://localhost/index.php/backend/catalog/category/edit/')
160158
);
161159
} 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);
167161
$this->assertArrayHasKey('messages', $result);
168162
$this->assertFalse($result['error']);
169163
$category = $result['category'];
@@ -182,7 +176,7 @@ public function testSaveActionFromProductCreationPage($postData)
182176
* @static
183177
* @return array
184178
*/
185-
public static function categoryCreatedFromProductCreationPageDataProvider()
179+
public static function categoryCreatedFromProductCreationPageDataProvider(): array
186180
{
187181
/* Keep in sync with new-category-dialog.js */
188182
$postData = [
@@ -201,8 +195,10 @@ public static function categoryCreatedFromProductCreationPageDataProvider()
201195

202196
/**
203197
* Test SuggestCategories finds any categories.
198+
*
199+
* @return void
204200
*/
205-
public function testSuggestCategoriesActionDefaultCategoryFound()
201+
public function testSuggestCategoriesActionDefaultCategoryFound(): void
206202
{
207203
$this->getRequest()->setParam('label_part', 'Default');
208204
$this->dispatch('backend/catalog/category/suggestCategories');
@@ -214,8 +210,10 @@ public function testSuggestCategoriesActionDefaultCategoryFound()
214210

215211
/**
216212
* Test SuggestCategories properly processes search by label.
213+
*
214+
* @return void
217215
*/
218-
public function testSuggestCategoriesActionNoSuggestions()
216+
public function testSuggestCategoriesActionNoSuggestions(): void
219217
{
220218
$this->getRequest()->setParam('label_part', strrev('Default'));
221219
$this->dispatch('backend/catalog/category/suggestCategories');
@@ -228,7 +226,7 @@ public function testSuggestCategoriesActionNoSuggestions()
228226
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
229227
* @return array
230228
*/
231-
public function saveActionDataProvider()
229+
public function saveActionDataProvider(): array
232230
{
233231
return [
234232
'default values' => [
@@ -352,64 +350,39 @@ public function saveActionDataProvider()
352350
'filter_price_range' => null
353351
],
354352
],
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-
]
406353
];
407354
}
408355

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+
409380
/**
410381
* Test validation.
382+
*
383+
* @return void
411384
*/
412-
public function testSaveActionCategoryWithDangerRequest()
385+
public function testSaveActionCategoryWithDangerRequest(): void
413386
{
414387
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
415388
$this->getRequest()->setPostValue(
@@ -428,7 +401,7 @@ public function testSaveActionCategoryWithDangerRequest()
428401
$this->dispatch('backend/catalog/category/save');
429402
$this->assertSessionMessages(
430403
$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
432405
);
433406
}
434407

@@ -444,18 +417,23 @@ public function testSaveActionCategoryWithDangerRequest()
444417
* @param int $grandChildId
445418
* @param string $grandChildUrlKey
446419
* @param boolean $error
420+
* @return void
447421
*/
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 {
450430
$urlKeys = [
451431
$childId => $childUrlKey,
452432
$grandChildId => $grandChildUrlKey,
453433
];
454434
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);
459437
if ($categoryId > 0) {
460438
$category->load($categoryId)
461439
->setUrlKey($urlKey)
@@ -477,7 +455,7 @@ public function testMoveAction($parentId, $childId, $childUrlKey, $grandChildId,
477455
*
478456
* @return array
479457
*/
480-
public function moveActionDataProvider()
458+
public function moveActionDataProvider(): array
481459
{
482460
return [
483461
[400, 401, 'first_url_key', 402, 'second_url_key', false],
@@ -496,11 +474,9 @@ public function moveActionDataProvider()
496474
*
497475
* @param array $postData
498476
*/
499-
public function testSaveCategoryWithProductPosition(array $postData)
477+
public function testSaveCategoryWithProductPosition(array $postData): void
500478
{
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');
504480
$storeId = $store->getId();
505481
$oldCategoryProductsCount = $this->getCategoryProductsCount();
506482
$this->getRequest()->setParam('store', $storeId);
@@ -522,7 +498,7 @@ public function testSaveCategoryWithProductPosition(array $postData)
522498
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
523499
* @return array
524500
*/
525-
public function saveActionWithDifferentWebsitesDataProvider()
501+
public function saveActionWithDifferentWebsitesDataProvider(): array
526502
{
527503
return [
528504
'default_values' => [

0 commit comments

Comments
 (0)