function FieldCrudTest::testCreateFieldCustomStorage

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateFieldCustomStorage()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateFieldCustomStorage()
  3. 11.x core/modules/field/tests/src/Kernel/FieldCrudTest.php \Drupal\Tests\field\Kernel\FieldCrudTest::testCreateFieldCustomStorage()

Tests creating a field with custom storage set.

File

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

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreateFieldCustomStorage() : void {
    $field_name = $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->assertSame('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.');
}

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