You are here

public function FieldImportCreateTest::testImportCreateDefault in Drupal 8

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

Tests creating field storages and fields during default config import.

File

core/modules/field/tests/src/Kernel/FieldImportCreateTest.php, line 19

Class

FieldImportCreateTest
Create field storages and fields during config create method invocation.

Namespace

Drupal\Tests\field\Kernel

Code

public function testImportCreateDefault() {
  $field_name = 'field_test_import';
  $field_storage_id = "entity_test.{$field_name}";
  $field_id = "entity_test.entity_test.{$field_name}";
  $field_name_2 = 'field_test_import_2';
  $field_storage_id_2 = "entity_test.{$field_name_2}";
  $field_id_2a = "entity_test.entity_test.{$field_name_2}";
  $field_id_2b = "entity_test.test_bundle.{$field_name_2}";

  // Check that the field storages and fields do not exist yet.
  $this
    ->assertNull(FieldStorageConfig::load($field_storage_id));
  $this
    ->assertNull(FieldConfig::load($field_id));
  $this
    ->assertNull(FieldStorageConfig::load($field_storage_id_2));
  $this
    ->assertNull(FieldConfig::load($field_id_2a));
  $this
    ->assertNull(FieldConfig::load($field_id_2b));

  // Create a second bundle for the 'Entity test' entity type.
  entity_test_create_bundle('test_bundle');

  // Enable field_test_config module and check that the field and storage
  // shipped in the module's default config were created.
  \Drupal::service('module_installer')
    ->install([
    'field_test_config',
  ]);

  // A field storage with one single field.
  $field_storage = FieldStorageConfig::load($field_storage_id);
  $this
    ->assertNotEmpty($field_storage, 'The field was created.');
  $field = FieldConfig::load($field_id);
  $this
    ->assertNotEmpty($field, 'The field was deleted.');

  // A field storage with two fields.
  $field_storage_2 = FieldStorageConfig::load($field_storage_id_2);
  $this
    ->assertNotEmpty($field_storage_2, 'The second field was created.');
  $field2a = FieldConfig::load($field_id_2a);
  $this
    ->assertEquals('entity_test', $field2a
    ->getTargetBundle(), 'The second field was created on bundle entity_test.');
  $field2b = FieldConfig::load($field_id_2b);
  $this
    ->assertEquals('test_bundle', $field2b
    ->getTargetBundle(), 'The second field was created on bundle test_bundle.');

  // Tests fields.
  $ids = \Drupal::entityQuery('field_config')
    ->condition('entity_type', 'entity_test')
    ->condition('bundle', 'entity_test')
    ->execute();
  $this
    ->assertCount(2, $ids);
  $this
    ->assertTrue(isset($ids['entity_test.entity_test.field_test_import']));
  $this
    ->assertTrue(isset($ids['entity_test.entity_test.field_test_import_2']));
  $ids = \Drupal::entityQuery('field_config')
    ->condition('entity_type', 'entity_test')
    ->condition('bundle', 'test_bundle')
    ->execute();
  $this
    ->assertCount(1, $ids);
  $this
    ->assertTrue(isset($ids['entity_test.test_bundle.field_test_import_2']));
}