You are here

public function ProductAttributesOverviewFormatterTest::testFormatterDisplay in Commerce Core 8.2

Test the formatters rendered display.

File

modules/product/tests/src/Kernel/ProductAttributesOverviewFormatterTest.php, line 136

Class

ProductAttributesOverviewFormatterTest
Tests the "commerce_product_attributes_overview" formatter.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testFormatterDisplay() {
  $this->productDefaultDisplay
    ->setComponent('variations', [
    'type' => 'commerce_product_attributes_overview',
    'settings' => [
      'attributes' => [
        'color' => 'color',
      ],
      'view_mode' => 'default',
    ],
  ]);
  $this->productDefaultDisplay
    ->save();
  $build = $this->productViewBuilder
    ->view($this->product);
  $this
    ->render($build);
  $this
    ->assertFieldByXPath('//h3[text()=\'Color\']');
  $this
    ->assertFieldByXPath('//ul/li[1]/a/div/div/div[text()=\'Name\']');
  $this
    ->assertFieldByXPath('//ul/li[1]/a/div/div/div[text()=\'Cyan\']');
  $this
    ->assertFieldByXPath('//ul/li[2]/a/div/div/div[text()=\'Name\']');
  $this
    ->assertFieldByXPath('//ul/li[2]/a/div/div/div[text()=\'Yellow\']');

  // Ensure Yellow rendered once, even though two variations have it.
  $this
    ->assertEquals(1, count($this
    ->xpath('//ul/li[2]/a/div/div/div[text()=\'Yellow\']')));
  $this->attributeDefaultDisplay
    ->setComponent('name', [
    'label' => 'hidden',
  ]);
  $this->attributeDefaultDisplay
    ->save();
  $this->productViewBuilder
    ->resetCache([
    $this->product,
  ]);
  $build = $this->productViewBuilder
    ->view($this->product);
  $this
    ->render($build);
  $this
    ->assertFieldByXPath('//h3[text()=\'Color\']');
  $this
    ->assertFieldByXPath('//ul/li[1]/a/div/div[text()=\'Cyan\']');
  $this
    ->assertFieldByXPath('//ul/li[2]/a/div/div[text()=\'Yellow\']');
  EntityViewMode::create([
    'id' => 'commerce_product_attribute_value.test_display',
    'label' => 'Test Display',
    'targetEntityType' => 'commerce_product_attribute_value',
  ])
    ->save();
  $test_attribute_display_mode = $this->attributeDefaultDisplay
    ->createCopy('test_display');
  $test_attribute_display_mode
    ->setStatus(TRUE);
  $test_attribute_display_mode
    ->setComponent('name', [
    'label' => 'inline',
  ]);
  $test_attribute_display_mode
    ->save();
  $this->productDefaultDisplay
    ->setComponent('variations', [
    'type' => 'commerce_product_attributes_overview',
    'settings' => [
      'attributes' => [
        'color' => 'color',
      ],
      'view_mode' => 'test_display',
    ],
  ]);
  $this->productDefaultDisplay
    ->save();
  $this->productViewBuilder
    ->resetCache([
    $this->product,
  ]);
  $build = $this->productViewBuilder
    ->view($this->product);
  $this
    ->render($build);
  $this
    ->assertFieldByXPath('//h3[text()=\'Color\']');
  $this
    ->assertFieldByXPath('//ul/li[1]/a/div/div/div[text()=\'Name\']');
  $this
    ->assertFieldByXPath('//ul/li[1]/a/div/div/div[text()=\'Cyan\']');
  $this
    ->assertFieldByXPath('//ul/li[2]/a/div/div/div[text()=\'Name\']');
  $this
    ->assertFieldByXPath('//ul/li[2]/a/div/div/div[text()=\'Yellow\']');
}