function FieldStorageCrudTest::testRead

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php \Drupal\Tests\field\Kernel\FieldStorageCrudTest::testRead()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php \Drupal\Tests\field\Kernel\FieldStorageCrudTest::testRead()
  3. 11.x 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 206

Class

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

Namespace

Drupal\Tests\field\Kernel

Code

public function testRead() : void {
  $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->assertEmpty($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();
}

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