diff core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
line wrap: on
line diff
--- a/core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php	Thu Feb 28 13:21:36 2019 +0000
+++ b/core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php	Thu May 09 15:33:08 2019 +0100
@@ -77,6 +77,19 @@
         'weight' => 0,
       ],
     ])->save();
+    FieldStorageConfig::create([
+      'entity_type' => 'entity_test_mulrev',
+      'field_name' => 'field_test_boolean',
+      'type' => 'boolean',
+      'cardinality' => 1,
+      'translatable' => FALSE,
+    ])->save();
+    FieldConfig::create([
+      'entity_type' => 'entity_test_mulrev',
+      'field_name' => 'field_test_boolean',
+      'bundle' => 'entity_test_mulrev',
+      'label' => 'Test boolean',
+    ])->save();
 
     // Create a test entity to serialize.
     $this->values = [
@@ -85,6 +98,9 @@
         'value' => $this->randomMachineName(),
         'format' => 'full_html',
       ],
+      'field_test_boolean' => [
+        'value' => FALSE,
+      ],
     ];
     $this->entity = EntityTestMulRev::create($this->values);
     $this->entity->save();
@@ -132,4 +148,96 @@
     $this->serializer->denormalize($normalized, $this->entityClass, 'json');
   }
 
+  /**
+   * Tests a format-agnostic normalizer.
+   *
+   * @param string[] $test_modules
+   *   The test modules to install.
+   * @param string $format
+   *   The format to test. (NULL results in the format-agnostic normalization.)
+   *
+   * @dataProvider providerTestCustomBooleanNormalization
+   */
+  public function testCustomBooleanNormalization(array $test_modules, $format) {
+    // Asserts the entity contains the value we set.
+    $this->assertSame(FALSE, $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->assertSame(FALSE, $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->assertSame(TRUE, $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);
+  }
+
+  /**
+   * Data provider.
+   *
+   * @return array
+   *   Test cases.
+   */
+  public function providerTestCustomBooleanNormalization() {
+    return [
+      'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
+        ['test_fieldtype_boolean_emoji_normalizer'],
+        NULL,
+      ],
+      'Format-agnostic @DataType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
+        ['test_datatype_boolean_emoji_normalizer'],
+        NULL,
+      ],
+      'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the JSON normalization' => [
+        ['test_fieldtype_boolean_emoji_normalizer'],
+        'json',
+      ],
+      'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON normalization' => [
+        ['test_datatype_boolean_emoji_normalizer'],
+        'json',
+      ],
+      'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
+        ['test_fieldtype_boolean_emoji_normalizer'],
+        'hal_json',
+      ],
+      'Format-agnostic @DataType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
+        ['test_datatype_boolean_emoji_normalizer', 'hal'],
+        'hal_json',
+      ],
+      'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the XML normalization' => [
+        ['test_fieldtype_boolean_emoji_normalizer'],
+        'xml',
+      ],
+      'Format-agnostic @DataType-level normalizers SHOULD be able to affect the XML normalization' => [
+        ['test_datatype_boolean_emoji_normalizer', 'hal'],
+        'xml',
+      ],
+    ];
+  }
+
 }