88namespace Magento \GraphQl \UrlRewrite ;
99
1010use Magento \Catalog \Api \CategoryRepositoryInterface ;
11+ use Magento \Catalog \Api \Data \ProductInterface ;
1112use Magento \Catalog \Api \ProductRepositoryInterface ;
13+ use Magento \TestFramework \Helper \Bootstrap ;
1214use Magento \TestFramework \ObjectManager ;
1315use Magento \TestFramework \TestCase \GraphQlAbstract ;
1416use Magento \UrlRewrite \Model \UrlFinderInterface ;
1517use Magento \UrlRewrite \Model \UrlRewrite ;
1618
1719/**
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.
1921 */
2022class RouteTest extends GraphQlAbstract
2123{
@@ -27,7 +29,7 @@ class RouteTest extends GraphQlAbstract
2729 */
2830 protected function setUp (): void
2931 {
30- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
32+ $ this ->objectManager = Bootstrap::getObjectManager ();
3133 }
3234
3335 /**
@@ -44,6 +46,10 @@ public function testProductUrlResolver()
4446
4547 $ response = $ this ->getRouteQueryResponse ($ this ->getProductUrlKey ($ productSku ));
4648 $ 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 ' ]);
4753 }
4854
4955 /**
@@ -57,40 +63,43 @@ public function testProductUrlWithNonSeoFriendlyUrlInput()
5763 /** @var ProductRepositoryInterface $productRepository */
5864 $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
5965 $ product = $ productRepository ->get ($ productSku , false , null , true );
60- $ storeId = $ product ->getStoreId ();
61-
62- $ urlPath = $ this ->getProductUrlKey ($ productSku );
6366
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 );
7268 $ nonSeoFriendlyPath = $ actualUrls ->getTargetPath ();
7369
7470 $ response = $ this ->getRouteQueryResponse ($ nonSeoFriendlyPath );
7571 $ this ->productTestAssertion ($ product , $ response );
7672 }
7773
7874 /**
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
8076 *
81- * @magentoApiDataFixture Magento/CatalogUrlRewrite /_files/product_with_category.php
77+ * @magentoApiDataFixture Magento/Catalog /_files/product_with_category.php
8278 */
8379 public function testProductUrlRewriteResolver ()
8480 {
85- $ productSku = 'p002 ' ;
81+ $ productSku = 'in-stock-product ' ;
8682 /** @var ProductRepositoryInterface $productRepository */
8783 $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
8884 $ 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 ;
9190
92- $ response = $ this ->getRouteQueryResponse ($ urlPath );
91+ $ response = $ this ->getRouteQueryResponse ($ newUrlPath );
9392 $ 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 ' ]);
94103 }
95104
96105 /**
@@ -112,7 +121,11 @@ public function testGetNonExistentUrlRewrite()
112121 $ urlRewrite ->load ($ urlPath , 'request_path ' );
113122
114123 $ response = $ this ->getRouteQueryResponse ($ urlPath );
124+ $ this ->assertNotNull ($ response ['route ' ]);
115125 $ 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 ' ]);
116129 }
117130
118131 /**
@@ -156,7 +169,10 @@ public function testCategoryUrlResolver()
156169
157170 $ this ->assertArrayHasKey ('route ' , $ response );
158171 $ 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 ' ]);
160176 }
161177
162178 /**
@@ -177,12 +193,17 @@ public function testCMSPageUrlResolver()
177193
178194 $ response = $ this ->getRouteQueryResponse ($ targetPath );
179195
196+ $ urlPath = $ urlPathGenerator ->getUrlPath ($ page );
197+
180198 $ this ->assertArrayHasKey ('route ' , $ response );
181199 $ this ->assertEquals ($ cmsPageData ['identifier ' ], $ response ['route ' ]['url_key ' ]);
182200 $ this ->assertEquals ($ cmsPageData ['title ' ], $ response ['route ' ]['title ' ]);
183201 $ this ->assertEquals ($ cmsPageData ['content ' ], $ response ['route ' ]['content ' ]);
184202 $ this ->assertEquals ($ cmsPageData ['content_heading ' ], $ response ['route ' ]['content_heading ' ]);
185203 $ 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 ' ]);
186207 }
187208
188209 /**
@@ -201,17 +222,26 @@ public function getRouteQueryResponse(string $urlKey): array
201222 ...on SimpleProduct {
202223 name
203224 sku
225+ relative_url
226+ redirect_code
227+ type
204228 }
205229 ...on CategoryTree {
206230 name
207- id
231+ uid
232+ relative_url
233+ redirect_code
234+ type
208235 }
209236 ...on CmsPage {
210237 title
211238 url_key
212239 page_layout
213240 content
214241 content_heading
242+ relative_url
243+ redirect_code
244+ type
215245 }
216246 }
217247}
@@ -243,13 +273,39 @@ public function getProductUrlKey(string $productSku): string
243273 }
244274
245275 /**
246- * @param \Magento\Catalog\Api\Data\ ProductInterface $product
276+ * @param ProductInterface $product
247277 * @param array $response
248278 */
249- private function productTestAssertion (\ Magento \ Catalog \ Api \ Data \ ProductInterface $ product , array $ response )
279+ private function productTestAssertion (ProductInterface $ product , array $ response )
250280 {
251281 $ this ->assertArrayHasKey ('route ' , $ response );
252282 $ this ->assertEquals ($ product ->getName (), $ response ['route ' ]['name ' ]);
253283 $ this ->assertEquals ($ product ->getSku (), $ response ['route ' ]['sku ' ]);
254284 }
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+ }
255311}
0 commit comments