10 use Magento\Mtf\Constraint\AbstractConstraint;
11 use Magento\UrlRewrite\Test\Page\Adminhtml\UrlRewriteIndex;
36 private $urlRewriteIndex;
54 UrlRewriteIndex $urlRewriteIndex,
55 Category $parentCategory =
null,
56 Category $childCategory =
null,
58 $filterByPath =
'target_path',
59 $redirectType =
'Permanent (301)' 61 $this->urlRewriteIndex = $urlRewriteIndex;
62 $this->webApi = $webApi;
64 $urlRewriteIndex->open();
65 $categoryId = $this->getCategoryId(
$category, $childCategory);
66 $nestingPath = $this->getNestingPath(
$category, $nestingLevel);
69 'request_path' => $nestingPath,
70 'target_path' =>
'catalog/category/view/id/' . $categoryId,
73 if ($parentCategory && $childCategory) {
74 $filter[
'request_path'] =
75 strtolower($parentCategory->getUrlKey() .
'/' . $childCategory->getUrlKey() .
'.html');
77 $this->rowVisibleAssertion($filter);
79 if ($redirectType != self::REDIRECT_TYPE_NO) {
80 if ($parentCategory && $childCategory) {
81 $urlPath = strtolower($parentCategory->getUrlKey() .
'/' . $childCategory->getUrlKey() .
'.html');
83 'request_path' => $nestingPath,
84 'target_path' => $urlPath,
85 'redirect_type' => $redirectType
88 $filter = [$filterByPath => strtolower(
$category->getUrlKey())];
90 $this->rowVisibleAssertion($filter);
101 private function getCategoryId(Category
$category, Category $childCategory =
null)
103 return ($childCategory ? $childCategory->getId() :
$category->getId())
105 : $this->retrieveCategory(
$category)[
'id'];
114 private function rowVisibleAssertion(array $filter)
116 $filterRow = implode(
', ', $filter);
117 \PHPUnit\Framework\Assert::assertTrue(
118 $this->urlRewriteIndex->getUrlRedirectGrid()->isRowVisible($filter,
true,
false),
119 'URL Rewrite with request path "' . $filterRow .
'" is absent in grid.' 130 private function getNestingPath(Category
$category, $nestingLevel)
132 if ($nestingLevel ===
null) {
133 return strtolower(
$category->getUrlKey() .
'.html');
135 $filterByRequestPathCondition = [];
136 for ($nestingIterator = 0; $nestingIterator < $nestingLevel; $nestingIterator++) {
137 $filterByRequestPathCondition[] =
$category->getUrlKey();
138 $category =
$category->getDataFieldConfig(
'parent_id')[
'source']->getParentCategory();
141 return strtolower(implode(
'/', array_reverse($filterByRequestPathCondition)) .
'.html');
150 private function retrieveCategory(Category
$category)
152 $childrenIds = explode(
',', $this->getResponse(
$category->getData(
'parent_id'))[
'children']);
153 while (
$id = array_pop($childrenIds)) {
154 $retrieveCategory = $this->getResponse(
$id);
155 if ($retrieveCategory[
'name'] ==
$category->getData(
'name')) {
156 return $retrieveCategory;
159 return [
'id' =>
null];
168 private function getResponse($categoryId)
170 $url = $_ENV[
'app_frontend_url'] .
'rest/all/V1/categories/' . $categoryId;
171 $this->webApi->write(
$url, [], WebapiDecorator::GET);
172 $response = json_decode($this->webApi->read(),
true);
173 $this->webApi->close();
184 return 'URL Rewrite is present in grid.';
processAssert(Category $category, WebapiDecorator $webApi, UrlRewriteIndex $urlRewriteIndex, Category $parentCategory=null, Category $childCategory=null, $nestingLevel=null, $filterByPath='target_path', $redirectType='Permanent(301)')