77
88namespace Magento \Catalog \Controller \Category ;
99
10+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11+ use Magento \Catalog \Model \Layer \Category ;
12+ use Magento \Catalog \Model \Layer \Resolver ;
13+ use Magento \Catalog \Model \Session as CatalogSession ;
1014use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
1115use Magento \Framework \App \Config \ScopeConfigInterface ;
1216use Magento \Framework \App \Response \Http ;
1317use Magento \Framework \Registry ;
1418use Magento \Store \Model \ScopeInterface ;
19+ use Magento \Store \Model \StoreManagerInterface ;
20+ use Magento \TestFramework \Request ;
21+ use Magento \TestFramework \Response ;
22+ use Magento \TestFramework \Store \ExecuteInStoreContext ;
1523use Magento \TestFramework \TestCase \AbstractController ;
1624
1725/**
1826 * Checks category availability on storefront by url rewrite
1927 *
28+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2029 * @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
2130 * @magentoDbIsolation enabled
2231 */
@@ -31,6 +40,18 @@ class CategoryUrlRewriteTest extends AbstractController
3140 /** @var string */
3241 private $ categoryUrlSuffix ;
3342
43+ /** @var StoreManagerInterface */
44+ private $ storeManager ;
45+
46+ /** @var CategoryRepositoryInterface */
47+ private $ categoryRepository ;
48+
49+ /** @var CatalogSession */
50+ private $ catalogSession ;
51+
52+ /** @var ExecuteInStoreContext */
53+ private $ executeInStoreContext ;
54+
3455 /**
3556 * @inheritdoc
3657 */
@@ -44,6 +65,10 @@ protected function setUp(): void
4465 CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX ,
4566 ScopeInterface::SCOPE_STORE
4667 );
68+ $ this ->storeManager = $ this ->_objectManager ->get (StoreManagerInterface::class);
69+ $ this ->categoryRepository = $ this ->_objectManager ->get (CategoryRepositoryInterface::class);
70+ $ this ->catalogSession = $ this ->_objectManager ->get (CatalogSession::class);
71+ $ this ->executeInStoreContext = $ this ->_objectManager ->get (ExecuteInStoreContext::class);
4772 }
4873
4974 /**
@@ -87,4 +112,87 @@ public function categoryRewriteProvider(): array
87112 ],
88113 ];
89114 }
115+
116+ /**
117+ * Test category url on different store view
118+ *
119+ * @magentoDataFixture Magento/Catalog/_files/category.php
120+ * @magentoDataFixture Magento/Store/_files/store.php
121+ * @return void
122+ */
123+ public function testCategoryUrlOnStoreView (): void
124+ {
125+ $ id = 333 ;
126+ $ secondStoreUrlKey = 'category-1-second ' ;
127+ $ currentStore = $ this ->storeManager ->getStore ();
128+ $ secondStore = $ this ->storeManager ->getStore ('test ' );
129+ $ this ->executeInStoreContext ->execute (
130+ $ secondStore ,
131+ [$ this , 'updateCategoryUrlKey ' ],
132+ $ id ,
133+ (int )$ secondStore ->getId (),
134+ $ secondStoreUrlKey
135+ );
136+ $ url = sprintf ('/ ' . $ secondStoreUrlKey . '%s ' , $ this ->categoryUrlSuffix );
137+ $ this ->executeInStoreContext ->execute ($ secondStore , [$ this , 'dispatch ' ], $ url );
138+ $ this ->assertCategoryIsVisible ();
139+ $ this ->assertEquals (
140+ $ secondStoreUrlKey ,
141+ $ this ->categoryRepository ->get ($ id , (int )$ secondStore ->getId ())->getUrlKey (),
142+ 'Wrong category is registered '
143+ );
144+ $ this ->cleanUpCachedObjects ();
145+ $ defaultStoreUrlKey = $ this ->categoryRepository ->get ($ id , $ currentStore ->getId ())->getUrlKey ();
146+ $ this ->dispatch (sprintf ($ defaultStoreUrlKey . '%s ' , $ this ->categoryUrlSuffix ));
147+ $ this ->assertCategoryIsVisible ();
148+ }
149+
150+ /**
151+ * Assert that category is available in storefront
152+ *
153+ * @return void
154+ */
155+ private function assertCategoryIsVisible (): void
156+ {
157+ $ this ->assertEquals (
158+ Response::STATUS_CODE_200 ,
159+ $ this ->getResponse ()->getHttpResponseCode (),
160+ 'Wrong response code is returned '
161+ );
162+ $ this ->assertNotNull ((int )$ this ->catalogSession ->getData ('last_viewed_category_id ' ));
163+ }
164+
165+ /**
166+ * Clean up cached objects
167+ *
168+ * @return void
169+ */
170+ private function cleanUpCachedObjects (): void
171+ {
172+ $ this ->catalogSession ->clearStorage ();
173+ $ this ->registry ->unregister ('current_category ' );
174+ $ this ->registry ->unregister ('category ' );
175+ $ this ->_objectManager ->removeSharedInstance (Request::class);
176+ $ this ->_objectManager ->removeSharedInstance (Response::class);
177+ $ this ->_objectManager ->removeSharedInstance (Resolver::class);
178+ $ this ->_objectManager ->removeSharedInstance (Category::class);
179+ $ this ->_objectManager ->removeSharedInstance ('categoryFilterList ' );
180+ $ this ->_response = null ;
181+ $ this ->_request = null ;
182+ }
183+
184+ /**
185+ * Update category url key
186+ *
187+ * @param int $id
188+ * @param int $storeId
189+ * @param string $categoryUrlKey
190+ * @return void
191+ */
192+ public function updateCategoryUrlKey (int $ id , int $ storeId , string $ categoryUrlKey ): void
193+ {
194+ $ category = $ this ->categoryRepository ->get ($ id , $ storeId );
195+ $ category ->setUrlKey ($ categoryUrlKey );
196+ $ this ->categoryRepository ->save ($ category );
197+ }
90198}
0 commit comments