88namespace Magento \Catalog \Model ;
99
1010use Magento \Catalog \Api \CategoryRepositoryInterface ;
11- use Magento \Catalog \Api \CategoryRepositoryInterfaceFactory ;
11+ use Magento \Catalog \Api \Data \ CategoryInterface ;
1212use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
1313use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
14+ use Magento \Cms \Api \GetBlockByIdentifierInterface ;
1415use Magento \Framework \Exception \LocalizedException ;
16+ use Magento \Framework \ObjectManagerInterface ;
17+ use Magento \Store \Api \StoreManagementInterface ;
18+ use Magento \Store \Model \StoreManagerInterface ;
1519use Magento \TestFramework \Catalog \Model \CategoryLayoutUpdateManager ;
1620use Magento \TestFramework \Helper \Bootstrap ;
1721use PHPUnit \Framework \TestCase ;
1822
1923/**
2024 * Provide tests for CategoryRepository model.
25+ *
26+ * @magentoDbIsolation enabled
2127 */
2228class 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