You are here

public function ProductVariationFieldRendererTest::testRenderFields in Commerce Core 8.2

Tests renderFields.

@covers ::renderFields

File

modules/product/tests/src/Kernel/ProductVariationFieldRendererTest.php, line 123

Class

ProductVariationFieldRendererTest
Tests the product variation field renderer.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testRenderFields() {
  $attribute_value = ProductAttributeValue::create([
    'attribute' => 'color',
    'name' => 'Blue',
  ]);
  $attribute_value
    ->save();
  $variation = ProductVariation::create([
    'type' => $this->secondVariationType
      ->id(),
    'sku' => strtolower($this
      ->randomMachineName()),
    'title' => $this
      ->randomString(),
    'attribute_color' => $attribute_value
      ->id(),
    'status' => 1,
  ]);
  $variation
    ->save();
  $product = Product::create([
    'type' => 'default',
    'variations' => [
      $variation,
    ],
  ]);
  $product
    ->save();
  $entity_display = commerce_get_entity_display('commerce_product_variation', $variation
    ->bundle(), 'view');
  $entity_display
    ->setComponent('sku', [
    'label' => 'above',
    'type' => 'string',
  ]);
  $entity_display
    ->setComponent('attribute_color', [
    'label' => 'above',
    'type' => 'entity_reference_label',
  ]);
  $entity_display
    ->setComponent('product_id', [
    'label' => 'above',
    'type' => 'entity_reference_entity_view',
  ]);
  $entity_display
    ->setComponent('list_price', [
    'label' => 'above',
    'type' => 'commerce_price_default',
  ]);
  $entity_display
    ->removeComponent('price');
  $entity_display
    ->save();
  $rendered_fields = $this->variationFieldRenderer
    ->renderFields($variation);

  // The product_id field should be skipped to avoid a render loop.
  $this
    ->assertArrayNotHasKey('product_id', $rendered_fields);
  $this
    ->assertArrayNotHasKey('price', $rendered_fields);
  $this
    ->assertArrayHasKey('sku', $rendered_fields);
  $this
    ->assertArrayHasKey('attribute_color', $rendered_fields);
  $this
    ->assertNotEmpty($rendered_fields['sku']);
  $this
    ->assertNotEmpty($rendered_fields['sku'][0]);
  $this
    ->assertNotEmpty($rendered_fields['attribute_color']);
  $this
    ->assertNotEmpty($rendered_fields['attribute_color'][0]);
  $this
    ->assertEquals('product--variation-field--variation_sku__' . $variation
    ->getProductId(), $rendered_fields['sku']['#ajax_replace_class']);
  $this
    ->assertEquals('product--variation-field--variation_attribute_color__' . $variation
    ->getProductId(), $rendered_fields['attribute_color']['#ajax_replace_class']);

  // Confirm that an empty field gets a rendered wrapper.
  $this
    ->assertArrayHasKey('list_price', $rendered_fields);
  $this
    ->assertNotEmpty($rendered_fields['list_price']);
  $this
    ->assertEquals('product--variation-field--variation_list_price__' . $variation
    ->getProductId(), $rendered_fields['list_price']['#ajax_replace_class']);
  $this
    ->assertEquals('container', $rendered_fields['list_price']['#type']);
  $product_view_builder = $this->container
    ->get('entity_type.manager')
    ->getViewBuilder('commerce_product');
  $product_build = $product_view_builder
    ->view($product);
  $this
    ->render($product_build);

  // Attributes are excluded by default in the twig template.
  $this
    ->assertEmpty($this
    ->cssSelect('.product--variation-field--variation_attribute_color__' . $variation
    ->getProductId()));

  // Verify that the SKU was displayed.
  $this
    ->assertEscaped($variation
    ->getSku());
}