View source
<?php
namespace Drupal\Tests\system\Kernel\Token;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\BubbleableMetadata;
class TokenReplaceKernelTest extends TokenReplaceKernelTestBase {
protected function setUp() {
parent::setUp();
$this
->config('system.site')
->set('name', 'Drupal')
->save();
}
public function testSystemTokenRecognition() {
$tests = [
[
'prefix' => 'this is the ',
'suffix' => ' site',
],
[
'prefix' => 'this is the',
'suffix' => 'site',
],
[
'prefix' => '[',
'suffix' => ']',
],
[
'prefix' => '',
'suffix' => ']]]',
],
[
'prefix' => '[[[',
'suffix' => '',
],
[
'prefix' => ':[:',
'suffix' => '--]',
],
[
'prefix' => '-[-',
'suffix' => ':]:',
],
[
'prefix' => '[:',
'suffix' => ']',
],
[
'prefix' => '[site:',
'suffix' => ':name]',
],
[
'prefix' => '[site:',
'suffix' => ']',
],
];
foreach ($tests as $test) {
$input = $test['prefix'] . '[site:name]' . $test['suffix'];
$expected = $test['prefix'] . 'Drupal' . $test['suffix'];
$output = $this->tokenService
->replace($input, [], [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$this
->assertTrue($output == $expected, new FormattableMarkup('Token recognized in string %string', [
'%string' => $input,
]));
}
$this
->assertEqual($this->tokenService
->replace('No tokens here.'), 'No tokens here.');
}
public function testClear() {
$source = '[site:name]';
$source .= '[user:name]';
$source .= '[bogus:token]';
$target = Html::escape($this
->config('system.site')
->get('name'));
$result = $this->tokenService
->replace($source, [], [
'langcode' => $this->interfaceLanguage
->getId(),
'clear' => TRUE,
]);
$this
->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
$target .= '[user:name]';
$target .= '[bogus:token]';
$result = $this->tokenService
->replace($source, [], [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$this
->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
}
public function testSystemSiteTokenReplacement() {
$url_options = [
'absolute' => TRUE,
'language' => $this->interfaceLanguage,
];
$slogan = '<blink>Slogan</blink>';
$safe_slogan = Xss::filterAdmin($slogan);
$config = $this
->config('system.site');
$config
->set('name', '<strong>Drupal<strong>')
->set('slogan', $slogan)
->set('mail', 'simpletest@example.com')
->save();
$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;
$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
->assertEqual($output, $expected, new FormattableMarkup('System site information token %token replaced.', [
'%token' => $input,
]));
$this
->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
}
}
public function testSystemDateTokenReplacement() {
$date = REQUEST_TIME - 3600;
$tests = [];
$date_formatter = \Drupal::service('date.formatter');
$tests['[date:short]'] = $date_formatter
->format($date, 'short', '', NULL, $this->interfaceLanguage
->getId());
$tests['[date:medium]'] = $date_formatter
->format($date, 'medium', '', NULL, $this->interfaceLanguage
->getId());
$tests['[date:long]'] = $date_formatter
->format($date, 'long', '', NULL, $this->interfaceLanguage
->getId());
$tests['[date:custom:m/j/Y]'] = $date_formatter
->format($date, 'custom', 'm/j/Y', NULL, $this->interfaceLanguage
->getId());
$tests['[date:since]'] = $date_formatter
->formatTimeDiffSince($date, [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$tests['[date:raw]'] = Xss::filter($date);
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$output = $this->tokenService
->replace($input, [
'date' => $date,
], [
'langcode' => $this->interfaceLanguage
->getId(),
]);
$this
->assertEqual($output, $expected, new FormattableMarkup('Date token %token replaced.', [
'%token' => $input,
]));
}
}
}