12
12
use Magento \TestFramework \TestCase \GraphQlAbstract ;
13
13
use Magento \UrlRewrite \Model \UrlFinderInterface ;
14
14
use Magento \UrlRewrite \Model \UrlRewrite ;
15
+ use Magento \UrlRewrite \Model \UrlPersistInterface ;
15
16
16
17
/**
17
18
* Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned.
@@ -65,7 +66,7 @@ public function testProductUrlResolver()
65
66
'store_id ' => $ storeId
66
67
]
67
68
);
68
- $ targetPath = $ actualUrls ->getTargetPath ();
69
+ $ relativePath = $ actualUrls ->getRequestPath ();
69
70
$ expectedType = $ actualUrls ->getEntityType ();
70
71
71
72
$ query
@@ -82,16 +83,16 @@ public function testProductUrlResolver()
82
83
$ response = $ this ->graphQlQuery ($ query );
83
84
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
84
85
$ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
85
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
86
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
86
87
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
87
88
}
88
89
89
90
/**
90
- * Tests the use case where relative_url is provided as resolver input in the Query
91
+ * Tests the use case where non seo friendly is provided as resolver input in the Query
91
92
*
92
93
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
93
94
*/
94
- public function testProductUrlWithCanonicalUrlInput ()
95
+ public function testProductUrlWithNonSeoFriendlyUrlInput ()
95
96
{
96
97
$ productSku = 'p002 ' ;
97
98
/** @var ProductRepositoryInterface $productRepository */
@@ -122,13 +123,14 @@ public function testProductUrlWithCanonicalUrlInput()
122
123
'store_id ' => $ storeId
123
124
]
124
125
);
125
- $ targetPath = $ actualUrls ->getTargetPath ();
126
+ // even of non seo friendly path requested, the seo friendly path should be prefered
127
+ $ relativePath = $ actualUrls ->getRequestPath ();
126
128
$ expectedType = $ actualUrls ->getEntityType ();
127
- $ canonicalPath = $ actualUrls ->getTargetPath ();
129
+ $ nonSeoFriendlyPath = $ actualUrls ->getTargetPath ();
128
130
$ query
129
131
= <<<QUERY
130
132
{
131
- urlResolver(url:" {$ canonicalPath }")
133
+ urlResolver(url:" {$ nonSeoFriendlyPath }")
132
134
{
133
135
id
134
136
relative_url
@@ -139,10 +141,158 @@ public function testProductUrlWithCanonicalUrlInput()
139
141
$ response = $ this ->graphQlQuery ($ query );
140
142
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
141
143
$ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
142
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
144
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
143
145
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
144
146
}
145
147
148
+
149
+ /**
150
+ * Tests the use case where non seo friendly is provided as resolver input in the Query
151
+ *
152
+ * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
153
+ */
154
+ public function testRedirectsAndCustomInput ()
155
+ {
156
+ $ productSku = 'p002 ' ;
157
+ /** @var ProductRepositoryInterface $productRepository */
158
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
159
+ $ product = $ productRepository ->get ($ productSku , false , null , true );
160
+
161
+ // generate permanent redirects
162
+ $ renamedKey = 'p002-ren ' ;
163
+ $ product ->setUrlKey ($ renamedKey );
164
+ $ product ->setData ('save_rewrites_history ' , true );
165
+ $ product ->save ();
166
+
167
+ $ storeId = $ product ->getStoreId ();
168
+
169
+ $ query
170
+ = <<<QUERY
171
+ {
172
+ products(filter: {sku: {eq: " {$ productSku }"}})
173
+ {
174
+ items {
175
+ url_key
176
+ url_suffix
177
+ }
178
+ }
179
+ }
180
+ QUERY ;
181
+ $ response = $ this ->graphQlQuery ($ query );
182
+ $ urlPath = $ response ['products ' ]['items ' ][0 ]['url_key ' ] . $ response ['products ' ]['items ' ][0 ]['url_suffix ' ];
183
+
184
+ /** @var UrlFinderInterface $urlFinder */
185
+ $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
186
+ $ actualUrls = $ urlFinder ->findOneByData (
187
+ [
188
+ 'request_path ' => $ urlPath ,
189
+ 'store_id ' => $ storeId
190
+ ]
191
+ );
192
+ // querying the end redirect gives the same record
193
+ $ relativePath = $ actualUrls ->getRequestPath ();
194
+ $ expectedType = $ actualUrls ->getEntityType ();
195
+ $ query
196
+ = <<<QUERY
197
+ {
198
+ urlResolver(url:" {$ renamedKey }.html")
199
+ {
200
+ id
201
+ relative_url
202
+ type
203
+ }
204
+ }
205
+ QUERY ;
206
+ $ response = $ this ->graphQlQuery ($ query );
207
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
208
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
209
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
210
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
211
+
212
+
213
+ // querying a url that's a redirect the active redirected final url
214
+ $ query
215
+ = <<<QUERY
216
+ {
217
+ urlResolver(url:" {$ productSku }.html")
218
+ {
219
+ id
220
+ relative_url
221
+ type
222
+ }
223
+ }
224
+ QUERY ;
225
+ $ response = $ this ->graphQlQuery ($ query );
226
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
227
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
228
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
229
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
230
+
231
+
232
+ // create custom url that doesn't redirect
233
+ /** @var UrlRewrite $urlRewriteModel */
234
+ $ urlRewriteModel = $ this ->objectManager ->create (UrlRewrite::class);
235
+
236
+ $ customUrl = 'custom-path ' ;
237
+ $ urlRewriteArray = [
238
+ 'entity_type ' => 'custom ' ,
239
+ 'entity_id ' => '0 ' ,
240
+ 'request_path ' => $ customUrl ,
241
+ 'target_path ' => 'p002.html ' ,
242
+ 'redirect_type ' => '0 ' ,
243
+ 'store_id ' => '1 ' ,
244
+ 'description ' => '' ,
245
+ 'is_autogenerated ' => '0 ' ,
246
+ 'metadata ' => null ,
247
+ ];
248
+ foreach ($ urlRewriteArray as $ key => $ value ) {
249
+ $ urlRewriteModel ->setData ($ key , $ value );
250
+ }
251
+ $ urlRewriteModel ->save ();
252
+
253
+ // querying a custom url that should return the target entity but relative should be the custom url
254
+ $ query
255
+ = <<<QUERY
256
+ {
257
+ urlResolver(url:" {$ customUrl }")
258
+ {
259
+ id
260
+ relative_url
261
+ type
262
+ }
263
+ }
264
+ QUERY ;
265
+ $ response = $ this ->graphQlQuery ($ query );
266
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
267
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
268
+ $ this ->assertEquals ($ customUrl , $ response ['urlResolver ' ]['relative_url ' ]);
269
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
270
+
271
+
272
+ // change custom url that does redirect
273
+ $ urlRewriteModel ->setRedirectType ('301 ' );
274
+ $ urlRewriteModel ->setId ($ urlRewriteModel ->getId ());
275
+ $ urlRewriteModel ->save ();
276
+
277
+ //modifying query to avoid getting cached values.
278
+ $ query
279
+ = <<<QUERY
280
+ {
281
+ urlResolver(url:" {$ customUrl }")
282
+ {
283
+ id relative_url type
284
+ }
285
+ }
286
+ QUERY ;
287
+ $ response = $ this ->graphQlQuery ($ query );
288
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
289
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
290
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
291
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
292
+
293
+ $ urlRewriteModel ->delete ();
294
+ }
295
+
146
296
/**
147
297
* Test for category entity
148
298
*
@@ -166,7 +316,7 @@ public function testCategoryUrlResolver()
166
316
]
167
317
);
168
318
$ categoryId = $ actualUrls ->getEntityId ();
169
- $ targetPath = $ actualUrls ->getTargetPath ();
319
+ $ relativePath = $ actualUrls ->getRequestPath ();
170
320
$ expectedType = $ actualUrls ->getEntityType ();
171
321
172
322
$ query
@@ -195,7 +345,7 @@ public function testCategoryUrlResolver()
195
345
$ response = $ this ->graphQlQuery ($ query );
196
346
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
197
347
$ this ->assertEquals ($ categoryId , $ response ['urlResolver ' ]['id ' ]);
198
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
348
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
199
349
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
200
350
}
201
351
@@ -238,7 +388,7 @@ public function testProductUrlRewriteResolver()
238
388
'store_id ' => $ storeId
239
389
]
240
390
);
241
- $ targetPath = $ actualUrls ->getTargetPath ();
391
+ $ relativePath = $ actualUrls ->getRequestPath ();
242
392
$ expectedType = $ actualUrls ->getEntityType ();
243
393
$ query
244
394
= <<<QUERY
@@ -254,7 +404,7 @@ public function testProductUrlRewriteResolver()
254
404
$ response = $ this ->graphQlQuery ($ query );
255
405
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
256
406
$ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
257
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
407
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
258
408
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
259
409
}
260
410
@@ -319,7 +469,7 @@ public function testCategoryUrlWithLeadingSlash()
319
469
]
320
470
);
321
471
$ categoryId = $ actualUrls ->getEntityId ();
322
- $ targetPath = $ actualUrls ->getTargetPath ();
472
+ $ relativePath = $ actualUrls ->getRequestPath ();
323
473
$ expectedType = $ actualUrls ->getEntityType ();
324
474
325
475
$ query
@@ -347,7 +497,7 @@ public function testCategoryUrlWithLeadingSlash()
347
497
$ response = $ this ->graphQlQuery ($ query );
348
498
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
349
499
$ this ->assertEquals ($ categoryId , $ response ['urlResolver ' ]['id ' ]);
350
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
500
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
351
501
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
352
502
}
353
503
@@ -371,7 +521,7 @@ public function testGetNonExistentUrlRewrite()
371
521
'store_id ' => 1
372
522
]
373
523
);
374
- $ targetPath = $ actualUrls ->getTargetPath ();
524
+ $ relativePath = $ actualUrls ->getRequestPath ();
375
525
376
526
$ query = <<<QUERY
377
527
{
@@ -386,6 +536,6 @@ public function testGetNonExistentUrlRewrite()
386
536
$ response = $ this ->graphQlQuery ($ query );
387
537
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
388
538
$ this ->assertEquals ('PRODUCT ' , $ response ['urlResolver ' ]['type ' ]);
389
- $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['relative_url ' ]);
539
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
390
540
}
391
541
}
0 commit comments