Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertUrlRewriteCategoryInGrid.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Constraint\AbstractConstraint;
11 use Magento\UrlRewrite\Test\Page\Adminhtml\UrlRewriteIndex;
13 
17 class AssertUrlRewriteCategoryInGrid extends AbstractConstraint
18 {
22  const REDIRECT_TYPE_NO = 'No';
23 
29  private $webApi;
30 
36  private $urlRewriteIndex;
37 
51  public function processAssert(
52  Category $category,
53  WebapiDecorator $webApi,
54  UrlRewriteIndex $urlRewriteIndex,
55  Category $parentCategory = null,
56  Category $childCategory = null,
57  $nestingLevel = null,
58  $filterByPath = 'target_path',
59  $redirectType = 'Permanent (301)'
60  ) {
61  $this->urlRewriteIndex = $urlRewriteIndex;
62  $this->webApi = $webApi;
63 
64  $urlRewriteIndex->open();
65  $categoryId = $this->getCategoryId($category, $childCategory);
66  $nestingPath = $this->getNestingPath($category, $nestingLevel);
67 
68  $filter = [
69  'request_path' => $nestingPath,
70  'target_path' => 'catalog/category/view/id/' . $categoryId,
71  'redirect_type' => self::REDIRECT_TYPE_NO
72  ];
73  if ($parentCategory && $childCategory) {
74  $filter['request_path'] =
75  strtolower($parentCategory->getUrlKey() . '/' . $childCategory->getUrlKey() . '.html');
76  }
77  $this->rowVisibleAssertion($filter);
78 
79  if ($redirectType != self::REDIRECT_TYPE_NO) {
80  if ($parentCategory && $childCategory) {
81  $urlPath = strtolower($parentCategory->getUrlKey() . '/' . $childCategory->getUrlKey() . '.html');
82  $filter = [
83  'request_path' => $nestingPath,
84  'target_path' => $urlPath,
85  'redirect_type' => $redirectType
86  ];
87  } else {
88  $filter = [$filterByPath => strtolower($category->getUrlKey())];
89  }
90  $this->rowVisibleAssertion($filter);
91  }
92  }
93 
101  private function getCategoryId(Category $category, Category $childCategory = null)
102  {
103  return ($childCategory ? $childCategory->getId() : $category->getId())
104  ? $category->getId()
105  : $this->retrieveCategory($category)['id'];
106  }
107 
114  private function rowVisibleAssertion(array $filter)
115  {
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.'
120  );
121  }
122 
130  private function getNestingPath(Category $category, $nestingLevel)
131  {
132  if ($nestingLevel === null) {
133  return strtolower($category->getUrlKey() . '.html');
134  }
135  $filterByRequestPathCondition = [];
136  for ($nestingIterator = 0; $nestingIterator < $nestingLevel; $nestingIterator++) {
137  $filterByRequestPathCondition[] = $category->getUrlKey();
138  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
139  }
140 
141  return strtolower(implode('/', array_reverse($filterByRequestPathCondition)) . '.html');
142  }
143 
150  private function retrieveCategory(Category $category)
151  {
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;
157  }
158  }
159  return ['id' => null];
160  }
161 
168  private function getResponse($categoryId)
169  {
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();
174  return $response;
175  }
176 
182  public function toString()
183  {
184  return 'URL Rewrite is present in grid.';
185  }
186 }
$response
Definition: 404.php:11
$id
Definition: fieldset.phtml:14
processAssert(Category $category, WebapiDecorator $webApi, UrlRewriteIndex $urlRewriteIndex, Category $parentCategory=null, Category $childCategory=null, $nestingLevel=null, $filterByPath='target_path', $redirectType='Permanent(301)')