You are here

public function FieldTest::testBaseFieldTokens in Token 8

Test that tokens are properly created for an entity's base fields.

File

tests/src/Kernel/FieldTest.php, line 421

Class

FieldTest
Tests field tokens.

Namespace

Drupal\Tests\token\Kernel

Code

public function testBaseFieldTokens() {

  // Create a new contact_message entity and verify that tokens are generated
  // for its base fields. The contact_message entity type is used because it
  // provides no tokens by default.
  $contact_form = ContactForm::create([
    'id' => 'form_id',
  ]);
  $contact_form
    ->save();
  $entity = Message::create([
    'contact_form' => 'form_id',
    'uuid' => '123',
    'langcode' => 'en',
    'name' => 'Test name',
    'mail' => 'Test mail',
    'subject' => 'Test subject',
    'message' => 'Test message',
    'copy' => FALSE,
  ]);
  $entity
    ->save();
  $this
    ->assertTokens('contact_message', [
    'contact_message' => $entity,
  ], [
    'uuid' => Markup::create('123'),
    'langcode' => Markup::create('English'),
    'name' => Markup::create('Test name'),
    'mail' => Markup::create('Test mail'),
    'subject' => Markup::create('Test subject'),
    'message' => Markup::create('Test message'),
    'copy' => 'Off',
  ]);

  // Test the metadata of one of the tokens.
  $tokenService = \Drupal::service('token');
  $token_info = $tokenService
    ->getTokenInfo('contact_message', 'subject');
  $this
    ->assertEquals($token_info['name'], 'Subject');
  $this
    ->assertEquals($token_info['description'], 'Text (plain) field.');
  $this
    ->assertEquals($token_info['module'], 'token');

  // Verify that node entity type doesn't have a uid token.
  $this
    ->assertNull($tokenService
    ->getTokenInfo('node', 'uid'));
}