You are here

public function ProductVariationFieldRendererTest::testRenderField in Commerce Core 8.2

Tests rendering a single field.

@covers ::renderField

File

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

Class

ProductVariationFieldRendererTest
Tests the product variation field renderer.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testRenderField() {
  $variation = ProductVariation::create([
    'type' => $this->secondVariationType
      ->id(),
    'sku' => strtolower($this
      ->randomMachineName()),
    'title' => $this
      ->randomString(),
    'price' => new Price('10', 'USD'),
    '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('list_price', [
    'label' => 'above',
    'type' => 'commerce_price_default',
  ]);
  $entity_display
    ->removeComponent('price');
  $entity_display
    ->save();
  $rendered_field = $this->variationFieldRenderer
    ->renderField('sku', $variation, 'default');
  $this
    ->assertNotEmpty($rendered_field);
  $this
    ->assertNotEmpty($rendered_field[0]);
  $this
    ->assertEquals('product--variation-field--variation_sku__' . $variation
    ->getProductId(), $rendered_field['#ajax_replace_class']);

  // Confirm that an empty field gets a rendered wrapper.
  $rendered_field = $this->variationFieldRenderer
    ->renderField('list_price', $variation, 'default');
  $this
    ->assertNotEmpty($rendered_field);
  $this
    ->assertEquals('product--variation-field--variation_list_price__' . $variation
    ->getProductId(), $rendered_field['#ajax_replace_class']);
  $this
    ->assertEquals('container', $rendered_field['#type']);

  // Confirm that hidden fields don't get AJAX classes.
  $rendered_field = $this->variationFieldRenderer
    ->renderField('price', $variation, 'default');
  $this
    ->assertEmpty($rendered_field);

  // Confirm that passing a custom formatter works.
  $rendered_field = $this->variationFieldRenderer
    ->renderField('price', $variation, [
    'type' => 'commerce_price_default',
  ]);
  $this
    ->assertNotEmpty($rendered_field);
  $this
    ->assertNotEmpty($rendered_field[0]);
  $this
    ->assertEquals('product--variation-field--variation_price__' . $variation
    ->getProductId(), $rendered_field['#ajax_replace_class']);
}