Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddToCartCrossSellTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Checkout\Test\Page\CheckoutCart;
10 use Magento\Cms\Test\Page\CmsIndex;
11 use Magento\Mtf\Fixture\InjectableFixture;
12 
26 {
27  /* tags */
28  const TEST_TYPE = 'acceptance_test, extended_acceptance_test';
29  const MVP = 'yes';
30  /* end tags */
31 
37  protected $cmsIndex;
38 
44  protected $checkoutCart;
45 
57  public function test(
58  $products,
59  $promotedProducts,
60  $navigateProductsOrder,
61  $productsToVerify,
62  CmsIndex $cmsIndex,
64  ) {
65  // Preconditions
66  $this->createProducts($products);
67  $this->assignPromotedProducts($promotedProducts, 'cross_sell_products');
68 
69  // Initialization
70  $this->cmsIndex = $cmsIndex;
71  $this->checkoutCart = $checkoutCart;
72  $navigateProductsOrder = $this->parseNavigateProductsOrder($navigateProductsOrder);
73  $productsToVerify = $this->parseProductsToVerify($productsToVerify);
74  $initialProductName = array_shift($navigateProductsOrder);
75  $initialProduct = $this->products[$initialProductName];
76  $initialProductToVerify = $productsToVerify[$initialProductName];
77 
78  // Steps
79  $this->checkoutCart->open();
80  $this->checkoutCart->getCartBlock()->clearShoppingCart();
81 
82  $this->browser->open($_ENV['app_frontend_url'] . $initialProduct->getUrlKey() . '.html');
83  $this->catalogProductView->getViewBlock()->addToCart($initialProduct);
84  $this->catalogProductView->getMessagesBlock()->waitSuccessMessage();
85  $this->assertCrossSellSection($initialProductToVerify);
86  foreach ($navigateProductsOrder as $productName) {
87  $this->addToCart($this->products[$productName]);
88 
89  if (empty($productsToVerify[$productName])) {
91  } else {
92  $this->assertCrossSellSection($productsToVerify[$productName]);
93  }
94  }
95  }
96 
103  protected function addToCart(InjectableFixture $product)
104  {
105  $this->checkoutCart->getCrosssellBlock()->getProductItem($product)->clickAddToCart();
106  if ($this->cmsIndex->getTitleBlock()->getTitle() == $product->getName()) {
107  $this->catalogProductView->getViewBlock()->addToCart($product);
108  }
109 
110  $this->catalogProductView->getMessagesBlock()->waitSuccessMessage();
111  }
112 
118  protected function assertAbsentCrossSellSection()
119  {
120  $this->checkoutCart->open();
121  \PHPUnit\Framework\Assert::assertFalse(
122  $this->checkoutCart->getCrosssellBlock()->isVisible(),
123  "Cross-sell block is present."
124  );
125  }
126 
133  protected function assertCrossSellSection(array $promotedProductNames)
134  {
135  $productNames = [];
136  $pageProductNames = [];
137 
138  foreach ($promotedProductNames as $promotedProductName) {
139  $productNames[] = $this->products[$promotedProductName]->getName();
140  }
141  $this->checkoutCart->open();
142  foreach ($this->checkoutCart->getCrosssellBlock()->getProducts() as $productItem) {
143  $pageProductNames[] = $productItem->getProductName();
144  }
145 
146  sort($productNames);
147  sort($pageProductNames);
148  \PHPUnit\Framework\Assert::assertEquals(
149  $productNames,
150  $pageProductNames,
151  'Wrong products are displayed in cross-sell section.'
152  );
153  }
154 }
test( $products, $promotedProducts, $navigateProductsOrder, $productsToVerify, CmsIndex $cmsIndex, CheckoutCart $checkoutCart)