Skip to content

Commit

Permalink
ENGCOM-5081: Fixed internal server error for query urlResolver magent…
Browse files Browse the repository at this point in the history
…o#445

 - Merge Pull Request magento/graphql-ce#445 from yogeshsuhagiya/graphql-ce:2.3-develop-graphql-PR-yogesh-2
 - Merged commits:
   1. ad4efb8
   2. 57d8908
   3. 8b39ecc
   4. 273ea48
   5. 43ba611
   6. 96c1e2c
   7. ec2ee49
  • Loading branch information
magento-engcom-team committed May 8, 2019
2 parents c56341c + ec2ee49 commit 9a0a1b2
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
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()) {
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,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
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
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 9a0a1b2

Please sign in to comment.