Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / Rest / BlockContentResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional\Rest;
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' => NULL,
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       ->setUnpublished();
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       'reusable' => [
96         [
97           'value' => TRUE,
98         ],
99       ],
100       'type' => [
101         [
102           'target_id' => 'basic',
103           'target_type' => 'block_content_type',
104           'target_uuid' => BlockContentType::load('basic')->uuid(),
105         ],
106       ],
107       'info' => [
108         [
109           'value' => 'Llama',
110         ],
111       ],
112       'revision_log' => [],
113       'changed' => [
114         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
115       ],
116       'revision_id' => [
117         [
118           'value' => 1,
119         ],
120       ],
121       'revision_created' => [
122         $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()),
123       ],
124       'revision_user' => [],
125       'revision_translation_affected' => [
126         [
127           'value' => TRUE,
128         ],
129       ],
130       'default_langcode' => [
131         [
132           'value' => TRUE,
133         ],
134       ],
135       'body' => [
136         [
137           'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
138           'format' => 'plain_text',
139           'summary' => NULL,
140           'processed' => "<p>The name &quot;llama&quot; was adopted by European settlers from native Peruvians.</p>\n",
141         ],
142       ],
143       'status' => [
144         [
145           'value' => FALSE,
146         ],
147       ],
148     ];
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   protected function getNormalizedPostEntity() {
155     return [
156       'type' => [
157         [
158           'target_id' => 'basic',
159         ],
160       ],
161       'info' => [
162         [
163           'value' => 'Dramallama',
164         ],
165       ],
166     ];
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   protected function getExpectedUnauthorizedAccessMessage($method) {
173     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
174       return parent::getExpectedUnauthorizedAccessMessage($method);
175     }
176
177     return parent::getExpectedUnauthorizedAccessMessage($method);
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   protected function getExpectedUnauthorizedAccessCacheability() {
184     // @see \Drupal\block_content\BlockContentAccessControlHandler()
185     return parent::getExpectedUnauthorizedAccessCacheability()
186       ->addCacheTags(['block_content:1']);
187   }
188
189   /**
190    * {@inheritdoc}
191    */
192   protected function getExpectedCacheTags() {
193     return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:filter.format.plain_text']);
194   }
195
196   /**
197    * {@inheritdoc}
198    */
199   protected function getExpectedCacheContexts() {
200     return Cache::mergeContexts(['url.site'], $this->container->getParameter('renderer.config')['required_cache_contexts']);
201   }
202
203 }