You are here

public function ProductVariationAttributeMapperTest::testPrepareAttributesOptional in Commerce Core 8.2

Tests preparing attributes when there are optional attributes.

@covers ::prepareAttributes

File

modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php, line 266

Class

ProductVariationAttributeMapperTest
Tests the product variation attribute mapper.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testPrepareAttributesOptional() {
  $product = $this
    ->generateThreeByTwoOptionalScenario();
  $variations = $product
    ->getVariations();

  // Test from the initial variation.
  $attributes = $this->mapper
    ->prepareAttributes(reset($variations), $variations);
  $ram_attribute = $attributes['attribute_ram'];
  $this
    ->assertEquals('ram', $ram_attribute
    ->getId());
  $this
    ->assertEquals('Ram', $ram_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $ram_attribute
    ->getElementType());
  $this
    ->assertTrue($ram_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '11' => '8GB',
    '12' => '16GB',
  ], $ram_attribute
    ->getValues());
  $disk1_attribute = $attributes['attribute_disk1'];
  $this
    ->assertEquals('disk1', $disk1_attribute
    ->getId());
  $this
    ->assertEquals('Disk1', $disk1_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $disk1_attribute
    ->getElementType());
  $this
    ->assertTrue($disk1_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '14' => '1TB',
  ], $disk1_attribute
    ->getValues());

  // The Disk 2 1TB option should not show. Only "none".
  // The default variation is 8GB x 1TB, which does not have the Disk 2 value
  // so it should only return "_none". The Disk 2 option should have only have
  // this option is the 16GB RAM option is chosen.
  $disk2_attribute = $attributes['attribute_disk2'];
  $this
    ->assertEquals('disk2', $disk2_attribute
    ->getId());
  $this
    ->assertEquals('Disk2', $disk2_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $disk2_attribute
    ->getElementType());
  $this
    ->assertFalse($disk2_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '_none' => '',
  ], $disk2_attribute
    ->getValues());

  // Test from the 16GB x 1TB x None variation.
  $attributes = $this->mapper
    ->prepareAttributes($variations[1], $variations);
  $ram_attribute = $attributes['attribute_ram'];
  $this
    ->assertEquals('ram', $ram_attribute
    ->getId());
  $this
    ->assertEquals('Ram', $ram_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $ram_attribute
    ->getElementType());
  $this
    ->assertTrue($ram_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '11' => '8GB',
    '12' => '16GB',
  ], $ram_attribute
    ->getValues());
  $disk1_attribute = $attributes['attribute_disk1'];
  $this
    ->assertEquals('disk1', $disk1_attribute
    ->getId());
  $this
    ->assertEquals('Disk1', $disk1_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $disk1_attribute
    ->getElementType());
  $this
    ->assertTrue($disk1_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '14' => '1TB',
  ], $disk1_attribute
    ->getValues());
  $disk2_attribute = $attributes['attribute_disk2'];
  $this
    ->assertEquals('disk2', $disk2_attribute
    ->getId());
  $this
    ->assertEquals('Disk2', $disk2_attribute
    ->getLabel());
  $this
    ->assertEquals('select', $disk2_attribute
    ->getElementType());
  $this
    ->assertFalse($disk2_attribute
    ->isRequired());
  $this
    ->assertEquals([
    '_none' => '',
    '17' => '1TB',
  ], $disk2_attribute
    ->getValues());
}