function FieldImportCreateTest::testImportCreate

Tests creating fields and instances during config import.

File

drupal/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php, line 75
Contains \Drupal\field\Tests\FieldImportCreateTest.

Class

FieldImportCreateTest
Tests creating fields and instances as part of config import.

Namespace

Drupal\field\Tests

Code

function testImportCreate() {

  // One field with one field instance.
  $field_id = 'field_test_import_staging';
  $instance_id = "test_entity.test_bundle.{$field_id}";
  $field_config_name = "field.field.{$field_id}";
  $instance_config_name = "field.instance.{$instance_id}";

  // One field with two field instances.
  $field_id_2 = 'field_test_import_staging_2';
  $instance_id_2a = "test_entity.test_bundle.{$field_id_2}";
  $instance_id_2b = "test_entity.test_bundle_2.{$field_id_2}";
  $field_config_name_2 = "field.field.{$field_id_2}";
  $instance_config_name_2a = "field.instance.{$instance_id_2a}";
  $instance_config_name_2b = "field.instance.{$instance_id_2b}";
  $active = $this->container
    ->get('config.storage');
  $staging = $this->container
    ->get('config.storage.staging');
  $this
    ->copyConfig($active, $staging);

  // Add the new files to the staging directory.
  $src_dir = drupal_get_path('module', 'field_test_config') . '/staging';
  $this
    ->assertTrue(file_unmanaged_copy("{$src_dir}/{$field_config_name}.yml", "public://config_staging/{$field_config_name}.yml"));
  $this
    ->assertTrue(file_unmanaged_copy("{$src_dir}/{$instance_config_name}.yml", "public://config_staging/{$instance_config_name}.yml"));
  $this
    ->assertTrue(file_unmanaged_copy("{$src_dir}/{$field_config_name_2}.yml", "public://config_staging/{$field_config_name_2}.yml"));
  $this
    ->assertTrue(file_unmanaged_copy("{$src_dir}/{$instance_config_name_2a}.yml", "public://config_staging/{$instance_config_name_2a}.yml"));
  $this
    ->assertTrue(file_unmanaged_copy("{$src_dir}/{$instance_config_name_2b}.yml", "public://config_staging/{$instance_config_name_2b}.yml"));

  // Import the content of the staging directory.
  $this
    ->configImporter()
    ->import();

  // Check that the field and instance were created.
  $field = entity_load('field_entity', $field_id);
  $this
    ->assertTrue($field, 'Test import field from staging exists');
  $instance = entity_load('field_instance', $instance_id);
  $this
    ->assertTrue($instance, 'Test import field instance from staging exists');
  $field = entity_load('field_entity', $field_id_2);
  $this
    ->assertTrue($field, 'Test import field 2 from staging exists');
  $instance = entity_load('field_instance', $instance_id_2a);
  $this
    ->assertTrue($instance, 'Test import field instance 2a from staging exists');
  $instance = entity_load('field_instance', $instance_id_2b);
  $this
    ->assertTrue($instance, 'Test import field instance 2b from staging exists');
}