3 namespace Drupal\Tests\token\Kernel;
6 * Test basic, low-level token functions.
10 class UnitTest extends KernelTestBase {
13 * @var \Drupal\token\Token
15 protected $tokenService;
22 public static $modules = ['file', 'node'];
27 public function setUp() {
29 $this->tokenService = \Drupal::token();
33 * Test invalid tokens.
35 public function testGetInvalidTokens() {
40 '[node:created:short]',
41 '[node:created:custom:invalid]',
42 '[node:created:custom:mm-YYYY]',
43 '[node:colons:in:name]',
46 '[current-date:short]',
48 '[current-user:ip-address]',
51 '[node:title:invalid]',
52 '[node:created:invalid]',
53 '[node:created:short:invalid]',
54 '[node:colons:in:name:invalid]',
71 '[node:created:short]',
72 '[node:created:custom:invalid]',
73 '[node:created:custom:mm-YYYY]',
74 '[node:colons:in:name]',
78 '[current-date:short]',
82 '[node:title:invalid]',
83 '[node:created:invalid]',
84 '[node:created:short:invalid]',
85 '[node:colons:in:name:invalid]',
98 foreach ($tests as $test) {
99 $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
102 $invalid_tokens = $this->tokenService->getInvalidTokensByContext(implode(' ', $tokens), $test['types']);
104 sort($invalid_tokens);
105 sort($test['invalid tokens']);
106 $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
111 * Test that tokens are generated only for content entities.
113 public function testContentEntityOnlyTokens() {
114 // Verify that type and token info for a config entity is not generated.
115 $this->assertNull($this->tokenService->getTokenInfo('user_role', 'original'));
116 $this->assertNull($this->tokenService->getTokenInfo('user_role', 'url'));
117 $this->assertNull($this->tokenService->getTypeInfo('user_role'));