Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NavigateRelatedProductsTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Checkout\Test\Page\CheckoutCart;
10 use Magento\Mtf\Fixture\InjectableFixture;
11 
26 {
27  /* tags */
28  const TEST_TYPE = 'acceptance_test, extended_acceptance_test';
29  const MVP = 'yes';
30  /* end tags */
31 
37  protected $productsToVerify;
38 
44  protected $selectable = [];
45 
51  protected $checkoutCart;
52 
64  public function test(
65  $products,
67  $promotedProducts,
68  $navigateProductsOrder,
71  ) {
72  // Preconditions
73  $this->createProducts($products);
74  $this->assignPromotedProducts($promotedProducts, 'related_products');
76 
77  // Initialization
78  $this->checkoutCart = $checkoutCart;
79  $this->productsToVerify = $this->parseProductsToVerify($productsToVerify);
80  $navigateProductsOrder = $this->parseNavigateProductsOrder($navigateProductsOrder);
81  $initialProductName = array_shift($navigateProductsOrder);
82  $initialProduct = $this->products[$initialProductName];
83  $lastProductName = end($navigateProductsOrder);
84  $lastProduct = $this->products[$lastProductName];
85 
86  // Steps
87  // Clear shopping cart
88  $this->checkoutCart->open();
89  $this->checkoutCart->getCartBlock()->clearShoppingCart();
90 
91  // Navigate through related products
92  $this->browser->open($_ENV['app_frontend_url'] . $initialProduct->getUrlKey() . '.html');
93  $this->assertRelatedSection($initialProductName);
94  foreach ($navigateProductsOrder as $productShortName) {
95  $this->navigate($productShortName);
96  }
97 
98  // Add last product with related product to cart and verify
99  $checkoutProducts = $this->selectRelatedProducts($lastProductName);
100  $checkoutProducts[] = $lastProduct;
101  $this->catalogProductView->getViewBlock()->addToCart($lastProduct);
102  $this->catalogProductView->getMessagesBlock()->waitSuccessMessage();
103  $this->assertCheckoutCart($checkoutProducts);
104  }
105 
112  protected function parseSelectable($selectable)
113  {
114  $list = array_map('trim', explode(',', $selectable));
115 
116  foreach ($list as $item) {
117  list($productName, $isSelectable) = array_map('trim', explode(':', $item));
118  $this->selectable[$productName] = $isSelectable;
119  }
120  }
121 
128  protected function getProductsToVerify($product)
129  {
130  $shortNames = $this->productsToVerify[$product];
131  $products = [];
132 
133  foreach ($shortNames as $shortName) {
134  $products[$shortName] = $this->products[$shortName];
135  }
136 
137  return $products;
138  }
139 
146  protected function navigate($productShortName)
147  {
148  $product = $this->products[$productShortName];
149  $this->catalogProductView->getRelatedProductBlock()->getProductItem($product)->open();
150 
151  if (empty($this->productsToVerify[$productShortName])) {
153  } else {
154  $this->assertRelatedSection($productShortName);
155  }
156  }
157 
164  protected function selectRelatedProducts($product)
165  {
166  $selected = [];
167 
168  foreach ($this->productsToVerify[$product] as $productShortName) {
169  $productToVerify = $this->products[$productShortName];
170  $isSelect = $this->selectable[$productShortName];
171 
172  if ('yes' == $isSelect) {
173  $this->catalogProductView->getRelatedProductBlock()->getProductItem($productToVerify)->select();
174  $selected[] = $productToVerify;
175  }
176  }
177 
178  return $selected;
179  }
180 
186  protected function assertAbsentRelatedSellSection()
187  {
188  \PHPUnit\Framework\Assert::assertFalse(
189  $this->catalogProductView->getRelatedProductBlock()->isVisible(),
190  "Related section is present."
191  );
192  }
193 
200  protected function assertRelatedSection($product)
201  {
203  $fixtureData = [];
204  $pageData = [];
205 
206  foreach ($productsToVerify as $shortName => $product) {
207  $productName = $product->getName();
208  $fixtureData[$productName] = $this->selectable[$shortName];
209  }
210  foreach ($this->catalogProductView->getRelatedProductBlock()->getProducts() as $productItem) {
211  $pageProductName = $productItem->getProductName();
212  $pageData[$pageProductName] = $productItem->isSelectable() ? 'yes' : 'no';
213  }
214 
215  asort($fixtureData);
216  asort($pageData);
217  \PHPUnit\Framework\Assert::assertEquals(
218  $pageData,
219  $fixtureData,
220  'Wrong products are displayed in related section.'
221  );
222  }
223 
230  protected function assertCheckoutCart(array $checkoutProducts)
231  {
232  $this->checkoutCart->open();
233 
234  foreach ($checkoutProducts as $product) {
235  \PHPUnit\Framework\Assert::assertTrue(
236  $this->checkoutCart->getCartBlock()->getCartItem($product)->isVisible(),
237  "Product {$product->getName()} absent in cart."
238  );
239  }
240  }
241 }