3 namespace Drupal\Tests\entity_test\Functional\Rest;
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;
13 class EntityTestTextItemNormalizerTest extends EntityTestResourceTestBase {
15 use AnonResourceTestTrait;
20 public static $modules = ['filter_test'];
25 protected static $format = 'json';
30 protected static $mimeType = 'application/json';
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']);
45 protected function getExpectedNormalizedEntity() {
46 $expected = parent::getExpectedNormalizedEntity();
47 $expected['field_test_text'] = [
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>',
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',
67 'filter_test_assets' => [
71 'filter_test_cache_tags' => [
75 'filter_test_cache_contexts' => [
79 'filter_test_cache_merge' => [
83 'filter_test_placeholders' => [
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',
104 protected function getNormalizedPostEntity() {
105 $post_entity = parent::getNormalizedPostEntity();
106 $post_entity['field_test_text'] = [
108 'value' => 'Llamas are awesome.',
109 'format' => 'my_text_format',
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.
125 // The cache tags set by the filter_test_cache_merge filter.
127 ], parent::getExpectedCacheTags());
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,
140 // The cache tags set by the filter_test_cache_merge filter.
142 ], parent::getExpectedCacheContexts());
146 * Tests GETting an entity with the test text field set to a specific format.
148 * @dataProvider providerTestGetWithFormat
150 public function testGetWithFormat($text_format_id, array $expected_cache_tags) {
151 FilterFormat::create([
152 'name' => 'Pablo Piccasso',
158 // Set TextItemBase field's value for testing, using the given text format.
160 'value' => $this->randomString(),
162 if ($text_format_id !== FALSE) {
163 $value['format'] = $text_format_id;
165 $this->entity->set('field_test_text', $value)->save();
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]));
178 public function providerTestGetWithFormat() {
180 'format specified (different from fallback format)' => [
182 ['config:filter.format.pablo'],
184 'format specified (happens to be the same as fallback format)' => [
186 ['config:filter.format.plain_text'],
188 'no format specified: fallback format used automatically' => [
190 ['config:filter.format.plain_text', 'config:filter.settings'],