You are here

public function ProductAttributeFieldManagerTest::testManager in Commerce Core 8.2

@covers ::getFieldDefinitions @covers ::getFieldMap @covers ::clearCaches @covers ::createField @covers ::canDeleteField @covers ::deleteField

File

modules/product/tests/src/Kernel/ProductAttributeFieldManagerTest.php, line 68

Class

ProductAttributeFieldManagerTest
Tests the attribute field manager.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testManager() {
  $color_attribute = ProductAttribute::create([
    'id' => 'color',
    'label' => 'Color',
  ]);
  $color_attribute
    ->save();
  $size_attribute = ProductAttribute::create([
    'id' => 'size',
    'label' => 'Size',
  ]);
  $size_attribute
    ->save();
  $this
    ->assertEquals([], $this->attributeFieldManager
    ->getFieldMap('shirt'));
  $this->attributeFieldManager
    ->createField($color_attribute, 'shirt');
  $this->attributeFieldManager
    ->createField($size_attribute, 'shirt');
  $field_map = $this->attributeFieldManager
    ->getFieldMap('shirt');
  $expected_field_map = [
    [
      'attribute_id' => 'color',
      'field_name' => 'attribute_color',
    ],
    [
      'attribute_id' => 'size',
      'field_name' => 'attribute_size',
    ],
  ];
  $this
    ->assertEquals($expected_field_map, $field_map);
  $this->attributeFieldManager
    ->createField($color_attribute, 'mug');
  $this->attributeFieldManager
    ->createField($size_attribute, 'mug');
  $this->attributeFieldManager
    ->deleteField($size_attribute, 'mug');
  $field_map = $this->attributeFieldManager
    ->getFieldMap('mug');
  $expected_field_map = [
    [
      'attribute_id' => 'color',
      'field_name' => 'attribute_color',
    ],
  ];
  $this
    ->assertEquals($expected_field_map, $field_map);
  $field_map = $this->attributeFieldManager
    ->getFieldMap();
  $expected_field_map = [
    'shirt' => [
      [
        'attribute_id' => 'color',
        'field_name' => 'attribute_color',
      ],
      [
        'attribute_id' => 'size',
        'field_name' => 'attribute_size',
      ],
    ],
    'mug' => [
      [
        'attribute_id' => 'color',
        'field_name' => 'attribute_color',
      ],
    ],
  ];
  $this
    ->assertEquals($expected_field_map, $field_map);
}