function ExternalNormalizersTest::testFormatAgnosticNormalizers
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Functional/ExternalNormalizersTest.php \Drupal\Tests\jsonapi\Functional\ExternalNormalizersTest::testFormatAgnosticNormalizers()
- 8.9.x core/modules/jsonapi/tests/src/Functional/ExternalNormalizersTest.php \Drupal\Tests\jsonapi\Functional\ExternalNormalizersTest::testFormatAgnosticNormalizers()
- 11.x core/modules/jsonapi/tests/src/Functional/ExternalNormalizersTest.php \Drupal\Tests\jsonapi\Functional\ExternalNormalizersTest::testFormatAgnosticNormalizers()
Tests a format-agnostic normalizer.
@dataProvider providerTestFormatAgnosticNormalizers
Parameters
string $test_module: The test module to install, which comes with a high-priority normalizer.
string $expected_value_jsonapi_normalization: The expected JSON:API normalization of the tested field. Must be either
- static::VALUE_ORIGINAL (normalizer IS NOT expected to override)
- static::VALUE_OVERRIDDEN (normalizer IS expected to override)
string $expected_value_jsonapi_denormalization: The expected JSON:API denormalization of the tested field. Must be either
- static::VALUE_OVERRIDDEN (denormalizer IS NOT expected to override)
- static::VALUE_ORIGINAL (denormalizer IS expected to override)
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ ExternalNormalizersTest.php, line 116
Class
- ExternalNormalizersTest
- Asserts external normalizers are handled as expected by the JSON:API module.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testFormatAgnosticNormalizers($test_module, $expected_value_jsonapi_normalization, $expected_value_jsonapi_denormalization) : void {
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));
// Asserts the entity contains the value we set.
$this->assertSame(static::VALUE_ORIGINAL, $this->entity->field_test->value);
// Asserts normalizing the entity using core's 'serializer' service DOES
// yield the value we set.
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity);
$this->assertSame(static::VALUE_ORIGINAL, $core_normalization['field_test'][0]['value']);
// Asserts denormalizing the entity using core's 'serializer' service DOES
// yield the value we set.
$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);
// Install test module that contains a high-priority alternative normalizer.
$this->container
->get('module_installer')
->install([
$test_module,
]);
$this->rebuildContainer();
// Asserts normalizing the entity using core's 'serializer' service DOES NOT
// ANYMORE yield the value we set.
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity);
$this->assertSame(static::VALUE_OVERRIDDEN, $core_normalization['field_test'][0]['value']);
// Asserts denormalizing the entity using core's 'serializer' service DOES
// NOT ANYMORE yield the value we set.
$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);
$this->assertSame(static::VALUE_ORIGINAL, $denormalized_entity->field_test->value);
// Asserts the expected JSON:API normalization.
// @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
$url = Url::fromRoute('jsonapi.entity_test--entity_test.individual', [
'entity' => $this->entity
->uuid(),
]);
// $url = $this->entity->toUrl('jsonapi');
$client = $this->getSession()
->getDriver()
->getClient()
->getClient();
$response = $client->request('GET', $url->setAbsolute(TRUE)
->toString());
$document = $this->getDocumentFromResponse($response);
$this->assertSame($expected_value_jsonapi_normalization, $document['data']['attributes']['field_test']);
// Asserts the expected JSON:API denormalization.
$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 = $this->getDocumentFromResponse($response);
$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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.