You are here

public function MultipleCartFormsTest::testMultipleRenderedProducts in Commerce Core 8.2

Tests that a page with multiple add to cart forms works properly.

File

modules/cart/tests/src/FunctionalJavascript/MultipleCartFormsTest.php, line 143

Class

MultipleCartFormsTest
Tests pages with multiple products rendered with add to cart forms.

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

public function testMultipleRenderedProducts() {

  // View of rendered products, each containing an add to cart form.
  $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');

  // Modify a single product's add to cart form.
  $current_form = $forms[2];
  $current_form
    ->fillField('quantity[0][value]', '3');
  $current_form
    ->selectFieldOption('Color', 'Blue');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $current_form
    ->selectFieldOption('Size', 'Medium');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $current_form
    ->selectFieldOption('Color', 'Red');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $current_form
    ->pressButton('Add to cart');
  $this->cart = Order::load($this->cart
    ->id());
  $order_items = $this->cart
    ->getItems();
  $this
    ->assertEquals(3, $order_items[0]
    ->getQuantity());

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

  // Modify one form, but submit another.
  $forms = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.commerce-order-item-add-to-cart-form');
  $current_form = $forms[0];
  $current_form
    ->fillField('quantity[0][value]', '2');
  $current_form
    ->selectFieldOption('Color', 'Red');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $current_form
    ->selectFieldOption('Size', 'Small');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $current_form
    ->selectFieldOption('Color', 'Blue');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $forms[1]
    ->selectFieldOption('Size', 'Large');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $forms[1]
    ->pressButton('Add to 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();
  $this
    ->assertEquals(1, $order_items[1]
    ->getQuantity());

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