55
66use IntegerNet \GlobalCustomLayout \Test \Util \CategoryLayoutUpdateManager ;
77use Magento \Catalog \Api \CategoryRepositoryInterface ;
8+ use Magento \Catalog \Api \Data \CategoryInterface ;
89use Magento \Framework \Exception \CouldNotSaveException ;
910use Magento \Framework \Exception \NoSuchEntityException ;
1011
1415 */
1516class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
1617{
18+ /** @var int */
19+ const CATEGORY_ID = 5 ;
20+
21+ /** @var CategoryRepositoryInterface $repository */
22+ protected $ repository ;
23+
24+ /** @var CategoryLayoutUpdateManager $layoutManager */
25+ protected $ layoutManager ;
26+
27+ /** @var CategoryInterface $category */
28+ protected $ category ;
29+
30+ protected function setUp ()
31+ {
32+ parent ::setUp ();
33+
34+ $ this ->layoutManager = $ this ->objectManager ->get (CategoryLayoutUpdateManager::class);
35+ $ this ->repository = $ this ->objectManager ->create (CategoryRepositoryInterface::class);
36+ }
37+
1738 /**
1839 * Check that Global Custom Layout Update files work for Category views.
1940 *
@@ -23,31 +44,113 @@ class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
2344 *
2445 * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
2546 */
26- public function testViewWithGlobalCustomUpdate (): void
47+ public function testViewContainsGlobalCustomUpdate (): void
48+ {
49+ $ this ->givenGlobalCustomUpdateSelected ();
50+ $ this ->whenCategoryViewed ();
51+ $ this ->thenContainsGlobalUpdateHandle ();
52+ }
53+
54+ /**
55+ * Check that Default Custom Layout Update files still work for Category views.
56+ *
57+ * @return void
58+ * @throws CouldNotSaveException
59+ * @throws NoSuchEntityException
60+ *
61+ * @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
62+ */
63+ public function testViewContainsDefaultCustomUpdate (): void
64+ {
65+ $ this ->givenDefaultCustomUpdateSelected ();
66+ $ this ->whenCategoryViewed ();
67+ $ this ->thenContainsDefaultUpdateHandle ();
68+ }
69+
70+ /**
71+ * @throws CouldNotSaveException
72+ * @throws NoSuchEntityException
73+ */
74+ protected function givenGlobalCustomUpdateSelected ()
75+ {
76+ $ this ->setCustomUpdate (self ::GLOBAL_IDENTIFIER );
77+ }
78+
79+ /**
80+ * @throws CouldNotSaveException
81+ * @throws NoSuchEntityException
82+ */
83+ protected function givenDefaultCustomUpdateSelected ()
2784 {
28- //Setting a fake file for the category.
29- $ file = 'test-file ' ;
30- $ categoryId = 5 ;
85+ $ this ->setCustomUpdate (self ::CATEGORY_ID );
86+ }
3187
32- /** @var CategoryLayoutUpdateManager $layoutManager */
33- $ layoutManager = $ this ->objectManager ->get (CategoryLayoutUpdateManager::class);
34- $ layoutManager ->setCategoryFakeFiles (0 , [$ file ]);
88+ /**
89+ * @param int $forCategoryId
90+ * @param string $fileName
91+ * @throws CouldNotSaveException
92+ * @throws NoSuchEntityException
93+ */
94+ protected function setCustomUpdate (int $ forCategoryId , string $ fileName = self ::TEST_FILE )
95+ {
96+ $ category = $ this ->getCategory ();
3597
36- /** @var CategoryRepositoryInterface $categoryRepo */
37- $ categoryRepo = $ this ->objectManager ->create (CategoryRepositoryInterface::class);
38- $ category = $ categoryRepo ->get ($ categoryId );
98+ $ this ->layoutManager ->setFakeFiles ($ forCategoryId , [$ fileName ]);
3999
40100 //Updating the custom attribute.
41- $ category ->setCustomAttribute ('custom_layout_update_file ' , $ file );
42- $ categoryRepo ->save ($ category );
101+ $ category ->setCustomAttribute ('custom_layout_update_file ' , $ fileName );
102+ $ this ->repository ->save ($ category );
103+ }
43104
44- //Viewing the category
45- $ this ->dispatch ("catalog/category/view/id/ $ categoryId " );
105+ /**
106+ * Viewing the category
107+ *
108+ * @param int $categoryId
109+ */
110+ protected function whenCategoryViewed (?int $ categoryId = null )
111+ {
112+ if (!$ categoryId ) {
113+ $ categoryId = self ::CATEGORY_ID ;
114+ }
115+ $ this ->dispatch ("catalog/category/view/id/ {$ categoryId }" );
116+ }
46117
47- //Layout handles must contain the file.
48- $ handles = $ this ->layoutInterface
49- ->getUpdate ()
50- ->getHandles ();
51- $ this ->assertContains ("catalog_category_view_selectable_0_ {$ file }" , $ handles );
118+ protected function thenContainsGlobalUpdateHandle ()
119+ {
120+ $ this ->containsUpdateHandle (self ::GLOBAL_IDENTIFIER , self ::TEST_FILE );
121+ }
122+
123+ protected function thenContainsDefaultUpdateHandle ()
124+ {
125+ $ this ->containsUpdateHandle (self ::CATEGORY_ID , self ::TEST_FILE );
126+ }
127+
128+ /**
129+ * Layout handles must contain the file.
130+ *
131+ * @param int|string $identifier
132+ * @param string $fileName
133+ */
134+ protected function containsUpdateHandle (
135+ $ identifier = self ::GLOBAL_IDENTIFIER ,
136+ string $ fileName = self ::TEST_FILE )
137+ {
138+ $ expectedHandle = "catalog_category_view_selectable_ {$ identifier }_ {$ fileName }" ;
139+
140+ $ handles = $ this ->layoutInterface ->getUpdate ()->getHandles ();
141+ $ this ->assertContains ($ expectedHandle , $ handles );
142+ }
143+
144+ /**
145+ * @param int $categoryId
146+ * @return CategoryInterface
147+ * @throws NoSuchEntityException
148+ */
149+ protected function getCategory (int $ categoryId = self ::CATEGORY_ID ): CategoryInterface
150+ {
151+ if (!$ this ->category ) {
152+ $ this ->category = $ this ->repository ->get ($ categoryId );
153+ }
154+ return $ this ->category ;
52155 }
53156}
0 commit comments