Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / UnitTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 /**
6  * Test basic, low-level token functions.
7  *
8  * @group token
9  */
10 class UnitTest extends KernelTestBase {
11
12   /**
13    * @var \Drupal\token\Token
14    */
15   protected $tokenService;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['file', 'node'];
23
24   /**
25    * {@inheritdoc}
26    */
27   public function setUp() {
28     parent::setUp();
29     $this->tokenService = \Drupal::token();
30   }
31
32   /**
33    * Test invalid tokens.
34    */
35   public function testGetInvalidTokens() {
36     $tests = [];
37     $tests[] = [
38       'valid tokens' => [
39         '[node:title]',
40         '[node:created:short]',
41         '[node:created:custom:invalid]',
42         '[node:created:custom:mm-YYYY]',
43         '[node:colons:in:name]',
44         '[site:name]',
45         '[site:slogan]',
46         '[current-date:short]',
47         '[current-user:uid]',
48         '[current-user:ip-address]',
49       ],
50       'invalid tokens' => [
51         '[node:title:invalid]',
52         '[node:created:invalid]',
53         '[node:created:short:invalid]',
54         '[node:colons:in:name:invalid]',
55         '[invalid:title]',
56         '[site:invalid]',
57         '[user:ip-address]',
58         '[user:uid]',
59         '[comment:cid]',
60         // Deprecated tokens
61         '[node:tnid]',
62         '[node:type]',
63         '[node:type-name]',
64         '[date:short]',
65       ],
66       'types' => ['node'],
67     ];
68     $tests[] = [
69       'valid tokens' => [
70         '[node:title]',
71         '[node:created:short]',
72         '[node:created:custom:invalid]',
73         '[node:created:custom:mm-YYYY]',
74         '[node:colons:in:name]',
75         '[site:name]',
76         '[site:slogan]',
77         '[user:uid]',
78         '[current-date:short]',
79         '[current-user:uid]',
80       ],
81       'invalid tokens' => [
82         '[node:title:invalid]',
83         '[node:created:invalid]',
84         '[node:created:short:invalid]',
85         '[node:colons:in:name:invalid]',
86         '[invalid:title]',
87         '[site:invalid]',
88         '[user:ip-address]',
89         '[comment:cid]',
90         // Deprecated tokens
91         '[node:tnid]',
92         '[node:type]',
93         '[node:type-name]',
94       ],
95       'types' => ['all'],
96     ];
97
98     foreach ($tests as $test) {
99       $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
100       shuffle($tokens);
101
102       $invalid_tokens = $this->tokenService->getInvalidTokensByContext(implode(' ', $tokens), $test['types']);
103
104       sort($invalid_tokens);
105       sort($test['invalid tokens']);
106       $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
107     }
108   }
109
110   /**
111    * Test that tokens are generated only for content entities.
112    */
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'));
118   }
119
120 }