comparison core/modules/jsonapi/tests/src/Functional/BlockTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Functional;
4
5 use Drupal\block\Entity\Block;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Core\Url;
8
9 /**
10 * JSON:API integration test for the "Block" config entity type.
11 *
12 * @group jsonapi
13 */
14 class BlockTest extends ResourceTestBase {
15
16 /**
17 * {@inheritdoc}
18 */
19 public static $modules = ['block'];
20
21 /**
22 * {@inheritdoc}
23 */
24 protected static $entityTypeId = 'block';
25
26 /**
27 * {@inheritdoc}
28 */
29 protected static $resourceTypeName = 'block--block';
30
31 /**
32 * {@inheritdoc}
33 *
34 * @var \Drupal\block\BlockInterface
35 */
36 protected $entity;
37
38 /**
39 * {@inheritdoc}
40 */
41 protected function setUpAuthorization($method) {
42 switch ($method) {
43 case 'GET':
44 $this->entity->setVisibilityConfig('user_role', [])->save();
45 break;
46 }
47 }
48
49 /**
50 * {@inheritdoc}
51 */
52 protected function createEntity() {
53 $block = Block::create([
54 'plugin' => 'llama_block',
55 'region' => 'header',
56 'id' => 'llama',
57 'theme' => 'classy',
58 ]);
59 // All blocks can be viewed by the anonymous user by default. An interesting
60 // side effect of this is that any anonymous user is also able to read the
61 // corresponding block config entity via REST, even if an authentication
62 // provider is configured for the block config entity REST resource! In
63 // other words: Block entities do not distinguish between 'view' as in
64 // "render on a page" and 'view' as in "read the configuration".
65 // This prevents that.
66 // @todo Fix this in https://www.drupal.org/node/2820315.
67 $block->setVisibilityConfig('user_role', [
68 'id' => 'user_role',
69 'roles' => ['non-existing-role' => 'non-existing-role'],
70 'negate' => FALSE,
71 'context_mapping' => [
72 'user' => '@user.current_user_context:current_user',
73 ],
74 ]);
75 $block->save();
76
77 return $block;
78 }
79
80 /**
81 * {@inheritdoc}
82 */
83 protected function getExpectedDocument() {
84 $self_url = Url::fromUri('base:/jsonapi/block/block/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
85 return [
86 'jsonapi' => [
87 'meta' => [
88 'links' => [
89 'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
90 ],
91 ],
92 'version' => '1.0',
93 ],
94 'links' => [
95 'self' => ['href' => $self_url],
96 ],
97 'data' => [
98 'id' => $this->entity->uuid(),
99 'type' => 'block--block',
100 'links' => [
101 'self' => ['href' => $self_url],
102 ],
103 'attributes' => [
104 'weight' => NULL,
105 'langcode' => 'en',
106 'status' => TRUE,
107 'dependencies' => [
108 'theme' => [
109 'classy',
110 ],
111 ],
112 'theme' => 'classy',
113 'region' => 'header',
114 'provider' => NULL,
115 'plugin' => 'llama_block',
116 'settings' => [
117 'id' => 'broken',
118 'label' => '',
119 'provider' => 'core',
120 'label_display' => 'visible',
121 ],
122 'visibility' => [],
123 'drupal_internal__id' => 'llama',
124 ],
125 ],
126 ];
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 protected function getPostDocument() {
133 // @todo Update once https://www.drupal.org/node/2300677 is fixed.
134 }
135
136 /**
137 * {@inheritdoc}
138 */
139 protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
140 // @see ::createEntity()
141 return array_values(array_diff(parent::getExpectedCacheContexts(), ['user.permissions']));
142 }
143
144 /**
145 * {@inheritdoc}
146 */
147 protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
148 // Because the 'user.permissions' cache context is missing, the cache tag
149 // for the anonymous user role is never added automatically.
150 return array_values(array_diff(parent::getExpectedCacheTags(), ['config:user.role.anonymous']));
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 protected function getExpectedUnauthorizedAccessMessage($method) {
157 switch ($method) {
158 case 'GET':
159 return "The block visibility condition 'user_role' denied access.";
160
161 default:
162 return parent::getExpectedUnauthorizedAccessMessage($method);
163 }
164 }
165
166 /**
167 * {@inheritdoc}
168 */
169 protected function getExpectedUnauthorizedAccessCacheability() {
170 // @see \Drupal\block\BlockAccessControlHandler::checkAccess()
171 return parent::getExpectedUnauthorizedAccessCacheability()
172 ->setCacheTags([
173 '4xx-response',
174 'config:block.block.llama',
175 'http_response',
176 'user:2',
177 ])
178 ->setCacheContexts(['url.site', 'user.roles']);
179 }
180
181 /**
182 * {@inheritdoc}
183 */
184 protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) {
185 return parent::getExpectedCollectionCacheability($account, $collection, $sparse_fieldset, $filtered)
186 ->addCacheTags(['user:2'])
187 ->addCacheContexts(['user.roles']);
188 }
189
190 }