You are here

public function MultipleCartMultipleVariationTypesTest::testAddToCart in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/cart/tests/src/Functional/MultipleCartMultipleVariationTypesTest.php \Drupal\Tests\commerce_cart\Functional\MultipleCartMultipleVariationTypesTest::testAddToCart()
  2. 8.2 modules/cart/tests/src/FunctionalJavascript/MultipleCartMultipleVariationTypesTest.php \Drupal\Tests\commerce_cart\FunctionalJavascript\MultipleCartMultipleVariationTypesTest::testAddToCart()

Tests that add to cart does not throw an exception.

File

modules/cart/tests/src/Functional/MultipleCartMultipleVariationTypesTest.php, line 141

Class

MultipleCartMultipleVariationTypesTest
Tests multiple cart page with different variation types.

Namespace

Drupal\Tests\commerce_cart\Functional

Code

public function testAddToCart() {
  $this
    ->drupalGet('/test-multiple-cart-forms');

  /** @var \Behat\Mink\Element\NodeElement[] $forms */
  $forms = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.commerce-order-item-add-to-cart-form');

  // Due to sorting by title, we know `My Colors FIRST` is the second form.
  $forms[1]
    ->selectFieldOption('Color', 'Blue');
  $forms[1]
    ->pressButton('Add to cart');
  $this
    ->assertSession()
    ->pageTextContains('My Colors FIRST - Blue added to your cart.');
  $this->cart = Order::load($this->cart
    ->id());
  $order_items = $this->cart
    ->getItems();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $order_items[0]
    ->getPurchasedEntity();
  $this
    ->assertEquals($this->colorAttributes['blue']
    ->id(), $variation
    ->getAttributeValueId('attribute_color'));

  /** @var \Behat\Mink\Element\NodeElement[] $forms */
  $forms = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.commerce-order-item-add-to-cart-form');
  $forms[0]
    ->selectFieldOption('Color', 'Green');
  $forms[0]
    ->selectFieldOption('Size', 'Medium');
  $forms[0]
    ->pressButton('Add to cart');
  $this
    ->assertSession()
    ->pageTextContains('My Colors and Sizes FIRST - Green, Medium added to your cart.');
  $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_order')
    ->resetCache([
    $this->cart
      ->id(),
  ]);
  $this->cart = Order::load($this->cart
    ->id());
  $order_items = $this->cart
    ->getItems();

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = $order_items[1]
    ->getPurchasedEntity();
  $this
    ->assertEquals($this->colorAttributes['green']
    ->id(), $variation
    ->getAttributeValueId('attribute_color'));
  $this
    ->assertEquals($this->sizeAttributes['medium']
    ->id(), $variation
    ->getAttributeValueId('attribute_size'));
}