3 namespace Drupal\token\Tests\Tree;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\token\Tests\TokenTestBase;
9 * Tests token tree page.
13 class TreeTest extends TokenTestBase {
15 use TokenTreeTestTrait;
18 * @var \Drupal\Core\Session\AccountInterface
27 public static $modules = ['node'];
29 public function setUp() {
32 $this->account = $this->drupalCreateUser(['administer account settings']);
33 $this->drupalLogin($this->account);
37 * Test various tokens that are possible on the site.
39 public function testAllTokens() {
40 $this->drupalGet($this->getTokenTreeUrl(['token_types' => 'all']));
42 $this->assertTokenGroup('Current date');
43 $this->assertTokenGroup('Site information');
45 $this->assertTokenInTree('[current-date:html_date]', 'current-date');
46 $this->assertTokenInTree('[current-date:html_week]', 'current-date');
47 $this->assertTokenInTree('[date:html_date]', 'date');
48 $this->assertTokenInTree('[date:html_week]', 'date');
50 $this->assertTokenInTree('[current-user:account-name]', 'current-user');
51 $this->assertTokenInTree('[user:account-name]', 'user');
53 $this->assertTokenInTree('[current-page:url:unaliased]', 'current-page--url');
54 $this->assertTokenInTree('[current-page:url:unaliased:args]', 'current-page--url--unaliased');
55 $this->assertTokenInTree('[user:original:account-name]', 'user--original');
59 * Test various tokens that are possible on the site.
61 public function testGlobalTokens() {
62 $this->drupalGet($this->getTokenTreeUrl());
64 $this->assertTokenGroup('Current date');
65 $this->assertTokenGroup('Site information');
67 // Assert that non-global tokens are not listed.
68 $this->assertTokenNotInTree('[user:account-name]', 'user');
69 $this->assertTokenNotInTree('[user:original:account-name]', 'user--original');
71 // Assert some of the global tokens, just to be sure.
72 $this->assertTokenInTree('[current-date:html_date]', 'current-date');
73 $this->assertTokenInTree('[current-date:html_week]', 'current-date');
75 $this->assertTokenInTree('[current-user:account-name]', 'current-user');
77 $this->assertTokenInTree('[current-page:url:unaliased]', 'current-page--url');
78 $this->assertTokenInTree('[current-page:url:unaliased:args]', 'current-page--url--unaliased');
82 * Tests if the token browser displays the user tokens.
84 public function testUserTokens() {
85 $this->drupalGet($this->getTokenTreeUrl(['token_types' => ['user']]));
87 $this->assertTokenGroup('Users');
89 $this->assertTokenInTree('[user:account-name]', 'user');
90 $this->assertTokenInTree('[user:original:account-name]', 'user--original');
92 // Assert some of the restricted tokens to ensure they are not shown.
93 $this->assertTokenNotInTree('[user:one-time-login-url]', 'user');
94 $this->assertTokenNotInTree('[user:original:cancel-url]', 'user--original');
96 // Request with show_restricted set to TRUE to show restricted tokens and
98 $this->drupalGet($this->getTokenTreeUrl(['token_types' => ['user'], 'show_restricted' => TRUE]));
99 $this->assertEqual('MISS', $this->drupalGetHeader('x-drupal-dynamic-cache'), 'Cache was not hit');
100 $this->assertTokenInTree('[user:one-time-login-url]', 'user');
101 $this->assertTokenInTree('[user:original:cancel-url]', 'user--original');
105 * Tests if the token browser displays the node tokens.
107 public function testNodeTokens() {
108 $this->drupalGet($this->getTokenTreeUrl(['token_types' => ['node']]));
110 $this->assertTokenGroup('Nodes');
112 $this->assertTokenInTree('[node:body]', 'node');
113 $this->assertTokenInTree('[node:author:original:account-name]', 'node--author--original');
117 * Get the URL for the token tree based on the specified options.
119 * The token tree route's URL requires CSRF and cannot be generated in the
120 * test code. The CSRF token generated using the test runner's session is
121 * different from the session inside the test environment. This is why the
122 * link has to be generated inside the environment.
124 * This function calls a page in token_module_test module which generates the
125 * link and the token. This then replaces the options query parameter with the
128 * The page also uses a title callback to set title to a render array, which
129 * allows us to test if [current-page:title] works properly.
131 * @param array $options
132 * The options for the token tree browser.
135 * The complete URL of the token tree browser with the CSRF token.
137 protected function getTokenTreeUrl($options = []) {
138 $this->drupalGet('token_module_test/browse');
139 $this->assertTitle('Available Tokens | Drupal');
140 $links = $this->xpath('//a[contains(@href, :href)]/@href', [':href' => 'token/tree']);
141 $link = $this->getAbsoluteUrl((string) current($links));
142 if (!empty($options)) {
143 $options = Json::encode($options);
144 $link = str_replace('options=%5B%5D', 'options=' . urlencode($options), $link);