Skip to content

Commit 4ffbd71

Browse files
committed
MC-37558: Create automated test for "Override Category settings on Store View level"
1 parent a92dee7 commit 4ffbd71

File tree

2 files changed

+221
-73
lines changed

2 files changed

+221
-73
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Category\Save;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Catalog\Model\Category;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
/**
16+
* Test related to update category.
17+
*
18+
* @magentoAppArea adminhtml
19+
* @magentoDbIsolation enabled
20+
*/
21+
class UpdateCategoryTest extends AbstractSaveCategoryTest
22+
{
23+
/** @var CategoryRepositoryInterface */
24+
private $categoryRepository;
25+
26+
/** @var StoreManagerInterface */
27+
private $storeManager;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
37+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
38+
}
39+
40+
/**
41+
* @dataProvider categoryDataProvider
42+
* @magentoDataFixture Magento/Store/_files/second_store.php
43+
* @magentoDataFixture Magento/Catalog/_files/category.php
44+
*
45+
* @param array $postData
46+
* @return void
47+
*/
48+
public function testUpdateCategoryForDefaultStoreView($postData): void
49+
{
50+
$storeId = (int)$this->storeManager->getStore('default')->getId();
51+
$postData = array_merge($postData, ['store_id' => $storeId]);
52+
$responseData = $this->performSaveCategoryRequest($postData);
53+
$this->assertRequestIsSuccessfullyPerformed($responseData);
54+
$category = $this->categoryRepository->get($postData['entity_id'], $postData['store_id']);
55+
unset($postData['use_default']);
56+
unset($postData['use_config']);
57+
foreach ($postData as $key => $value) {
58+
$this->assertEquals($value, $category->getData($key));
59+
}
60+
}
61+
62+
/**
63+
* @return array
64+
*/
65+
public function categoryDataProvider(): array
66+
{
67+
return [
68+
[
69+
'post_data' => [
70+
'entity_id' => 333,
71+
CategoryInterface::KEY_IS_ACTIVE => '0',
72+
CategoryInterface::KEY_INCLUDE_IN_MENU => '0',
73+
CategoryInterface::KEY_NAME => 'Category default store',
74+
'description' => 'Description for default store',
75+
'landing_page' => '',
76+
'display_mode' => Category::DM_MIXED,
77+
CategoryInterface::KEY_AVAILABLE_SORT_BY => ['name', 'price'],
78+
'default_sort_by' => 'price',
79+
'filter_price_range' => 5,
80+
'url_key' => 'default-store-category',
81+
'meta_title' => 'meta_title default store',
82+
'meta_keywords' => 'meta_keywords default store',
83+
'meta_description' => 'meta_description default store',
84+
'custom_use_parent_settings' => '0',
85+
'custom_design' => '2',
86+
'page_layout' => '2columns-right',
87+
'custom_apply_to_products' => '1',
88+
'use_default' => [
89+
CategoryInterface::KEY_NAME => '0',
90+
CategoryInterface::KEY_IS_ACTIVE => '0',
91+
CategoryInterface::KEY_INCLUDE_IN_MENU => '0',
92+
'url_key' => '0',
93+
'meta_title' => '0',
94+
'custom_use_parent_settings' => '0',
95+
'custom_apply_to_products' => '0',
96+
'description' => '0',
97+
'landing_page' => '0',
98+
'display_mode' => '0',
99+
'custom_design' => '0',
100+
'page_layout' => '0',
101+
'meta_keywords' => '0',
102+
'meta_description' => '0',
103+
'custom_layout_update' => '0',
104+
],
105+
'use_config' => [
106+
CategoryInterface::KEY_AVAILABLE_SORT_BY => false,
107+
'default_sort_by' => false,
108+
'filter_price_range' => false,
109+
],
110+
],
111+
],
112+
];
113+
}
114+
}

dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryRepositoryTest.php

Lines changed: 107 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,101 +8,90 @@
88
namespace Magento\Catalog\Model;
99

1010
use Magento\Catalog\Api\CategoryRepositoryInterface;
11-
use Magento\Catalog\Api\CategoryRepositoryInterfaceFactory;
11+
use Magento\Catalog\Api\Data\CategoryInterface;
1212
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1313
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
14+
use Magento\Cms\Api\GetBlockByIdentifierInterface;
1415
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\Store\Api\StoreManagementInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
1519
use Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager;
1620
use Magento\TestFramework\Helper\Bootstrap;
1721
use PHPUnit\Framework\TestCase;
1822

