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