diff -u b/core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php b/core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php --- b/core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php +++ b/core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php @@ -68,29 +68,29 @@ */ public function testToolbarCacheContextsCaller() { // Test with default combination and permission to see toolbar. - $this->testToolbarCacheContexts(['user'], 'Expected cache contexts found for default combination and permission to see toolbar.'); + $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found for default combination and permission to see toolbar.'); // Test without user toolbar tab. User module is a required module so we have to // manually remove the user toolbar tab. - \Drupal::service('module_installer')->install(['toolbar_disable_user_toolbar']); - $this->testToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found without user toolbar tab.'); + $this->installModules(['toolbar_disable_user_toolbar']); + $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found without user toolbar tab.'); // Test with the toolbar and contextual enabled. - \Drupal::service('module_installer')->install(['contextual']); + $this->installModules(['contextual']); $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access contextual links'])); - $this->testToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with contextual module enabled.'); + $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with contextual module enabled.'); \Drupal::service('module_installer')->uninstall(['contextual']); // Test with the tour module enabled. - \Drupal::service('module_installer')->install(['tour']); + $this->installModules(['tour']); $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access tour'])); - $this->testToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with tour module enabled.'); + $this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with tour module enabled.'); \Drupal::service('module_installer')->uninstall(['tour']); // Test with shortcut module enabled. - \Drupal::service('module_installer')->install(['shortcut']); + $this->installModules(['shortcut']); $this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access shortcuts', 'administer shortcuts'])); - $this->testToolbarCacheContexts(['user'], 'Expected cache contexts found with shortcut module enabled.'); + $this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found with shortcut module enabled.'); } /** @@ -101,10 +101,7 @@ * @param string $message * (optional) A verbose message to output. */ - private function testToolbarCacheContexts(array $cache_contexts, $message = NULL) { - // Caches have to be rebuilt since modules might have added some routes. - drupal_flush_all_caches(); - + protected function assertToolbarCacheContexts(array $cache_contexts, $message = NULL) { // Default cache contexts that should exist on all test cases. $default_cache_contexts = [ 'languages:language_interface', @@ -126,2 +123,16 @@ + /** + * Installs a given list of modules and rebuilds the cache. + * + * @param string[] $module_list + * An array of module names. + */ + protected function installModules(array $module_list) { + \Drupal::service('module_installer')->install($module_list); + + // Caches have to be rebuilt since modules might have added some routes. + drupal_flush_all_caches(); + } + + }