Commit 939a8a8d authored by alexpott's avatar alexpott
Browse files

Issue #1960574 by swentel, smiletrl: Add more tests for Field config import.

parent fc7897f3
......@@ -7,6 +7,8 @@
namespace Drupal\field\Tests;
use Drupal\field\Field;
/**
* Tests creating fields and instances as part of config import.
*/
......@@ -26,29 +28,65 @@ public static function getInfo() {
function testImportCreateDefault() {
$field_id = 'field_test_import';
$instance_id = "test_entity.test_bundle.$field_id";
$field_id_2 = 'field_test_import_2';
$instance_id_2a = "test_entity.test_bundle.$field_id_2";
$instance_id_2b = "test_entity.test_bundle_2.$field_id_2";
// Check that the field and instance do not exist yet.
// Check that the fields and instances do not exist yet.
$this->assertFalse(entity_load('field_entity', $field_id));
$this->assertFalse(entity_load('field_instance', $instance_id));
$this->assertFalse(entity_load('field_entity', $field_id_2));
$this->assertFalse(entity_load('field_instance', $instance_id_2a));
$this->assertFalse(entity_load('field_instance', $instance_id_2b));
// Create a second bundle for the 'Entity test' entity type.
entity_test_create_bundle('test_bundle_2');
// Enable field_test_config module and check that the field and instance
// shipped in the module's default config were created.
module_enable(array('field_test_config'));
// A field with one instance.
$field = entity_load('field_entity', $field_id);
$this->assertTrue($field, 'The field was created.');
$instance = entity_load('field_instance', $instance_id);
$this->assertTrue($instance, 'The field instance was deleted.');
// A field with multiple instances.
$field_2 = entity_load('field_entity', $field_id_2);
$this->assertTrue($field_2, 'The second field was created.');
$instance_2a = entity_load('field_instance', $instance_id_2a);
$this->assertTrue($instance->bundle, 'test_bundle', 'The second field instance was created on bundle test_bundle.');
$instance_2b = entity_load('field_instance', $instance_id_2b);
$this->assertTrue($instance->bundle, 'test_bundle_2', 'The second field instance was created on bundle test_bundle_2.');
// Tests field info contains the right data.
$instances = Field::fieldInfo()->getInstances('test_entity');
$this->assertEqual(count($instances['test_bundle']), 2);
$this->assertTrue(isset($instances['test_bundle']['field_test_import']));
$this->assertTrue(isset($instances['test_bundle']['field_test_import_2']));
$this->assertEqual(count($instances['test_bundle_2']), 1);
$this->assertTrue(isset($instances['test_bundle_2']['field_test_import_2']));
}
/**
* Tests creating fields and instances during config import.
*/
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);
......@@ -57,6 +95,9 @@ function testImportCreate() {
$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();
......@@ -66,6 +107,12 @@ function testImportCreate() {
$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');
}
}
......@@ -32,43 +32,62 @@ public static function getInfo() {
*/
public function testImportDelete() {
$field_id = 'field_test_import';
$field_id_2 = 'field_test_import_2';
$instance_id = "test_entity.test_bundle.$field_id";
$instance_id_2a = "test_entity.test_bundle.$field_id_2";
$instance_id_2b = "test_entity.test_bundle_2.$field_id_2";
$field_config_name = "field.field.$field_id";
$field_config_name_2 = "field.field.$field_id_2";
$instance_config_name = "field.instance.$instance_id";
$instance_config_name_2a = "field.instance.$instance_id_2a";
$instance_config_name_2b = "field.instance.$instance_id_2b";
// Create a second bundle for the 'Entity test' entity type.
entity_test_create_bundle('test_bundle_2');
// Import default config.
$this->installConfig(array('field_test_config'));
// Check that the config was correctly imported.
$field = entity_load('field_entity', $field_id);
$this->assertTrue($field, 'The field was created.');
$instance = entity_load('field_instance', $instance_id);
$this->assertTrue($instance, 'The field instance was created.');
// Get the uuid's for the fields.
$field_uuid = entity_load('field_entity', $field_id)->uuid();
$field_uuid_2 = entity_load('field_entity', $field_id_2)->uuid();
$field_uuid = $field->uuid;
$active = $this->container->get('config.storage');
$staging = $this->container->get('config.storage.staging');
$this->copyConfig($active, $staging);
$staging->delete($field_config_name);
$staging->delete($field_config_name_2);
$staging->delete($instance_config_name);
$staging->delete($instance_config_name_2a);
$staging->delete($instance_config_name_2b);
// Import the content of the staging directory.
$this->configImporter()->import();
// Check that the field and instance are gone.
// Check that the fields and instances are gone.
$field = entity_load('field_entity', $field_id, TRUE);
$this->assertFalse($field, 'The field was deleted.');
$field_2 = entity_load('field_entity', $field_id_2, TRUE);
$this->assertFalse($field_2, 'The second field was deleted.');
$instance = entity_load('field_instance', $instance_id, TRUE);
$this->assertFalse($instance, 'The field instance was deleted.');
$instance_2a = entity_load('field_instance', $instance_id_2a, TRUE);
$this->assertFalse($instance_2a, 'The second field instance on test bundle was deleted.');
$instance_2b = entity_load('field_instance', $instance_id_2b, TRUE);
$this->assertFalse($instance_2b, 'The second field instance on test bundle 2 was deleted.');
// Check that all config files are gone.
$active = $this->container->get('config.storage');
$this->assertIdentical($active->listAll($field_config_name), array());
$this->assertIdentical($active->listAll($field_config_name_2), array());
$this->assertIdentical($active->listAll($instance_config_name), array());
$this->assertIdentical($active->listAll($instance_config_name_2a), array());
$this->assertIdentical($active->listAll($instance_config_name_2b), array());
// Check that the field definition is preserved in state.
$deleted_fields = \Drupal::state()->get('field.field.deleted') ?: array();
$this->assertTrue(isset($deleted_fields[$field_uuid]));
$this->assertTrue(isset($deleted_fields[$field_uuid_2]));
// Purge field data, and check that the field definition has been completely
// removed once the data is purged.
......
......@@ -4,13 +4,13 @@ type: text
settings:
max_length: '255'
module: text
active: 1
active: '1'
entity_types: { }
storage:
type: field_sql_storage
settings: { }
module: field_sql_storage
active: 1
active: '1'
locked: '0'
cardinality: '1'
translatable: false
......
id: field_test_import_2
langcode: und
type: text
settings:
max_length: '255'
module: text
active: '1'
entity_types: { }
storage:
type: field_sql_storage
settings: { }
module: field_sql_storage
active: '1'
locked: '0'
cardinality: '1'
translatable: false
indexes:
format:
- format
......@@ -5,7 +5,7 @@ entity_type: test_entity
bundle: test_bundle
label: 'Test import field'
description: ''
required: 0
required: '0'
default_value: { }
default_value_function: ''
settings:
......
id: test_entity.test_bundle.field_test_import_2
langcode: und
field_name: field_test_import_2
entity_type: test_entity
bundle: test_bundle
label: 'Test import field 2 on test bundle'
description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
user_register_form: false
widget:
weight: '-2'
type: text_textfield
module: text
settings:
size: '60'
field_type: text
id: test_entity.test_bundle_2.field_test_import_2
langcode: und
field_name: field_test_import_2
entity_type: test_entity
bundle: test_bundle_2
label: 'Test import field 2 on test bundle 2'
description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
user_register_form: false
widget:
weight: '-2'
type: text_textfield
module: text
settings:
size: '60'
field_type: text
id: field_test_import_staging_2
uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
langcode: und
type: text
settings:
max_length: '255'
module: text
active: '1'
entity_types: { }
storage:
type: field_sql_storage
settings: { }
module: field_sql_storage
active: '1'
locked: '0'
cardinality: '1'
translatable: '0'
indexes:
format:
- format
id: test_entity.test_bundle.field_test_import_staging_2
uuid: f07794a2-d7cc-45b6-b40d-13cf021b5552
langcode: und
field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
entity_type: test_entity
bundle: test_bundle
label: 'Test import field 2 on test bundle'
description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
user_register_form: false
widget:
weight: '-2'
type: text_textfield
module: text
settings:
size: '60'
field_type: text
id: test_entity.test_bundle_2.field_test_import_staging_2
uuid: 49d6dd19-5097-443d-8f00-fc79525bebce
langcode: und
field_uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
entity_type: test_entity
bundle: test_bundle_2
label: 'Test import field 2 on test bundle 2'
description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
user_register_form: false
widget:
weight: '-2'
type: text_textfield
module: text
settings:
size: '60'
field_type: text
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment