You are here

public function DenormalizeTest::testMarkFieldForDeletion in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Tests/DenormalizeTest.php \Drupal\hal\Tests\DenormalizeTest::testMarkFieldForDeletion()

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

File

core/modules/hal/src/Tests/DenormalizeTest.php, line 85
Contains \Drupal\hal\Tests\DenormalizeTest.

Class

DenormalizeTest
Tests that entities can be denormalized from HAL.

Namespace

Drupal\hal\Tests

Code

public function testMarkFieldForDeletion() {

  // Add a default value for a field.
  $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
  $field
    ->setDefaultValue(array(
    array(
      '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 = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array(
          '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 = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array(
          'absolute' => TRUE,
        ))
          ->toString(),
      ),
    ),
    'field_test_text' => array(),
  );
  $entity = $this->serializer
    ->denormalize($data, get_class($entity), $this->format, [
    'target_instance' => $entity,
  ]);
  $this
    ->assertEqual($entity->field_test_text
    ->count(), 0);
}