You are here

protected function MultipleCartFormsTest::setUp in Commerce Core 8.2

Overrides CartWebDriverTestBase::setUp

File

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

Class

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

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();
  $this->maximumMetaRefreshCount = 0;

  // Delete parent test product.
  $this->variation
    ->getProduct()
    ->setUnpublished();
  $this->variation
    ->getProduct()
    ->save();

  /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $order_item_form_display */
  $order_item_form_display = EntityFormDisplay::load('commerce_order_item.default.add_to_cart');
  $order_item_form_display
    ->setComponent('quantity', [
    'type' => 'commerce_quantity',
  ]);
  $order_item_form_display
    ->save();
  $variation_type = ProductVariationType::load('default');
  $color_attributes = $this
    ->createAttributeSet($variation_type, 'color', [
    'red' => 'Red',
    'blue' => 'Blue',
  ]);
  $this->colorAttributes = $color_attributes;
  $size_attributes = $this
    ->createAttributeSet($variation_type, 'size', [
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large',
  ]);
  $this->sizeAttributes = $size_attributes;
  $attribute_values_matrix = [
    [
      'red',
      'small',
    ],
    [
      'red',
      'medium',
    ],
    [
      'red',
      'large',
    ],
    [
      'blue',
      'small',
    ],
    [
      'blue',
      'medium',
    ],
    [
      'blue',
      'large',
    ],
  ];
  for ($i = 1; $i < 5; $i++) {

    // Create a product variation.
    $variations = [];

    // Generate variations off of the attributes values matrix.
    foreach ($attribute_values_matrix as $key => $value) {
      $variation = $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'sku' => $this
          ->randomMachineName(),
        'price' => [
          'number' => (string) 3 * $i,
          'currency_code' => 'USD',
        ],
        'attribute_color' => $color_attributes[$value[0]],
        'attribute_size' => $size_attributes[$value[1]],
      ]);
      $variations[] = $variation;
    }
    $this->products[] = $this
      ->createEntity('commerce_product', [
      'type' => 'default',
      'title' => $this
        ->randomMachineName(),
      'stores' => [
        $this->store,
      ],
      'variations' => $variations,
    ]);
  }
}