7
7
8
8
namespace Magento \Catalog \Controller \Category ;
9
9
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 ;
10
14
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
11
15
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
16
use Magento \Framework \App \Response \Http ;
13
17
use Magento \Framework \Registry ;
14
18
use 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 ;
15
23
use Magento \TestFramework \TestCase \AbstractController ;
16
24
17
25
/**
18
26
* Checks category availability on storefront by url rewrite
19
27
*
28
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
29
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
21
30
* @magentoDbIsolation enabled
22
31
*/
@@ -31,6 +40,18 @@ class CategoryUrlRewriteTest extends AbstractController
31
40
/** @var string */
32
41
private $ categoryUrlSuffix ;
33
42
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
+
34
55
/**
35
56
* @inheritdoc
36
57
*/
@@ -44,6 +65,10 @@ protected function setUp(): void
44
65
CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX ,
45
66
ScopeInterface::SCOPE_STORE
46
67
);
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);
47
72
}
48
73
49
74
/**
@@ -87,4 +112,87 @@ public function categoryRewriteProvider(): array
87
112
],
88
113
];
89
114
}
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
+ }
90
198
}
0 commit comments