3 namespace Drupal\Tests\system\Kernel\Token;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\Xss;
8 use Drupal\Core\Render\BubbleableMetadata;
11 * Generates text using placeholders for dummy content to check token
16 class TokenReplaceKernelTest extends TokenReplaceKernelTestBase {
21 protected function setUp() {
23 // Set the site name to something other than an empty string.
24 $this->config('system.site')->set('name', 'Drupal')->save();
28 * Test whether token-replacement works in various contexts.
30 public function testSystemTokenRecognition() {
31 // Generate prefixes and suffixes for the token context.
33 ['prefix' => 'this is the ', 'suffix' => ' site'],
34 ['prefix' => 'this is the', 'suffix' => 'site'],
35 ['prefix' => '[', 'suffix' => ']'],
36 ['prefix' => '', 'suffix' => ']]]'],
37 ['prefix' => '[[[', 'suffix' => ''],
38 ['prefix' => ':[:', 'suffix' => '--]'],
39 ['prefix' => '-[-', 'suffix' => ':]:'],
40 ['prefix' => '[:', 'suffix' => ']'],
41 ['prefix' => '[site:', 'suffix' => ':name]'],
42 ['prefix' => '[site:', 'suffix' => ']'],
45 // Check if the token is recognized in each of the contexts.
46 foreach ($tests as $test) {
47 $input = $test['prefix'] . '[site:name]' . $test['suffix'];
48 $expected = $test['prefix'] . 'Drupal' . $test['suffix'];
49 $output = $this->tokenService->replace($input, [], ['langcode' => $this->interfaceLanguage->getId()]);
50 $this->assertTrue($output == $expected, format_string('Token recognized in string %string', ['%string' => $input]));
53 // Test token replacement when the string contains no tokens.
54 $this->assertEqual($this->tokenService->replace('No tokens here.'), 'No tokens here.');
58 * Tests the clear parameter.
60 public function testClear() {
62 $source = '[site:name]';
63 // No user passed in, should be untouched.
64 $source .= '[user:name]';
65 // Non-existing token.
66 $source .= '[bogus:token]';
68 // Replace with the clear parameter, only the valid token should remain.
69 $target = Html::escape($this->config('system.site')->get('name'));
70 $result = $this->tokenService->replace($source, [], ['langcode' => $this->interfaceLanguage->getId(), 'clear' => TRUE]);
71 $this->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
73 $target .= '[user:name]';
74 $target .= '[bogus:token]';
75 $result = $this->tokenService->replace($source, [], ['langcode' => $this->interfaceLanguage->getId()]);
76 $this->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
80 * Tests the generation of all system site information tokens.
82 public function testSystemSiteTokenReplacement() {
85 'language' => $this->interfaceLanguage,
88 $slogan = '<blink>Slogan</blink>';
89 $safe_slogan = Xss::filterAdmin($slogan);
91 // Set a few site variables.
92 $config = $this->config('system.site');
94 ->set('name', '<strong>Drupal<strong>')
95 ->set('slogan', $slogan)
96 ->set('mail', 'simpletest@example.com')
99 // Generate and test tokens.
101 $tests['[site:name]'] = Html::escape($config->get('name'));
102 $tests['[site:slogan]'] = $safe_slogan;
103 $tests['[site:mail]'] = $config->get('mail');
104 $tests['[site:url]'] = \Drupal::url('<front>', [], $url_options);
105 $tests['[site:url-brief]'] = preg_replace(['!^https?://!', '!/$!'], '', \Drupal::url('<front>', [], $url_options));
106 $tests['[site:login-url]'] = \Drupal::url('user.page', [], $url_options);
108 $base_bubbleable_metadata = new BubbleableMetadata();
110 $metadata_tests = [];
111 $metadata_tests['[site:name]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
112 $metadata_tests['[site:slogan]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
113 $metadata_tests['[site:mail]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
114 $bubbleable_metadata = clone $base_bubbleable_metadata;
115 $metadata_tests['[site:url]'] = $bubbleable_metadata->addCacheContexts(['url.site']);
116 $metadata_tests['[site:url-brief]'] = $bubbleable_metadata;
117 $metadata_tests['[site:login-url]'] = $bubbleable_metadata;
119 // Test to make sure that we generated something for each token.
120 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
122 foreach ($tests as $input => $expected) {
123 $bubbleable_metadata = new BubbleableMetadata();
124 $output = $this->tokenService->replace($input, [], ['langcode' => $this->interfaceLanguage->getId()], $bubbleable_metadata);
125 $this->assertEqual($output, $expected, new FormattableMarkup('System site information token %token replaced.', ['%token' => $input]));
126 $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
131 * Tests the generation of all system date tokens.
133 public function testSystemDateTokenReplacement() {
134 // Set time to one hour before request.
135 $date = REQUEST_TIME - 3600;
137 // Generate and test tokens.
139 $date_formatter = \Drupal::service('date.formatter');
140 $tests['[date:short]'] = $date_formatter->format($date, 'short', '', NULL, $this->interfaceLanguage->getId());
141 $tests['[date:medium]'] = $date_formatter->format($date, 'medium', '', NULL, $this->interfaceLanguage->getId());
142 $tests['[date:long]'] = $date_formatter->format($date, 'long', '', NULL, $this->interfaceLanguage->getId());
143 $tests['[date:custom:m/j/Y]'] = $date_formatter->format($date, 'custom', 'm/j/Y', NULL, $this->interfaceLanguage->getId());
144 $tests['[date:since]'] = $date_formatter->formatTimeDiffSince($date, ['langcode' => $this->interfaceLanguage->getId()]);
145 $tests['[date:raw]'] = Xss::filter($date);
147 // Test to make sure that we generated something for each token.
148 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
150 foreach ($tests as $input => $expected) {
151 $output = $this->tokenService->replace($input, ['date' => $date], ['langcode' => $this->interfaceLanguage->getId()]);
152 $this->assertEqual($output, $expected, format_string('Date token %token replaced.', ['%token' => $input]));