Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductVisibleInCategory.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11 use Magento\Cms\Test\Page\CmsIndex;
12 use Magento\Mtf\Constraint\AbstractConstraint;
13 use Magento\Mtf\Fixture\FixtureInterface;
14 
18 class AssertProductVisibleInCategory extends AbstractConstraint
19 {
25  protected $errorMessage = 'Product is absent on category page.';
26 
32  protected $successfulMessage = 'Product is visible in the assigned category.';
33 
45  public function processAssert(
46  CatalogCategoryView $catalogCategoryView,
47  CmsIndex $cmsIndex,
48  FixtureInterface $product,
49  Category $category = null
50  ) {
51  $categoryName = $product->hasData('category_ids') ? $product->getCategoryIds()[0] : $category->getName();
52  $cmsIndex->open();
53  $cmsIndex->getTopmenu()->selectCategoryByName($categoryName);
54 
55  $isProductVisible = $catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisible();
56  while (!$isProductVisible && $catalogCategoryView->getBottomToolbar()->nextPage()) {
57  $isProductVisible = $catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisible();
58  }
59 
60  if (($product->getVisibility() === 'Search') || ($this->getStockStatus($product) === 'Out of Stock')) {
61  $isProductVisible = !$isProductVisible;
62  $this->errorMessage = 'Product found in this category.';
63  $this->successfulMessage = 'Asserts that the product could not be found in this category.';
64  }
65 
66  \PHPUnit\Framework\Assert::assertTrue(
67  $isProductVisible,
68  $this->errorMessage
69  );
70  }
71 
78  protected function getStockStatus(FixtureInterface $product)
79  {
80  $quantityAndStockStatus = $product->getQuantityAndStockStatus();
81  return isset($quantityAndStockStatus['is_in_stock']) ? $quantityAndStockStatus['is_in_stock'] : null;
82  }
83 
89  public function toString()
90  {
92  }
93 }
processAssert(CatalogCategoryView $catalogCategoryView, CmsIndex $cmsIndex, FixtureInterface $product, Category $category=null)