8
8
namespace Magento \GraphQl \UrlRewrite ;
9
9
10
10
use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
11
12
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
+ use Magento \TestFramework \Helper \Bootstrap ;
12
14
use Magento \TestFramework \ObjectManager ;
13
15
use Magento \TestFramework \TestCase \GraphQlAbstract ;
14
16
use Magento \UrlRewrite \Model \UrlFinderInterface ;
15
17
use Magento \UrlRewrite \Model \UrlRewrite ;
16
18
17
19
/**
18
- * Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned.
20
+ * Test the GraphQL endpoint's URLResolver query to verify url route information is correctly returned.
19
21
*/
20
22
class RouteTest extends GraphQlAbstract
21
23
{
@@ -27,7 +29,7 @@ class RouteTest extends GraphQlAbstract
27
29
*/
28
30
protected function setUp (): void
29
31
{
30
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
32
+ $ this ->objectManager = Bootstrap::getObjectManager ();
31
33
}
32
34
33
35
/**
@@ -44,6 +46,10 @@ public function testProductUrlResolver()
44
46
45
47
$ response = $ this ->getRouteQueryResponse ($ this ->getProductUrlKey ($ productSku ));
46
48
$ this ->productTestAssertion ($ product , $ response );
49
+ $ expectedUrls = $ this ->getProductUrlRewriteData ($ productSku );
50
+ $ this ->assertEquals ($ expectedUrls ->getRequestPath (), $ response ['route ' ]['relative_url ' ]);
51
+ $ this ->assertEquals ($ expectedUrls ->getRedirectType (), $ response ['route ' ]['redirect_code ' ]);
52
+ $ this ->assertEquals (strtoupper ($ expectedUrls ->getEntityType ()), $ response ['route ' ]['type ' ]);
47
53
}
48
54
49
55
/**
@@ -57,40 +63,43 @@ public function testProductUrlWithNonSeoFriendlyUrlInput()
57
63
/** @var ProductRepositoryInterface $productRepository */
58
64
$ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
59
65
$ product = $ productRepository ->get ($ productSku , false , null , true );
60
- $ storeId = $ product ->getStoreId ();
61
-
62
- $ urlPath = $ this ->getProductUrlKey ($ productSku );
63
66
64
- /** @var UrlFinderInterface $urlFinder */
65
- $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
66
- $ actualUrls = $ urlFinder ->findOneByData (
67
- [
68
- 'request_path ' => $ urlPath ,
69
- 'store_id ' => $ storeId
70
- ]
71
- );
67
+ $ actualUrls = $ this ->getProductUrlRewriteData ($ productSku );
72
68
$ nonSeoFriendlyPath = $ actualUrls ->getTargetPath ();
73
69
74
70
$ response = $ this ->getRouteQueryResponse ($ nonSeoFriendlyPath );
75
71
$ this ->productTestAssertion ($ product , $ response );
76
72
}
77
73
78
74
/**
79
- * Test the use case where the url_key of the existing product is changed
75
+ * Test the use case where url_key of the existing product is changed and verify final url is redirected correctly
80
76
*
81
- * @magentoApiDataFixture Magento/CatalogUrlRewrite /_files/product_with_category.php
77
+ * @magentoApiDataFixture Magento/Catalog /_files/product_with_category.php
82
78
*/
83
79
public function testProductUrlRewriteResolver ()
84
80
{
85
- $ productSku = 'p002 ' ;
81
+ $ productSku = 'in-stock-product ' ;
86
82
/** @var ProductRepositoryInterface $productRepository */
87
83
$ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
88
84
$ product = $ productRepository ->get ($ productSku , false , null , true );
89
- $ product ->setUrlKey ('p002-new ' )->save ();
90
- $ urlPath = $ this ->getProductUrlKey ($ productSku );
85
+ $ initialUrlPath = $ this ->getProductUrlKey ($ productSku );
86
+ $ renamedKey = 'simple-product-in-stock-new ' ;
87
+ $ suffix = '.html ' ;
88
+ $ product ->setUrlKey ($ renamedKey )->setData ('save_rewrites_history ' , true )->save ();
89
+ $ newUrlPath = $ renamedKey . $ suffix ;
91
90
92
- $ response = $ this ->getRouteQueryResponse ($ urlPath );
91
+ $ response = $ this ->getRouteQueryResponse ($ newUrlPath );
93
92
$ this ->productTestAssertion ($ product , $ response );
93
+
94
+ $ expectedUrls = $ this ->getProductUrlRewriteData ($ productSku );
95
+ $ this ->assertEquals ($ expectedUrls ->getRequestPath (), $ response ['route ' ]['relative_url ' ]);
96
+ $ this ->assertEquals ($ expectedUrls ->getRedirectType (), $ response ['route ' ]['redirect_code ' ]);
97
+ $ this ->assertEquals (strtoupper ($ expectedUrls ->getEntityType ()), $ response ['route ' ]['type ' ]);
98
+
99
+ // verify that product url is redirected to the final url with the correct redirectType
100
+ $ response = $ this ->getRouteQueryResponse ($ initialUrlPath );
101
+ $ this ->assertEquals ('simple-product-in-stock-new.html ' , $ response ['route ' ]['relative_url ' ]);
102
+ $ this ->assertEquals (301 , $ response ['route ' ]['redirect_code ' ]);
94
103
}
95
104
96
105
/**
@@ -112,7 +121,11 @@ public function testGetNonExistentUrlRewrite()
112
121
$ urlRewrite ->load ($ urlPath , 'request_path ' );
113
122
114
123
$ response = $ this ->getRouteQueryResponse ($ urlPath );
124
+ $ this ->assertNotNull ($ response ['route ' ]);
115
125
$ this ->productTestAssertion ($ product , $ response );
126
+ $ this ->assertEquals ($ urlPath , $ response ['route ' ]['relative_url ' ]);
127
+ $ this ->assertEquals (0 , $ response ['route ' ]['redirect_code ' ]);
128
+ $ this ->assertEquals ('PRODUCT ' , $ response ['route ' ]['type ' ]);
116
129
}
117
130
118
131
/**
@@ -156,7 +169,10 @@ public function testCategoryUrlResolver()
156
169
157
170
$ this ->assertArrayHasKey ('route ' , $ response );
158
171
$ this ->assertEquals ($ category ->getName (), $ response ['route ' ]['name ' ]);
159
- $ this ->assertEquals ($ category ->getId (), $ response ['route ' ]['id ' ]);
172
+ $ this ->assertEquals (base64_encode ($ category ->getId ()), $ response ['route ' ]['uid ' ]);
173
+ $ this ->assertEquals ($ urlPath , $ response ['route ' ]['relative_url ' ]);
174
+ $ this ->assertEquals (0 , $ response ['route ' ]['redirect_code ' ]);
175
+ $ this ->assertEquals ('CATEGORY ' , $ response ['route ' ]['type ' ]);
160
176
}
161
177
162
178
/**
@@ -177,12 +193,17 @@ public function testCMSPageUrlResolver()
177
193
178
194
$ response = $ this ->getRouteQueryResponse ($ targetPath );
179
195
196
+ $ urlPath = $ urlPathGenerator ->getUrlPath ($ page );
197
+
180
198
$ this ->assertArrayHasKey ('route ' , $ response );
181
199
$ this ->assertEquals ($ cmsPageData ['identifier ' ], $ response ['route ' ]['url_key ' ]);
182
200
$ this ->assertEquals ($ cmsPageData ['title ' ], $ response ['route ' ]['title ' ]);
183
201
$ this ->assertEquals ($ cmsPageData ['content ' ], $ response ['route ' ]['content ' ]);
184
202
$ this ->assertEquals ($ cmsPageData ['content_heading ' ], $ response ['route ' ]['content_heading ' ]);
185
203
$ this ->assertEquals ($ cmsPageData ['page_layout ' ], $ response ['route ' ]['page_layout ' ]);
204
+ $ this ->assertEquals ($ urlPath , $ response ['route ' ]['relative_url ' ]);
205
+ $ this ->assertEquals (0 , $ response ['route ' ]['redirect_code ' ]);
206
+ $ this ->assertEquals ('CMS_PAGE ' , $ response ['route ' ]['type ' ]);
186
207
}
187
208
188
209
/**
@@ -201,17 +222,26 @@ public function getRouteQueryResponse(string $urlKey): array
201
222
...on SimpleProduct {
202
223
name
203
224
sku
225
+ relative_url
226
+ redirect_code
227
+ type
204
228
}
205
229
...on CategoryTree {
206
230
name
207
- id
231
+ uid
232
+ relative_url
233
+ redirect_code
234
+ type
208
235
}
209
236
...on CmsPage {
210
237
title
211
238
url_key
212
239
page_layout
213
240
content
214
241
content_heading
242
+ relative_url
243
+ redirect_code
244
+ type
215
245
}
216
246
}
217
247
}
@@ -243,13 +273,39 @@ public function getProductUrlKey(string $productSku): string
243
273
}
244
274
245
275
/**
246
- * @param \Magento\Catalog\Api\Data\ ProductInterface $product
276
+ * @param ProductInterface $product
247
277
* @param array $response
248
278
*/
249
- private function productTestAssertion (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product , array $ response )
279
+ private function productTestAssertion (ProductInterface $ product , array $ response )
250
280
{
251
281
$ this ->assertArrayHasKey ('route ' , $ response );
252
282
$ this ->assertEquals ($ product ->getName (), $ response ['route ' ]['name ' ]);
253
283
$ this ->assertEquals ($ product ->getSku (), $ response ['route ' ]['sku ' ]);
254
284
}
285
+
286
+ /**
287
+ * @param $productSku
288
+ * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite
289
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
290
+ */
291
+ private function getProductUrlRewriteData ($ productSku ): \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite
292
+ {
293
+ /** @var ProductRepositoryInterface $productRepository */
294
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
295
+ $ product = $ productRepository ->get ($ productSku , false , null , true );
296
+ $ storeId = $ product ->getStoreId ();
297
+
298
+ $ urlPath = $ this ->getProductUrlKey ($ productSku );
299
+
300
+ /** @var UrlFinderInterface $urlFinder */
301
+ $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
302
+ /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $actualUrls */
303
+ $ actualUrls = $ urlFinder ->findOneByData (
304
+ [
305
+ 'request_path ' => $ urlPath ,
306
+ 'store_id ' => $ storeId
307
+ ]
308
+ );
309
+ return $ actualUrls ;
310
+ }
255
311
}
0 commit comments