Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertCategoryLayeredNavigation.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11 use Magento\Mtf\Client\BrowserInterface;
12 use Magento\Mtf\Constraint\AbstractConstraint;
13 
17 class AssertCategoryLayeredNavigation extends AbstractConstraint
18 {
24  private $browser;
25 
34  public function processAssert(
35  CatalogCategoryView $catalogCategoryView,
36  Category $category,
37  BrowserInterface $browser
38  ) {
39  $this->browser = $browser;
40  $this->openCategory($category->getDataFieldConfig('parent_id')['source']->getParentCategory());
41 
42  \PHPUnit\Framework\Assert::assertTrue(
43  $catalogCategoryView->getLayeredNavigationBlock()->isCategoryVisible($category, 1),
44  'Category ' . $category->getName() . ' is absent in Layered Navigation.'
45  );
46 
47  $productsOnCategoryPage = $catalogCategoryView->getListProductBlock()->getProductNames();
48  $productsInCategory = $category->getDataFieldConfig('category_products')['source']->getProducts();
49  foreach ($productsInCategory as $product) {
50  \PHPUnit\Framework\Assert::assertTrue(
51  in_array($product->getName(), $productsOnCategoryPage),
52  'Product ' . $product->getName() . ' is absent on category page.'
53  );
54  }
55  }
56 
63  private function openCategory(Category $category)
64  {
65  $categoryUrlKey = [];
66 
67  while ($category) {
68  $categoryUrlKey[] = $category->hasData('url_key')
69  ? strtolower($category->getUrlKey())
70  : trim(strtolower(preg_replace('#[^0-9a-z%]+#i', '-', $category->getName())), '-');
71 
72  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
73  if ($category !== null && 1 == $category->getParentId()) {
74  $category = null;
75  }
76  }
77  $categoryUrlKey = $_ENV['app_frontend_url'] . implode('/', array_reverse($categoryUrlKey)) . '.html';
78 
79  $this->browser->open($categoryUrlKey);
80  }
81 
87  public function toString()
88  {
89  return 'Category is present in layered navigation and product is visible in product grid.';
90  }
91 }
processAssert(CatalogCategoryView $catalogCategoryView, Category $category, BrowserInterface $browser)