You are here

protected function MultipleCartMultipleVariationTypesTest::setUp in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/cart/tests/src/Functional/MultipleCartMultipleVariationTypesTest.php \Drupal\Tests\commerce_cart\Functional\MultipleCartMultipleVariationTypesTest::setUp()
  2. 8.2 modules/cart/tests/src/FunctionalJavascript/MultipleCartMultipleVariationTypesTest.php \Drupal\Tests\commerce_cart\FunctionalJavascript\MultipleCartMultipleVariationTypesTest::setUp()

Overrides CartBrowserTestBase::setUp

File

modules/cart/tests/src/Functional/MultipleCartMultipleVariationTypesTest.php, line 43

Class

MultipleCartMultipleVariationTypesTest
Tests multiple cart page with different variation types.

Namespace

Drupal\Tests\commerce_cart\Functional

Code

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

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

  // Create three variation types.
  $this
    ->createProductAndVariationType('color_sizes', 'Colors and Sizes');
  $this
    ->createProductAndVariationType('colors', 'Colors');
  $this
    ->createProductAndVariationType('sizes', 'Sizes');

  // Create the attributes.
  $color_attribute = ProductAttribute::create([
    'id' => 'color',
    'label' => 'Color',
  ]);
  $color_attribute
    ->save();
  $this->attributeFieldManager
    ->createField($color_attribute, 'color_sizes');
  $this->attributeFieldManager
    ->createField($color_attribute, 'colors');
  $options = [
    'red' => 'Red',
    'green' => 'Green',
    'blue' => 'Blue',
  ];
  foreach ($options as $key => $value) {
    $this->colorAttributes[$key] = $this
      ->createAttributeValue($color_attribute
      ->id(), $value);
  }
  $size_attribute = ProductAttribute::create([
    'id' => 'size',
    'label' => 'Size',
  ]);
  $size_attribute
    ->save();
  $this->attributeFieldManager
    ->createField($size_attribute, 'color_sizes');
  $this->attributeFieldManager
    ->createField($size_attribute, 'sizes');
  $options = [
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large',
  ];
  foreach ($options as $key => $value) {
    $this->sizeAttributes[$key] = $this
      ->createAttributeValue($size_attribute
      ->id(), $value);
  }

  // Create products.
  $product_matrix = [
    'My Colors and Sizes FIRST' => [
      'type' => 'color_sizes',
      'variations' => [
        [
          'attribute_color' => $this->colorAttributes['red']
            ->id(),
          'attribute_size' => $this->sizeAttributes['small']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['red']
            ->id(),
          'attribute_size' => $this->sizeAttributes['medium']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['red']
            ->id(),
          'attribute_size' => $this->sizeAttributes['large']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['green']
            ->id(),
          'attribute_size' => $this->sizeAttributes['small']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['green']
            ->id(),
          'attribute_size' => $this->sizeAttributes['medium']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['green']
            ->id(),
          'attribute_size' => $this->sizeAttributes['large']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['blue']
            ->id(),
          'attribute_size' => $this->sizeAttributes['small']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['blue']
            ->id(),
          'attribute_size' => $this->sizeAttributes['medium']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['blue']
            ->id(),
          'attribute_size' => $this->sizeAttributes['large']
            ->id(),
        ],
      ],
    ],
    'My Colors FIRST' => [
      'type' => 'colors',
      'variations' => [
        [
          'attribute_color' => $this->colorAttributes['red']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['green']
            ->id(),
        ],
        [
          'attribute_color' => $this->colorAttributes['blue']
            ->id(),
        ],
      ],
    ],
    'My Sizes FIRST' => [
      'type' => 'sizes',
      'variations' => [
        [
          'attribute_size' => $this->sizeAttributes['small']
            ->id(),
        ],
        [
          'attribute_size' => $this->sizeAttributes['medium']
            ->id(),
        ],
        [
          'attribute_size' => $this->sizeAttributes['large']
            ->id(),
        ],
      ],
    ],
  ];
  foreach ($product_matrix as $product_title => $product_data) {

    /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
    $product = $this
      ->createEntity('commerce_product', [
      'type' => $product_data['type'],
      'title' => $product_title,
      'stores' => [
        $this->store,
      ],
    ]);
    foreach ($product_data['variations'] as $variation_data) {
      $variation_data += [
        'type' => $product_data['type'],
        'sku' => 'sku-' . $this
          ->randomMachineName(),
        'price' => [
          'number' => '10',
          'currency_code' => 'USD',
        ],
      ];

      /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
      $variation = $this
        ->createEntity('commerce_product_variation', $variation_data);
      $product
        ->addVariation($variation);
    }
    $product
      ->save();
  }
}