You are here

protected function AddToCartMultilingualTest::setUp in Commerce Core 8.2

Overrides CartWebDriverTestBase::setUp

See also

\Drupal\Tests\content_translation\Functional\ContentTranslationTestBase

File

modules/cart/tests/src/FunctionalJavascript/AddToCartMultilingualTest.php, line 60

Class

AddToCartMultilingualTest
Tests the add to cart form for multilingual.

Namespace

Drupal\Tests\commerce_cart\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->setupMultilingual();

  /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
  $variation_type = ProductVariationType::load($this->variation
    ->bundle());
  $color_attributes = $this
    ->createAttributeSet($variation_type, 'color', [
    'red' => 'Red',
    'blue' => 'Blue',
  ]);
  foreach ($color_attributes as $key => $color_attribute) {
    $color_attribute
      ->addTranslation('fr', [
      'name' => 'FR ' . $color_attribute
        ->label(),
    ]);
    $color_attribute
      ->save();
  }
  $size_attributes = $this
    ->createAttributeSet($variation_type, 'size', [
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large',
  ]);
  foreach ($size_attributes as $key => $size_attribute) {
    $size_attribute
      ->addTranslation('fr', [
      'name' => 'FR ' . $size_attribute
        ->label(),
    ]);
    $size_attribute
      ->save();
  }

  // Reload the variation since we have new fields.
  $this->variation = ProductVariation::load($this->variation
    ->id());

  // Translate the product's title.
  $product = $this->variation
    ->getProduct();
  $product
    ->setTitle('My Super Product');
  $product
    ->addTranslation('fr', [
    'title' => 'Mon super produit',
  ]);
  $product
    ->save();

  // Update first variation to have the attribute's value.
  $this->variation
    ->get('attribute_color')
    ->setValue($color_attributes['red']);
  $this->variation
    ->get('attribute_size')
    ->setValue($size_attributes['small']);
  $this->variation
    ->save();

  // The matrix is intentionally uneven, blue / large is missing.
  $attribute_values_matrix = [
    [
      'red',
      'small',
    ],
    [
      'red',
      'medium',
    ],
    [
      'red',
      'large',
    ],
    [
      'blue',
      'small',
    ],
    [
      'blue',
      'medium',
    ],
  ];

  // Generate variations off of the attributes values matrix.
  foreach ($attribute_values_matrix as $key => $value) {

    /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
    $variation = $this
      ->createEntity('commerce_product_variation', [
      'type' => $variation_type
        ->id(),
      'sku' => $this
        ->randomMachineName(),
      'price' => [
        'number' => 999,
        'currency_code' => 'USD',
      ],
    ]);
    $variation
      ->get('attribute_color')
      ->setValue($color_attributes[$value[0]]);
    $variation
      ->get('attribute_size')
      ->setValue($size_attributes[$value[1]]);
    $variation
      ->save();
    $product
      ->addVariation($variation);
  }
  $product
    ->save();
  $this->product = Product::load($product
    ->id());

  // Create a translation for each variation on the product.
  foreach ($this->product
    ->getVariations() as $variation) {
    $variation
      ->addTranslation('fr')
      ->save();
  }
  $this->variations = $product
    ->getVariations();
  $this->colorAttributes = $color_attributes;
  $this->sizeAttributes = $size_attributes;
}