View source
<?php
namespace Drupal\Tests\toolbar\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\BrowserTestBase;
class ToolbarCacheContextsTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'toolbar',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $adminUser2;
protected $perms = [
'access toolbar',
'access administration pages',
'administer site configuration',
];
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->perms);
$this->adminUser2 = $this
->drupalCreateUser($this->perms);
}
public function testCacheIntegration() {
$this
->installExtraModules([
'csrf_test',
'dynamic_page_cache',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this
->assertCacheContexts([
'session',
'user',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
], 'Expected cache contexts found with CSRF token link.');
$this
->drupalGet('test-page');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this
->assertCacheContexts([
'session',
'user',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
], 'Expected cache contexts found with CSRF token link.');
}
public function testToolbarCacheContextsCaller() {
$this
->assertToolbarCacheContexts([
'user',
], 'Expected cache contexts found for default combination and permission to see toolbar.');
$this
->installExtraModules([
'toolbar_disable_user_toolbar',
]);
$this
->assertToolbarCacheContexts([
'user.permissions',
], 'Expected cache contexts found without user toolbar tab.');
$this
->installExtraModules([
'contextual',
]);
$this->adminUser2 = $this
->drupalCreateUser(array_merge($this->perms, [
'access contextual links',
]));
$this
->assertToolbarCacheContexts([
'user.permissions',
], 'Expected cache contexts found with contextual module enabled.');
\Drupal::service('module_installer')
->uninstall([
'contextual',
]);
$this
->installExtraModules([
'tour',
]);
$this->adminUser2 = $this
->drupalCreateUser(array_merge($this->perms, [
'access tour',
]));
$this
->assertToolbarCacheContexts([
'user.permissions',
], 'Expected cache contexts found with tour module enabled.');
\Drupal::service('module_installer')
->uninstall([
'tour',
]);
}
protected function assertToolbarCacheContexts(array $cache_contexts, string $message = NULL) : void {
$default_cache_contexts = [
'languages:language_interface',
'theme',
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
];
$cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertCacheContexts($cache_contexts, $message);
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('test-page');
$this
->assertCacheContexts($cache_contexts, $message);
}
protected function installExtraModules(array $module_list) {
\Drupal::service('module_installer')
->install($module_list);
$this->container = \Drupal::getContainer();
$this->container
->get('router.builder')
->rebuildIfNeeded();
}
}