You are here

public function FieldStorageCrudTest::testRead in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php \Drupal\Tests\field\Kernel\FieldStorageCrudTest::testRead()

Tests reading field storage definitions.

File

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

Class

FieldStorageCrudTest
Tests field storage create, read, update, and delete.

Namespace

Drupal\Tests\field\Kernel

Code

public function testRead() {
  $field_storage_definition = [
    'field_name' => 'field_1',
    'entity_type' => 'entity_test',
    'type' => 'test_field',
  ];
  $field_storage = FieldStorageConfig::create($field_storage_definition);
  $field_storage
    ->save();
  $id = $field_storage
    ->id();

  // Check that 'single column' criteria works.
  $field_storage_config_storage = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config');
  $fields = $field_storage_config_storage
    ->loadByProperties([
    'field_name' => $field_storage_definition['field_name'],
  ]);
  $this
    ->assertCount(1, $fields, 'The field was properly read.');
  $this
    ->assertArrayHasKey($id, $fields, 'The field has the correct key.');

  // Check that 'multi column' criteria works.
  $fields = $field_storage_config_storage
    ->loadByProperties([
    'field_name' => $field_storage_definition['field_name'],
    'type' => $field_storage_definition['type'],
    'entity_type' => $field_storage_definition['entity_type'],
  ]);
  $this
    ->assertCount(1, $fields, 'The field was properly read.');
  $this
    ->assertArrayHasKey($id, $fields, 'The field has the correct key.');
  $fields = $field_storage_config_storage
    ->loadByProperties([
    'field_name' => $field_storage_definition['field_name'],
    'type' => 'foo',
  ]);
  $this
    ->assertTrue(empty($fields), 'No field was found.');

  // Create a field from the field storage.
  $field_definition = [
    'field_name' => $field_storage_definition['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ];
  FieldConfig::create($field_definition)
    ->save();
}