Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / tests / src / Functional / Rest / EntityTestTextItemNormalizerTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_test\Functional\Rest;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
9
10 /**
11  * @group rest
12  */
13 class EntityTestTextItemNormalizerTest extends EntityTestResourceTestBase {
14
15   use AnonResourceTestTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['filter_test'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected static $format = 'json';
26
27   /**
28    * {@inheritdoc}
29    */
30   protected static $mimeType = 'application/json';
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUpAuthorization($method) {
36     parent::setUpAuthorization($method);
37     if (in_array($method, ['POST', 'PATCH'], TRUE)) {
38       $this->grantPermissionsToTestedRole(['use text format my_text_format']);
39     }
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function getExpectedNormalizedEntity() {
46     $expected = parent::getExpectedNormalizedEntity();
47     $expected['field_test_text'] = [
48       [
49         'value' => 'Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.',
50         'format' => 'my_text_format',
51         'processed' => '<p>Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.</p>' . "\n" . '<p>This is a dynamic llama.</p>',
52       ],
53     ];
54     return $expected;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function createEntity() {
61     $entity = parent::createEntity();
62     if (!FilterFormat::load('my_text_format')) {
63       FilterFormat::create([
64         'format' => 'my_text_format',
65         'name' => 'My Text Format',
66         'filters' => [
67           'filter_test_assets' => [
68             'weight' => -1,
69             'status' => TRUE,
70           ],
71           'filter_test_cache_tags' => [
72             'weight' => 0,
73             'status' => TRUE,
74           ],
75           'filter_test_cache_contexts' => [
76             'weight' => 0,
77             'status' => TRUE,
78           ],
79           'filter_test_cache_merge' => [
80             'weight' => 0,
81             'status' => TRUE,
82           ],
83           'filter_test_placeholders' => [
84             'weight' => 1,
85             'status' => TRUE,
86           ],
87           'filter_autop' => [
88             'status' => TRUE,
89           ],
90         ],
91       ])->save();
92     }
93     $entity->field_test_text = [
94       'value' => 'Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.',
95       'format' => 'my_text_format',
96     ];
97     $entity->save();
98     return $entity;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   protected function getNormalizedPostEntity() {
105     $post_entity = parent::getNormalizedPostEntity();
106     $post_entity['field_test_text'] = [
107       [
108         'value' => 'Llamas are awesome.',
109         'format' => 'my_text_format',
110       ],
111     ];
112     return $post_entity;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   protected function getExpectedCacheTags() {
119     return Cache::mergeTags([
120       // The cache tag set by the processed_text element itself.
121       'config:filter.format.my_text_format',
122       // The cache tags set by the filter_test_cache_tags filter.
123       'foo:bar',
124       'foo:baz',
125       // The cache tags set by the filter_test_cache_merge filter.
126       'merge:tag',
127     ], parent::getExpectedCacheTags());
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function getExpectedCacheContexts() {
134     return Cache::mergeContexts([
135       // The cache context set by the filter_test_cache_contexts filter.
136       'languages:' . LanguageInterface::TYPE_CONTENT,
137       // The default cache contexts for Renderer.
138       'languages:' . LanguageInterface::TYPE_INTERFACE,
139       'theme',
140       // The cache tags set by the filter_test_cache_merge filter.
141       'user.permissions',
142     ], parent::getExpectedCacheContexts());
143   }
144
145   /**
146    * Tests GETting an entity with the test text field set to a specific format.
147    *
148    * @dataProvider providerTestGetWithFormat
149    */
150   public function testGetWithFormat($text_format_id, array $expected_cache_tags) {
151     FilterFormat::create([
152       'name' => 'Pablo Piccasso',
153       'format' => 'pablo',
154       'langcode' => 'es',
155       'filters' => [],
156     ])->save();
157
158     // Set TextItemBase field's value for testing, using the given text format.
159     $value = [
160       'value' => $this->randomString(),
161     ];
162     if ($text_format_id !== FALSE) {
163       $value['format'] = $text_format_id;
164     }
165     $this->entity->set('field_test_text', $value)->save();
166
167     $this->initAuthentication();
168     $url = $this->getEntityResourceUrl();
169     $url->setOption('query', ['_format' => static::$format]);
170     $request_options = $this->getAuthenticationRequestOptions('GET');
171     $this->provisionEntityResource();
172     $this->setUpAuthorization('GET');
173     $response = $this->request('GET', $url, $request_options);
174     $expected_cache_tags = Cache::mergeTags($expected_cache_tags, parent::getExpectedCacheTags());
175     $this->assertSame($expected_cache_tags, explode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0]));
176   }
177
178   public function providerTestGetWithFormat() {
179     return [
180       'format specified (different from fallback format)' => [
181         'pablo',
182         ['config:filter.format.pablo'],
183       ],
184       'format specified (happens to be the same as fallback format)' => [
185         'plain_text',
186         ['config:filter.format.plain_text'],
187       ],
188       'no format specified: fallback format used automatically' => [
189         FALSE,
190         ['config:filter.format.plain_text', 'config:filter.settings'],
191       ],
192     ];
193   }
194
195 }