Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductIsNotDisplayingOnFrontend.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11 use Magento\Catalog\Test\Page\Product\CatalogProductView;
12 use Magento\CatalogSearch\Test\Page\CatalogsearchResult;
13 use Magento\Cms\Test\Page\CmsIndex;
14 use Magento\Mtf\Client\BrowserInterface;
15 use Magento\Mtf\Constraint\AbstractConstraint;
16 use Magento\Mtf\Fixture\FixtureInterface;
17 
21 class AssertProductIsNotDisplayingOnFrontend extends AbstractConstraint
22 {
26  const NOT_FOUND_MESSAGE = 'Whoops, our bad...';
27 
34 
41 
48 
54  protected $cmsIndex;
55 
61  protected $browser;
62 
68  protected $category;
69 
81  public function processAssert(
82  CatalogProductView $catalogProductView,
83  CatalogsearchResult $catalogSearchResult,
84  CatalogCategoryView $catalogCategoryView,
85  CmsIndex $cmsIndex,
86  $product,
87  BrowserInterface $browser,
88  Category $category = null
89  ) {
90  $this->browser = $browser;
91  $this->catalogProductView = $catalogProductView;
92  $this->catalogSearchResult = $catalogSearchResult;
93  $this->catalogCategoryView = $catalogCategoryView;
94  $this->cmsIndex = $cmsIndex;
95  $this->category = $category;
96  $products = is_array($product) ? $product : [$product];
97  $errors = [];
98  foreach ($products as $product) {
99  $errors = array_merge($errors, $this->isNotDisplayingOnFrontendAssert($product));
100  }
101  \PHPUnit\Framework\Assert::assertEmpty(
102  $errors,
103  "In the process of checking product availability on the frontend, found the following errors:\n"
104  . implode("\n", $errors)
105  );
106  }
107 
114  protected function isNotDisplayingOnFrontendAssert(FixtureInterface $product)
115  {
116  $errors = [];
117  // Check the product page is not available
118  // TODO fix initialization url for frontend page
119  $this->browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
120  $titleBlock = $this->catalogProductView->getTitleBlock();
121 
122  if ($titleBlock->getTitle() !== self::NOT_FOUND_MESSAGE) {
123  $errors[] = '- the headline on the page does not match, the text should be -> "'
124  . self::NOT_FOUND_MESSAGE . '".';
125  }
126 
127  $this->cmsIndex->open();
128  $this->cmsIndex->getSearchBlock()->search($product->getSku());
129  if ($this->catalogSearchResult->getListProductBlock()->getProductItem($product)->isVisible()) {
130  $errors[] = '- successful product search.';
131  }
132 
133  $categoryName = ($product->hasData('category_ids'))
134  ? $product->getCategoryIds()[0]
135  : $this->category->getName();
136  $this->cmsIndex->open();
137  $this->cmsIndex->getTopmenu()->selectCategoryByName($categoryName);
138  $isProductVisible = $this->catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisible();
139  while (!$isProductVisible && $this->catalogCategoryView->getBottomToolbar()->nextPage()) {
140  $isProductVisible = $this->catalogCategoryView->getListProductBlock()
141  ->getProductItem($product)->isVisible();
142  }
143 
144  if ($isProductVisible) {
145  $errors[] = "- product with name '{$product->getName()}' is found in this category.";
146  }
147 
148  return $errors;
149  }
150 
156  public function toString()
157  {
158  return 'Assertion that the product is not available on the pages of the frontend.';
159  }
160 }
processAssert(CatalogProductView $catalogProductView, CatalogsearchResult $catalogSearchResult, CatalogCategoryView $catalogCategoryView, CmsIndex $cmsIndex, $product, BrowserInterface $browser, Category $category=null)
$errors
Definition: overview.phtml:9