You are here

public function AddressDefaultFormatterTest::testUnitedStatesIncompleteAddress in Address 8

Tests US address formatting.

File

tests/src/Kernel/Formatter/AddressDefaultFormatterTest.php, line 126

Class

AddressDefaultFormatterTest
Tests the address_default formatter.

Namespace

Drupal\Tests\address\Kernel\Formatter

Code

public function testUnitedStatesIncompleteAddress() {
  $entity = EntityTest::create([]);
  $entity->{$this->fieldName} = [
    'country_code' => 'US',
    'administrative_area' => 'CA',
    'address_line1' => '1098 Alta Ave',
    'postal_code' => '94043',
  ];
  $this
    ->renderEntityFields($entity, $this->display);
  $expected = implode('', [
    'line1' => '<p class="address" translate="no">',
    'line2' => '<span class="address-line1">1098 Alta Ave</span><br>' . "\n",
    'line3' => '<span class="administrative-area">CA</span> <span class="postal-code">94043</span><br>' . "\n",
    'line4' => '<span class="country">United States</span>',
    'line5' => '</p>',
  ]);
  $this
    ->assertRaw($expected, 'The US address has been properly formatted.');

  // Now add the locality, but remove the administrative area.
  $entity->{$this->fieldName}->locality = 'Mountain View';
  $entity->{$this->fieldName}->administrative_area = '';
  $this
    ->renderEntityFields($entity, $this->display);
  $expected = implode('', [
    'line1' => '<p class="address" translate="no">',
    'line2' => '<span class="address-line1">1098 Alta Ave</span><br>' . "\n",
    'line3' => '<span class="locality">Mountain View</span>, <span class="postal-code">94043</span><br>' . "\n",
    'line4' => '<span class="country">United States</span>',
    'line5' => '</p>',
  ]);
  $this
    ->assertRaw($expected, 'The US address has been properly formatted.');
}