You are here

public function FieldCrudTest::testCreateFieldCustomStorage in Zircon Profile 8

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

Test creating a field with custom storage set.

File

core/modules/field/src/Tests/FieldCrudTest.php, line 144
Contains \Drupal\field\Tests\FieldCrudTest.

Class

FieldCrudTest
Create field entities by attaching fields to entities.

Namespace

Drupal\field\Tests

Code

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