3 namespace Drupal\toolbar\Tests;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
12 * Tests the cache contexts for toolbar.
16 class ToolbarCacheContextsTest extends WebTestBase {
18 use AssertPageCacheContextsAndTagsTrait;
25 public static $modules = ['toolbar', 'test_page_test'];
28 * An authenticated user to use for testing.
30 * @var \Drupal\user\UserInterface
35 * An authenticated user to use for testing.
37 * @var \Drupal\user\UserInterface
39 protected $adminUser2;
42 * A list of default permissions for test users.
48 'access administration pages',
49 'administer site configuration',
55 protected function setUp() {
58 $this->adminUser = $this->drupalCreateUser($this->perms);
59 $this->adminUser2 = $this->drupalCreateUser($this->perms);
63 * Tests toolbar cache contexts.
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.');
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.');
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']);
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']);
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.');
93 * Tests that cache contexts are applied for both users.
95 * @param string[] $cache_contexts
96 * Expected cache contexts for both users.
97 * @param string $message
98 * (optional) A verbose message to output.
101 * TRUE if the assertion succeeded, FALSE otherwise.
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',
108 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
110 $cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
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();
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);
124 $this->pass($message);
127 $this->fail($message);
133 * Installs a given list of modules and rebuilds the cache.
135 * @param string[] $module_list
136 * An array of module names.
138 protected function installExtraModules(array $module_list) {
139 \Drupal::service('module_installer')->install($module_list);
141 // Installing modules updates the container and needs a router rebuild.
142 $this->container = \Drupal::getContainer();
143 $this->container->get('router.builder')->rebuildIfNeeded();