You are here

function TaxonomyTest::testTaxonomyTokens in Token 8

Test the additional taxonomy term tokens.

File

tests/src/Kernel/TaxonomyTest.php, line 46

Class

TaxonomyTest
Tests taxonomy tokens.

Namespace

Drupal\Tests\token\Kernel

Code

function testTaxonomyTokens() {
  $root_term = $this
    ->addTerm($this->vocab, [
    'name' => 'Root term',
    'path' => [
      'alias' => '/root-term',
    ],
  ]);
  $tokens = [
    'url' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $root_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $root_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $root_term
        ->id(),
    ], [
      'absolute' => FALSE,
    ])
      ->toString(),
    'url:path' => '/root-term',
    'url:unaliased:path' => "/taxonomy/term/{$root_term->id()}",
    'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', [
      'taxonomy_term' => $root_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'parents' => NULL,
    'parents:count' => NULL,
    'parents:keys' => NULL,
    'root' => NULL,
    // Deprecated tokens
    'url:alias' => '/root-term',
  ];
  $this
    ->assertTokens('term', [
    'term' => $root_term,
  ], $tokens);
  $parent_term = $this
    ->addTerm($this->vocab, [
    'name' => 'Parent term',
    'parent' => $root_term
      ->id(),
  ]);
  $tokens = [
    'url' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $parent_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $parent_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', [
      'taxonomy_term' => $parent_term
        ->id(),
    ], [
      'absolute' => FALSE,
    ])
      ->toString(),
    'url:path' => "/taxonomy/term/{$parent_term->id()}",
    'url:unaliased:path' => "/taxonomy/term/{$parent_term->id()}",
    'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', [
      'taxonomy_term' => $parent_term
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'parents' => 'Root term',
    'parents:count' => 1,
    'parents:keys' => $root_term
      ->id(),
    'root' => $root_term
      ->label(),
    'root:tid' => $root_term
      ->id(),
    // Deprecated tokens
    'url:alias' => "/taxonomy/term/{$parent_term->id()}",
  ];
  $this
    ->assertTokens('term', [
    'term' => $parent_term,
  ], $tokens);
  $term = $this
    ->addTerm($this->vocab, [
    'name' => 'Test term',
    'parent' => $parent_term
      ->id(),
  ]);
  $tokens = [
    'parents' => 'Root term, Parent term',
    'parents:count' => 2,
    'parents:keys' => implode(', ', [
      $root_term
        ->id(),
      $parent_term
        ->id(),
    ]),
  ];
  $this
    ->assertTokens('term', [
    'term' => $term,
  ], $tokens);
}