You are here

protected function ProductAttributeTestTrait::createAttributeSet in Commerce Core 8.2

Creates an attribute field and set of attribute values.

Parameters

\Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type: The variation type.

string $name: The attribute field name.

array $options: Associative array of key name values. [red => Red].

bool $test_field: Flag to create a test field on the attribute.

Return value

\Drupal\commerce_product\Entity\ProductAttributeValueInterface[] Array of attribute entities.

8 calls to ProductAttributeTestTrait::createAttributeSet()
AddToCartFormTest::testOptionalProductAttribute in modules/cart/tests/src/Functional/AddToCartFormTest.php
Tests the behavior of optional product attributes.
AddToCartFormTest::testRenderedAttributeElement in modules/cart/tests/src/Functional/AddToCartFormTest.php
Tests that the add to cart form renders an attribute entity.
AddToCartFormTest::testVariationCanonicalLinkAddToCartForm in modules/cart/tests/src/Functional/AddToCartFormTest.php
Test adding a product to the cart, via the variant's canonical link.
AddToCartMultiAttributeTest::testMultipleVariations in modules/cart/tests/src/FunctionalJavascript/AddToCartMultiAttributeTest.php
Tests adding a product to the cart when there are multiple variations.
AddToCartMultiAttributeTest::testRenderedVariationFields in modules/cart/tests/src/FunctionalJavascript/AddToCartMultiAttributeTest.php
Tests that the cart refreshes rendered variation fields.

... See full list

File

modules/product/tests/src/Traits/ProductAttributeTestTrait.php, line 38

Class

ProductAttributeTestTrait
Defines a trait for attribute-related functional tests.

Namespace

Drupal\Tests\commerce_product\Traits

Code

protected function createAttributeSet(ProductVariationTypeInterface $variation_type, $name, array $options, $test_field = FALSE) {
  $attribute = ProductAttribute::create([
    'id' => $name,
    'label' => ucfirst($name),
  ]);
  $attribute
    ->save();
  $this->attributeFieldManager
    ->createField($attribute, $variation_type
    ->id());
  if ($test_field) {
    $field_storage = FieldStorageConfig::loadByName('commerce_product_attribute_value', 'rendered_test');
    if (!$field_storage) {
      $field_storage = FieldStorageConfig::create([
        'field_name' => 'rendered_test',
        'entity_type' => 'commerce_product_attribute_value',
        'type' => 'text',
      ]);
      $field_storage
        ->save();
    }
    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $attribute
        ->id(),
    ])
      ->save();

    /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $attribute_view_display */
    $attribute_view_display = EntityViewDisplay::create([
      'targetEntityType' => 'commerce_product_attribute_value',
      'bundle' => $name,
      'mode' => 'add_to_cart',
      'status' => TRUE,
    ]);
    $attribute_view_display
      ->removeComponent('name');
    $attribute_view_display
      ->setComponent('rendered_test', [
      'label' => 'hidden',
      'type' => 'string',
    ]);
    $attribute_view_display
      ->save();
  }
  $attribute_set = [];
  foreach ($options as $key => $value) {
    $attribute_set[$key] = $this
      ->createAttributeValue($name, $value);
  }
  return $attribute_set;
}