View source
<?php
namespace Drupal\Tests\entity_test\Functional\Rest;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
class EntityTestTextItemNormalizerTest extends EntityTestResourceTestBase {
use AnonResourceTestTrait;
protected static $modules = [
'filter_test',
];
protected $defaultTheme = 'stark';
protected static $format = 'json';
protected static $mimeType = 'application/json';
protected function setUpAuthorization($method) {
parent::setUpAuthorization($method);
if (in_array($method, [
'POST',
'PATCH',
], TRUE)) {
$this
->grantPermissionsToTestedRole([
'use text format my_text_format',
]);
}
}
protected function getExpectedNormalizedEntity() {
$expected = parent::getExpectedNormalizedEntity();
$expected['field_test_text'] = [
[
'value' => 'Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.',
'format' => 'my_text_format',
'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>',
],
];
return $expected;
}
protected function createEntity() {
$entity = parent::createEntity();
if (!FilterFormat::load('my_text_format')) {
FilterFormat::create([
'format' => 'my_text_format',
'name' => 'My Text Format',
'filters' => [
'filter_test_assets' => [
'weight' => -1,
'status' => TRUE,
],
'filter_test_cache_tags' => [
'weight' => 0,
'status' => TRUE,
],
'filter_test_cache_contexts' => [
'weight' => 0,
'status' => TRUE,
],
'filter_test_cache_merge' => [
'weight' => 0,
'status' => TRUE,
],
'filter_test_placeholders' => [
'weight' => 1,
'status' => TRUE,
],
'filter_autop' => [
'status' => TRUE,
],
],
])
->save();
}
$entity->field_test_text = [
'value' => 'Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.',
'format' => 'my_text_format',
];
$entity
->save();
return $entity;
}
protected function getNormalizedPostEntity() {
$post_entity = parent::getNormalizedPostEntity();
$post_entity['field_test_text'] = [
[
'value' => 'Llamas are awesome.',
'format' => 'my_text_format',
],
];
return $post_entity;
}
protected function getExpectedCacheTags() {
return Cache::mergeTags([
'config:filter.format.my_text_format',
'foo:bar',
'foo:baz',
'merge:tag',
], parent::getExpectedCacheTags());
}
protected function getExpectedCacheContexts() {
return Cache::mergeContexts([
'languages:' . LanguageInterface::TYPE_CONTENT,
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
], parent::getExpectedCacheContexts());
}
public function testGetWithFormat($text_format_id, array $expected_cache_tags) {
FilterFormat::create([
'name' => 'Pablo Picasso',
'format' => 'pablo',
'langcode' => 'es',
'filters' => [],
])
->save();
$value = [
'value' => $this
->randomString(),
];
if ($text_format_id !== FALSE) {
$value['format'] = $text_format_id;
}
$this->entity
->set('field_test_text', $value)
->save();
$this
->initAuthentication();
$url = $this
->getEntityResourceUrl();
$url
->setOption('query', [
'_format' => static::$format,
]);
$request_options = $this
->getAuthenticationRequestOptions('GET');
$this
->provisionEntityResource();
$this
->setUpAuthorization('GET');
$response = $this
->request('GET', $url, $request_options);
$expected_cache_tags = Cache::mergeTags($expected_cache_tags, parent::getExpectedCacheTags());
$this
->assertEqualsCanonicalizing($expected_cache_tags, explode(' ', $response
->getHeader('X-Drupal-Cache-Tags')[0]));
}
public function providerTestGetWithFormat() {
return [
'format specified (different from fallback format)' => [
'pablo',
[
'config:filter.format.pablo',
],
],
'format specified (happens to be the same as fallback format)' => [
'plain_text',
[
'config:filter.format.plain_text',
],
],
'no format specified: fallback format used automatically' => [
FALSE,
[
'config:filter.format.plain_text',
'config:filter.settings',
],
],
];
}
}