Skip to content

Commit 2234b99

Browse files
author
Krishna Kumar
committed
Fixed the failing unit test cases and MD test cases
1 parent e40257a commit 2234b99

File tree

3 files changed

+95
-101
lines changed

3 files changed

+95
-101
lines changed

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/AbstractEntityUrl.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,95 @@
77

88
namespace Magento\UrlRewriteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1014
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1115
use Magento\UrlRewrite\Model\UrlFinderInterface;
16+
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
1217

1318
abstract class AbstractEntityUrl {
19+
/**
20+
* @var CustomUrlLocatorInterface
21+
*/
22+
private $customUrlLocator;
1423

1524
/**
1625
* @var UrlFinderInterface
1726
*/
1827
private $urlFinder;
1928

29+
/**
30+
* @var int
31+
*/
2032
protected $redirectType = 0;
2133

2234
/**
2335
* AbstractUrlHelper constructor.
36+
* @param CustomUrlLocatorInterface $customUrlLocator
2437
* @param UrlFinderInterface $urlFinder
2538
*/
2639
public function __construct (
40+
CustomUrlLocatorInterface $customUrlLocator,
2741
UrlFinderInterface $urlFinder
2842
) {
43+
$this->customUrlLocator = $customUrlLocator;
2944
$this->urlFinder = $urlFinder;
3045
}
3146

47+
/**
48+
* @param Field $field
49+
* @param $context
50+
* @param ResolveInfo $info
51+
* @param array|null $value
52+
* @param array|null $args
53+
* @return array|null
54+
* @throws GraphQlInputException
55+
* @throws GraphQlNoSuchEntityException
56+
*/
57+
public function resolve(
58+
Field $field,
59+
$context,
60+
ResolveInfo $info,
61+
array $value = null,
62+
array $args = null
63+
) {
64+
if (!isset($args['url']) || empty(trim($args['url']))) {
65+
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
66+
}
67+
68+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
69+
$result = null;
70+
$url = $args['url'];
71+
if (substr($url, 0, 1) === '/' && $url !== '/') {
72+
$url = ltrim($url, '/');
73+
}
74+
$this->redirectType = 0;
75+
$customUrl = $this->customUrlLocator->locateUrl($url);
76+
$url = $customUrl ?: $url;
77+
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
78+
if ($finalUrlRewrite) {
79+
$relativeUrl = $finalUrlRewrite->getRequestPath();
80+
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
81+
'id' => $finalUrlRewrite->getEntityId(),
82+
'canonical_url' => $relativeUrl,
83+
'relative_url' => $relativeUrl,
84+
'redirectCode' => $this->redirectType,
85+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
86+
];
87+
88+
if (empty($resultArray['id'])) {
89+
throw new GraphQlNoSuchEntityException(
90+
__('No such entity found with matching URL key: %url', ['url' => $url])
91+
);
92+
}
93+
94+
$result = $resultArray;
95+
}
96+
return $result;
97+
}
98+
3299
/**
33100
* Handle custom urls with and without redirects
34101
*
@@ -46,7 +113,6 @@ protected function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId):
46113
? $finalCustomUrlRewrite->getRequestPath() : $finalUrlRewrite->getRequestPath();
47114
return [
48115
'id' => $finalUrlRewrite->getEntityId(),
49-
'entity_uid' => $this->idEncoder->encode((string)$finalUrlRewrite->getEntityId()),
50116
'canonical_url' => $relativeUrl,
51117
'relative_url' => $relativeUrl,
52118
'redirectCode' => $finalCustomUrlRewrite->getRedirectType(),

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,16 @@
77

88
namespace Magento\UrlRewriteGraphQl\Model\Resolver;
99

10-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1210
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1311
use Magento\Framework\GraphQl\Config\Element\Field;
1412
use Magento\Framework\GraphQl\Query\ResolverInterface;
15-
use Magento\UrlRewrite\Model\UrlFinderInterface;
1613
use Magento\UrlRewriteGraphQl\Model\Resolver\AbstractEntityUrl;
17-
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
1814

1915
/**
2016
* UrlRewrite field resolver, used for GraphQL request processing.
2117
*/
2218
class EntityUrl extends AbstractEntityUrl implements ResolverInterface
2319
{
24-
/**
25-
* @var CustomUrlLocatorInterface
26-
*/
27-
private $customUrlLocator;
28-
29-
/**
30-
* @param UrlFinderInterface $urlFinder
31-
* @param CustomUrlLocatorInterface $customUrlLocator
32-
*/
33-
public function __construct(
34-
UrlFinderInterface $urlFinder,
35-
CustomUrlLocatorInterface $customUrlLocator
36-
) {
37-
parent::__construct($urlFinder);
38-
$this->customUrlLocator = $customUrlLocator;
39-
}
40-
4120
/**
4221
* @inheritdoc
4322
*/
@@ -48,38 +27,12 @@ public function resolve(
4827
array $value = null,
4928
array $args = null
5029
) {
51-
if (!isset($args['url']) || empty(trim($args['url']))) {
52-
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
53-
}
54-
55-
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
56-
$result = null;
57-
$url = $args['url'];
58-
if (substr($url, 0, 1) === '/' && $url !== '/') {
59-
$url = ltrim($url, '/');
60-
}
61-
$this->redirectType = 0;
62-
$customUrl = $this->customUrlLocator->locateUrl($url);
63-
$url = $customUrl ?: $url;
64-
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
65-
if ($finalUrlRewrite) {
66-
$relativeUrl = $finalUrlRewrite->getRequestPath();
67-
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
68-
'id' => $finalUrlRewrite->getEntityId(),
69-
'canonical_url' => $relativeUrl,
70-
'relative_url' => $relativeUrl,
71-
'redirectCode' => $this->redirectType,
72-
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
73-
];
74-
75-
if (empty($resultArray['id'])) {
76-
throw new GraphQlNoSuchEntityException(
77-
__('No such entity found with matching URL key: %url', ['url' => $url])
78-
);
79-
}
80-
81-
$result = $resultArray;
82-
}
83-
return $result;
30+
return parent::resolve(
31+
$field,
32+
$context,
33+
$info,
34+
$value,
35+
$args
36+
);
8437
}
8538
}

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/Route.php

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ class Route extends AbstractEntityUrl implements ResolverInterface
2626
const CMS_PAGE = 'CMS_PAGE';
2727
const PRODUCT = 'PRODUCT';
2828
const CATEGORY = 'CATEGORY';
29-
/**
30-
* @var PageDataProvider
31-
*/
32-
private $pageDataProvider;
3329

