Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Token / TokenReplaceKernelTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Token;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\Xss;
8 use Drupal\Core\Render\BubbleableMetadata;
9
10 /**
11  * Generates text using placeholders for dummy content to check token
12  * replacement.
13  *
14  * @group system
15  */
16 class TokenReplaceKernelTest extends TokenReplaceKernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function setUp() {
22     parent::setUp();
23     // Set the site name to something other than an empty string.
24     $this->config('system.site')->set('name', 'Drupal')->save();
25   }
26
27   /**
28    * Test whether token-replacement works in various contexts.
29    */
30   public function testSystemTokenRecognition() {
31     // Generate prefixes and suffixes for the token context.
32     $tests = [
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' => ']'],
43     ];
44
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]));
51     }
52
53     // Test token replacement when the string contains no tokens.
54     $this->assertEqual($this->tokenService->replace('No tokens here.'), 'No tokens here.');
55   }
56
57   /**
58    * Tests the clear parameter.
59    */
60   public function testClear() {
61     // Valid token.
62     $source = '[site:name]';
63     // No user passed in, should be untouched.
64     $source .= '[user:name]';
65     // Non-existing token.
66     $source .= '[bogus:token]';
67
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.');
72
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.');
77   }
78
79   /**
80    * Tests the generation of all system site information tokens.
81    */
82   public function testSystemSiteTokenReplacement() {
83     $url_options = [
84       'absolute' => TRUE,
85       'language' => $this->interfaceLanguage,
86     ];
87
88     $slogan = '<blink>Slogan</blink>';
89     $safe_slogan = Xss::filterAdmin($slogan);
90
91     // Set a few site variables.
92     $config = $this->config('system.site');
93     $config
94       ->set('name', '<strong>Drupal<strong>')
95       ->set('slogan', $slogan)
96       ->set('mail', 'simpletest@example.com')
97       ->save();
98
99     // Generate and test tokens.
100     $tests = [];
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);
107
108     $base_bubbleable_metadata = new BubbleableMetadata();
109
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;
118
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.');
121
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]);
127     }
128   }
129
130   /**
131    * Tests the generation of all system date tokens.
132    */
133   public function testSystemDateTokenReplacement() {
134     // Set time to one hour before request.
135     $date = REQUEST_TIME - 3600;
136
137     // Generate and test tokens.
138     $tests = [];
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);
146
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.');
149
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]));
153     }
154   }
155
156 }