public function FieldItemNormalizerTest::testNormalizeFieldItem in Drupal 9
Tests normalizing field item.
File
- core/
modules/ jsonapi/ tests/ src/ Kernel/ Normalizer/ FieldItemNormalizerTest.php, line 73
Class
- FieldItemNormalizerTest
- @coversDefaultClass \Drupal\jsonapi\Normalizer\FieldItemNormalizer @group jsonapi
Namespace
Drupal\Tests\jsonapi\Kernel\NormalizerCode
public function testNormalizeFieldItem() : void {
$entity = EntityTest::create([
'name' => 'Test entity',
'links' => [
[
'uri' => 'https://www.drupal.org',
'title' => 'Drupal.org',
'options' => [
'query' => 'foo=bar',
],
],
],
'internal_property_value' => [
[
'value' => 'Internal property testing!',
],
],
'no_main_property_value' => [
[
'value' => 'No main property testing!',
],
],
]);
// Verify a field with one property is flattened.
$result = $this->normalizer
->normalize($entity
->get('name')
->first());
assert($result instanceof CacheableNormalization);
$this
->assertEquals('Test entity', $result
->getNormalization());
// Verify a field with multiple public properties has all of them returned.
$result = $this->normalizer
->normalize($entity
->get('links')
->first());
assert($result instanceof CacheableNormalization);
$this
->assertEquals([
'uri' => 'https://www.drupal.org',
'title' => 'Drupal.org',
'options' => [
'query' => 'foo=bar',
],
], $result
->getNormalization());
// Verify a field with one public property and one internal only returns the
// public property, and is flattened.
$result = $this->normalizer
->normalize($entity
->get('internal_property_value')
->first());
assert($result instanceof CacheableNormalization);
// Property `internal_value` will not exist.
$this
->assertEquals('Internal property testing!', $result
->getNormalization());
// Verify a field with one public property but no main property is not
// flattened.
$result = $this->normalizer
->normalize($entity
->get('no_main_property_value')
->first());
assert($result instanceof CacheableNormalization);
$this
->assertEquals([
'value' => 'No main property testing!',
], $result
->getNormalization());
}