Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / BlockContent / BlockContentResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\BlockContent;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
9 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
10
11 /**
12  * ResourceTestBase for BlockContent entity.
13  */
14 abstract class BlockContentResourceTestBase extends EntityResourceTestBase {
15
16   use BcTimestampNormalizerUnixTestTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['block_content'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected static $entityTypeId = 'block_content';
27
28   /**
29    * {@inheritdoc}
30    */
31   protected static $patchProtectedFieldNames = [
32     'changed',
33   ];
34
35   /**
36    * @var \Drupal\block_content\BlockContentInterface
37    */
38   protected $entity;
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUpAuthorization($method) {
44     $this->grantPermissionsToTestedRole(['administer blocks']);
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function createEntity() {
51     if (!BlockContentType::load('basic')) {
52       $block_content_type = BlockContentType::create([
53         'id' => 'basic',
54         'label' => 'basic',
55         'revision' => TRUE,
56       ]);
57       $block_content_type->save();
58       block_content_add_body_field($block_content_type->id());
59     }
60
61     // Create a "Llama" custom block.
62     $block_content = BlockContent::create([
63       'info' => 'Llama',
64       'type' => 'basic',
65       'body' => [
66         'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
67         'format' => 'plain_text',
68       ],
69     ])
70       ->setPublished(FALSE);
71     $block_content->save();
72     return $block_content;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   protected function getExpectedNormalizedEntity() {
79     return [
80       'id' => [
81         [
82           'value' => 1,
83         ],
84       ],
85       'uuid' => [
86         [
87           'value' => $this->entity->uuid(),
88         ],
89       ],
90       'langcode' => [
91         [
92           'value' => 'en',
93         ],
94       ],
95       'type' => [
96         [
97           'target_id' => 'basic',
98           'target_type' => 'block_content_type',
99           'target_uuid' => BlockContentType::load('basic')->uuid(),
100         ],
101       ],
102       'info' => [
103         [
104           'value' => 'Llama',
105         ],
106       ],
107       'revision_log' => [],
108       'changed' => [
109         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
110       ],
111       'revision_id' => [
112         [
113           'value' => 1,
114         ],
115       ],
116       'revision_created' => [
117         $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()),
118       ],
119       'revision_user' => [],
120       'revision_translation_affected' => [
121         [
122           'value' => TRUE,
123         ],
124       ],
125       'default_langcode' => [
126         [
127           'value' => TRUE,
128         ],
129       ],
130       'body' => [
131         [
132           'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
133           'format' => 'plain_text',
134           'summary' => NULL,
135           'processed' => "<p>The name &quot;llama&quot; was adopted by European settlers from native Peruvians.</p>\n",
136         ],
137       ],
138       'status' => [
139         [
140           'value' => FALSE,
141         ],
142       ],
143     ];
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   protected function getNormalizedPostEntity() {
150     return [
151       'type' => [
152         [
153           'target_id' => 'basic',
154         ],
155       ],
156       'info' => [
157         [
158           'value' => 'Dramallama',
159         ],
160       ],
161     ];
162   }
163
164
165   /**
166    * {@inheritdoc}
167    */
168   protected function getExpectedUnauthorizedAccessMessage($method) {
169     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
170       return parent::getExpectedUnauthorizedAccessMessage($method);
171     }
172
173     return parent::getExpectedUnauthorizedAccessMessage($method);
174   }
175
176   /**
177    * {@inheritdoc}
178    */
179   protected function getExpectedUnauthorizedAccessCacheability() {
180     // @see \Drupal\block_content\BlockContentAccessControlHandler()
181     return parent::getExpectedUnauthorizedAccessCacheability()
182       ->addCacheTags(['block_content:1']);
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   protected function getExpectedCacheTags() {
189     return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:filter.format.plain_text']);
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   protected function getExpectedCacheContexts() {
196     return Cache::mergeContexts(['url.site'], $this->container->getParameter('renderer.config')['required_cache_contexts']);
197   }
198
199 }