comparison core/modules/jsonapi/tests/src/Functional/BlockContentTest.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_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Url;
9 use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait;
10
11 /**
12 * JSON:API integration test for the "BlockContent" content entity type.
13 *
14 * @group jsonapi
15 */
16 class BlockContentTest extends ResourceTestBase {
17
18 use CommonCollectionFilterAccessTestPatternsTrait;
19
20 /**
21 * {@inheritdoc}
22 */
23 public static $modules = ['block_content'];
24
25 /**
26 * {@inheritdoc}
27 */
28 protected static $entityTypeId = 'block_content';
29
30 /**
31 * {@inheritdoc}
32 */
33 protected static $resourceTypeName = 'block_content--basic';
34
35 /**
36 * {@inheritdoc}
37 *
38 * @var \Drupal\block_content\BlockContentInterface
39 */
40 protected $entity;
41
42 /**
43 * {@inheritdoc}
44 */
45 protected static $patchProtectedFieldNames = [
46 'changed' => NULL,
47 ];
48
49 /**
50 * {@inheritdoc}
51 */
52 protected function setUpAuthorization($method) {
53 $this->grantPermissionsToTestedRole(['administer blocks']);
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function createEntity() {
60 if (!BlockContentType::load('basic')) {
61 $block_content_type = BlockContentType::create([
62 'id' => 'basic',
63 'label' => 'basic',
64 'revision' => FALSE,
65 ]);
66 $block_content_type->save();
67 block_content_add_body_field($block_content_type->id());
68 }
69
70 // Create a "Llama" custom block.
71 $block_content = BlockContent::create([
72 'info' => 'Llama',
73 'type' => 'basic',
74 'body' => [
75 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
76 'format' => 'plain_text',
77 ],
78 ])
79 ->setUnpublished();
80 $block_content->save();
81 return $block_content;
82 }
83
84 /**
85 * {@inheritdoc}
86 */
87 protected function getExpectedDocument() {
88 $self_url = Url::fromUri('base:/jsonapi/block_content/basic/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
89 return [
90 'jsonapi' => [
91 'meta' => [
92 'links' => [
93 'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
94 ],
95 ],
96 'version' => '1.0',
97 ],
98 'links' => [
99 'self' => ['href' => $self_url],
100 ],
101 'data' => [
102 'id' => $this->entity->uuid(),
103 'type' => 'block_content--basic',
104 'links' => [
105 'self' => ['href' => $self_url],
106 ],
107 'attributes' => [
108 'body' => [
109 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
110 'format' => 'plain_text',
111 'summary' => NULL,
112 'processed' => "<p>The name &quot;llama&quot; was adopted by European settlers from native Peruvians.</p>\n",
113 ],
114 'changed' => (new \DateTime())->setTimestamp($this->entity->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
115 'info' => 'Llama',
116 'revision_log' => NULL,
117 'revision_created' => (new \DateTime())->setTimestamp($this->entity->getRevisionCreationTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
118 'revision_translation_affected' => TRUE,
119 'status' => FALSE,
120 'langcode' => 'en',
121 'default_langcode' => TRUE,
122 'drupal_internal__id' => 1,
123 'drupal_internal__revision_id' => 1,
124 'reusable' => TRUE,
125 ],
126 'relationships' => [
127 'block_content_type' => [
128 'data' => [
129 'id' => BlockContentType::load('basic')->uuid(),
130 'type' => 'block_content_type--block_content_type',
131 ],
132 'links' => [
133 'related' => ['href' => $self_url . '/block_content_type'],
134 'self' => ['href' => $self_url . '/relationships/block_content_type'],
135 ],
136 ],
137 'revision_user' => [
138 'data' => NULL,
139 'links' => [
140 'related' => ['href' => $self_url . '/revision_user'],
141 'self' => ['href' => $self_url . '/relationships/revision_user'],
142 ],
143 ],
144 ],
145 ],
146 ];
147 }
148
149 /**
150 * {@inheritdoc}
151 */
152 protected function getPostDocument() {
153 return [
154 'data' => [
155 'type' => 'block_content--basic',
156 'attributes' => [
157 'info' => 'Dramallama',
158 ],
159 ],
160 ];
161 }
162
163 /**
164 * {@inheritdoc}
165 */
166 protected function getExpectedUnauthorizedAccessCacheability() {
167 // @see \Drupal\block_content\BlockContentAccessControlHandler()
168 return parent::getExpectedUnauthorizedAccessCacheability()
169 ->addCacheTags(['block_content:1']);
170 }
171
172 /**
173 * {@inheritdoc}
174 */
175 protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
176 $tags = parent::getExpectedCacheTags($sparse_fieldset);
177 if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
178 $tags = Cache::mergeTags($tags, ['config:filter.format.plain_text']);
179 }
180 return $tags;
181 }
182
183 /**
184 * {@inheritdoc}
185 */
186 protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
187 $contexts = parent::getExpectedCacheContexts($sparse_fieldset);
188 if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
189 $contexts = Cache::mergeContexts($contexts, ['languages:language_interface', 'theme']);
190 }
191 return $contexts;
192 }
193
194 /**
195 * {@inheritdoc}
196 */
197 public function testRelated() {
198 $this->markTestSkipped('Remove this in https://www.drupal.org/project/jsonapi/issues/2940339');
199 }
200
201 /**
202 * {@inheritdoc}
203 */
204 public function testCollectionFilterAccess() {
205 $this->entity->setPublished()->save();
206 $this->doTestCollectionFilterAccessForPublishableEntities('info', NULL, 'administer blocks');
207 }
208
209 }