3 namespace Drupal\Tests\toolbar\Functional;
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;
11 * Tests the cache contexts for toolbar.
15 class ToolbarCacheContextsTest extends BrowserTestBase {
17 use AssertPageCacheContextsAndTagsTrait;
24 public static $modules = ['toolbar', 'test_page_test'];
27 * An authenticated user to use for testing.
29 * @var \Drupal\user\UserInterface
34 * An authenticated user to use for testing.
36 * @var \Drupal\user\UserInterface
38 protected $adminUser2;
41 * A list of default permissions for test users.
47 'access administration pages',
48 'administer site configuration',
54 protected function setUp() {
57 $this->adminUser = $this->drupalCreateUser($this->perms);
58 $this->adminUser2 = $this->drupalCreateUser($this->perms);
62 * Tests toolbar cache contexts.
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.');
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.');
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']);
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']);
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.');
92 * Tests that cache contexts are applied for both users.
94 * @param string[] $cache_contexts
95 * Expected cache contexts for both users.
96 * @param string $message
97 * (optional) A verbose message to output.
100 * TRUE if the assertion succeeded, FALSE otherwise.
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',
107 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
109 $cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
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();
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);
123 $this->pass($message);
126 $this->fail($message);
132 * Installs a given list of modules and rebuilds the cache.
134 * @param string[] $module_list
135 * An array of module names.
137 protected function installExtraModules(array $module_list) {
138 \Drupal::service('module_installer')->install($module_list);
140 // Installing modules updates the container and needs a router rebuild.
141 $this->container = \Drupal::getContainer();
142 $this->container->get('router.builder')->rebuildIfNeeded();