You are here

public function ProductLayoutBuilderIntegrationTest::testFieldInjectionOverAjax in Commerce Core 8.2

Test field injection on a Layout Builder enabled product.

@group debug

File

modules/product/tests/src/FunctionalJavascript/ProductLayoutBuilderIntegrationTest.php, line 180

Class

ProductLayoutBuilderIntegrationTest
@group commerce

Namespace

Drupal\Tests\commerce_product\FunctionalJavascript

Code

public function testFieldInjectionOverAjax() {
  $variation_type = ProductVariationType::load('default');
  $variation_type
    ->setGenerateTitle(FALSE);
  $variation_type
    ->save();

  // Use the title widget so that we do not need to use attributes.
  $order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.add_to_cart');
  $order_item_form_display
    ->setComponent('purchased_entity', [
    'type' => 'commerce_product_variation_title',
  ]);
  $order_item_form_display
    ->save();
  $first_variation = $this
    ->createEntity('commerce_product_variation', [
    'title' => 'First variation',
    'type' => 'default',
    'sku' => 'first-variation',
    'price' => [
      'number' => 10,
      'currency_code' => 'USD',
    ],
  ]);
  $second_variation = $this
    ->createEntity('commerce_product_variation', [
    'title' => 'Second variation',
    'type' => 'default',
    'sku' => 'second-variation',
    'price' => [
      'number' => 20,
      'currency_code' => 'USD',
    ],
  ]);
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => $this->stores,
    'body' => [
      'value' => 'Testing product variation field injection!',
    ],
    'variations' => [
      $first_variation,
      $second_variation,
    ],
  ]);
  $this
    ->enableLayoutsForBundle('default');
  $this
    ->configureDefaultLayout('default');
  $this
    ->drupalGet($product
    ->toUrl());
  $price_field_selector = '.block-field-blockcommerce-product-variationdefaultprice';
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', '$10');
  $this
    ->assertSession()
    ->fieldValueEquals('purchased_entity[0][variation]', $first_variation
    ->id());
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('purchased_entity[0][variation]', $second_variation
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', '$20');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('purchased_entity[0][variation]', $first_variation
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementExists('css', $price_field_selector);
  $this
    ->assertSession()
    ->elementTextContains('css', $price_field_selector . ' .field__item', '$10');
}