annotate core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\serialization\Kernel;
Chris@0 4
Chris@0 5 use Drupal\entity_test\Entity\EntityTestMulRev;
Chris@0 6 use Drupal\field\Entity\FieldConfig;
Chris@0 7 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 8 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Test field level normalization process.
Chris@0 12 *
Chris@0 13 * @group serialization
Chris@0 14 */
Chris@0 15 class FieldItemSerializationTest extends NormalizerTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * {@inheritdoc}
Chris@0 19 */
Chris@0 20 public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'field_normalization_test'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The class name of the test class.
Chris@0 24 *
Chris@0 25 * @var string
Chris@0 26 */
Chris@0 27 protected $entityClass = 'Drupal\entity_test\Entity\EntityTestMulRev';
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The test values.
Chris@0 31 *
Chris@0 32 * @var array
Chris@0 33 */
Chris@0 34 protected $values;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The test entity.
Chris@0 38 *
Chris@0 39 * @var \Drupal\Core\Entity\ContentEntityBase
Chris@0 40 */
Chris@0 41 protected $entity;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * The serializer service.
Chris@0 45 *
Chris@17 46 * @var \Symfony\Component\Serializer\Serializer
Chris@0 47 */
Chris@0 48 protected $serializer;
Chris@0 49
Chris@0 50 /**
Chris@0 51 * {@inheritdoc}
Chris@0 52 */
Chris@0 53 protected function setUp() {
Chris@0 54 parent::setUp();
Chris@0 55
Chris@0 56 // Auto-create a field for testing default field values.
Chris@0 57 FieldStorageConfig::create([
Chris@0 58 'entity_type' => 'entity_test_mulrev',
Chris@0 59 'field_name' => 'field_test_text_default',
Chris@0 60 'type' => 'text',
Chris@0 61 'cardinality' => 1,
Chris@0 62 'translatable' => FALSE,
Chris@0 63 ])->save();
Chris@0 64 FieldConfig::create([
Chris@0 65 'entity_type' => 'entity_test_mulrev',
Chris@0 66 'field_name' => 'field_test_text_default',
Chris@0 67 'bundle' => 'entity_test_mulrev',
Chris@0 68 'label' => 'Test text-field with default',
Chris@0 69 'default_value' => [
Chris@0 70 [
Chris@0 71 'value' => 'This is the default',
Chris@0 72 'format' => 'full_html',
Chris@0 73 ],
Chris@0 74 ],
Chris@0 75 'widget' => [
Chris@0 76 'type' => 'text_textfield',
Chris@0 77 'weight' => 0,
Chris@0 78 ],
Chris@0 79 ])->save();
Chris@18 80 FieldStorageConfig::create([
Chris@18 81 'entity_type' => 'entity_test_mulrev',
Chris@18 82 'field_name' => 'field_test_boolean',
Chris@18 83 'type' => 'boolean',
Chris@18 84 'cardinality' => 1,
Chris@18 85 'translatable' => FALSE,
Chris@18 86 ])->save();
Chris@18 87 FieldConfig::create([
Chris@18 88 'entity_type' => 'entity_test_mulrev',
Chris@18 89 'field_name' => 'field_test_boolean',
Chris@18 90 'bundle' => 'entity_test_mulrev',
Chris@18 91 'label' => 'Test boolean',
Chris@18 92 ])->save();
Chris@0 93
Chris@0 94 // Create a test entity to serialize.
Chris@0 95 $this->values = [
Chris@0 96 'name' => $this->randomMachineName(),
Chris@0 97 'field_test_text' => [
Chris@0 98 'value' => $this->randomMachineName(),
Chris@0 99 'format' => 'full_html',
Chris@0 100 ],
Chris@18 101 'field_test_boolean' => [
Chris@18 102 'value' => FALSE,
Chris@18 103 ],
Chris@0 104 ];
Chris@0 105 $this->entity = EntityTestMulRev::create($this->values);
Chris@0 106 $this->entity->save();
Chris@0 107
Chris@0 108 $this->serializer = $this->container->get('serializer');
Chris@0 109
Chris@0 110 $this->installConfig(['field']);
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * Tests normalizing and denormalizing an entity with field item normalizer.
Chris@0 115 */
Chris@0 116 public function testFieldNormalizeDenormalize() {
Chris@0 117 $normalized = $this->serializer->normalize($this->entity, 'json');
Chris@0 118
Chris@0 119 $expected_field_value = $this->entity->field_test_text[0]->getValue()['value'] . '::silly_suffix';
Chris@0 120 $this->assertEquals($expected_field_value, $normalized['field_test_text'][0]['value'], 'Text field item normalized');
Chris@0 121 $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, 'json');
Chris@0 122
Chris@0 123 $this->assertEquals($denormalized->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.');
Chris@0 124 $this->assertEquals($denormalized->field_test_text_default[0]->getValue(), $this->entity->field_test_text_default[0]->getValue(), 'Text field item with default denormalized.');
Chris@0 125
Chris@0 126 // Unset the values for text field that has a default value.
Chris@0 127 unset($normalized['field_test_text_default']);
Chris@0 128 $denormalized_without_all_fields = $this->serializer->denormalize($normalized, $this->entityClass, 'json');
Chris@0 129 // Check that denormalized entity is still the same even if not all fields
Chris@0 130 // are not provided.
Chris@0 131 $this->assertEquals($denormalized_without_all_fields->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.');
Chris@0 132 // Even though field_test_text_default value was unset before
Chris@0 133 // denormalization it should still have the default values for the field.
Chris@0 134 $this->assertEquals($denormalized_without_all_fields->field_test_text_default[0]->getValue(), $this->entity->field_test_text_default[0]->getValue(), 'Text field item with default denormalized.');
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Tests denormalizing using a scalar field value.
Chris@0 139 */
Chris@0 140 public function testFieldDenormalizeWithScalarValue() {
Chris@0 141 $this->setExpectedException(UnexpectedValueException::class, 'Field values for "uuid" must use an array structure');
Chris@0 142
Chris@0 143 $normalized = $this->serializer->normalize($this->entity, 'json');
Chris@0 144
Chris@0 145 // Change the UUID value to use the UUID directly. No array structure.
Chris@0 146 $normalized['uuid'] = $normalized['uuid'][0]['value'];
Chris@0 147
Chris@0 148 $this->serializer->denormalize($normalized, $this->entityClass, 'json');
Chris@0 149 }
Chris@0 150
Chris@18 151 /**
Chris@18 152 * Tests a format-agnostic normalizer.
Chris@18 153 *
Chris@18 154 * @param string[] $test_modules
Chris@18 155 * The test modules to install.
Chris@18 156 * @param string $format
Chris@18 157 * The format to test. (NULL results in the format-agnostic normalization.)
Chris@18 158 *
Chris@18 159 * @dataProvider providerTestCustomBooleanNormalization
Chris@18 160 */
Chris@18 161 public function testCustomBooleanNormalization(array $test_modules, $format) {
Chris@18 162 // Asserts the entity contains the value we set.
Chris@18 163 $this->assertSame(FALSE, $this->entity->field_test_boolean->value);
Chris@18 164
Chris@18 165 // Asserts normalizing the entity using core's 'serializer' service DOES
Chris@18 166 // yield the value we set.
Chris@18 167 $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format);
Chris@18 168 $this->assertSame(FALSE, $core_normalization['field_test_boolean'][0]['value']);
Chris@18 169
Chris@18 170 $assert_denormalization = function (array $normalization) use ($format) {
Chris@18 171 $denormalized_entity = $this->container->get('serializer')->denormalize($normalization, EntityTestMulRev::class, $format, []);
Chris@18 172 $this->assertInstanceOf(EntityTestMulRev::class, $denormalized_entity);
Chris@18 173 $this->assertSame(TRUE, $denormalized_entity->field_test_boolean->value);
Chris@18 174 };
Chris@18 175
Chris@18 176 // Asserts denormalizing the entity DOES yield the value we set:
Chris@18 177 // - when using the detailed representation
Chris@18 178 $core_normalization['field_test_boolean'][0]['value'] = TRUE;
Chris@18 179 $assert_denormalization($core_normalization);
Chris@18 180 // - and when using the shorthand representation
Chris@18 181 $core_normalization['field_test_boolean'][0] = TRUE;
Chris@18 182 $assert_denormalization($core_normalization);
Chris@18 183
Chris@18 184 // Install test module that contains a high-priority alternative normalizer.
Chris@18 185 $this->enableModules($test_modules);
Chris@18 186
Chris@18 187 // Asserts normalizing the entity DOES NOT ANYMORE yield the value we set.
Chris@18 188 $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format);
Chris@18 189 $this->assertSame('👎', $core_normalization['field_test_boolean'][0]['value']);
Chris@18 190
Chris@18 191 // Asserts denormalizing the entity DOES NOT ANYMORE yield the value we set:
Chris@18 192 // - when using the detailed representation
Chris@18 193 $core_normalization['field_test_boolean'][0]['value'] = '👍';
Chris@18 194 $assert_denormalization($core_normalization);
Chris@18 195 // - and when using the shorthand representation
Chris@18 196 $core_normalization['field_test_boolean'][0] = '👍';
Chris@18 197 $assert_denormalization($core_normalization);
Chris@18 198 }
Chris@18 199
Chris@18 200 /**
Chris@18 201 * Data provider.
Chris@18 202 *
Chris@18 203 * @return array
Chris@18 204 * Test cases.
Chris@18 205 */
Chris@18 206 public function providerTestCustomBooleanNormalization() {
Chris@18 207 return [
Chris@18 208 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
Chris@18 209 ['test_fieldtype_boolean_emoji_normalizer'],
Chris@18 210 NULL,
Chris@18 211 ],
Chris@18 212 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
Chris@18 213 ['test_datatype_boolean_emoji_normalizer'],
Chris@18 214 NULL,
Chris@18 215 ],
Chris@18 216 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the JSON normalization' => [
Chris@18 217 ['test_fieldtype_boolean_emoji_normalizer'],
Chris@18 218 'json',
Chris@18 219 ],
Chris@18 220 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON normalization' => [
Chris@18 221 ['test_datatype_boolean_emoji_normalizer'],
Chris@18 222 'json',
Chris@18 223 ],
Chris@18 224 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
Chris@18 225 ['test_fieldtype_boolean_emoji_normalizer'],
Chris@18 226 'hal_json',
Chris@18 227 ],
Chris@18 228 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
Chris@18 229 ['test_datatype_boolean_emoji_normalizer', 'hal'],
Chris@18 230 'hal_json',
Chris@18 231 ],
Chris@18 232 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the XML normalization' => [
Chris@18 233 ['test_fieldtype_boolean_emoji_normalizer'],
Chris@18 234 'xml',
Chris@18 235 ],
Chris@18 236 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the XML normalization' => [
Chris@18 237 ['test_datatype_boolean_emoji_normalizer', 'hal'],
Chris@18 238 'xml',
Chris@18 239 ],
Chris@18 240 ];
Chris@18 241 }
Chris@18 242
Chris@0 243 }