function FieldSqlStorageTest::testNullSerializedFieldLoad

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldSqlStorageTest::testNullSerializedFieldLoad()

Tests loading base and configurable serialized fields with NULL values.

Note that this tests loading field values that are literally NULL. It doesn't test that NULL values are serialized and unserialized properly.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php, line 600

Class

FieldSqlStorageTest
Tests Field SQL Storage .

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testNullSerializedFieldLoad() : void {
  $connection = Database::getConnection();
  $entity_type = $bundle = 'entity_test_serialized_field';
  $this->installEntitySchema($entity_type);
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type);
  // The serialized base field is already part of the test entity type, but
  // a configurable field has to be created.
  $serialized_field = $this->randomMachineName();
  $field_storage = FieldStorageConfig::create([
    'field_name' => $serialized_field,
    'entity_type' => $entity_type,
    'type' => 'nullable_serialized_item_test',
    'cardinality' => 1,
    'required' => FALSE,
  ]);
  $field_storage->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $bundle,
  ]);
  $field->save();
  $entity = $storage->create();
  $entity->save();
  // Manually set the field values to NULL with database queries.
  $connection->update('entity_test_serialized_fields')
    ->fields([
    'serialized' => NULL,
  ])
    ->condition('id', $entity->id())
    ->execute();
  $connection->insert($entity_type . '__' . $serialized_field)
    ->fields([
    'bundle' => $bundle,
    'deleted' => 0,
    'entity_id' => $entity->id(),
    'revision_id' => $entity->id(),
    'langcode' => $entity->language()
      ->getId(),
    'delta' => 0,
    $serialized_field . '_value' => NULL,
  ])
    ->execute();
  $storage->load($entity->id());
  $this->assertNull($entity->serialized->value);
  $this->assertNull($entity->{$serialized_field}->value);
}

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