You are here

public function AddToCartFormTest::testOptionalProductAttribute in Commerce Core 8.2

Tests the behavior of optional product attributes.

File

modules/cart/tests/src/Functional/AddToCartFormTest.php, line 215

Class

AddToCartFormTest
Tests the add to cart form.

Namespace

Drupal\Tests\commerce_cart\Functional

Code

public function testOptionalProductAttribute() {

  /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
  $variation_type = ProductVariationType::load($this->variation
    ->bundle());
  $size_attributes = $this
    ->createAttributeSet($variation_type, 'size', [
    'small' => 'Small',
    'medium' => 'Medium',
    'large' => 'Large',
  ]);
  $color_attributes = $this
    ->createAttributeSet($variation_type, 'color', [
    'red' => 'Red',
  ]);

  // Make the color attribute optional.
  $color_field = FieldConfig::loadByName('commerce_product_variation', 'default', 'attribute_color');
  $color_field
    ->setRequired(TRUE);
  $color_field
    ->save();

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

  // Update the first variation to have the attribute values.
  $this->variation->attribute_size = $size_attributes['small']
    ->id();
  $this->variation->attribute_color = $color_attributes['red']
    ->id();
  $this->variation
    ->save();
  $attribute_values_matrix = [
    [
      'medium',
      'red',
    ],
    [
      'large',
      'red',
    ],
  ];
  $variations = [
    $this->variation,
  ];

  // Generate variations off of the attributes values matrix.
  foreach ($attribute_values_matrix as $key => $value) {
    $variation = $this
      ->createEntity('commerce_product_variation', [
      'type' => $variation_type
        ->id(),
      'sku' => $this
        ->randomMachineName(),
      'price' => [
        'number' => 999,
        'currency_code' => 'USD',
      ],
      'attribute_size' => $size_attributes[$value[0]]
        ->id(),
      'attribute_color' => $color_attributes[$value[1]]
        ->id(),
    ]);
    $variations[] = $variation;
    $product->variations
      ->appendItem($variation);
  }
  $product
    ->save();

  // The color element should be required because each variation has a color.
  $this
    ->drupalGet($product
    ->toUrl());
  $this
    ->assertSession()
    ->fieldExists('purchased_entity[0][attributes][attribute_size]');
  $this
    ->assertSession()
    ->elementExists('xpath', '//select[@id="edit-purchased-entity-0-attributes-attribute-color" and @required]');

  // Remove the color value from all variations.
  // The color element should now be hidden.
  foreach ($variations as $variation) {
    $variation->attribute_color = NULL;
    $this->variation
      ->save();
  }
  $this
    ->drupalGet($product
    ->toUrl());
  $this
    ->assertSession()
    ->fieldExists('purchased_entity[0][attributes][attribute_size]');
  $this
    ->assertSession()
    ->fieldNotExists('purchased_entity[0][attributes][attribute_color]');
}