Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlResolverTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
20 {
21 
23  private $objectManager;
24 
25  protected function setUp()
26  {
28  }
29 
35  public function testProductUrlResolver()
36  {
37  $productSku = 'p002';
38  $urlPath = 'p002.html';
40  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
41  $product = $productRepository->get($productSku, false, null, true);
42  $storeId = $product->getStoreId();
43 
45  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
46  $actualUrls = $urlFinder->findOneByData(
47  [
48  'request_path' => $urlPath,
49  'store_id' => $storeId
50  ]
51  );
52  $targetPath = $actualUrls->getTargetPath();
53  $expectedType = $actualUrls->getEntityType();
54  $query
55  = <<<QUERY
56 {
57  urlResolver(url:"{$urlPath}")
58  {
59  id
60  canonical_url
61  type
62  }
63 }
64 QUERY;
65  $response = $this->graphQlQuery($query);
66  $this->assertArrayHasKey('urlResolver', $response);
67  $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
68  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
69  $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
70  }
71 
77  public function testProductUrlWithCanonicalUrlInput()
78  {
79  $productSku = 'p002';
80  $urlPath = 'p002.html';
82  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
83  $product = $productRepository->get($productSku, false, null, true);
84  $storeId = $product->getStoreId();
85  $product->getUrlKey();
86 
88  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
89  $actualUrls = $urlFinder->findOneByData(
90  [
91  'request_path' => $urlPath,
92  'store_id' => $storeId
93  ]
94  );
95  $targetPath = $actualUrls->getTargetPath();
96  $expectedType = $actualUrls->getEntityType();
97  $canonicalPath = $actualUrls->getTargetPath();
98  $query
99  = <<<QUERY
100 {
101  urlResolver(url:"{$canonicalPath}")
102  {
103  id
104  canonical_url
105  type
106  }
107 }
108 QUERY;
109  $response = $this->graphQlQuery($query);
110  $this->assertArrayHasKey('urlResolver', $response);
111  $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
112  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
113  $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
114  }
115 
121  public function testCategoryUrlResolver()
122  {
123  $productSku = 'p002';
124  $urlPath2 = 'cat-1.html';
126  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
127  $product = $productRepository->get($productSku, false, null, true);
128  $storeId = $product->getStoreId();
129 
131  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
132  $actualUrls = $urlFinder->findOneByData(
133  [
134  'request_path' => $urlPath2,
135  'store_id' => $storeId
136  ]
137  );
138  $categoryId = $actualUrls->getEntityId();
139  $targetPath = $actualUrls->getTargetPath();
140  $expectedType = $actualUrls->getEntityType();
141  $query
142  = <<<QUERY
143 {
144  urlResolver(url:"{$urlPath2}")
145  {
146  id
147  canonical_url
148  type
149  }
150 }
151 QUERY;
152  $response = $this->graphQlQuery($query);
153  $this->assertArrayHasKey('urlResolver', $response);
154  $this->assertEquals($categoryId, $response['urlResolver']['id']);
155  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
156  $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
157  }
158 
162  public function testCMSPageUrlResolver()
163  {
165  $page = $this->objectManager->get(\Magento\Cms\Model\Page::class);
166  $page->load('page100');
167  $cmsPageId = $page->getId();
168  $requestPath = $page->getIdentifier();
169 
171  $urlPathGenerator = $this->objectManager->get(\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator::class);
172 
174  $targetPath = $urlPathGenerator->getCanonicalUrlPath($page);
175  $expectedEntityType = CmsPageUrlRewriteGenerator::ENTITY_TYPE;
176 
177  $query
178  = <<<QUERY
179 {
180  urlResolver(url:"{$requestPath}")
181  {
182  id
183  canonical_url
184  type
185  }
186 }
187 QUERY;
188  $response = $this->graphQlQuery($query);
189  $this->assertEquals($cmsPageId, $response['urlResolver']['id']);
190  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
191  $this->assertEquals(strtoupper(str_replace('-', '_', $expectedEntityType)), $response['urlResolver']['type']);
192  }
193 
199  public function testProductUrlRewriteResolver()
200  {
201  $productSku = 'p002';
203  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
204  $product = $productRepository->get($productSku, false, null, true);
205  $storeId = $product->getStoreId();
206  $product->setUrlKey('p002-new')->save();
207  $urlPath = $product->getUrlKey() . '.html';
208  $this->assertEquals($urlPath, 'p002-new.html');
209 
211  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
212  $actualUrls = $urlFinder->findOneByData(
213  [
214  'request_path' => $urlPath,
215  'store_id' => $storeId
216  ]
217  );
218  $targetPath = $actualUrls->getTargetPath();
219  $expectedType = $actualUrls->getEntityType();
220  $query
221  = <<<QUERY
222 {
223  urlResolver(url:"{$urlPath}")
224  {
225  id
226  canonical_url
227  type
228  }
229 }
230 QUERY;
231  $response = $this->graphQlQuery($query);
232  $this->assertArrayHasKey('urlResolver', $response);
233  $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
234  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
235  $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
236  }
237 
243  public function testInvalidUrlResolverInput()
244  {
245  $productSku = 'p002';
246  $urlPath = 'p002';
248  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
249  $product = $productRepository->get($productSku, false, null, true);
250  $storeId = $product->getStoreId();
251 
253  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
254  $urlFinder->findOneByData(
255  [
256  'request_path' => $urlPath,
257  'store_id' => $storeId
258  ]
259  );
260  $query
261  = <<<QUERY
262 {
263  urlResolver(url:"{$urlPath}")
264  {
265  id
266  canonical_url
267  type
268  }
269 }
270 QUERY;
271  $response = $this->graphQlQuery($query);
272  $this->assertArrayHasKey('urlResolver', $response);
273  $this->assertNull($response['urlResolver']);
274  }
275 
281  public function testCategoryUrlWithLeadingSlash()
282  {
283  $productSku = 'p002';
284  $urlPath = 'cat-1.html';
286  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
287  $product = $productRepository->get($productSku, false, null, true);
288  $storeId = $product->getStoreId();
289 
291  $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
292  $actualUrls = $urlFinder->findOneByData(
293  [
294  'request_path' => $urlPath,
295  'store_id' => $storeId
296  ]
297  );
298  $categoryId = $actualUrls->getEntityId();
299  $targetPath = $actualUrls->getTargetPath();
300  $expectedType = $actualUrls->getEntityType();
301 
302  $query = <<<QUERY
303 {
304  urlResolver(url:"/{$urlPath}")
305  {
306  id
307  canonical_url
308  type
309  }
310 }
311 QUERY;
312  $response = $this->graphQlQuery($query);
313  $this->assertArrayHasKey('urlResolver', $response);
314  $this->assertEquals($categoryId, $response['urlResolver']['id']);
315  $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
316  $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
317  }
318 
322  public function testResolveSlash()
323  {
324  $query
325  = <<<QUERY
326 {
327  urlResolver(url:"/")
328  {
329  id
330  canonical_url
331  type
332  }
333 }
334 QUERY;
335  $response = $this->graphQlQuery($query);
336 
337  $this->assertArrayHasKey('urlResolver', $response);
338  $this->assertEquals(2, $response['urlResolver']['id']);
339  $this->assertEquals('cms/page/view/page_id/2', $response['urlResolver']['canonical_url']);
340  $this->assertEquals('CMS_PAGE', $response['urlResolver']['type']);
341  }
342 }
$response
Definition: 404.php:11
$page
Definition: pages.php:8
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])