You are here

public function ProductAttributeTest::testManualAttributeField in Commerce Core 8.2

Tests a manually created attribute field.

The attribute and variation type forms should handle the existence of manually-created attribute fields without crashing.

File

modules/product/tests/src/Functional/ProductAttributeTest.php, line 130

Class

ProductAttributeTest
Create, edit, delete, and change product attributes.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testManualAttributeField() {

  /** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */
  $attribute = $this
    ->createEntity('commerce_product_attribute', [
    'id' => 'color',
    'label' => 'Color',
  ]);
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_my_attribute',
    'entity_type' => 'commerce_product_variation',
    'type' => 'entity_reference',
    'cardinality' => 1,
    'settings' => [
      'target_type' => 'commerce_product_attribute_value',
    ],
    'translatable' => FALSE,
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'default',
    'label' => 'Custom Color',
    'required' => TRUE,
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [
        'target_bundles' => [
          'color',
        ],
      ],
    ],
    'translatable' => FALSE,
  ]);
  $field
    ->save();
  $this
    ->drupalGet($attribute
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $page = $this
    ->getSession()
    ->getPage();
  $variation_type_checkbox = $page
    ->findField('Default');
  $this
    ->assertTrue($variation_type_checkbox
    ->isChecked());
  $this
    ->assertTrue($variation_type_checkbox
    ->hasAttribute('disabled'));
  $this
    ->drupalGet('admin/commerce/config/product-variation-types/default/edit');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $page = $this
    ->getSession()
    ->getPage();
  $attribute_checkbox = $page
    ->findField('Color');
  $this
    ->assertTrue($attribute_checkbox
    ->isChecked());
  $this
    ->assertTrue($attribute_checkbox
    ->hasAttribute('disabled'));
}