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

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 integration.
63 */
64 public function testCacheIntegration() {
65 $this->installExtraModules(['dynamic_page_cache']);
66 $this->drupalLogin($this->adminUser);
67 $this->drupalGet('test-page');
68 $this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
69 $this->drupalGet('test-page');
70 $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
71 }
72
73 /**
74 * Tests toolbar cache contexts.
75 */
76 public function testToolbarCacheContextsCaller() {
77 // Test with default combination and permission to see toolbar.
78 $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found for default combination and permission to see toolbar.');
79
80 // Test without user toolbar tab. User module is a required module so we have to
81 // manually remove the user toolbar tab.
82 $this->installExtraModules(['toolbar_disable_user_toolbar']);
83 $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found without user toolbar tab.');
84
85 // Test with the toolbar and contextual enabled.
86 $this->installExtraModules(['contextual']);
87 $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access contextual links']));
88 $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with contextual module enabled.');
89 \Drupal::service('module_installer')->uninstall(['contextual']);
90
91 // Test with the tour module enabled.
92 $this->installExtraModules(['tour']);
93 $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access tour']));
94 $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with tour module enabled.');
95 \Drupal::service('module_installer')->uninstall(['tour']);
96
97 // Test with shortcut module enabled.
98 $this->installExtraModules(['shortcut']);
99 $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access shortcuts', 'administer shortcuts']));
100 $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found with shortcut module enabled.');
101 }
102
103 /**
104 * Tests that cache contexts are applied for both users.
105 *
106 * @param string[] $cache_contexts
107 * Expected cache contexts for both users.
108 * @param string $message
109 * (optional) A verbose message to output.
110 *
111 * @return
112 * TRUE if the assertion succeeded, FALSE otherwise.
113 */
114 protected function assertToolbarCacheContexts(array $cache_contexts, $message = NULL) {
115 // Default cache contexts that should exist on all test cases.
116 $default_cache_contexts = [
117 'languages:language_interface',
118 'theme',
119 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
120 ];
121 $cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
122
123 // Assert contexts for user1 which has only default permissions.
124 $this->drupalLogin($this->adminUser);
125 $this->drupalGet('test-page');
126 $return = $this->assertCacheContexts($cache_contexts);
127 $this->drupalLogout();
128
129 // Assert contexts for user2 which has some additional permissions.
130 $this->drupalLogin($this->adminUser2);
131 $this->drupalGet('test-page');
132 $return = $return && $this->assertCacheContexts($cache_contexts);
133
134 if ($return) {
135 $this->pass($message);
136 }
137 else {
138 $this->fail($message);
139 }
140 return $return;
141 }
142
143 /**
144 * Installs a given list of modules and rebuilds the cache.
145 *
146 * @param string[] $module_list
147 * An array of module names.
148 */
149 protected function installExtraModules(array $module_list) {
150 \Drupal::service('module_installer')->install($module_list);
151
152 // Installing modules updates the container and needs a router rebuild.
153 $this->container = \Drupal::getContainer();
154 $this->container->get('router.builder')->rebuildIfNeeded();
155 }
156
157 }