You are here

protected function AddToCartFieldReplacementTest::setUp in Commerce Core 8.2

Overrides CartWebDriverTestBase::setUp

File

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

Class

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

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();

  // 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();

  // Create an optional field that will have a value only on 1 variation.
  FieldStorageConfig::create([
    'field_name' => 'field_number',
    'entity_type' => 'commerce_product_variation',
    'type' => 'integer',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_number',
    'entity_type' => 'commerce_product_variation',
    'bundle' => 'default',
    'settings' => [],
  ])
    ->save();

  // Set up the Full view modes.
  EntityViewMode::create([
    'id' => 'commerce_product.full',
    'label' => 'Full',
    'targetEntityType' => 'commerce_product',
  ])
    ->save();
  EntityViewMode::create([
    'id' => 'commerce_product_variation.full',
    'label' => 'Full',
    'targetEntityType' => 'commerce_product_variation',
  ])
    ->save();

  // Use a different price widget for the two displays, to use that as
  // an indicator of the right view mode being used.
  $default_view_display = EntityViewDisplay::load('commerce_product_variation.default.default');
  if (!$default_view_display) {
    $default_view_display = EntityViewDisplay::create([
      'targetEntityType' => 'commerce_product_variation',
      'bundle' => 'default',
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $default_view_display
    ->setComponent('price', [
    'type' => 'commerce_price_default',
  ]);
  $default_view_display
    ->save();
  $full_view_display = EntityViewDisplay::load('commerce_product_variation.default.full');
  if (!$full_view_display) {
    $full_view_display = EntityViewDisplay::create([
      'targetEntityType' => 'commerce_product_variation',
      'bundle' => 'default',
      'mode' => 'full',
      'status' => TRUE,
    ]);
  }
  $full_view_display
    ->setComponent('field_number', [
    'type' => 'number_integer',
  ]);
  $full_view_display
    ->setComponent('price', [
    'type' => 'commerce_price_plain',
  ]);
  $full_view_display
    ->save();
  $this->firstVariation = $this
    ->createEntity('commerce_product_variation', [
    'title' => 'First variation',
    'type' => 'default',
    'sku' => 'first-variation',
    'price' => [
      'number' => 10,
      'currency_code' => 'USD',
    ],
    'field_number' => 202,
  ]);
  $this->secondVariation = $this
    ->createEntity('commerce_product_variation', [
    'title' => 'Second variation',
    'type' => 'default',
    'sku' => 'second-variation',
    'price' => [
      'number' => 20,
      'currency_code' => 'USD',
    ],
  ]);
  $this->product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Test product',
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->firstVariation,
      $this->secondVariation,
    ],
  ]);
}