You are here

public function MultipleCartFormsTest::testUniqueAddToCartFormIds in Commerce Core 8.2

Tests that the form IDs are unique on load, and AJAX rebuild.

File

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

Class

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

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

public function testUniqueAddToCartFormIds() {
  $this
    ->drupalGet('/test-multiple-cart-forms');
  $seen_ids = [];

  /** @var \Behat\Mink\Element\NodeElement[] $forms */
  $forms = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.commerce-order-item-add-to-cart-form');
  $this
    ->assertCount(4, $forms);
  foreach ($forms as $form) {
    $form_id = $form
      ->find('xpath', '//input[@type="hidden" and @name="form_id"]')
      ->getValue();
    $this
      ->assertFalse(in_array($form_id, $seen_ids));
    $seen_ids[] = $form_id;
  }
  $forms[1]
    ->selectFieldOption('Size', 'Large');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  /** @var \Behat\Mink\Element\NodeElement[] $forms */
  $forms = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.commerce-order-item-add-to-cart-form');
  $this
    ->assertCount(4, $forms);
  $ajax_seen_ids = [];
  foreach ($forms as $form) {
    $form_id = $form
      ->find('xpath', '//input[@type="hidden" and @name="form_id"]')
      ->getValue();
    $this
      ->assertFalse(in_array($form_id, $ajax_seen_ids));
    $ajax_seen_ids[] = $form_id;
  }
  $this
    ->assertEquals($seen_ids, $ajax_seen_ids);
}