3430
/**
3531
* @var CustomUrlLocatorInterface
3632
*/
3733
private $customUrlLocator;
3834

35+
/**
36+
* @var PageDataProvider
37+
*/
38+
private $pageDataProvider;
39+
3940
/**
4041
* @var ProductRepository
4142
*/
@@ -57,13 +58,14 @@ class Route extends AbstractEntityUrl implements ResolverInterface
5758
private $categoryRepository;
5859

5960
/**
61+
* Route constructor.
6062
* @param UrlFinderInterface $urlFinder
61-
* @param CustomUrlLocatorInterface $customUrlLocator
6263
* @param ProductRepository $productRepository
6364
* @param CategoryTreeDataProvider $categoryTree
6465
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
6566
* @param PageDataProvider $pageDataProvider
6667
* @param CategoryRepository $categoryRepository
68+
* @param CustomUrlLocatorInterface $customUrlLocator
6769
*/
6870
public function __construct(
6971
UrlFinderInterface $urlFinder,
@@ -74,8 +76,7 @@ public function __construct(
7476
PageDataProvider $pageDataProvider,
7577
CategoryRepository $categoryRepository
7678
) {
77-
parent::__construct($urlFinder);
78-
$this->customUrlLocator = $customUrlLocator;
79+
parent::__construct($customUrlLocator, $urlFinder);
7980
$this->productRepository = $productRepository;
8081
$this->categoryTree = $categoryTree;
8182
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
@@ -93,63 +94,37 @@ public function resolve(
9394
array $value = null,
9495
array $args = null
9596
) {
96-
if (!isset($args['url']) || empty(trim($args['url']))) {
97-
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
98-
}
99-
100-
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
10197
$result = null;
102-
$url = $args['url'];
103-
if (substr($url, 0, 1) === '/' && $url !== '/') {
104-
$url = ltrim($url, '/');
105-
}
106-
$this->redirectType = 0;
107-
$customUrl = $this->customUrlLocator->locateUrl($url);
108-
$url = $customUrl ?: $url;
109-
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
110-
111-
if ($finalUrlRewrite) {
112-
$relativeUrl = $finalUrlRewrite->getRequestPath();
113-
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
114-
'id' => $finalUrlRewrite->getEntityId(),
115-
'canonical_url' => $relativeUrl,
116-
'relative_url' => $relativeUrl,
117-
'redirectCode' => $this->redirectType,
118-
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
119-
];
120-
121-
if (empty($resultArray['id'])) {
122-
throw new GraphQlNoSuchEntityException(
123-
__('No such entity found with matching URL key: %url', ['url' => $url])
124-
);
125-
}
126-
98+
$resultArray = parent::resolve(
99+
$field,
100+
$context,
101+
$info,
102+
$value,
103+
$args
104+
);
105+
106+
if ($resultArray) {
127107
if ($resultArray['type'] == self::CMS_PAGE) {
128108
$result = $this->pageDataProvider->getDataByPageId((int)$resultArray['id']);
129109
$result['type_id'] = self::CMS_PAGE;
130110
} else if ($resultArray['type'] == self::CATEGORY) {
131111
$categoryId = (int)$resultArray['id'];
132-
$categoty = $this->categoryRepository->get($categoryId);
133-
112+
$category = $this->categoryRepository->get($categoryId);
134113
$categoriesTree = $this->categoryTree->getTree($info, $categoryId);
135114
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
136115
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
137116
}
138-
139117
$result = current($this->extractDataFromCategoryTree->execute($categoriesTree));
140-
141-
$result['meta_title'] = $categoty->getData()['meta_title'] ?? null;
142-
$result['meta_keywords'] = $categoty->getData()['meta_keywords'] ?? null;
143-
$result['meta_description'] = $categoty->getData()['meta_description'] ?? null;
118+
$result['meta_title'] = $category->getData()['meta_title'] ?? null;
119+
$result['meta_keywords'] = $category->getData()['meta_keywords'] ?? null;
120+
$result['meta_description'] = $category->getData()['meta_description'] ?? null;
144121
$result['type_id'] = self::CATEGORY;
145122
} else if ($resultArray['type'] == self::PRODUCT) {
146123
$product = $this->productRepository->getById($resultArray['id']);
147124
$result = $product->getData();
148125
$result['model'] = $product;
149126
}
150-
151127
}
152-
153128
return $result;
154129
}
155130
}

0 commit comments

Comments
 (0)