diff --git a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php index c842d660a6176..6e6c915959a16 100644 --- a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php +++ b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php @@ -8,6 +8,7 @@ namespace Magento\UrlRewriteGraphQl\Model\Resolver; use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -73,6 +74,11 @@ public function resolve( $url = $customUrl ?: $url; $urlRewrite = $this->findCanonicalUrl($url); if ($urlRewrite) { + if (!$urlRewrite->getEntityId()) { + throw new GraphQlNoSuchEntityException( + __('No such entity found with matching URL key: %url', ['url' => $url]) + ); + } $result = [ 'id' => $urlRewrite->getEntityId(), 'canonical_url' => $urlRewrite->getTargetPath(), @@ -100,6 +106,9 @@ private function findCanonicalUrl(string $requestPath) : ?\Magento\UrlRewrite\Se if (!$urlRewrite) { $urlRewrite = $this->findUrlFromTargetPath($requestPath); } + if ($urlRewrite && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated()) { + $urlRewrite = $this->findUrlFromTargetPath($urlRewrite->getTargetPath()); + } return $urlRewrite; } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php index 370121a1dad78..8eaf33483531d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/UrlRewrite/UrlResolverTest.php @@ -15,6 +15,7 @@ use Magento\Cms\Helper\Page as PageHelper; use Magento\Store\Model\ScopeInterface; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\UrlRewrite\Model\UrlRewrite; /** * Test the GraphQL endpoint's URLResolver query to verify canonical URL's are correctly returned. @@ -355,4 +356,68 @@ public function testResolveSlash() $this->assertEquals($targetPath, $response['urlResolver']['relative_url']); $this->assertEquals('CMS_PAGE', $response['urlResolver']['type']); } + + /** + * Test for custom type which point to the valid product/category/cms page. + * + * @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php + */ + public function testGetNonExistentUrlRewrite() + { + $urlPath = 'non-exist-product.html'; + /** @var UrlRewrite $urlRewrite */ + $urlRewrite = $this->objectManager->create(UrlRewrite::class); + $urlRewrite->load($urlPath, 'request_path'); + + /** @var UrlFinderInterface $urlFinder */ + $urlFinder = $this->objectManager->get(UrlFinderInterface::class); + $actualUrls = $urlFinder->findOneByData( + [ + 'request_path' => $urlPath, + 'store_id' => 1 + ] + ); + $targetPath = $actualUrls->getTargetPath(); + + $query = <<graphQlQuery($query); + $this->assertArrayHasKey('urlResolver', $response); + $this->assertEquals('PRODUCT', $response['urlResolver']['type']); + $this->assertEquals($targetPath, $response['urlResolver']['relative_url']); + } + + /** + * Test for custom type which point to the invalid product/category/cms page. + * + * @magentoApiDataFixture Magento/UrlRewrite/_files/url_rewrite_not_existing_entity.php + */ + public function testNonExistentEntityUrlRewrite() + { + $urlPath = 'non-exist-entity.html'; + + $query = <<expectExceptionMessage( + "No such entity found with matching URL key: " . $urlPath + ); + $this->graphQlQuery($query); + } } diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php index 2f3d4ea4c3e7f..3c03318942167 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php @@ -14,6 +14,7 @@ use Magento\Framework\Indexer\IndexerRegistry; use Magento\Store\Model\StoreManagerInterface; use Magento\TestFramework\Helper\Bootstrap; +use Magento\UrlRewrite\Model\UrlRewrite; /** * @var \Magento\Store\Model\Store $store @@ -80,3 +81,15 @@ /** @var CategoryLinkManagementInterface $linkManagement */ $linkManagement = $objectManager->get(CategoryLinkManagementInterface::class); $linkManagement->assignProductToCategories($product->getSku(), [Category::TREE_ROOT_ID, $category->getEntityId()]); + +/** @var UrlRewrite $urlRewrite */ +$urlRewrite = $objectManager->create(UrlRewrite::class); +$urlRewrite->setEntityType('custom') + ->setRequestPath('non-exist-product.html') + ->setTargetPath('catalog/product/view/id/' . $product->getId()) + ->setRedirectType(0) + ->setStoreId(1) + ->setDescription(null) + ->setIsAutogenerated(0); + +$urlRewrite->save(); diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category_rollback.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category_rollback.php index 2598dd6693500..3a1e379213342 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category_rollback.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category_rollback.php @@ -9,6 +9,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\TestFramework\Helper\Bootstrap; +use Magento\UrlRewrite\Model\UrlRewrite; $objectManager = Bootstrap::getObjectManager(); /** @var \Magento\Framework\Registry $registry */ @@ -41,6 +42,11 @@ $categoryRepository->delete($category); } +/** @var UrlRewrite $urlRewrite */ +$urlRewrite = $objectManager->create(UrlRewrite::class); +$urlRewrite->load('non-exist-product.html', 'request_path'); +$urlRewrite->delete(); + $registry->unregister('isSecureArea'); $registry->register('isSecureArea', false); diff --git a/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity.php b/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity.php new file mode 100644 index 0000000000000..052eac6f79d48 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity.php @@ -0,0 +1,25 @@ +create(UrlRewrite::class); +$urlRewrite->setEntityType('custom') + ->setRequestPath('non-exist-entity.html') + ->setTargetPath('catalog/product/view/id/' . $productId) + ->setRedirectType(0) + ->setStoreId(1) + ->setDescription(null) + ->setIsAutogenerated(0); + +$urlRewrite->save(); diff --git a/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity_rollback.php b/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity_rollback.php new file mode 100644 index 0000000000000..12cbd2c704bfb --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite_not_existing_entity_rollback.php @@ -0,0 +1,15 @@ +create(UrlRewrite::class); +$urlRewrite->load('non-exist-entity.html', 'request_path'); +$urlRewrite->delete();