only in patch2: unchanged: --- /dev/null +++ b/core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php @@ -0,0 +1,151 @@ +adminUser = $this->drupalCreateUser($perms); + } + + /** + * Tests toolbar cache contexts. + */ + public function testToolbarCacheContextsCaller() { + // Test with the default combination. + $anonymous_cache_contexts = [ + 'languages:language_interface', + 'theme', + ]; + $authenticated_cache_contexts = [ + 'languages:language_interface', + 'theme', + 'user', + ]; + $this->testToolbarCacheContexts($anonymous_cache_contexts, $authenticated_cache_contexts); + + // Test without user menu. User module is required so we have to + // manually remove the user toolbar. + \Drupal::service('module_installer')->install(['toolbar_disable_user_toolbar']); + $anonymous_cache_contexts = [ + 'languages:language_interface', + 'theme', + ]; + $authenticated_cache_contexts = [ + 'languages:language_interface', + 'theme', + 'user.permissions', + ]; + $this->testToolbarCacheContexts($anonymous_cache_contexts, $authenticated_cache_contexts); + + // Test with the toolbar and contextual enabled. + \Drupal::service('module_installer')->install(['contextual']); + + $anonymous_cache_contexts = [ + 'languages:language_interface', + 'theme', + ]; + $authenticated_cache_contexts = [ + 'languages:language_interface', + 'theme', + 'user.permissions', + ]; + $this->testToolbarCacheContexts($anonymous_cache_contexts, $authenticated_cache_contexts); + \Drupal::service('module_installer')->uninstall(['contextual']); + + // Test with the tour module enabled. + \Drupal::service('module_installer')->install(['tour']); + $this->rebuildContainer(); + + $anonymous_cache_contexts = [ + 'languages:language_interface', + 'theme', + ]; + $authenticated_cache_contexts = [ + 'languages:language_interface', + 'theme', + 'user.permissions', + ]; + $this->testToolbarCacheContexts($anonymous_cache_contexts, $authenticated_cache_contexts); + \Drupal::service('module_installer')->uninstall(['tour']); + + // Test with shortcut module enabled. + \Drupal::service('module_installer')->install(['shortcut']); + + $anonymous_cache_contexts = [ + 'languages:language_interface', + 'theme', + ]; + $authenticated_cache_contexts = [ + 'languages:language_interface', + 'theme', + 'user', + ]; + $this->testToolbarCacheContexts($anonymous_cache_contexts, $authenticated_cache_contexts); + } + + /** + * Tests toolbar cache contexts with contextual toolbar. + * + * @param array $anonymous_cache_contexts + * Expected cache contexts for anonymous user. + * @param array $authenticated_cache_contexts + * Expected cache contexts for authenticated user. + */ + private function testToolbarCacheContexts(array $anonymous_cache_contexts, array $authenticated_cache_contexts) { + // All the caches has to be cleared since modules have changed. + drupal_flush_all_caches(); + // Make sure that user without permissions to see toolbar doesn't have + // user cache context. + $this->drupalGet('test-page'); + + // Assert that the toolbar is not present when user doesn't have + // permissions. + $this->assertNoRaw('id="toolbar-administration"'); + + $this->assertCacheContexts($anonymous_cache_contexts); + + // User with a permission to see toolbar should have user context. + $this->drupalLogin($this->adminUser); + $this->drupalGet('test-page'); + + // Assert that the toolbar is present in the HTML. + $this->assertRaw('id="toolbar-administration"'); + + $this->assertCacheContexts($authenticated_cache_contexts); + $this->drupalLogout(); + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/modules/toolbar/tests/modules/toolbar_disable_user_toolbar/toolbar_disable_user_toolbar.info.yml @@ -0,0 +1,6 @@ +name: 'Disbale user toolbar' +type: module +description: 'Support module for testing toolbar without user toolbar' +package: Testing +version: VERSION +core: 8.x only in patch2: unchanged: --- /dev/null +++ b/core/modules/toolbar/tests/modules/toolbar_disable_user_toolbar/toolbar_disable_user_toolbar.module @@ -0,0 +1,8 @@ +