8
8
namespace Magento \Catalog \Model ;
9
9
10
10
use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
- use Magento \Catalog \Api \CategoryRepositoryInterfaceFactory ;
11
+ use Magento \Catalog \Api \Data \ CategoryInterface ;
12
12
use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
13
13
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
14
+ use Magento \Cms \Api \GetBlockByIdentifierInterface ;
14
15
use Magento \Framework \Exception \LocalizedException ;
16
+ use Magento \Framework \ObjectManagerInterface ;
17
+ use Magento \Store \Api \StoreManagementInterface ;
18
+ use Magento \Store \Model \StoreManagerInterface ;
15
19
use Magento \TestFramework \Catalog \Model \CategoryLayoutUpdateManager ;
16
20
use Magento \TestFramework \Helper \Bootstrap ;
17
21
use PHPUnit \Framework \TestCase ;
18
22
19
23
/**
20
24
* Provide tests for CategoryRepository model.
25
+ *
26
+ * @magentoDbIsolation enabled
21
27
*/
22
28
class CategoryRepositoryTest extends TestCase
23
29
{
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 ;
28
32
29
- /**
30
- * @var CategoryLayoutUpdateManager
31
- */
33
+ /** @var CategoryLayoutUpdateManager */
32
34
private $ layoutManager ;
33
35
34
- /**
35
- * @var CategoryRepositoryInterfaceFactory
36
- */
37
- private $ repositoryFactory ;
36
+ /** @var CategoryRepositoryInterface */
37
+ private $ categoryRepository ;
38
38
39
- /**
40
- * @var CollectionFactory
41
- */
39
+ /** @var CollectionFactory */
42
40
private $ productCollectionFactory ;
43
41
44
- /**
45
- * @var CategoryCollectionFactory
46
- */
42
+ /** @var CategoryCollectionFactory */
47
43
private $ categoryCollectionFactory ;
48
44
45
+ /** @var StoreManagementInterface */
46
+ private $ storeManager ;
47
+
48
+ /** @var GetBlockByIdentifierInterface */
49
+ private $ getBlockByIdentifier ;
50
+
49
51
/**
50
- * Sets up common objects.
51
- *
52
- * @inheritDoc
52
+ * @inheritdoc
53
53
*/
54
54
protected function setUp (): void
55
55
{
56
- Bootstrap::getObjectManager ()->configure ([
56
+ $ this ->objectManager = Bootstrap::getObjectManager ();
57
+ $ this ->objectManager ->configure ([
57
58
'preferences ' => [
58
59
\Magento \Catalog \Model \Category \Attribute \LayoutUpdateManager::class
59
60
=> \Magento \TestFramework \Catalog \Model \CategoryLayoutUpdateManager::class
60
61
]
61
62
]);
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);
76
69
}
77
70
78
71
/**
79
72
* Test that custom layout file attribute is saved.
80
73
*
81
- * @return void
82
- * @throws \Throwable
83
74
* @magentoDataFixture Magento/Catalog/_files/category.php
84
- * @magentoDbIsolation enabled
85
75
* @magentoAppIsolation enabled
76
+ *
77
+ * @return void
86
78
*/
87
79
public function testCustomLayout (): void
88
80
{
89
- //New valid value
90
- $ repo = $ this ->createRepo ();
91
- $ category = $ repo ->get (self ::FIXTURE_CATEGORY_ID );
81
+ $ category = $ this ->categoryRepository ->get (333 );
92
82
$ newFile = 'test ' ;
93
- $ this ->layoutManager ->setCategoryFakeFiles (self :: FIXTURE_CATEGORY_ID , [$ newFile ]);
83
+ $ this ->layoutManager ->setCategoryFakeFiles (333 , [$ newFile ]);
94
84
$ 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 );
98
87
$ this ->assertEquals ($ newFile , $ category ->getCustomAttribute ('custom_layout_update_file ' )->getValue ());
99
88
100
89
//Setting non-existent value
101
90
$ newFile = 'does not exist ' ;
102
91
$ category ->setCustomAttribute ('custom_layout_update_file ' , $ newFile );
103
92
$ caughtException = false ;
104
93
try {
105
- $ repo ->save ($ category );
94
+ $ this -> categoryRepository ->save ($ category );
106
95
} catch (LocalizedException $ exception ) {
107
96
$ caughtException = true ;
108
97
}
@@ -112,17 +101,17 @@ public function testCustomLayout(): void
112
101
/**
113
102
* Test removal of categories.
114
103
*
115
- * @magentoDbIsolation enabled
116
104
* @magentoDataFixture Magento/Catalog/_files/categories.php
117
105
* @magentoAppArea adminhtml
106
+ *
118
107
* @return void
119
108
*/
120
109
public function testCategoryBehaviourAfterDelete (): void
121
110
{
122
111
$ productCollection = $ this ->productCollectionFactory ->create ();
123
112
$ deletedCategories = ['3 ' , '4 ' , '5 ' , '13 ' ];
124
113
$ categoryCollectionIds = $ this ->categoryCollectionFactory ->create ()->getAllIds ();
125
- $ this ->createRepo () ->deleteByIdentifier (3 );
114
+ $ this ->categoryRepository ->deleteByIdentifier (3 );
126
115
$ this ->assertEquals (
127
116
0 ,
128
117
$ productCollection ->addCategoriesFilter (['in ' => $ deletedCategories ])->getSize (),
@@ -131,42 +120,87 @@ public function testCategoryBehaviourAfterDelete(): void
131
120
$ newCategoryCollectionIds = $ this ->categoryCollectionFactory ->create ()->getAllIds ();
132
121
$ difference = array_diff ($ categoryCollectionIds , $ newCategoryCollectionIds );
133
122
sort ($ difference );
134
- $ this ->assertEquals (
135
- $ deletedCategories ,
136
- $ difference ,
137
- 'Wrong categories was deleted '
138
- );
123
+ $ this ->assertEquals ($ deletedCategories , $ difference , 'Wrong categories was deleted ' );
139
124
}
140
125
141
126
/**
142
127
* Verifies whether `get()` method `$storeId` attribute works as expected.
143
128
*
144
- * @magentoDbIsolation enabled
145
129
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
146
130
* @magentoDataFixture Magento/Catalog/_files/category_with_two_stores.php
131
+ *
132
+ * @return void
147
133
*/
148
- public function testGetCategoryForProvidedStore ()
134
+ public function testGetCategoryForProvidedStore (): void
149
135
{
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 );
156
138
$ 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 );
163
141
$ 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
+ }
164
146
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
+ }
169
192
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
+ }
171
205
}
172
206
}
0 commit comments