RenderCacheTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Drupal\KernelTests\Core\Render;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\Tests\user\Traits\UserCreationTrait;
  5. /**
  6. * Tests the caching of render items via functional tests.
  7. *
  8. * @group Render
  9. */
  10. class RenderCacheTest extends KernelTestBase {
  11. use UserCreationTrait;
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['user', 'system'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->installEntitySchema('user');
  24. $this->installConfig(['user']);
  25. $this->installSchema('system', ['sequences']);
  26. }
  27. /**
  28. * Tests that user 1 has a different permission context with the same roles.
  29. */
  30. public function testUser1PermissionContext() {
  31. $this->doTestUser1WithContexts(['user.permissions']);
  32. }
  33. /**
  34. * Tests that user 1 has a different roles context with the same roles.
  35. */
  36. public function testUser1RolesContext() {
  37. $this->doTestUser1WithContexts(['user.roles']);
  38. }
  39. /**
  40. * Ensures that user 1 has a unique render cache for the given context.
  41. *
  42. * @param string[] $contexts
  43. * List of cache contexts to use.
  44. */
  45. protected function doTestUser1WithContexts($contexts) {
  46. // Test that user 1 does not share the cache with other users who have the
  47. // same roles, even when using a role-based cache context.
  48. $user1 = $this->createUser();
  49. $this->assertEqual($user1->id(), 1);
  50. $first_authenticated_user = $this->createUser();
  51. $second_authenticated_user = $this->createUser();
  52. $admin_user = $this->createUser([], NULL, TRUE);
  53. $this->assertEqual($user1->getRoles(), $first_authenticated_user->getRoles(), 'User 1 has the same roles as an authenticated user.');
  54. // Impersonate user 1 and render content that only user 1 should have
  55. // permission to see.
  56. \Drupal::service('account_switcher')->switchTo($user1);
  57. $test_element = [
  58. '#cache' => [
  59. 'keys' => ['test'],
  60. 'contexts' => $contexts,
  61. ],
  62. ];
  63. $element = $test_element;
  64. $element['#markup'] = 'content for user 1';
  65. $output = \Drupal::service('renderer')->renderRoot($element);
  66. $this->assertEqual($output, 'content for user 1');
  67. // Verify the cache is working by rendering the same element but with
  68. // different markup passed in; the result should be the same.
  69. $element = $test_element;
  70. $element['#markup'] = 'should not be used';
  71. $output = \Drupal::service('renderer')->renderRoot($element);
  72. $this->assertEqual($output, 'content for user 1');
  73. \Drupal::service('account_switcher')->switchBack();
  74. // Verify that the first authenticated user does not see the same content
  75. // as user 1.
  76. \Drupal::service('account_switcher')->switchTo($first_authenticated_user);
  77. $element = $test_element;
  78. $element['#markup'] = 'content for authenticated users';
  79. $output = \Drupal::service('renderer')->renderRoot($element);
  80. $this->assertEqual($output, 'content for authenticated users');
  81. \Drupal::service('account_switcher')->switchBack();
  82. // Verify that the second authenticated user shares the cache with the
  83. // first authenticated user.
  84. \Drupal::service('account_switcher')->switchTo($second_authenticated_user);
  85. $element = $test_element;
  86. $element['#markup'] = 'should not be used';
  87. $output = \Drupal::service('renderer')->renderRoot($element);
  88. $this->assertEqual($output, 'content for authenticated users');
  89. \Drupal::service('account_switcher')->switchBack();
  90. // Verify that the admin user (who has an admin role without explicit
  91. // permissions) does not share the same cache.
  92. \Drupal::service('account_switcher')->switchTo($admin_user);
  93. $element = $test_element;
  94. $element['#markup'] = 'content for admin user';
  95. $output = \Drupal::service('renderer')->renderRoot($element);
  96. $this->assertEqual($output, 'content for admin user');
  97. \Drupal::service('account_switcher')->switchBack();
  98. }
  99. }