Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Fixed internal server error for query urlResolver #445

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,11 @@ public function resolve(
$url = $customUrl ?: $url;
$urlRewrite = $this->findCanonicalUrl($url);
if ($urlRewrite) {
if (!$urlRewrite->getEntityId()) {
yogeshsuhagiya marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

@TomashKhamlai TomashKhamlai Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We hide errors here.

Simple example:

  1. Create Custom URL Rewrite
    Request Path: go-to-admin
    Target Path: /admin
    Redirect Type: No
  2. Perform query:
query{
  urlResolver(
    url: "go-to-admin"
  ) {
    id
    relative_url
    type
  }
}

For this case expected response in Internal Server Error which means

Exception #0 (LogicException): Front controller reached 100 router match iterations

but we simply got "No such entity found with matching URL key: go-to-admin"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to rely on Magento error handling then just check if our response is casting to true or false? @naydav?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception #0 (LogicException): Front controller reached 100 router match iterations
Looks like it's bug in Magento. expected result: 404 Page not found

Does everything else work?

throw new GraphQlNoSuchEntityException(
__('No such entity found with matching URL key: %url', ['url' => $url])
);
}
$result = [
'id' => $urlRewrite->getEntityId(),
'canonical_url' => $urlRewrite->getTargetPath(),
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -355,4 +356,42 @@ 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()
yogeshsuhagiya marked this conversation as resolved.
Show resolved Hide resolved
{
$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 = <<<QUERY
{
urlResolver(url:"{$urlPath}")
{
id
relative_url
type
}
}
QUERY;
$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('urlResolver', $response);
$this->assertEquals('PRODUCT', $response['urlResolver']['type']);
$this->assertEquals($targetPath, $response['urlResolver']['relative_url']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Expand Down