Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertUrlRewriteProductInGrid.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Constraint\AbstractConstraint;
11 use Magento\Mtf\Fixture\FixtureInterface;
13 use Magento\UrlRewrite\Test\Page\Adminhtml\UrlRewriteIndex;
14 
18 class AssertUrlRewriteProductInGrid extends AbstractConstraint
19 {
25  private $parentCategoryIndex;
26 
32  private $webApi;
33 
39  private $targetPathTemplate = 'catalog/product/view/id/%s/category/%s';
40 
50  public function processAssert(
51  UrlRewriteIndex $urlRewriteIndex,
52  WebapiDecorator $webApi,
53  FixtureInterface $product,
54  Category $category = null
55  ) {
56  $this->webApi = $webApi;
57  $urlRewriteIndex->open();
58  $categories = $product->getDataFieldConfig('category_ids')['source']->getCategories();
59  $rootCategoryArray = [];
60  foreach ($categories as $index => $category) {
61  $parentName = $category->getDataFieldConfig('parent_id')['source']->getParentCategory()->getName();
62  $rootCategoryArray[$parentName]['name'] = !empty($category->getUrlKey())
63  ? strtolower($category->getUrlKey())
64  : strtolower($category->getName());
65  $rootCategoryArray[$parentName]['index'] = $index;
66  }
67 
68  $stores = $product->getDataFieldConfig('website_ids')['source']->getStores();
69  foreach ($stores as $store) {
70  $rootCategoryName = $store->getDataFieldConfig('group_id')['source']
71  ->getStoreGroup()
72  ->getDataFieldConfig('root_category_id')['source']
73  ->getCategory()
74  ->getName();
75 
76  $this->parentCategoryIndex = $rootCategoryArray[$rootCategoryName]['index'];
77 
78  $storeName = $store->getName();
79  $filters = [
80  [
81  'request_path' => $product->getUrlKey() . '.html',
82  'store_id' => $storeName
83  ],
84  [
85  'request_path' => $rootCategoryArray[$rootCategoryName]['name'] . '.html',
86  'store_id' => $storeName
87  ],
88  [
89  'request_path' =>
90  $rootCategoryArray[$rootCategoryName]['name'] . '/' . $product->getUrlKey() . '.html',
91  'target_path' => $this->getTargetPath($product, $category),
92  'store_id' => $storeName
93  ],
94  ];
95  foreach ($filters as $filter) {
96  \PHPUnit\Framework\Assert::assertTrue(
97  $urlRewriteIndex->getUrlRedirectGrid()->isRowVisible($filter, true, false),
98  'URL Rewrite with request path \'' . $filter['request_path'] . '\' is absent in grid.'
99  );
100  }
101  }
102  }
103 
111  private function getTargetPath(FixtureInterface $product, FixtureInterface $category = null)
112  {
113  $productId = $product->getId()
114  ? $product->getId()
115  : $this->retrieveProductBySku($product->getSku())['id'];
116  $categoryId = $product->hasData('category_ids')
117  ? $this->getCategoryId($product)
118  : ($category ? $category->getId() : '');
119  return sprintf($this->targetPathTemplate, $productId, $categoryId);
120  }
121 
128  private function getCategoryId(FixtureInterface $product)
129  {
130  $productSku = $product->getSku();
131  $categoryId = $product->getDataFieldConfig('category_ids')['source']
132  ->getCategories()[$this->parentCategoryIndex]->getId();
133  $categoryId = $categoryId
134  ? $categoryId
135  : $this->retrieveProductBySku($productSku)
136  ['extension_attributes']['category_links'][$this->parentCategoryIndex]['category_id'];
137  return $categoryId;
138  }
139 
146  public function retrieveProductBySku($sku)
147  {
148  $url = $_ENV['app_frontend_url'] . 'rest/all/V1/products/' . $sku;
149  $this->webApi->write($url, [], WebapiDecorator::GET);
150  $response = json_decode($this->webApi->read(), true);
151  $this->webApi->close();
152  return $response;
153  }
154 
160  public function toString()
161  {
162  return 'URL Rewrite is present in grid.';
163  }
164 }
$filters
Definition: uploader.phtml:11
processAssert(UrlRewriteIndex $urlRewriteIndex, WebapiDecorator $webApi, FixtureInterface $product, Category $category=null)
$categories
$storeName
Definition: logo.phtml:13
$index
Definition: list.phtml:44