function DenormalizeTest::testMarkFieldForDeletion

Same name in other branches
  1. 9 core/modules/hal/tests/src/Kernel/DenormalizeTest.php \Drupal\Tests\hal\Kernel\DenormalizeTest::testMarkFieldForDeletion()

Test that a field set to an empty array is different than an absent field.

File

core/modules/hal/tests/src/Kernel/DenormalizeTest.php, line 110

Class

DenormalizeTest
Tests HAL denormalization edge cases for EntityResource.

Namespace

Drupal\Tests\hal\Kernel

Code

public function testMarkFieldForDeletion() {
    // Add a default value for a field.
    $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
    $field->setDefaultValue([
        [
            'value' => 'Llama',
        ],
    ]);
    $field->save();
    // Denormalize data that contains no entry for the field, and check that
    // the default value is present in the resulting entity.
    $data = [
        '_links' => [
            'type' => [
                'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
                    'absolute' => TRUE,
                ])->toString(),
            ],
        ],
    ];
    $entity = $this->serializer
        ->denormalize($data, $this->entityClass, $this->format);
    $this->assertEqual($entity->field_test_text
        ->count(), 1);
    $this->assertEqual($entity->field_test_text->value, 'Llama');
    // Denormalize data that contains an empty entry for the field, and check
    // that the field is empty in the resulting entity.
    $data = [
        '_links' => [
            'type' => [
                'href' => Url::fromUri('base:rest/type/entity_test/entity_test', [
                    'absolute' => TRUE,
                ])->toString(),
            ],
        ],
        'field_test_text' => [],
    ];
    $entity = $this->serializer
        ->denormalize($data, get_class($entity), $this->format, [
        'target_instance' => $entity,
    ]);
    $this->assertEqual($entity->field_test_text
        ->count(), 0);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.