View source
<?php
namespace Drupal\Tests\token\Functional\Tree;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\token\Functional\TokenTestBase;
class TreeTest extends TokenTestBase {
use TokenTreeTestTrait;
protected $account;
public static $modules = [
'node',
];
public function setUp() {
parent::setUp();
$this->account = $this
->drupalCreateUser([
'administer account settings',
]);
$this
->drupalLogin($this->account);
}
public function testAllTokens() {
$this
->drupalGet($this
->getTokenTreeUrl([
'token_types' => 'all',
]));
$this
->assertTokenGroup('Current date');
$this
->assertTokenGroup('Site information');
$this
->assertTokenInTree('[current-date:html_date]', 'current-date');
$this
->assertTokenInTree('[current-date:html_week]', 'current-date');
$this
->assertTokenInTree('[date:html_date]', 'date');
$this
->assertTokenInTree('[date:html_week]', 'date');
$this
->assertTokenInTree('[current-user:account-name]', 'current-user');
$this
->assertTokenInTree('[user:account-name]', 'user');
$this
->assertTokenInTree('[current-page:url:unaliased]', 'current-page--url');
$this
->assertTokenInTree('[current-page:url:unaliased:args]', 'current-page--url--unaliased');
$this
->assertTokenInTree('[user:original:account-name]', 'user--original');
}
public function testGlobalTokens() {
$this
->drupalGet($this
->getTokenTreeUrl());
$this
->assertTokenGroup('Current date');
$this
->assertTokenGroup('Site information');
$this
->assertTokenNotInTree('[user:account-name]', 'user');
$this
->assertTokenNotInTree('[user:original:account-name]', 'user--original');
$this
->assertTokenInTree('[current-date:html_date]', 'current-date');
$this
->assertTokenInTree('[current-date:html_week]', 'current-date');
$this
->assertTokenInTree('[current-user:account-name]', 'current-user');
$this
->assertTokenInTree('[current-page:url:unaliased]', 'current-page--url');
$this
->assertTokenInTree('[current-page:url:unaliased:args]', 'current-page--url--unaliased');
}
public function testUserTokens() {
$this
->drupalGet($this
->getTokenTreeUrl([
'token_types' => [
'user',
],
]));
$this
->assertTokenGroup('Users');
$this
->assertTokenInTree('[user:account-name]', 'user');
$this
->assertTokenInTree('[user:original:account-name]', 'user--original');
$this
->assertTokenNotInTree('[user:one-time-login-url]', 'user');
$this
->assertTokenNotInTree('[user:original:cancel-url]', 'user--original');
$this
->drupalGet($this
->getTokenTreeUrl([
'token_types' => [
'user',
],
'show_restricted' => TRUE,
]));
$this
->assertEquals('MISS', $this
->drupalGetHeader('x-drupal-dynamic-cache'), 'Cache was not hit');
$this
->assertTokenInTree('[user:one-time-login-url]', 'user');
$this
->assertTokenInTree('[user:original:cancel-url]', 'user--original');
}
public function testNodeTokens() {
$this
->drupalGet($this
->getTokenTreeUrl([
'token_types' => [
'node',
],
]));
$this
->assertTokenGroup('Nodes');
$this
->assertTokenInTree('[node:body]', 'node');
$this
->assertTokenInTree('[node:author:original:account-name]', 'node--author--original');
}
protected function getTokenTreeUrl($options = []) {
$this
->drupalGet('token_module_test/browse');
$this
->assertSession()
->titleEquals('Available Tokens | Drupal');
$links = $this
->xpath('//a[contains(@href, :href)]/@href', [
':href' => 'token/tree',
]);
$link = $this
->getAbsoluteUrl(current($links)
->getText());
if (!empty($options)) {
$options = Json::encode($options);
$link = str_replace('options=%5B%5D', 'options=' . urlencode($options), $link);
}
return $link;
}
}