public function TokenReplaceKernelTest::testSystemSiteTokenReplacement in Drupal 10
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testSystemSiteTokenReplacement()
- 9 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testSystemSiteTokenReplacement()
Tests the generation of all system site information tokens.
File
- core/
modules/ system/ tests/ src/ Kernel/ Token/ TokenReplaceKernelTest.php, line 83
Class
- TokenReplaceKernelTest
- Generates text using placeholders for dummy content to check token replacement.
Namespace
Drupal\Tests\system\Kernel\TokenCode
public function testSystemSiteTokenReplacement() {
$url_options = [
'absolute' => TRUE,
'language' => $this->interfaceLanguage,
];
$slogan = '<blink>Slogan</blink>';
$safe_slogan = Xss::filterAdmin($slogan);
// Set a few site variables.
$config = $this
->config('system.site');
$config
->set('name', '<strong>Drupal<strong>')
->set('slogan', $slogan)
->set('mail', 'simpletest@example.com')
->save();
// Generate and test tokens.
$tests = [];
$tests['[site:name]'] = Html::escape($config
->get('name'));
$tests['[site:slogan]'] = $safe_slogan;
$tests['[site:mail]'] = $config
->get('mail');
$tests['[site:url]'] = Url::fromRoute('<front>', [], $url_options)
->toString();
$tests['[site:url-brief]'] = preg_replace([
'!^https?://!',
'!/$!',
], '', Url::fromRoute('<front>', [], $url_options)
->toString());
$tests['[site:login-url]'] = Url::fromRoute('user.page', [], $url_options)
->toString();
$base_bubbleable_metadata = new BubbleableMetadata();
$metadata_tests = [];
$metadata_tests['[site:name]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
$metadata_tests['[site:slogan]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
$metadata_tests['[site:mail]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[site:url]'] = $bubbleable_metadata
->addCacheContexts([
'url.site',
]);
$metadata_tests['[site:url-brief]'] = $bubbleable_metadata;
$metadata_tests['[site:login-url]'] = $bubbleable_metadata;
// Test to make sure that we generated something for each token.
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $this->tokenService
->replace($input, [], [
'langcode' => $this->interfaceLanguage
->getId(),
], $bubbleable_metadata);
$this
->assertEquals($expected, $output, new FormattableMarkup('System site information token %token replaced.', [
'%token' => $input,
]));
$this
->assertEquals($metadata_tests[$input], $bubbleable_metadata);
}
}