You are here

public function FieldCrudTest::testCreateFieldCustomStorage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateFieldCustomStorage()

Test creating a field with custom storage set.

File

core/modules/field/tests/src/Kernel/FieldCrudTest.php, line 200

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreateFieldCustomStorage() {
  $field_name = mb_strtolower($this
    ->randomMachineName());
  \Drupal::state()
    ->set('field_test_custom_storage', $field_name);
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'test_field',
    'custom_storage' => TRUE,
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => $field_storage
      ->getName(),
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ]);
  $field
    ->save();
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  // Check that no table has been created for the field.
  $this
    ->assertFalse(\Drupal::database()
    ->schema()
    ->tableExists('entity_test__' . $field_storage
    ->getName()));

  // Save an entity with a value in the custom storage field and verify no
  // data is retrieved on load.
  $entity = EntityTest::create([
    'name' => $this
      ->randomString(),
    $field_name => 'Test value',
  ]);
  $this
    ->assertIdentical('Test value', $entity->{$field_name}->value, 'The test value is set on the field.');
  $entity
    ->save();
  $entity = EntityTest::load($entity
    ->id());
  $this
    ->assertNull($entity->{$field_name}->value, 'The loaded entity field value is NULL.');
}