View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use GuzzleHttp\RequestOptions;
class ExternalNormalizersTest extends BrowserTestBase {
const VALUE_ORIGINAL = 'Llamas are super awesome!';
const VALUE_OVERRIDDEN = 'Llamas are NOT awesome!';
protected static $modules = [
'jsonapi',
'entity_test',
];
protected $entity;
public function setUp() {
parent::setUp();
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('view test entity')
->grantPermission('create entity_test entity_test_with_bundle entities')
->save();
$this
->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
FieldStorageConfig::create([
'field_name' => 'field_test',
'type' => 'string',
'entity_type' => 'entity_test',
])
->save();
FieldConfig::create([
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
])
->save();
$this->entity = EntityTest::create([
'name' => 'Llama',
'type' => 'entity_test',
'field_test' => static::VALUE_ORIGINAL,
]);
$this->entity
->save();
}
public function testFormatAgnosticNormalizers($test_module, $expected_value_jsonapi_normalization, $expected_value_jsonapi_denormalization) {
assert(in_array($expected_value_jsonapi_normalization, [
static::VALUE_ORIGINAL,
static::VALUE_OVERRIDDEN,
], TRUE));
assert(in_array($expected_value_jsonapi_denormalization, [
static::VALUE_ORIGINAL,
static::VALUE_OVERRIDDEN,
], TRUE));
$this
->assertSame(static::VALUE_ORIGINAL, $this->entity->field_test->value);
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity);
$this
->assertSame(static::VALUE_ORIGINAL, $core_normalization['field_test'][0]['value']);
$core_normalization['field_test'][0]['value'] = static::VALUE_OVERRIDDEN;
$denormalized_entity = $this->container
->get('serializer')
->denormalize($core_normalization, EntityTest::class, 'json', []);
$this
->assertInstanceOf(EntityTest::class, $denormalized_entity);
$this
->assertSame(static::VALUE_OVERRIDDEN, $denormalized_entity->field_test->value);
$this->container
->get('module_installer')
->install([
$test_module,
]);
$this
->rebuildContainer();
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity);
$this
->assertSame(static::VALUE_OVERRIDDEN, $core_normalization['field_test'][0]['value']);
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity);
$core_normalization['field_test'][0]['value'] = static::VALUE_OVERRIDDEN;
$denormalized_entity = $this->container
->get('serializer')
->denormalize($core_normalization, EntityTest::class, 'json', []);
$this
->assertInstanceOf(EntityTest::class, $denormalized_entity);
if ($test_module === 'jsonapi_test_field_type') {
$this
->assertSame(static::VALUE_ORIGINAL, $denormalized_entity->field_test->value);
}
$url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', [
'entity' => $this->entity
->uuid(),
]);
$client = $this
->getSession()
->getDriver()
->getClient()
->getClient();
$response = $client
->request('GET', $url
->setAbsolute(TRUE)
->toString());
$document = Json::decode((string) $response
->getBody());
$this
->assertSame($expected_value_jsonapi_normalization, $document['data']['attributes']['field_test']);
$request_options = [];
$request_options[RequestOptions::BODY] = Json::encode([
'data' => [
'type' => 'entity_test--entity_test',
'attributes' => [
'field_test' => static::VALUE_OVERRIDDEN,
],
],
]);
$request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
$response = $client
->request('POST', Url::fromRoute('jsonapi.entity_test--entity_test.collection.post')
->setAbsolute(TRUE)
->toString(), $request_options);
$document = Json::decode((string) $response
->getBody());
$this
->assertSame(static::VALUE_OVERRIDDEN, $document['data']['attributes']['field_test']);
$entity_type_manager = $this->container
->get('entity_type.manager');
$uuid_key = $entity_type_manager
->getDefinition('entity_test')
->getKey('uuid');
$entities = $entity_type_manager
->getStorage('entity_test')
->loadByProperties([
$uuid_key => $document['data']['id'],
]);
$created_entity = reset($entities);
$this
->assertSame($expected_value_jsonapi_denormalization, $created_entity->field_test->value);
}
public function providerTestFormatAgnosticNormalizers() {
return [
'Format-agnostic @FieldType-level normalizers SHOULD NOT be able to affect the JSON:API normalization' => [
'jsonapi_test_field_type',
static::VALUE_ORIGINAL,
static::VALUE_OVERRIDDEN,
],
'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON:API normalization' => [
'jsonapi_test_data_type',
static::VALUE_OVERRIDDEN,
static::VALUE_ORIGINAL,
],
];
}
}