Skip to content

Commit e7b412d

Browse files
committed
PWA-1558: Include test coverages from the deprecated url resolver
- add test coverage from deprecated query
1 parent 64d4fb9 commit e7b412d

File tree

1 file changed

+204
-9
lines changed
  • dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite

1 file changed

+204
-9
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/RouteTest.php

Lines changed: 204 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
use Magento\Catalog\Api\CategoryRepositoryInterface;
1111
use Magento\Catalog\Api\Data\ProductInterface;
1212
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Framework\Exception\AlreadyExistsException;
1314
use Magento\TestFramework\Helper\Bootstrap;
1415
use Magento\TestFramework\ObjectManager;
1516
use Magento\TestFramework\TestCase\GraphQlAbstract;
17+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewrite as UrlRewriteResourceModel;
1618
use Magento\UrlRewrite\Model\UrlFinderInterface;
17-
use Magento\UrlRewrite\Model\UrlRewrite;
19+
use Magento\UrlRewrite\Model\UrlRewrite as UrlRewriteModel;
20+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite as UrlRewriteService;
1821

1922
/**
20-
* Test the GraphQL endpoint's URLResolver query to verify url route information is correctly returned.
23+
* Test the GraphQL endpoint's Route query to verify url route information is correctly returned.
2124
*/
2225
class RouteTest extends GraphQlAbstract
2326
{
@@ -116,9 +119,9 @@ public function testGetNonExistentUrlRewrite()
116119
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
117120
$product = $productRepository->get($productSku, false, null, true);
118121

119-
/** @var UrlRewrite $urlRewrite */
120-
$urlRewrite = $this->objectManager->create(UrlRewrite::class);
121-
$urlRewrite->load($urlPath, 'request_path');
122+
/** @var UrlRewriteModel $urlRewriteModel */
123+
$urlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
124+
$urlRewriteModel->load($urlPath, 'request_path');
122125

123126
$response = $this->getRouteQueryResponse($urlPath);
124127
$this->assertNotNull($response['route']);
@@ -285,10 +288,10 @@ private function productTestAssertion(ProductInterface $product, array $response
285288

286289
/**
287290
* @param $productSku
288-
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite
291+
* @return UrlRewriteService
289292
* @throws \Magento\Framework\Exception\NoSuchEntityException
290293
*/
291-
private function getProductUrlRewriteData($productSku): \Magento\UrlRewrite\Service\V1\Data\UrlRewrite
294+
private function getProductUrlRewriteData($productSku): UrlRewriteService
292295
{
293296
/** @var ProductRepositoryInterface $productRepository */
294297
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
@@ -299,13 +302,205 @@ private function getProductUrlRewriteData($productSku): \Magento\UrlRewrite\Serv
299302

300303
/** @var UrlFinderInterface $urlFinder */
301304
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
302-
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $actualUrls */
305+
/** @var UrlRewriteService $actualUrls */
303306
$actualUrls = $urlFinder->findOneByData(
304307
[
305308
'request_path' => $urlPath,
306309
'store_id' => $storeId
307310
]
308311
);
309-
return $actualUrls;
312+
return $actualUrls;
313+
}
314+
315+
/**
316+
* Test for custom type which point to the invalid product/category/cms page.
317+
*
318+
* @magentoApiDataFixture Magento/UrlRewrite/_files/url_rewrite_not_existing_entity.php
319+
*/
320+
public function testNonExistentEntityUrlRewrite()
321+
{
322+
$urlPath = 'non-exist-entity.html';
323+
324+
$query = <<<QUERY
325+
{
326+
route(url:"{$urlPath}")
327+
{
328+
relative_url
329+
type
330+
redirect_code
331+
}
332+
}
333+
QUERY;
334+
335+
$this->expectExceptionMessage(
336+
"No such entity found with matching URL key: " . $urlPath
337+
);
338+
$this->graphQlQuery($query);
339+
}
340+
341+
/**
342+
* Test for url rewrite to clean cache on rewrites update
343+
*
344+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_category.php
345+
* @magentoApiDataFixture Magento/Cms/_files/pages.php
346+
*
347+
* @dataProvider urlRewriteEntitiesDataProvider
348+
* @param string $requestPath
349+
* @throws AlreadyExistsException
350+
*/
351+
public function testUrlRewriteCleansCacheOnChange(string $requestPath)
352+
{
353+
354+
/** @var UrlRewriteResourceModel $urlRewriteResourceModel */
355+
$urlRewriteResourceModel = $this->objectManager->create(UrlRewriteResourceModel::class);
356+
$storeId = 1;
357+
$query = function ($requestUrl) {
358+
return <<<QUERY
359+
{
360+
route(url:"{$requestUrl}")
361+
{
362+
relative_url
363+
type
364+
redirect_code
365+
}
366+
}
367+
QUERY;
368+
};
369+
370+
// warming up route API response cache for entity and validate proper response
371+
$apiResponse = $this->graphQlQuery($query($requestPath));
372+
$this->assertEquals($requestPath, $apiResponse['route']['relative_url']);
373+
374+
$urlRewrite = $this->getUrlRewriteModelByRequestPath($requestPath, $storeId);
375+
376+
// renaming entity request path and validating that API will not return cached response
377+
$urlRewrite->setRequestPath('test' . $requestPath);
378+
$urlRewriteResourceModel->save($urlRewrite);
379+
$apiResponse = $this->graphQlQuery($query($requestPath));
380+
$this->assertNull($apiResponse['route']);
381+
382+
// rolling back changes
383+
$urlRewrite->setRequestPath($requestPath);
384+
$urlRewriteResourceModel->save($urlRewrite);
385+
}
386+
387+
public function urlRewriteEntitiesDataProvider(): array
388+
{
389+
return [
390+
[
391+
'simple-product-in-stock.html'
392+
],
393+
[
394+
'category-1.html'
395+
],
396+
[
397+
'page100'
398+
]
399+
];
400+
}
401+
402+
/**
403+
* Test for custom url rewrite to clean cache on update combinations
404+
*
405+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_category.php
406+
* @magentoApiDataFixture Magento/Cms/_files/pages.php
407+
*
408+
* @throws AlreadyExistsException
409+
*/
410+
public function testUrlRewriteCleansCacheForCustomRewrites()
411+
{
412+
413+
/** @var UrlRewriteResourceModel $urlRewriteResourceModel */
414+
$urlRewriteResourceModel = $this->objectManager->create(UrlRewriteResourceModel::class);
415+
$storeId = 1;
416+
$query = function ($requestUrl) {
417+
return <<<QUERY
418+
{
419+
route(url:"{$requestUrl}")
420+
{
421+
relative_url
422+
type
423+
redirect_code
424+
}
425+
}
426+
QUERY;
427+
};
428+
429+
$customRequestPath = 'test.html';
430+
$customSecondRequestPath = 'test2.html';
431+
$entitiesRequestPaths = [
432+
'simple-product-in-stock.html',
433+
'category-1.html',
434+
'page100'
435+
];
436+
437+
// create custom url rewrite
438+
$urlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
439+
$urlRewriteModel->setEntityType('custom')
440+
->setRedirectType(302)
441+
->setStoreId($storeId)
442+
->setDescription(null)
443+
->setIsAutogenerated(0);
444+
445+
// create second custom url rewrite and target it to previous one to check
446+
// if proper final target url will be resolved
447+
$secondUrlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
448+
$secondUrlRewriteModel->setEntityType('custom')
449+
->setRedirectType(302)
450+
->setStoreId($storeId)
451+
->setRequestPath($customSecondRequestPath)
452+
->setTargetPath($customRequestPath)
453+
->setDescription(null)
454+
->setIsAutogenerated(0);
455+
$urlRewriteResourceModel->save($secondUrlRewriteModel);
456+
457+
foreach ($entitiesRequestPaths as $entityRequestPath) {
458+
// updating custom rewrite for each entity
459+
$urlRewriteModel->setRequestPath($customRequestPath)
460+
->setTargetPath($entityRequestPath);
461+
$urlRewriteResourceModel->save($urlRewriteModel);
462+
463+
// confirm that API returns non-cached response for the first custom rewrite
464+
$apiResponse = $this->graphQlQuery($query($customRequestPath));
465+
$this->assertEquals($entityRequestPath, $apiResponse['route']['relative_url']);
466+
467+
// confirm that API returns non-cached response for the second custom rewrite
468+
$apiResponse = $this->graphQlQuery($query($customSecondRequestPath));
469+
$this->assertEquals($entityRequestPath, $apiResponse['route']['relative_url']);
470+
}
471+
472+
$urlRewriteResourceModel->delete($secondUrlRewriteModel);
473+
474+
// delete custom rewrite and validate that API will not return cached response
475+
$urlRewriteResourceModel->delete($urlRewriteModel);
476+
$apiResponse = $this->graphQlQuery($query($customRequestPath));
477+
$this->assertNull($apiResponse['route']);
478+
}
479+
480+
/**
481+
* Return UrlRewrite model instance by request_path
482+
*
483+
* @param string $requestPath
484+
* @param int $storeId
485+
* @return UrlRewriteModel
486+
*/
487+
private function getUrlRewriteModelByRequestPath(string $requestPath, int $storeId): UrlRewriteModel
488+
{
489+
/** @var UrlFinderInterface $urlFinder */
490+
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
491+
492+
/** @var UrlRewriteService $urlRewriteService */
493+
$urlRewriteService = $urlFinder->findOneByData(
494+
[
495+
'request_path' => $requestPath,
496+
'store_id' => $storeId
497+
]
498+
);
499+
500+
/** @var UrlRewriteModel $urlRewrite */
501+
$urlRewrite = $this->objectManager->create(UrlRewriteModel::class);
502+
$urlRewrite->load($urlRewriteService->getUrlRewriteId());
503+
504+
return $urlRewrite;
310505
}
311506
}

0 commit comments

Comments
 (0)