You are here

public function AddToCartFieldReplacementTest::testFieldReplacement in Commerce Core 8.2

Tests the field replacement.

Expectations: 1) The initial view mode is preserved on AJAX refresh. 2) Optional fields are correctly replaced even if the field is empty.

File

modules/cart/tests/src/FunctionalJavascript/AddToCartFieldReplacementTest.php, line 152

Class

AddToCartFieldReplacementTest
Tests AJAX field replacement on the add to cart form.

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

public function testFieldReplacement() {
  $this
    ->drupalGet($this->product
    ->toUrl());
  $page = $this
    ->getSession()
    ->getPage();
  $renderer = $this->container
    ->get('renderer');
  $first_variation_price = [
    '#theme' => 'commerce_price_plain',
    '#number' => $this->firstVariation
      ->getPrice()
      ->getNumber(),
    '#currency' => Currency::load($this->firstVariation
      ->getPrice()
      ->getCurrencyCode()),
  ];
  $first_variation_price = trim($renderer
    ->renderPlain($first_variation_price));
  $second_variation_price = [
    '#theme' => 'commerce_price_plain',
    '#number' => $this->secondVariation
      ->getPrice()
      ->getNumber(),
    '#currency' => Currency::load($this->secondVariation
      ->getPrice()
      ->getCurrencyCode()),
  ];
  $second_variation_price = trim($renderer
    ->renderPlain($second_variation_price));
  $price_field_selector = '.product--variation-field--variation_price__' . $this->product
    ->id();
  $integer_field_selector = '.product--variation-field--variation_field_number__' . $this->product
    ->id();
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementExists('css', $integer_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', $first_variation_price);
  $this
    ->assertSession()
    ->elementTextContains('css', $integer_field_selector . ' .field__item', $this->firstVariation
    ->get('field_number')->value);
  $this
    ->assertSession()
    ->fieldValueEquals('purchased_entity[0][variation]', $this->firstVariation
    ->id());
  $page
    ->selectFieldOption('purchased_entity[0][variation]', $this->secondVariation
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementExists('css', $integer_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', $second_variation_price);
  $this
    ->assertSession()
    ->elementNotExists('css', $integer_field_selector . ' .field__item');
  $page
    ->selectFieldOption('purchased_entity[0][variation]', $this->firstVariation
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementExists('css', $integer_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', $first_variation_price);
  $this
    ->assertSession()
    ->elementTextContains('css', $integer_field_selector . ' .field__item', $this->firstVariation
    ->get('field_number')->value);
}