You are here

public function CountryNameTokenTest::testMultilingualFields in Address 8

Test tokens for multilingual fields and entities.

File

tests/src/Kernel/CountryNameTokenTest.php, line 227

Class

CountryNameTokenTest
Tests the country name token.

Namespace

Drupal\Tests\address\Kernel

Code

public function testMultilingualFields() {

  // Create an english term and add a german translation for it.
  $term = $this
    ->createTerm($this->vocabulary, [
    'name' => 'english-test-term',
    'langcode' => 'en',
    'term_address_field' => [
      'country_code' => 'US',
      'administrative_area' => 'CA',
      'address_line1' => '1098 Alta Ave',
      'postal_code' => '94043',
    ],
  ]);
  $term
    ->addTranslation('de', [
    'name' => 'german-test-term',
    'term_address_field' => [
      'country_code' => 'US',
      'administrative_area' => 'CA',
      'address_line1' => '1098 Alta Ave',
      'postal_code' => '94043',
    ],
  ])
    ->save();
  $german_term = $term
    ->getTranslation('de');

  // Create an english node, add a german translation for it and add the
  // english term to the english node's entity reference field and the
  // german term to the german's entity reference field.
  $node = Node::create([
    'title' => 'english-node-title',
    'type' => 'article',
    'test_term_reference' => [
      'target_id' => $term
        ->id(),
    ],
    'test_address' => [
      'country_code' => 'FR',
      'locality' => 'Paris',
      'postal_code' => '75014',
      'address_line1' => '218 rue de la Tombe-Issoire',
    ],
  ]);
  $node
    ->addTranslation('de', [
    'title' => 'german-node-title',
    'test_term_reference' => [
      'target_id' => $german_term
        ->id(),
    ],
    'test_address' => [
      'country_code' => 'FR',
      'locality' => 'Paris',
      'postal_code' => '75014',
      'address_line1' => '218 rue de la Tombe-Issoire',
    ],
  ])
    ->save();

  // Verify the :country_name token of the english term the english node
  // refers to. Also verify the value of the term's country_name token.
  $this
    ->assertTokens('node', [
    'node' => $node,
  ], [
    'test_term_reference:entity:term_address_field:country_name' => 'United States',
    'test_address:country_name' => 'France',
  ]);

  // Same test for the german node and its german term.
  $german_node = $node
    ->getTranslation('de');
  $this
    ->assertTokens('node', [
    'node' => $german_node,
  ], [
    'test_term_reference:entity:term_address_field:country_name' => 'Vereinigte Staaten',
    'test_address:country_name' => 'Frankreich',
  ]);

  // If the langcode is specified, it should have priority over the node's
  // active language.
  $tokens = [
    'test_term_reference:entity:term_address_field:country_name' => 'Vereinigte Staaten',
    'test_address:country_name' => 'Frankreich',
  ];
  $this
    ->assertTokens('node', [
    'node' => $node,
  ], $tokens, [
    'langcode' => 'de',
  ]);
}