Version 1
[yaffs-website] / web / core / modules / toolbar / src / Tests / ToolbarCacheContextsTest.php
1 <?php
2
3 namespace Drupal\toolbar\Tests;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
9
10
11 /**
12  * Tests the cache contexts for toolbar.
13  *
14  * @group toolbar
15  */
16 class ToolbarCacheContextsTest extends WebTestBase {
17
18   use AssertPageCacheContextsAndTagsTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['toolbar', 'test_page_test'];
26
27   /**
28    * An authenticated user to use for testing.
29    *
30    * @var \Drupal\user\UserInterface
31    */
32   protected $adminUser;
33
34   /**
35    * An authenticated user to use for testing.
36    *
37    * @var \Drupal\user\UserInterface
38    */
39   protected $adminUser2;
40
41   /**
42    * A list of default permissions for test users.
43    *
44    * @var array
45    */
46   protected $perms = [
47     'access toolbar',
48     'access administration pages',
49     'administer site configuration',
50   ];
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function setUp() {
56     parent::setUp();
57
58     $this->adminUser = $this->drupalCreateUser($this->perms);
59     $this->adminUser2 = $this->drupalCreateUser($this->perms);
60   }
61
62   /**
63    * Tests toolbar cache contexts.
64    */
65   public function testToolbarCacheContextsCaller() {
66     // Test with default combination and permission to see toolbar.
67     $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found for default combination and permission to see toolbar.');
68
69     // Test without user toolbar tab. User module is a required module so we have to
70     // manually remove the user toolbar tab.
71     $this->installExtraModules(['toolbar_disable_user_toolbar']);
72     $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found without user toolbar tab.');
73
74     // Test with the toolbar and contextual enabled.
75     $this->installExtraModules(['contextual']);
76     $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access contextual links']));
77     $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with contextual module enabled.');
78     \Drupal::service('module_installer')->uninstall(['contextual']);
79
80     // Test with the tour module enabled.
81     $this->installExtraModules(['tour']);
82     $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access tour']));
83     $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with tour module enabled.');
84     \Drupal::service('module_installer')->uninstall(['tour']);
85
86     // Test with shortcut module enabled.
87     $this->installExtraModules(['shortcut']);
88     $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access shortcuts', 'administer shortcuts']));
89     $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found with shortcut module enabled.');
90   }
91
92   /**
93    * Tests that cache contexts are applied for both users.
94    *
95    * @param string[] $cache_contexts
96    *   Expected cache contexts for both users.
97    * @param string $message
98    *   (optional) A verbose message to output.
99    *
100    * @return
101    *   TRUE if the assertion succeeded, FALSE otherwise.
102    */
103   protected function assertToolbarCacheContexts(array $cache_contexts, $message = NULL) {
104     // Default cache contexts that should exist on all test cases.
105     $default_cache_contexts = [
106       'languages:language_interface',
107       'theme',
108       'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
109     ];
110     $cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
111
112     // Assert contexts for user1 which has only default permissions.
113     $this->drupalLogin($this->adminUser);
114     $this->drupalGet('test-page');
115     $return = $this->assertCacheContexts($cache_contexts);
116     $this->drupalLogout();
117
118     // Assert contexts for user2 which has some additional permissions.
119     $this->drupalLogin($this->adminUser2);
120     $this->drupalGet('test-page');
121     $return = $return && $this->assertCacheContexts($cache_contexts);
122
123     if ($return) {
124       $this->pass($message);
125     }
126     else {
127       $this->fail($message);
128     }
129     return $return;
130   }
131
132   /**
133    * Installs a given list of modules and rebuilds the cache.
134    *
135    * @param string[] $module_list
136    *   An array of module names.
137    */
138   protected function installExtraModules(array $module_list) {
139     \Drupal::service('module_installer')->install($module_list);
140
141     // Installing modules updates the container and needs a router rebuild.
142     $this->container = \Drupal::getContainer();
143     $this->container->get('router.builder')->rebuildIfNeeded();
144   }
145
146 }