3 namespace Drupal\KernelTests\Core\Render;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\simpletest\UserCreationTrait;
9 * Tests the caching of render items via functional tests.
13 class RenderCacheTest extends KernelTestBase {
15 use UserCreationTrait;
22 public static $modules = ['user', 'system'];
27 protected function setUp() {
29 $this->installEntitySchema('user');
30 $this->installConfig(['user']);
31 $this->installSchema('system', ['sequences']);
35 * Tests that user 1 has a different permission context with the same roles.
37 public function testUser1PermissionContext() {
38 $this->doTestUser1WithContexts(['user.permissions']);
42 * Tests that user 1 has a different roles context with the same roles.
44 public function testUser1RolesContext() {
45 $this->doTestUser1WithContexts(['user.roles']);
49 * Ensures that user 1 has a unique render cache for the given context.
51 * @param string[] $contexts
52 * List of cache contexts to use.
54 protected function doTestUser1WithContexts($contexts) {
55 // Test that user 1 does not share the cache with other users who have the
56 // same roles, even when using a role-based cache context.
57 $user1 = $this->createUser();
58 $this->assertEqual($user1->id(), 1);
59 $first_authenticated_user = $this->createUser();
60 $second_authenticated_user = $this->createUser();
61 $admin_user = $this->createUser([], NULL, TRUE);
63 $this->assertEqual($user1->getRoles(), $first_authenticated_user->getRoles(), 'User 1 has the same roles as an authenticated user.');
64 // Impersonate user 1 and render content that only user 1 should have
66 \Drupal::service('account_switcher')->switchTo($user1);
70 'contexts' => $contexts,
73 $element = $test_element;
74 $element['#markup'] = 'content for user 1';
75 $output = \Drupal::service('renderer')->renderRoot($element);
76 $this->assertEqual($output, 'content for user 1');
78 // Verify the cache is working by rendering the same element but with
79 // different markup passed in; the result should be the same.
80 $element = $test_element;
81 $element['#markup'] = 'should not be used';
82 $output = \Drupal::service('renderer')->renderRoot($element);
83 $this->assertEqual($output, 'content for user 1');
84 \Drupal::service('account_switcher')->switchBack();
86 // Verify that the first authenticated user does not see the same content
88 \Drupal::service('account_switcher')->switchTo($first_authenticated_user);
89 $element = $test_element;
90 $element['#markup'] = 'content for authenticated users';
91 $output = \Drupal::service('renderer')->renderRoot($element);
92 $this->assertEqual($output, 'content for authenticated users');
93 \Drupal::service('account_switcher')->switchBack();
95 // Verify that the second authenticated user shares the cache with the
96 // first authenticated user.
97 \Drupal::service('account_switcher')->switchTo($second_authenticated_user);
98 $element = $test_element;
99 $element['#markup'] = 'should not be used';
100 $output = \Drupal::service('renderer')->renderRoot($element);
101 $this->assertEqual($output, 'content for authenticated users');
102 \Drupal::service('account_switcher')->switchBack();
104 // Verify that the admin user (who has an admin role without explicit
105 // permissions) does not share the same cache.
106 \Drupal::service('account_switcher')->switchTo($admin_user);
107 $element = $test_element;
108 $element['#markup'] = 'content for admin user';
109 $output = \Drupal::service('renderer')->renderRoot($element);
110 $this->assertEqual($output, 'content for admin user');
111 \Drupal::service('account_switcher')->switchBack();