View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Utility;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Markup;
use Drupal\Core\Utility\Token;
use Drupal\Tests\UnitTestCase;
class TokenTest extends UnitTestCase {
protected $cache;
protected $languageManager;
protected $moduleHandler;
protected $language;
protected $token;
protected $cacheTagsInvalidator;
protected $cacheContextManager;
protected $renderer;
protected function setUp() : void {
parent::setUp();
$this->cache = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$this->languageManager = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->moduleHandler = $this
->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->language = $this
->createMock('\\Drupal\\Core\\Language\\LanguageInterface');
$this->cacheTagsInvalidator = $this
->createMock('\\Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this->renderer = $this
->createMock('Drupal\\Core\\Render\\RendererInterface');
$this->token = new Token($this->moduleHandler, $this->cache, $this->languageManager, $this->cacheTagsInvalidator, $this->renderer);
$container = new ContainerBuilder();
$this->cacheContextManager = new CacheContextsManager($container, [
'current_user',
'custom_context',
]);
$container
->set('cache_contexts_manager', $this->cacheContextManager);
\Drupal::setContainer($container);
}
public function testGetInfo() {
$token_info = [
'types' => [
'foo' => [
'name' => $this
->randomMachineName(),
],
],
];
$this->language
->expects($this
->atLeastOnce())
->method('getId')
->willReturn($this
->randomMachineName());
$this->languageManager
->expects($this
->once())
->method('getCurrentLanguage')
->with(LanguageInterface::TYPE_CONTENT)
->willReturn($this->language);
$this->cache
->expects($this
->once())
->method('get');
$this->cache
->expects($this
->once())
->method('set')
->with('token_info:' . $this->language
->getId(), $token_info);
$this->moduleHandler
->expects($this
->once())
->method('invokeAll')
->with('token_info')
->willReturn($token_info);
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('token_info', $token_info);
$this->token
->getInfo();
$this->token
->getInfo();
}
public function testReplaceWithBubbleableMetadataObject() {
$this->moduleHandler
->expects($this
->any())
->method('invokeAll')
->willReturn([
'[node:title]' => 'hello world',
]);
$bubbleable_metadata = new BubbleableMetadata();
$bubbleable_metadata
->setCacheContexts([
'current_user',
]);
$bubbleable_metadata
->setCacheMaxAge(12);
$node = $this
->prophesize('Drupal\\node\\NodeInterface');
$node
->getCacheTags()
->willReturn([
'node:1',
]);
$node
->getCacheContexts()
->willReturn([
'custom_context',
]);
$node
->getCacheMaxAge()
->willReturn(10);
$node = $node
->reveal();
$result = $this->token
->replace('[node:title]', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertEquals('hello world', $result);
$this
->assertEquals([
'node:1',
], $bubbleable_metadata
->getCacheTags());
$this
->assertEquals([
'current_user',
'custom_context',
], $bubbleable_metadata
->getCacheContexts());
$this
->assertEquals(10, $bubbleable_metadata
->getCacheMaxAge());
}
public function testReplaceWithHookTokensWithBubbleableMetadata() {
$this->moduleHandler
->expects($this
->any())
->method('invokeAll')
->willReturnCallback(function ($hook_name, $args) {
$cacheable_metadata = $args[4];
$cacheable_metadata
->addCacheContexts([
'custom_context',
]);
$cacheable_metadata
->addCacheTags([
'node:1',
]);
$cacheable_metadata
->setCacheMaxAge(10);
return [
'[node:title]' => 'hello world',
];
});
$node = $this
->prophesize('Drupal\\node\\NodeInterface');
$node
->getCacheContexts()
->willReturn([]);
$node
->getCacheTags()
->willReturn([]);
$node
->getCacheMaxAge()
->willReturn(14);
$node = $node
->reveal();
$bubbleable_metadata = new BubbleableMetadata();
$bubbleable_metadata
->setCacheContexts([
'current_user',
]);
$bubbleable_metadata
->setCacheMaxAge(12);
$result = $this->token
->replace('[node:title]', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertEquals('hello world', $result);
$this
->assertEquals([
'node:1',
], $bubbleable_metadata
->getCacheTags());
$this
->assertEquals([
'current_user',
'custom_context',
], $bubbleable_metadata
->getCacheContexts());
$this
->assertEquals(10, $bubbleable_metadata
->getCacheMaxAge());
}
public function testReplaceWithHookTokensAlterWithBubbleableMetadata() {
$this->moduleHandler
->expects($this
->any())
->method('invokeAll')
->willReturn([]);
$this->moduleHandler
->expects($this
->any())
->method('alter')
->willReturnCallback(function ($hook_name, array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
$replacements['[node:title]'] = 'hello world';
$bubbleable_metadata
->addCacheContexts([
'custom_context',
]);
$bubbleable_metadata
->addCacheTags([
'node:1',
]);
$bubbleable_metadata
->setCacheMaxAge(10);
});
$node = $this
->prophesize('Drupal\\node\\NodeInterface');
$node
->getCacheContexts()
->willReturn([]);
$node
->getCacheTags()
->willReturn([]);
$node
->getCacheMaxAge()
->willReturn(14);
$node = $node
->reveal();
$bubbleable_metadata = new BubbleableMetadata();
$bubbleable_metadata
->setCacheContexts([
'current_user',
]);
$bubbleable_metadata
->setCacheMaxAge(12);
$result = $this->token
->replace('[node:title]', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertEquals('hello world', $result);
$this
->assertEquals([
'node:1',
], $bubbleable_metadata
->getCacheTags());
$this
->assertEquals([
'current_user',
'custom_context',
], $bubbleable_metadata
->getCacheContexts());
$this
->assertEquals(10, $bubbleable_metadata
->getCacheMaxAge());
}
public function testResetInfo() {
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with([
'token_info',
]);
$this->token
->resetInfo();
}
public function testReplaceEscaping($string, array $tokens, $expected) {
$this->moduleHandler
->expects($this
->any())
->method('invokeAll')
->willReturnCallback(function ($type, $args) {
return $args[2]['tokens'];
});
$result = $this->token
->replace($string, [
'tokens' => $tokens,
]);
$this
->assertIsString($result);
$this
->assertEquals($expected, $result);
}
public static function providerTestReplaceEscaping() {
$data = [];
$data['no-tokens'] = [
'muh',
[],
'muh',
];
$data['html-in-string'] = [
'<h1>Giraffe</h1>',
[],
'<h1>Giraffe</h1>',
];
$data['html-in-string-quote'] = [
'<h1>Giraffe"</h1>',
[],
'<h1>Giraffe"</h1>',
];
$data['simple-placeholder-with-plain-text'] = [
'<h1>[token:meh]</h1>',
[
'[token:meh]' => 'Giraffe"',
],
'<h1>' . Html::escape('Giraffe"') . '</h1>',
];
$data['simple-placeholder-with-safe-html'] = [
'<h1>[token:meh]</h1>',
[
'[token:meh]' => Markup::create('<em>Emphasized</em>'),
],
'<h1><em>Emphasized</em></h1>',
];
return $data;
}
public function testReplacePlain() {
$this
->setupSiteTokens();
$base = 'Wow, great "[site:name]" has a slogan "[site:slogan]"';
$plain = $this->token
->replacePlain($base);
$this
->assertEquals($plain, 'Wow, great "Your <best> buys" has a slogan "We are best"');
}
public function testScan() {
$text = 'First a [valid:simple], but dummy token, and a dummy [valid:token with: spaces].';
$text .= 'Then a [not valid:token].';
$text .= 'Then an [:empty token type].';
$text .= 'Then an [empty token:].';
$text .= 'Then a totally empty token: [:].';
$text .= 'Last an existing token: [node:author:name].';
$token_wannabes = $this->token
->scan($text);
$this
->assertTrue(isset($token_wannabes['valid']['simple']), 'A simple valid token has been matched.');
$this
->assertTrue(isset($token_wannabes['valid']['token with: spaces']), 'A valid token with space characters in the token name has been matched.');
$this
->assertFalse(isset($token_wannabes['not valid']), 'An invalid token with spaces in the token type has not been matched.');
$this
->assertFalse(isset($token_wannabes['empty token']), 'An empty token has not been matched.');
$this
->assertFalse(isset($token_wannabes['']['empty token type']), 'An empty token type has not been matched.');
$this
->assertFalse(isset($token_wannabes['']['']), 'An empty token and type has not been matched.');
$this
->assertTrue(isset($token_wannabes['node']), 'An existing valid token has been matched.');
}
public function testScanDeprecation() {
$this
->expectDeprecation('Calling Drupal\\Core\\Utility\\Token::scan() with a $text parameter of type other than string is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3334317');
$this
->assertSame([], $this->token
->scan(NULL));
}
protected function setupSiteTokens() {
$tokens = [
'[site:name]' => 'Your <best> buys',
'[site:slogan]' => Markup::Create('We are <b>best</b>'),
];
$this->moduleHandler
->expects($this
->any())
->method('invokeAll')
->willReturn($tokens);
}
}