You are here

public function UnitTest::testGetInvalidTokens in Token 8

Test invalid tokens.

File

tests/src/Kernel/UnitTest.php, line 35

Class

UnitTest
Test basic, low-level token functions.

Namespace

Drupal\Tests\token\Kernel

Code

public function testGetInvalidTokens() {
  $tests = [];
  $tests[] = [
    'valid tokens' => [
      '[node:title]',
      '[node:created:short]',
      '[node:created:custom:invalid]',
      '[node:created:custom:mm-YYYY]',
      '[node:colons:in:name]',
      '[site:name]',
      '[site:slogan]',
      '[current-date:short]',
      '[current-user:uid]',
      '[current-user:ip-address]',
    ],
    'invalid tokens' => [
      '[node:title:invalid]',
      '[node:created:invalid]',
      '[node:created:short:invalid]',
      '[node:colons:in:name:invalid]',
      '[invalid:title]',
      '[site:invalid]',
      '[user:ip-address]',
      '[user:uid]',
      '[comment:cid]',
      // Deprecated tokens
      '[node:tnid]',
      '[node:type]',
      '[node:type-name]',
      '[date:short]',
    ],
    'types' => [
      'node',
    ],
  ];
  $tests[] = [
    'valid tokens' => [
      '[node:title]',
      '[node:created:short]',
      '[node:created:custom:invalid]',
      '[node:created:custom:mm-YYYY]',
      '[node:colons:in:name]',
      '[site:name]',
      '[site:slogan]',
      '[user:uid]',
      '[current-date:short]',
      '[current-user:uid]',
    ],
    'invalid tokens' => [
      '[node:title:invalid]',
      '[node:created:invalid]',
      '[node:created:short:invalid]',
      '[node:colons:in:name:invalid]',
      '[invalid:title]',
      '[site:invalid]',
      '[user:ip-address]',
      '[comment:cid]',
      // Deprecated tokens
      '[node:tnid]',
      '[node:type]',
      '[node:type-name]',
    ],
    'types' => [
      'all',
    ],
  ];
  foreach ($tests as $test) {
    $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
    shuffle($tokens);
    $invalid_tokens = $this->tokenService
      ->getInvalidTokensByContext(implode(' ', $tokens), $test['types']);
    sort($invalid_tokens);
    sort($test['invalid tokens']);
    $this
      ->assertEquals($test['invalid tokens'], $invalid_tokens, 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
  }
}