@@ -19,10 +19,6 @@ class UrlResolverTest extends GraphQlAbstract
19
19
20
20
/** @var ObjectManager */
21
21
private $ objectManager ;
22
- /**
23
- * @var StoreManagerInterface
24
- */
25
- // private $storeManager;
26
22
27
23
/**
28
24
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
@@ -35,6 +31,8 @@ protected function setUp()
35
31
}
36
32
37
33
/**
34
+ * Tests if the target_path(canonical_url) when a Product entity with a valid url_key (request_path) is provided
35
+ *
38
36
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
39
37
*/
40
38
public function testProductUrlResolver ()
@@ -45,15 +43,6 @@ public function testProductUrlResolver()
45
43
$ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
46
44
$ product = $ productRepository ->get ($ productSku , false , null , true );
47
45
$ storeId = $ product ->getStoreId ();
48
- $ product ->getUrlKey ();
49
-
50
- /** @var UrlRewrite $productUrlRewrite */
51
- // $productUrlRewrite = $this->objectManager->get(UrlRewrite::class);
52
- /** @var ProductUrlRewriteGenerator $generator */
53
- // $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class);
54
-
55
- /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */
56
- // $urls = $generator->generate($product);
57
46
58
47
/** @var UrlFinderInterface $urlFinder */
59
48
$ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
@@ -87,22 +76,58 @@ public function testProductUrlResolver()
87
76
/**
88
77
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
89
78
*/
90
- public function testCategoryUrlResolver ()
79
+ public function testProductUrlWithCanonicalUrlInput ()
91
80
{
92
81
$ productSku = 'p002 ' ;
93
- $ urlPath2 = 'cat-1 .html ' ;
82
+ $ urlPath = 'p002 .html ' ;
94
83
/** @var ProductRepositoryInterface $productRepository */
95
84
$ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
96
85
$ product = $ productRepository ->get ($ productSku , false , null , true );
97
86
$ storeId = $ product ->getStoreId ();
87
+ $ product ->getUrlKey ();
98
88
99
- /** @var UrlRewrite $productUrlRewrite */
100
- // $productUrlRewrite = $this->objectManager->get(UrlRewrite::class);
101
- /** @var ProductUrlRewriteGenerator $generator */
102
- // $generator = $this->objectManager->get(ProductUrlRewriteGenerator::class);
89
+ /** @var UrlFinderInterface $urlFinder */
90
+ $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
91
+ $ actualUrls = $ urlFinder ->findOneByData (
92
+ [
93
+ 'request_path ' =>$ urlPath ,
94
+ 'store_id ' => $ storeId
95
+ ]
96
+ );
97
+ $ targetPath = $ actualUrls ->getTargetPath ();
98
+ $ expectedType = $ actualUrls ->getEntityType ();
99
+ $ canonicalPath = $ actualUrls ->getTargetPath ();
100
+ $ query
101
+ = <<<QUERY
102
+ {
103
+ urlResolver(url:" {$ canonicalPath }")
104
+ {
105
+ id
106
+ canonical_url
107
+ type
108
+ }
109
+
110
+ }
111
+ QUERY ;
112
+ $ response = $ this ->graphQlQuery ($ query );
113
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
114
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
115
+ $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['canonical_url ' ]);
116
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
117
+ }
103
118
104
- /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $urls */
105
- // $urls = $generator->generate($product);
119
+ /**
120
+ *Test the
121
+ * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
122
+ */
123
+ public function testCategoryUrlResolver ()
124
+ {
125
+ $ productSku = 'p002 ' ;
126
+ $ urlPath2 = 'cat-1.html ' ;
127
+ /** @var ProductRepositoryInterface $productRepository */
128
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
129
+ $ product = $ productRepository ->get ($ productSku , false , null , true );
130
+ $ storeId = $ product ->getStoreId ();
106
131
107
132
/** @var UrlFinderInterface $urlFinder */
108
133
$ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
@@ -133,4 +158,88 @@ public function testCategoryUrlResolver()
133
158
$ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['canonical_url ' ]);
134
159
$ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
135
160
}
161
+
162
+ /**
163
+ * Tests the use case where the url_key of the existing product is changed
164
+ *
165
+ * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
166
+ */
167
+ public function testProductUrlRewriteResolver ()
168
+ {
169
+ $ productSku = 'p002 ' ;
170
+ /** @var ProductRepositoryInterface $productRepository */
171
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
172
+ $ product = $ productRepository ->get ($ productSku , false , null , true );
173
+ $ storeId = $ product ->getStoreId ();
174
+ $ product ->setUrlKey ('p002-new ' )->save ();
175
+ $ urlPath = $ product ->getUrlKey () .'.html ' ;
176
+ $ this ->assertEquals ($ urlPath , 'p002-new.html ' );
177
+
178
+ /** @var UrlFinderInterface $urlFinder */
179
+ $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
180
+ $ actualUrls = $ urlFinder ->findOneByData (
181
+ [
182
+ 'request_path ' =>$ urlPath ,
183
+ 'store_id ' => $ storeId
184
+ ]
185
+ );
186
+ $ targetPath = $ actualUrls ->getTargetPath ();
187
+ $ expectedType = $ actualUrls ->getEntityType ();
188
+ $ query
189
+ = <<<QUERY
190
+ {
191
+ urlResolver(url:" {$ urlPath }")
192
+ {
193
+ id
194
+ canonical_url
195
+ type
196
+ }
197
+
198
+ }
199
+ QUERY ;
200
+ $ response = $ this ->graphQlQuery ($ query );
201
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
202
+ $ this ->assertEquals ($ product ->getEntityId (), $ response ['urlResolver ' ]['id ' ]);
203
+ $ this ->assertEquals ($ targetPath , $ response ['urlResolver ' ]['canonical_url ' ]);
204
+ $ this ->assertEquals (strtoupper ($ expectedType ), $ response ['urlResolver ' ]['type ' ]);
205
+ }
206
+
207
+ /**
208
+ * Tests if null is returned when an invalid request_path is provided as input to urlResolver
209
+ *
210
+ * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
211
+ */
212
+ public function testInvalidUrlResolverInput ()
213
+ {
214
+ $ productSku = 'p002 ' ;
215
+ $ urlPath = 'p002 ' ;
216
+ /** @var ProductRepositoryInterface $productRepository */
217
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
218
+ $ product = $ productRepository ->get ($ productSku , false , null , true );
219
+ $ storeId = $ product ->getStoreId ();
220
+
221
+ /** @var UrlFinderInterface $urlFinder */
222
+ $ urlFinder = $ this ->objectManager ->get (UrlFinderInterface::class);
223
+ $ urlFinder ->findOneByData (
224
+ [
225
+ 'request_path ' =>$ urlPath ,
226
+ 'store_id ' => $ storeId
227
+ ]
228
+ );
229
+ $ query
230
+ = <<<QUERY
231
+ {
232
+ urlResolver(url:" {$ urlPath }")
233
+ {
234
+ id
235
+ canonical_url
236
+ type
237
+ }
238
+
239
+ }
240
+ QUERY ;
241
+ $ response = $ this ->graphQlQuery ($ query );
242
+ $ this ->assertArrayHasKey ('urlResolver ' , $ response );
243
+ $ this ->assertNull ($ response ['urlResolver ' ]);
244
+ }
136
245
}
0 commit comments