123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\GraphQl\UrlRewrite;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator;
- use Magento\TestFramework\ObjectManager;
- use Magento\TestFramework\TestCase\GraphQlAbstract;
- use Magento\UrlRewrite\Model\UrlFinderInterface;
- use Magento\Cms\Helper\Page as PageHelper;
- use Magento\Store\Model\ScopeInterface;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- /**
- * Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned.
- */
- class UrlResolverTest extends GraphQlAbstract
- {
- /** @var ObjectManager */
- private $objectManager;
- protected function setUp()
- {
- $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- }
- /**
- * Tests if target_path(canonical_url) is resolved for Product entity
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testProductUrlResolver()
- {
- $productSku = 'p002';
- $urlPath = 'p002.html';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $actualUrls = $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath,
- 'store_id' => $storeId
- ]
- );
- $targetPath = $actualUrls->getTargetPath();
- $expectedType = $actualUrls->getEntityType();
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$urlPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
- }
- /**
- * Tests the use case where canonical_url is provided as resolver input in the Query
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testProductUrlWithCanonicalUrlInput()
- {
- $productSku = 'p002';
- $urlPath = 'p002.html';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- $product->getUrlKey();
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $actualUrls = $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath,
- 'store_id' => $storeId
- ]
- );
- $targetPath = $actualUrls->getTargetPath();
- $expectedType = $actualUrls->getEntityType();
- $canonicalPath = $actualUrls->getTargetPath();
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$canonicalPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
- }
- /**
- * Test for category entity
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testCategoryUrlResolver()
- {
- $productSku = 'p002';
- $urlPath2 = 'cat-1.html';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $actualUrls = $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath2,
- 'store_id' => $storeId
- ]
- );
- $categoryId = $actualUrls->getEntityId();
- $targetPath = $actualUrls->getTargetPath();
- $expectedType = $actualUrls->getEntityType();
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$urlPath2}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($categoryId, $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
- }
- /**
- * @magentoApiDataFixture Magento/Cms/_files/pages.php
- */
- public function testCMSPageUrlResolver()
- {
- /** @var \Magento\Cms\Model\Page $page */
- $page = $this->objectManager->get(\Magento\Cms\Model\Page::class);
- $page->load('page100');
- $cmsPageId = $page->getId();
- $requestPath = $page->getIdentifier();
- /** @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $urlPathGenerator */
- $urlPathGenerator = $this->objectManager->get(\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator::class);
- /** @param \Magento\Cms\Api\Data\PageInterface $page */
- $targetPath = $urlPathGenerator->getCanonicalUrlPath($page);
- $expectedEntityType = CmsPageUrlRewriteGenerator::ENTITY_TYPE;
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$requestPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertEquals($cmsPageId, $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper(str_replace('-', '_', $expectedEntityType)), $response['urlResolver']['type']);
- }
- /**
- * Tests the use case where the url_key of the existing product is changed
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testProductUrlRewriteResolver()
- {
- $productSku = 'p002';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- $product->setUrlKey('p002-new')->save();
- $urlPath = $product->getUrlKey() . '.html';
- $this->assertEquals($urlPath, 'p002-new.html');
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $actualUrls = $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath,
- 'store_id' => $storeId
- ]
- );
- $targetPath = $actualUrls->getTargetPath();
- $expectedType = $actualUrls->getEntityType();
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$urlPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($product->getEntityId(), $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
- }
- /**
- * Tests if null is returned when an invalid request_path is provided as input to urlResolver
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testInvalidUrlResolverInput()
- {
- $productSku = 'p002';
- $urlPath = 'p002';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath,
- 'store_id' => $storeId
- ]
- );
- $query
- = <<<QUERY
- {
- urlResolver(url:"{$urlPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertNull($response['urlResolver']);
- }
- /**
- * Test for category entity with leading slash
- *
- * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
- */
- public function testCategoryUrlWithLeadingSlash()
- {
- $productSku = 'p002';
- $urlPath = 'cat-1.html';
- /** @var ProductRepositoryInterface $productRepository */
- $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
- $product = $productRepository->get($productSku, false, null, true);
- $storeId = $product->getStoreId();
- /** @var UrlFinderInterface $urlFinder */
- $urlFinder = $this->objectManager->get(UrlFinderInterface::class);
- $actualUrls = $urlFinder->findOneByData(
- [
- 'request_path' => $urlPath,
- 'store_id' => $storeId
- ]
- );
- $categoryId = $actualUrls->getEntityId();
- $targetPath = $actualUrls->getTargetPath();
- $expectedType = $actualUrls->getEntityType();
- $query = <<<QUERY
- {
- urlResolver(url:"/{$urlPath}")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($categoryId, $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals(strtoupper($expectedType), $response['urlResolver']['type']);
- }
- /**
- * Test resolution of '/' path to home page
- */
- public function testResolveSlash()
- {
- /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface */
- $scopeConfigInterface = $this->objectManager->get(ScopeConfigInterface::class);
- $homePageIdentifier = $scopeConfigInterface->getValue(
- PageHelper::XML_PATH_HOME_PAGE,
- ScopeInterface::SCOPE_STORE
- );
- /** @var \Magento\Cms\Model\Page $page */
- $page = $this->objectManager->get(\Magento\Cms\Model\Page::class);
- $page->load($homePageIdentifier);
- $homePageId = $page->getId();
- /** @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $urlPathGenerator */
- $urlPathGenerator = $this->objectManager->get(\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator::class);
- /** @param \Magento\Cms\Api\Data\PageInterface $page */
- $targetPath = $urlPathGenerator->getCanonicalUrlPath($page);
- $query
- = <<<QUERY
- {
- urlResolver(url:"/")
- {
- id
- canonical_url
- type
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- $this->assertArrayHasKey('urlResolver', $response);
- $this->assertEquals($homePageId, $response['urlResolver']['id']);
- $this->assertEquals($targetPath, $response['urlResolver']['canonical_url']);
- $this->assertEquals('CMS_PAGE', $response['urlResolver']['type']);
- }
- }
|