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