You are here

function EntityTest::testEntityMapping in Token 8

File

tests/src/Kernel/EntityTest.php, line 44

Class

EntityTest
Tests entity tokens.

Namespace

Drupal\Tests\token\Kernel

Code

function testEntityMapping() {

  /** @var \Drupal\token\TokenEntityMapperInterface $mapper */
  $mapper = \Drupal::service('token.entity_mapper');
  $this
    ->assertSame('node', $mapper
    ->getEntityTypeForTokenType('node'));
  $this
    ->assertSame('taxonomy_term', $mapper
    ->getEntityTypeForTokenType('term'));
  $this
    ->assertSame('taxonomy_vocabulary', $mapper
    ->getEntityTypeForTokenType('vocabulary'));
  $this
    ->assertSame(FALSE, $mapper
    ->getEntityTypeForTokenType('invalid'));
  $this
    ->assertSame('invalid', $mapper
    ->getEntityTypeForTokenType('invalid', TRUE));
  $this
    ->assertSame('node', $mapper
    ->getTokenTypeForEntityType('node'));
  $this
    ->assertSame('term', $mapper
    ->getTokenTypeForEntityType('taxonomy_term'));
  $this
    ->assertSame('vocabulary', $mapper
    ->getTokenTypeForEntityType('taxonomy_vocabulary'));
  $this
    ->assertSame(FALSE, $mapper
    ->getTokenTypeForEntityType('invalid'));
  $this
    ->assertSame('invalid', $mapper
    ->getTokenTypeForEntityType('invalid', TRUE));

  // Test that when we send the mis-matched entity type into
  // Drupal\Core\Utility\Token::replace() that we still get the tokens
  // replaced.
  $vocabulary = Vocabulary::load('tags');
  $term = $this
    ->addTerm($vocabulary);
  $this
    ->assertSame($vocabulary
    ->label(), \Drupal::token()
    ->replace('[vocabulary:name]', [
    'taxonomy_vocabulary' => $vocabulary,
  ]));
  $this
    ->assertSame($term
    ->label() . $vocabulary
    ->label(), \Drupal::token()
    ->replace('[term:name][term:vocabulary:name]', [
    'taxonomy_term' => $term,
  ]));
}