View source
<?php
namespace Drupal\Tests\address_display\Kernel\Formatter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
class AddressDisplayTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
protected static $modules = [
'system',
'field',
'text',
'entity_test',
'user',
'address',
'address_display',
];
protected $fieldName;
protected $display;
protected $entity;
protected $defaultDisplaySettings = [
'address_display' => [
'organization' => [
'display' => TRUE,
'glue' => '',
'weight' => -1,
],
'address_line1' => [
'display' => TRUE,
'glue' => '',
'weight' => 0,
],
'address_line2' => [
'display' => TRUE,
'glue' => ',',
'weight' => 1,
],
'locality' => [
'display' => TRUE,
'glue' => ',',
'weight' => 2,
],
'postal_code' => [
'display' => TRUE,
'glue' => '',
'weight' => 3,
],
'country_code' => [
'display' => TRUE,
'glue' => '',
'weight' => 4,
],
'langcode' => [
'display' => FALSE,
'glue' => ',',
'weight' => 100,
],
'administrative_area' => [
'display' => FALSE,
'glue' => ',',
'weight' => 100,
],
'dependent_locality' => [
'display' => FALSE,
'glue' => ',',
'weight' => 100,
],
'sorting_code' => [
'display' => FALSE,
'glue' => ',',
'weight' => 100,
],
'given_name' => [
'display' => TRUE,
'glue' => '',
'weight' => 100,
],
'family_name' => [
'display' => TRUE,
'glue' => ',',
'weight' => 100,
],
],
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'system',
]);
$this
->installConfig([
'field',
]);
$this
->installConfig([
'text',
]);
$this
->installConfig([
'address',
]);
$this
->installConfig([
'entity_test',
]);
$this
->installEntitySchema('entity_test');
$this->fieldName = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'address',
]);
$field_storage
->save();
$field_config = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this
->randomMachineName(),
]);
$field_config
->save();
if (version_compare(\Drupal::VERSION, '8.8', '<')) {
$this->display = \Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test', 'default');
}
else {
$this->display = \Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test', 'default');
}
$this->entity = EntityTest::create([]);
$this->entity->{$this->fieldName} = [
'organization' => 'Test company',
'country_code' => 'PL',
'locality' => 'Kraków',
'postal_code' => '31-042',
'address_line1' => 'Rynek Główny 1/3',
'given_name' => 'Jan',
'family_name' => 'Nowak',
];
}
public function testRenderAddressComponents() {
$this->display
->setComponent($this->fieldName, [
'type' => 'address_display_formatter',
'settings' => [],
]);
$this->display
->save();
$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);
$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();
$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);
$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>');
$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();
$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);
}
public function testSingleAddressComponent() {
$settings = [
'address_display' => [
'organization' => [
'display' => TRUE,
'glue' => 'glue',
'weight' => 0,
],
'address_line1' => [
'display' => FALSE,
'glue' => '',
'weight' => 0,
],
],
];
$this->display
->setComponent($this->fieldName, [
'type' => 'address_display_formatter',
'settings' => $settings,
]);
$this->display
->save();
$content = $this->display
->build($this->entity);
$this
->render($content);
$this
->assertRaw('<span class="address-display-element organization-element">');
$this
->assertNoRaw('<span class="address-display-element address-line1-element">');
$this
->assertRaw('<span class="address-display-element organization-element">Test company</span>');
$this->entity->{$this->fieldName}->organization = '';
$content = $this->display
->build($this->entity);
$this
->render($content);
$this
->assertNoRaw('<span class="address-display-element organization-element">');
}
}