function CacheContextOptimizationTest::testUserPermissionCacheContextOptimization
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Cache/CacheContextOptimizationTest.php \Drupal\KernelTests\Core\Cache\CacheContextOptimizationTest::testUserPermissionCacheContextOptimization()
- 10 core/tests/Drupal/KernelTests/Core/Cache/CacheContextOptimizationTest.php \Drupal\KernelTests\Core\Cache\CacheContextOptimizationTest::testUserPermissionCacheContextOptimization()
- 11.x core/tests/Drupal/KernelTests/Core/Cache/CacheContextOptimizationTest.php \Drupal\KernelTests\Core\Cache\CacheContextOptimizationTest::testUserPermissionCacheContextOptimization()
Ensures that 'user.permissions' cache context is able to define cache tags.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Cache/ CacheContextOptimizationTest.php, line 38
Class
- CacheContextOptimizationTest
- Tests the cache context optimization.
Namespace
Drupal\KernelTests\Core\CacheCode
public function testUserPermissionCacheContextOptimization() {
$user1 = $this->createUser();
$this->assertEqual($user1->id(), 1);
$authenticated_user = $this->createUser([
'administer permissions',
]);
$role = $authenticated_user->getRoles()[1];
$test_element = [
'#cache' => [
'keys' => [
'test',
],
'contexts' => [
'user',
'user.permissions',
],
],
];
\Drupal::service('account_switcher')->switchTo($authenticated_user);
$element = $test_element;
$element['#markup'] = 'content for authenticated users';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'content for authenticated users');
// Verify that the render caching is working so that other tests can be
// trusted.
$element = $test_element;
$element['#markup'] = 'this should not be visible';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'content for authenticated users');
// Even though the cache contexts have been optimized to only include 'user'
// cache context, the element should have been changed because
// 'user.permissions' cache context defined a cache tags for permission
// changes, which should have bubbled up for the element when it was
// optimized away.
Role::load($role)->revokePermission('administer permissions')
->save();
$element = $test_element;
$element['#markup'] = 'this should be visible';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'this should be visible');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.