3 namespace Drupal\KernelTests\Core\Cache;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\simpletest\UserCreationTrait;
7 use Drupal\user\Entity\Role;
10 * Tests the cache context optimization.
14 class CacheContextOptimizationTest extends KernelTestBase {
16 use UserCreationTrait;
23 public static $modules = ['user', 'system'];
28 protected function setUp() {
30 $this->installEntitySchema('user');
31 $this->installConfig(['user']);
32 $this->installSchema('system', ['sequences']);
36 * Ensures that 'user.permissions' cache context is able to define cache tags.
38 public function testUserPermissionCacheContextOptimization() {
39 $user1 = $this->createUser();
40 $this->assertEqual($user1->id(), 1);
42 $authenticated_user = $this->createUser(['administer permissions']);
43 $role = $authenticated_user->getRoles()[1];
48 'contexts' => ['user', 'user.permissions'],
51 \Drupal::service('account_switcher')->switchTo($authenticated_user);
52 $element = $test_element;
53 $element['#markup'] = 'content for authenticated users';
54 $output = \Drupal::service('renderer')->renderRoot($element);
55 $this->assertEqual($output, 'content for authenticated users');
57 // Verify that the render caching is working so that other tests can be
59 $element = $test_element;
60 $element['#markup'] = 'this should not be visible';
61 $output = \Drupal::service('renderer')->renderRoot($element);
62 $this->assertEqual($output, 'content for authenticated users');
64 // Even though the cache contexts have been optimized to only include 'user'
65 // cache context, the element should have been changed because
66 // 'user.permissions' cache context defined a cache tags for permission
67 // changes, which should have bubbled up for the element when it was
70 ->revokePermission('administer permissions')
72 $element = $test_element;
73 $element['#markup'] = 'this should be visible';
74 $output = \Drupal::service('renderer')->renderRoot($element);
75 $this->assertEqual($output, 'this should be visible');
79 * Ensures that 'user.roles' still works when it is optimized away.
81 public function testUserRolesCacheContextOptimization() {
82 $root_user = $this->createUser();
83 $this->assertEqual($root_user->id(), 1);
85 $authenticated_user = $this->createUser(['administer permissions']);
86 $role = $authenticated_user->getRoles()[1];
91 'contexts' => ['user', 'user.roles'],
94 \Drupal::service('account_switcher')->switchTo($authenticated_user);
95 $element = $test_element;
96 $element['#markup'] = 'content for authenticated users';
97 $output = \Drupal::service('renderer')->renderRoot($element);
98 $this->assertEqual($output, 'content for authenticated users');
100 // Verify that the render caching is working so that other tests can be
102 $element = $test_element;
103 $element['#markup'] = 'this should not be visible';
104 $output = \Drupal::service('renderer')->renderRoot($element);
105 $this->assertEqual($output, 'content for authenticated users');
107 // Even though the cache contexts have been optimized to only include 'user'
108 // cache context, the element should have been changed because 'user.roles'
109 // cache context defined a cache tag for user entity changes, which should
110 // have bubbled up for the element when it was optimized away.
111 $authenticated_user->removeRole($role);
112 $authenticated_user->save();
113 $element = $test_element;
114 $element['#markup'] = 'this should be visible';
115 $output = \Drupal::service('renderer')->renderRoot($element);
116 $this->assertEqual($output, 'this should be visible');