Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Cache / CacheContextOptimizationTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Cache;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\simpletest\UserCreationTrait;
7 use Drupal\user\Entity\Role;
8
9 /**
10  * Tests the cache context optimization.
11  *
12  * @group Render
13  */
14 class CacheContextOptimizationTest extends KernelTestBase {
15
16   use UserCreationTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var string[]
22    */
23   public static $modules = ['user', 'system'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30     $this->installEntitySchema('user');
31     $this->installConfig(['user']);
32     $this->installSchema('system', ['sequences']);
33   }
34
35   /**
36    * Ensures that 'user.permissions' cache context is able to define cache tags.
37    */
38   public function testUserPermissionCacheContextOptimization() {
39     $user1 = $this->createUser();
40     $this->assertEqual($user1->id(), 1);
41
42     $authenticated_user = $this->createUser(['administer permissions']);
43     $role = $authenticated_user->getRoles()[1];
44
45     $test_element = [
46       '#cache' => [
47         'keys' => ['test'],
48         'contexts' => ['user', 'user.permissions'],
49       ],
50     ];
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');
56
57     // Verify that the render caching is working so that other tests can be
58     // trusted.
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');
63
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
68     // optimized away.
69     Role::load($role)
70       ->revokePermission('administer permissions')
71       ->save();
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');
76   }
77
78   /**
79    * Ensures that 'user.roles' still works when it is optimized away.
80    */
81   public function testUserRolesCacheContextOptimization() {
82     $root_user = $this->createUser();
83     $this->assertEqual($root_user->id(), 1);
84
85     $authenticated_user = $this->createUser(['administer permissions']);
86     $role = $authenticated_user->getRoles()[1];
87
88     $test_element = [
89       '#cache' => [
90         'keys' => ['test'],
91         'contexts' => ['user', 'user.roles'],
92       ],
93     ];
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');
99
100     // Verify that the render caching is working so that other tests can be
101     // trusted.
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');
106
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');
117   }
118
119 }