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

Commit

Permalink
ENGCOM-5081: Fixed internal server error for query urlResolver #445
Browse files Browse the repository at this point in the history
naydav authored May 8, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents e981e66 + 9a0a1b2 commit 7a231da
Showing 6 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 = <<<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']);
}

/**
* 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 = <<<QUERY
{
urlResolver(url:"{$urlPath}")
{
id
relative_url
type
}
}
QUERY;

$this->expectExceptionMessage(
"No such entity found with matching URL key: " . $urlPath
);
$this->graphQlQuery($query);
}
}
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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);

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\UrlRewrite;

$objectManager = Bootstrap::getObjectManager();

/** @var int Product Id */
$productId = 708579;

/** @var UrlRewrite $urlRewrite */
$urlRewrite = $objectManager->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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\UrlRewrite;

$objectManager = Bootstrap::getObjectManager();

/** @var UrlRewrite $urlRewrite */
$urlRewrite = $objectManager->create(UrlRewrite::class);
$urlRewrite->load('non-exist-entity.html', 'request_path');
$urlRewrite->delete();

0 comments on commit 7a231da

Please sign in to comment.