You are here

public function AddressDisplayTest::testRenderAddressComponents in Address Display 8

Tests rendering of address components.

File

tests/src/Kernel/Formatter/AddressDisplayTest.php, line 183

Class

AddressDisplayTest
Tests the address_display_formatter formatter.

Namespace

Drupal\Tests\address_display\Kernel\Formatter

Code

public function testRenderAddressComponents() {

  // Tests default settings.
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'address_display_formatter',
    'settings' => [],
  ]);
  $this->display
    ->save();

  // Render entity fields.
  $content = $this->display
    ->build($this->entity);
  $this
    ->render($content);
  $expected = implode("\n", [
    '<span class="address-display-element organization-element">Test company</span>',
    '<span class="address-display-element address-line1-element">Rynek Główny 1/3</span>',
    '<span class="address-display-element locality-element">Kraków,</span>',
    '<span class="address-display-element postal-code-element">31-042</span>',
    '<span class="address-display-element country-code-element">Poland</span>',
    '<span class="address-display-element given-name-element">Jan</span>',
    '<span class="address-display-element family-name-element">Nowak</span>',
  ]);
  $this
    ->assertRaw($expected);

  // Tests 'display' option.
  $settings = $this->defaultDisplaySettings;
  $settings['address_display']['country_code']['display'] = FALSE;
  $settings['address_display']['given_name']['display'] = FALSE;
  $settings['address_display']['family_name']['display'] = FALSE;
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'address_display_formatter',
    'settings' => $settings,
  ]);
  $this->display
    ->save();

  // Render entity fields.
  $content = $this->display
    ->build($this->entity);
  $this
    ->render($content);
  $expected = implode("\n", [
    '<span class="address-display-element organization-element">Test company</span>',
    '<span class="address-display-element address-line1-element">Rynek Główny 1/3</span>',
    '<span class="address-display-element locality-element">Kraków,</span>',
    '<span class="address-display-element postal-code-element">31-042</span>',
  ]);
  $this
    ->assertRaw($expected);

  // Skip hidden items.
  $this
    ->assertNoRaw('<span class="address-display-element country-code-element">Poland</span>');
  $this
    ->assertNoRaw('<span class="address-display-element given-name-element">Jan</span>');
  $this
    ->assertNoRaw('<span class="address-display-element family-name-element">Nowak</span>');

  // Tests 'glue' option.
  $settings = $this->defaultDisplaySettings;
  $settings['address_display']['organization']['glue'] = ':';
  $settings['address_display']['locality']['glue'] = '';
  $settings['address_display']['family_name']['glue'] = ',';
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'address_display_formatter',
    'settings' => $settings,
  ]);
  $this->display
    ->save();

  // Render entity fields.
  $content = $this->display
    ->build($this->entity);
  $this
    ->render($content);
  $expected = implode("\n", [
    '<span class="address-display-element organization-element">Test company:</span>',
    '<span class="address-display-element address-line1-element">Rynek Główny 1/3</span>',
    '<span class="address-display-element locality-element">Kraków</span>',
    '<span class="address-display-element postal-code-element">31-042</span>',
    '<span class="address-display-element country-code-element">Poland</span>',
    '<span class="address-display-element given-name-element">Jan</span>',
    // Don't display the last item separator.
    '<span class="address-display-element family-name-element">Nowak</span>',
  ]);
  $this
    ->assertRaw($expected);
}