BlockResourceTestBase.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Drupal\Tests\block\Functional\Rest;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
  5. abstract class BlockResourceTestBase extends EntityResourceTestBase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static $modules = ['block'];
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected static $entityTypeId = 'block';
  14. /**
  15. * @var \Drupal\block\BlockInterface
  16. */
  17. protected $entity;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUpAuthorization($method) {
  22. switch ($method) {
  23. case 'GET':
  24. $this->entity->setVisibilityConfig('user_role', [])->save();
  25. break;
  26. case 'POST':
  27. $this->grantPermissionsToTestedRole(['administer blocks']);
  28. break;
  29. case 'PATCH':
  30. $this->grantPermissionsToTestedRole(['administer blocks']);
  31. break;
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function createEntity() {
  38. $block = Block::create([
  39. 'plugin' => 'llama_block',
  40. 'region' => 'header',
  41. 'id' => 'llama',
  42. 'theme' => 'classy',
  43. ]);
  44. // All blocks can be viewed by the anonymous user by default. An interesting
  45. // side effect of this is that any anonymous user is also able to read the
  46. // corresponding block config entity via REST, even if an authentication
  47. // provider is configured for the block config entity REST resource! In
  48. // other words: Block entities do not distinguish between 'view' as in
  49. // "render on a page" and 'view' as in "read the configuration".
  50. // This prevents that.
  51. // @todo Fix this in https://www.drupal.org/node/2820315.
  52. $block->setVisibilityConfig('user_role', [
  53. 'id' => 'user_role',
  54. 'roles' => ['non-existing-role' => 'non-existing-role'],
  55. 'negate' => FALSE,
  56. 'context_mapping' => [
  57. 'user' => '@user.current_user_context:current_user',
  58. ],
  59. ]);
  60. $block->save();
  61. return $block;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function getExpectedNormalizedEntity() {
  67. $normalization = [
  68. 'uuid' => $this->entity->uuid(),
  69. 'id' => 'llama',
  70. 'weight' => NULL,
  71. 'langcode' => 'en',
  72. 'status' => TRUE,
  73. 'dependencies' => [
  74. 'theme' => [
  75. 'classy',
  76. ],
  77. ],
  78. 'theme' => 'classy',
  79. 'region' => 'header',
  80. 'provider' => NULL,
  81. 'plugin' => 'llama_block',
  82. 'settings' => [
  83. 'id' => 'broken',
  84. 'label' => '',
  85. 'provider' => 'core',
  86. 'label_display' => 'visible',
  87. ],
  88. 'visibility' => [],
  89. ];
  90. return $normalization;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function getNormalizedPostEntity() {
  96. // @todo Update in https://www.drupal.org/node/2300677.
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. protected function getExpectedCacheContexts() {
  102. // @see ::createEntity()
  103. return ['url.site'];
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. protected function getExpectedCacheTags() {
  109. // Because the 'user.permissions' cache context is missing, the cache tag
  110. // for the anonymous user role is never added automatically.
  111. return array_values(array_diff(parent::getExpectedCacheTags(), ['config:user.role.anonymous']));
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. protected function getExpectedUnauthorizedAccessMessage($method) {
  117. if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
  118. return parent::getExpectedUnauthorizedAccessMessage($method);
  119. }
  120. switch ($method) {
  121. case 'GET':
  122. return "You are not authorized to view this block entity.";
  123. default:
  124. return parent::getExpectedUnauthorizedAccessMessage($method);
  125. }
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. protected function getExpectedUnauthorizedAccessCacheability() {
  131. // @see \Drupal\block\BlockAccessControlHandler::checkAccess()
  132. return parent::getExpectedUnauthorizedAccessCacheability()
  133. ->setCacheTags([
  134. '4xx-response',
  135. 'config:block.block.llama',
  136. 'http_response',
  137. static::$auth ? 'user:2' : 'user:0',
  138. ])
  139. ->setCacheContexts(['user.roles']);
  140. }
  141. }