Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / src / Tests / Tree / TreeTest.php
1 <?php
2
3 namespace Drupal\token\Tests\Tree;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\token\Tests\TokenTestBase;
7
8 /**
9  * Tests token tree page.
10  *
11  * @group token
12  */
13 class TreeTest extends TokenTestBase {
14
15   use TokenTreeTestTrait;
16
17   /**
18    * @var \Drupal\Core\Session\AccountInterface
19    */
20   protected $account;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['node'];
28
29   public function setUp() {
30     parent::setUp();
31
32     $this->account = $this->drupalCreateUser(['administer account settings']);
33     $this->drupalLogin($this->account);
34   }
35
36   /**
37    * Test various tokens that are possible on the site.
38    */
39   public function testAllTokens() {
40     $this->drupalGet($this->getTokenTreeUrl(['token_types' => 'all']));
41
42     $this->assertTokenGroup('Current date');
43     $this->assertTokenGroup('Site information');
44
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');
49
50     $this->assertTokenInTree('[current-user:account-name]', 'current-user');
51     $this->assertTokenInTree('[user:account-name]', 'user');
52
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');
56   }
57
58   /**
59    * Test various tokens that are possible on the site.
60    */
61   public function testGlobalTokens() {
62     $this->drupalGet($this->getTokenTreeUrl());
63
64     $this->assertTokenGroup('Current date');
65     $this->assertTokenGroup('Site information');
66
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');
70
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');
74
75     $this->assertTokenInTree('[current-user:account-name]', 'current-user');
76
77     $this->assertTokenInTree('[current-page:url:unaliased]', 'current-page--url');
78     $this->assertTokenInTree('[current-page:url:unaliased:args]', 'current-page--url--unaliased');
79   }
80
81   /**
82    * Tests if the token browser displays the user tokens.
83    */
84   public function testUserTokens() {
85     $this->drupalGet($this->getTokenTreeUrl(['token_types' => ['user']]));
86
87     $this->assertTokenGroup('Users');
88
89     $this->assertTokenInTree('[user:account-name]', 'user');
90     $this->assertTokenInTree('[user:original:account-name]', 'user--original');
91
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');
95
96     // Request with show_restricted set to TRUE to show restricted tokens and
97     // check for them.
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');
102   }
103
104   /**
105    * Tests if the token browser displays the node tokens.
106    */
107   public function testNodeTokens() {
108     $this->drupalGet($this->getTokenTreeUrl(['token_types' => ['node']]));
109
110     $this->assertTokenGroup('Nodes');
111
112     $this->assertTokenInTree('[node:body]', 'node');
113     $this->assertTokenInTree('[node:author:original:account-name]', 'node--author--original');
114   }
115
116   /**
117    * Get the URL for the token tree based on the specified options.
118    *
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.
123    *
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
126    * specified options.
127    *
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.
130    *
131    * @param array $options
132    *   The options for the token tree browser.
133    *
134    * @return string
135    *   The complete URL of the token tree browser with the CSRF token.
136    */
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);
145     }
146     return $link;
147   }
148 }