1923
/**
2024
* Provide tests for CategoryRepository model.
25+
*
26+
* @magentoDbIsolation enabled
2127
*/
2228
class CategoryRepositoryTest extends TestCase
2329
{
24-
private const FIXTURE_CATEGORY_ID = 333;
25-
private const FIXTURE_TWO_STORES_CATEGORY_ID = 555;
26-
private const FIXTURE_SECOND_STORE_CODE = 'fixturestore';
27-
private const FIXTURE_FIRST_STORE_CODE = 'default';
30+
/** @var ObjectManagerInterface */
31+
private $objectManager;
2832

29-
/**
30-
* @var CategoryLayoutUpdateManager
31-
*/
33+
/** @var CategoryLayoutUpdateManager */
3234
private $layoutManager;
3335

34-
/**
35-
* @var CategoryRepositoryInterfaceFactory
36-
*/
37-
private $repositoryFactory;
36+
/** @var CategoryRepositoryInterface */
37+
private $categoryRepository;
3838

39-
/**
40-
* @var CollectionFactory
41-
*/
39+
/** @var CollectionFactory */
4240
private $productCollectionFactory;
4341

44-
/**
45-
* @var CategoryCollectionFactory
46-
*/
42+
/** @var CategoryCollectionFactory */
4743
private $categoryCollectionFactory;
4844

45+
/** @var StoreManagementInterface */
46+
private $storeManager;
47+
48+
/** @var GetBlockByIdentifierInterface */
49+
private $getBlockByIdentifier;
50+
4951
/**
50-
* Sets up common objects.
51-
*
52-
* @inheritDoc
52+
* @inheritdoc
5353
*/
5454
protected function setUp(): void
5555
{
56-
Bootstrap::getObjectManager()->configure([
56+
$this->objectManager = Bootstrap::getObjectManager();
57+
$this->objectManager->configure([
5758
'preferences' => [
5859
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class
5960
=> \Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager::class
6061
]
6162
]);
62-
$this->repositoryFactory = Bootstrap::getObjectManager()->get(CategoryRepositoryInterfaceFactory::class);
63-
$this->layoutManager = Bootstrap::getObjectManager()->get(CategoryLayoutUpdateManager::class);
64-
$this->productCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
65-
$this->categoryCollectionFactory = Bootstrap::getObjectManager()->create(CategoryCollectionFactory::class);
66-
}
67-
68-
/**
69-
* Create subject object.
70-
*
71-
* @return CategoryRepositoryInterface
72-
*/
73-
private function createRepo(): CategoryRepositoryInterface
74-
{
75-
return $this->repositoryFactory->create();
63+
$this->layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
64+
$this->productCollectionFactory = $this->objectManager->get(CollectionFactory::class);
65+
$this->categoryCollectionFactory = $this->objectManager->get(CategoryCollectionFactory::class);
66+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
67+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
68+
$this->getBlockByIdentifier = $this->objectManager->get(GetBlockByIdentifierInterface::class);
7669
}
7770

7871
/**
7972
* Test that custom layout file attribute is saved.
8073
*
81-
* @return void
82-
* @throws \Throwable
8374
* @magentoDataFixture Magento/Catalog/_files/category.php
84-
* @magentoDbIsolation enabled
8575
* @magentoAppIsolation enabled
76+
*
77+
* @return void
8678
*/
8779
public function testCustomLayout(): void
8880
{
89-
//New valid value
90-
$repo = $this->createRepo();
91-
$category = $repo->get(self::FIXTURE_CATEGORY_ID);
81+
$category = $this->categoryRepository->get(333);
9282
$newFile = 'test';
93-
$this->layoutManager->setCategoryFakeFiles(self::FIXTURE_CATEGORY_ID, [$newFile]);
83+
$this->layoutManager->setCategoryFakeFiles(333, [$newFile]);
9484
$category->setCustomAttribute('custom_layout_update_file', $newFile);
95-
$repo->save($category);
96-
$repo = $this->createRepo();
97-
$category = $repo->get(self::FIXTURE_CATEGORY_ID);
85+
$this->categoryRepository->save($category);
86+
$category = $this->categoryRepository->get(333);
9887
$this->assertEquals($newFile, $category->getCustomAttribute('custom_layout_update_file')->getValue());
9988

10089
//Setting non-existent value
10190
$newFile = 'does not exist';
10291
$category->setCustomAttribute('custom_layout_update_file', $newFile);
10392
$caughtException = false;
10493
try {
105-
$repo->save($category);
94+
$this->categoryRepository->save($category);
10695
} catch (LocalizedException $exception) {
10796
$caughtException = true;
10897
}
@@ -112,17 +101,17 @@ public function testCustomLayout(): void
112101
/**
113102
* Test removal of categories.
114103
*
115-
* @magentoDbIsolation enabled
116104
* @magentoDataFixture Magento/Catalog/_files/categories.php
117105
* @magentoAppArea adminhtml
106+
*
118107
* @return void
119108
*/
120109
public function testCategoryBehaviourAfterDelete(): void
121110
{
122111
$productCollection = $this->productCollectionFactory->create();
123112
$deletedCategories = ['3', '4', '5', '13'];
124113
$categoryCollectionIds = $this->categoryCollectionFactory->create()->getAllIds();
125-
$this->createRepo()->deleteByIdentifier(3);
114+
$this->categoryRepository->deleteByIdentifier(3);
126115
$this->assertEquals(
127116
0,
128117
$productCollection->addCategoriesFilter(['in' => $deletedCategories])->getSize(),
@@ -131,42 +120,87 @@ public function testCategoryBehaviourAfterDelete(): void
131120
$newCategoryCollectionIds = $this->categoryCollectionFactory->create()->getAllIds();
132121
$difference = array_diff($categoryCollectionIds, $newCategoryCollectionIds);
133122
sort($difference);
134-
$this->assertEquals(
135-
$deletedCategories,
136-
$difference,
137-
'Wrong categories was deleted'
138-
);
123+
$this->assertEquals($deletedCategories, $difference, 'Wrong categories was deleted');
139124
}
140125

141126
/**
142127
* Verifies whether `get()` method `$storeId` attribute works as expected.
143128
*
144-
* @magentoDbIsolation enabled
145129
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
146130
* @magentoDataFixture Magento/Catalog/_files/category_with_two_stores.php
131+
*
132+
* @return void
147133
*/
148-
public function testGetCategoryForProvidedStore()
134+
public function testGetCategoryForProvidedStore(): void
149135
{
150-
$categoryRepository = $this->repositoryFactory->create();
151-
152-
$categoryDefault = $categoryRepository->get(
153-
self::FIXTURE_TWO_STORES_CATEGORY_ID
154-
);
155-
136+
$categoryId = 555;
137+
$categoryDefault = $this->categoryRepository->get($categoryId);
156138
$this->assertSame('category-defaultstore', $categoryDefault->getUrlKey());
157-
158-
$categoryFirstStore = $categoryRepository->get(
159-
self::FIXTURE_TWO_STORES_CATEGORY_ID,
160-
self::FIXTURE_FIRST_STORE_CODE
161-
);
162-
139+
$defaultStoreId = $this->storeManager->getStore('default')->getId();
140+
$categoryFirstStore = $this->categoryRepository->get($categoryId, $defaultStoreId);
163141
$this->assertSame('category-defaultstore', $categoryFirstStore->getUrlKey());
142+
$fixtureStoreId = $this->storeManager->getStore('fixturestore')->getId();
143+
$categorySecondStore = $this->categoryRepository->get($categoryId, $fixtureStoreId);
144+
$this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey());
145+
}
164146

165-
$categorySecondStore = $categoryRepository->get(
166-
self::FIXTURE_TWO_STORES_CATEGORY_ID,
167-
self::FIXTURE_SECOND_STORE_CODE
168-
);
147+
/**
148+
* @magentoDataFixture Magento/Store/_files/second_store.php
149+
* @magentoDataFixture Magento/Catalog/_files/category.php
150+
* @magentoDataFixture Magento/Cms/_files/block.php
151+
*
152+
* @return void
153+
*/
154+
public function testUpdateCategoryDefaultStoreView(): void
155+
{
156+
$categoryId = 333;
157+
$defaultStoreId = (int)$this->storeManager->getStore('default')->getId();
158+
$secondStoreId = (int)$this->storeManager->getStore('fixture_second_store')->getId();
159+
$blockId = $this->getBlockByIdentifier->execute('fixture_block', $defaultStoreId)->getId();
160+
$origData = $this->categoryRepository->get($categoryId)->getData();
161+
unset($origData[CategoryInterface::KEY_UPDATED_AT]);
162+
$category = $this->categoryRepository->get($categoryId, $defaultStoreId);
163+
$dataForDefaultStore = [
164+
CategoryInterface::KEY_IS_ACTIVE => 0,
165+
CategoryInterface::KEY_INCLUDE_IN_MENU => 0,
166+
CategoryInterface::KEY_NAME => 'Category default store',
167+
'image' => 'test.png',
168+
'description' => 'Description for default store',
169+
'landing_page' => $blockId,
170+
'display_mode' => Category::DM_MIXED,
171+
CategoryInterface::KEY_AVAILABLE_SORT_BY => ['name', 'price'],
172+
'default_sort_by' => 'price',
173+
'filter_price_range' => 5,
174+
'url_key' => 'default-store-category',
175+
'meta_title' => 'meta_title default store',
176+
'meta_keywords' => 'meta_keywords default store',
177+
'meta_description' => 'meta_description default store',
178+
'custom_use_parent_settings' => '0',
179+
'custom_design' => '2',
180+
'page_layout' => '2columns-right',
181+
'custom_apply_to_products' => '1',
182+
];
183+
$category->addData($dataForDefaultStore);
184+
$updatedCategory = $this->categoryRepository->save($category);
185+
$this->assertCategoryData($dataForDefaultStore, $updatedCategory);
186+
$categorySecondStore = $this->categoryRepository->get($categoryId, $secondStoreId);
187+
$this->assertCategoryData($origData, $categorySecondStore);
188+
foreach ($dataForDefaultStore as $key => $value) {
189+
$this->assertNotEquals($value, $categorySecondStore->getData($key));
190+
}
191+
}
169192

170-
$this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey());
193+
/**
194+
* Assert category data.
195+
*
196+
* @param array $expectedData
197+
* @param CategoryInterface $category
198+
* @return void
199+
*/
200+
private function assertCategoryData(array $expectedData, CategoryInterface $category): void
201+
{
202+
foreach ($expectedData as $key => $value) {
203+
$this->assertEquals($value, $category->getData($key));
204+
}
171205
}
172206
}

0 commit comments

Comments
 (0)