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 ;
16
15
17
16
/**
18
17
* Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned.
@@ -69,26 +68,16 @@ public function testProductUrlResolver()
69
68
$ relativePath = $ actualUrls ->getRequestPath ();
70
69
$ expectedType = $ actualUrls ->getEntityType ();
71
70
72
- $ query
73
- = <<<QUERY
74
- {
75
- urlResolver(url:" {$ urlPath }")
76
- {
77
- id
78
- relative_url
79
- type
80
- }
81
- }
82
- QUERY ;
83
- $ response = $ this ->graphQlQuery ($ query );
84
- $ this ->assertArrayHasKey ('urlResolver ' , $ response );
85
- $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
86
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
87
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
71
+ $ this ->assertResponseFromGraphQl (
72
+ (int ) $ product ->getEntityId (),
73
+ $ urlPath ,
74
+ $ relativePath ,
75
+ $ expectedType
76
+ );
88
77
}
89
78
90
79
/**
91
- * Tests the use case where non seo friendly is provided as resolver input in the Query
80
+ * Test the use case where non seo friendly is provided as resolver input in the Query
92
81
*
93
82
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
94
83
*/
@@ -127,27 +116,17 @@ public function testProductUrlWithNonSeoFriendlyUrlInput()
127
116
$ relativePath = $ actualUrls ->getRequestPath ();
128
117
$ expectedType = $ actualUrls ->getEntityType ();
129
118
$ nonSeoFriendlyPath = $ actualUrls ->getTargetPath ();
130
- $ query
131
- = <<<QUERY
132
- {
133
- urlResolver(url:" {$ nonSeoFriendlyPath }")
134
- {
135
- id
136
- relative_url
137
- type
138
- }
139
- }
140
- QUERY ;
141
- $ response = $ this ->graphQlQuery ($ query );
142
- $ this ->assertArrayHasKey ('urlResolver ' , $ response );
143
- $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
144
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
145
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
146
- }
147
119
120
+ $ this ->assertResponseFromGraphQl (
121
+ (int ) $ product ->getEntityId (),
122
+ $ nonSeoFriendlyPath ,
123
+ $ relativePath ,
124
+ $ expectedType
125
+ );
126
+ }
148
127
149
128
/**
150
- * Tests the use case where non seo friendly is provided as resolver input in the Query
129
+ * Test the use case where non seo friendly is provided as resolver input in the Query
151
130
*
152
131
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
153
132
*/
@@ -180,6 +159,7 @@ public function testRedirectsAndCustomInput()
180
159
QUERY ;
181
160
$ response = $ this ->graphQlQuery ($ query );
182
161
$ urlPath = $ response ['products ' ]['items ' ][0 ]['url_key ' ] . $ response ['products ' ]['items ' ][0 ]['url_suffix ' ];
162
+ $ suffix = $ response ['products ' ]['items ' ][0 ]['url_suffix ' ];
183
163
184
164
/** @var UrlFinderInterface $urlFinder */
185
165
$ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
@@ -190,44 +170,20 @@ public function testRedirectsAndCustomInput()
190
170
]
191
171
);
192
172
// 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
-
173
+ $ this ->assertResponseFromGraphQl (
174
+ (int ) $ product ->getEntityId (),
175
+ $ renamedKey . $ suffix ,
176
+ $ actualUrls ->getRequestPath (),
177
+ $ actualUrls ->getEntityType ()
178
+ );
212
179
213
180
// 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
-
181
+ $ this ->assertResponseFromGraphQl (
182
+ (int ) $ product ->getEntityId (),
183
+ $ productSku . $ suffix ,
184
+ $ actualUrls ->getRequestPath (),
185
+ $ actualUrls ->getEntityType ()
186
+ );
231
187
232
188
// create custom url that doesn't redirect
233
189
/** @var UrlRewrite $urlRewriteModel */
@@ -251,44 +207,35 @@ public function testRedirectsAndCustomInput()
251
207
$ urlRewriteModel ->save ();
252
208
253
209
// 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
-
210
+ $ this ->assertResponseFromGraphQl (
211
+ (int ) $ product ->getEntityId (),
212
+ $ customUrl ,
213
+ $ customUrl ,
214
+ $ actualUrls ->getEntityType ()
215
+ );
271
216
272
217
// change custom url that does redirect
273
218
$ urlRewriteModel ->setRedirectType ('301 ' );
274
219
$ urlRewriteModel ->setId ($ urlRewriteModel ->getId ());
275
220
$ urlRewriteModel ->save ();
276
221
277
- //modifying query to avoid getting cached values.
222
+ //modifying query by adding spaces to avoid getting cached values.
278
223
$ query
279
224
= <<<QUERY
280
- {
225
+ {
281
226
urlResolver(url:" {$ customUrl }")
282
227
{
283
- id relative_url type
228
+ id
229
+ relative_url
230
+ type
284
231
}
285
232
}
286
233
QUERY ;
287
234
$ response = $ this ->graphQlQuery ($ query );
288
235
$ this ->assertArrayHasKey ('urlResolver ' , $ response );
289
236
$ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
290
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
291
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
237
+ $ this ->assertEquals ($ actualUrls -> getRequestPath () , $ response ['urlResolver ' ]['relative_url ' ]);
238
+ $ this ->assertEquals (strtoupper ($ actualUrls -> getEntityType () ), $ response ['urlResolver ' ]['type ' ]);
292
239
293
240
$ urlRewriteModel ->delete ();
294
241
}
@@ -331,26 +278,16 @@ public function testCategoryUrlResolver()
331
278
$ response = $ this ->graphQlQuery ($ query );
332
279
$ urlPath = $ response ['category ' ]['url_key ' ] . $ response ['category ' ]['url_suffix ' ];
333
280
334
- $ query
335
- = <<<QUERY
336
- {
337
- urlResolver(url:" {$ urlPath }")
338
- {
339
- id
340
- relative_url
341
- type
342
- }
343
- }
344
- QUERY ;
345
- $ response = $ this ->graphQlQuery ($ query );
346
- $ this ->assertArrayHasKey ('urlResolver ' , $ response );
347
- $ this ->assertEquals ($ categoryId , $ response ['urlResolver ' ]['id ' ]);
348
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
349
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
281
+ $ this ->assertResponseFromGraphQl (
282
+ (int ) $ categoryId ,
283
+ $ urlPath ,
284
+ $ relativePath ,
285
+ $ expectedType
286
+ );
350
287
}
351
288
352
289
/**
353
- * Tests the use case where the url_key of the existing product is changed
290
+ * Test the use case where the url_key of the existing product is changed
354
291
*
355
292
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
356
293
*/
@@ -390,22 +327,13 @@ public function testProductUrlRewriteResolver()
390
327
);
391
328
$ relativePath = $ actualUrls ->getRequestPath ();
392
329
$ expectedType = $ actualUrls ->getEntityType ();
393
- $ query
394
- = <<<QUERY
395
- {
396
- urlResolver(url:" {$ urlPath }")
397
- {
398
- id
399
- relative_url
400
- type
401
- }
402
- }
403
- QUERY ;
404
- $ response = $ this ->graphQlQuery ($ query );
405
- $ this ->assertArrayHasKey ('urlResolver ' , $ response );
406
- $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
407
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
408
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
330
+
331
+ $ this ->assertResponseFromGraphQl (
332
+ (int ) $ product ->getEntityId (),
333
+ $ urlPath ,
334
+ $ relativePath ,
335
+ $ expectedType
336
+ );
409
337
}
410
338
411
339
/**
@@ -484,21 +412,12 @@ public function testCategoryUrlWithLeadingSlash()
484
412
$ response = $ this ->graphQlQuery ($ query );
485
413
$ urlPath = $ response ['category ' ]['url_key ' ] . $ response ['category ' ]['url_suffix ' ];
486
414
487
- $ query = <<<QUERY
488
- {
489
- urlResolver(url:"/ {$ urlPath }")
490
- {
491
- id
492
- relative_url
493
- type
494
- }
495
- }
496
- QUERY ;
497
- $ response = $ this ->graphQlQuery ($ query );
498
- $ this ->assertArrayHasKey ('urlResolver ' , $ response );
499
- $ this ->assertEquals ($ categoryId , $ response ['urlResolver ' ]['id ' ]);
500
- $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
501
- $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
415
+ $ this ->assertResponseFromGraphQl (
416
+ (int ) $ categoryId ,
417
+ $ urlPath ,
418
+ $ relativePath ,
419
+ $ expectedType
420
+ );
502
421
}
503
422
504
423
/**
@@ -538,4 +457,36 @@ public function testGetNonExistentUrlRewrite()
538
457
$ this ->assertEquals ('PRODUCT ' , $ response ['urlResolver ' ]['type ' ]);
539
458
$ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
540
459
}
460
+
461
+ /**
462
+ * Assert response from GraphQl
463
+ *
464
+ * @param string $productId
465
+ * @param string $urlKey
466
+ * @param string $relativePath
467
+ * @param string $expectedType
468
+ */
469
+ private function assertResponseFromGraphQl (
470
+ int $ productId ,
471
+ string $ urlKey ,
472
+ string $ relativePath ,
473
+ string $ expectedType
474
+ ): void {
475
+ $ query
476
+ = <<<QUERY
477
+ {
478
+ urlResolver(url:" {$ urlKey }")
479
+ {
480
+ id
481
+ relative_url
482
+ type
483
+ }
484
+ }
485
+ QUERY ;
486
+ $ response = $ this ->graphQlQuery ($ query );
487
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
488
+ $ this ->assertEquals ($ productId , $ response ['urlResolver ' ]['id ' ]);
489
+ $ this ->assertEquals ($ relativePath , $ response ['urlResolver ' ]['relative_url ' ]);
490
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
491
+ }
541
492
}
0 commit comments