function FieldItemSerializationTest::testCustomBooleanNormalization
Same name in this branch
- 9 core/modules/hal/tests/src/Kernel/serialization/FieldItemSerializationTest.php \Drupal\Tests\hal\Kernel\serialization\FieldItemSerializationTest::testCustomBooleanNormalization()
Same name in other branches
- 8.9.x core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php \Drupal\Tests\serialization\Kernel\FieldItemSerializationTest::testCustomBooleanNormalization()
- 10 core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php \Drupal\Tests\serialization\Kernel\FieldItemSerializationTest::testCustomBooleanNormalization()
- 11.x core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php \Drupal\Tests\serialization\Kernel\FieldItemSerializationTest::testCustomBooleanNormalization()
Tests a format-agnostic normalizer.
@dataProvider providerTestCustomBooleanNormalization
Parameters
string[] $test_modules: The test modules to install.
string $format: The format to test. (NULL results in the format-agnostic normalization.)
File
-
core/
modules/ serialization/ tests/ src/ Kernel/ FieldItemSerializationTest.php, line 171
Class
- FieldItemSerializationTest
- Test field level normalization process.
Namespace
Drupal\Tests\serialization\KernelCode
public function testCustomBooleanNormalization(array $test_modules, $format) {
// Asserts the entity contains the value we set.
$this->assertFalse($this->entity->field_test_boolean->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, $format);
$this->assertFalse($core_normalization['field_test_boolean'][0]['value']);
$assert_denormalization = function (array $normalization) use ($format) {
$denormalized_entity = $this->container
->get('serializer')
->denormalize($normalization, EntityTestMulRev::class, $format, []);
$this->assertInstanceOf(EntityTestMulRev::class, $denormalized_entity);
$this->assertTrue($denormalized_entity->field_test_boolean->value);
};
// Asserts denormalizing the entity DOES yield the value we set:
// - when using the detailed representation
$core_normalization['field_test_boolean'][0]['value'] = TRUE;
$assert_denormalization($core_normalization);
// - and when using the shorthand representation
$core_normalization['field_test_boolean'][0] = TRUE;
$assert_denormalization($core_normalization);
// Install test module that contains a high-priority alternative normalizer.
$this->enableModules($test_modules);
// Asserts normalizing the entity DOES NOT ANYMORE yield the value we set.
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity, $format);
$this->assertSame('👎', $core_normalization['field_test_boolean'][0]['value']);
// Asserts denormalizing the entity DOES NOT ANYMORE yield the value we set:
// - when using the detailed representation
$core_normalization['field_test_boolean'][0]['value'] = '👍';
$assert_denormalization($core_normalization);
// - and when using the shorthand representation
$core_normalization['field_test_boolean'][0] = '👍';
$assert_denormalization($core_normalization);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.