comparison core/modules/toolbar/tests/src/Functional/ToolbarCacheContextsTest.php @ 0:4c8ae668cc8c

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