diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldUUIDConflictTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldUUIDConflictTest.php new file mode 100644 index 0000000..a161130 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldUUIDConflictTest.php @@ -0,0 +1,108 @@ + 'UUID conflict', + 'description' => 'Tests staging and importing a field with the same ID but a different UUID.', + 'group' => 'Field API', + ); + } + + public function setUp() { + parent::setUp(); + + // Import the default config. + $this->installConfig(array('field_test_config')); + + // These are the types used in field_test_config's default config. + $this->entity_type = 'test_entity'; + $this->bundle = 'test_bundle'; + $this->field_id = 'field_test_import'; + $this->instance_id = "{$this->entity_type}.{$this->bundle}.{$this->field_id}"; + // Load the original field and instance entities. + $this->original_field = entity_load('field_entity', $this->field_id); + $this->original_instance = entity_load('field_instance', $this->instance_id); + + } + + /** + * Tests importing an updated field instance. + */ + function testChangedUUID() { + // Stage and attempt to import a changed field UUID. + $this->stageUUIDChange($this->field_id, 'field.field'); + $this->assertNoImport(); + + // Stage and attempt to import a changed instance UUID. + $this->stageUUIDChange($this->instance_id, 'field.instance'); + $this->assertNoImport(); + + // Stage and attempt to import both changed field and instance UUIDs. + $this->stageUUIDChange($this->field_id, 'field.field'); + $this->stageUUIDChange($this->instance_id, 'field.instance'); + $this->assertNoImport(); + + } + + /** + * Creates and stages a copy of a configuration object with a different UUID. + */ + public function stageUUIDChange($id, $prefix) { + // Create a copy of the object with the same ID but a different UUID. + $active = $this->container->get('config.storage'); + $config = $active->read("$prefix.$id"); + $uuid = new Uuid(); + $new_uuid = $uuid->generate(); + $config['uuid'] = $new_uuid; + + // Clear any previously staged config and save a file in the staging + // directory. + $staging = $this->container->get('config.storage.staging'); + $staging->deleteAll(); + $staging->write("$prefix.$id", $config); + } + + /** + * Asserts that an exception is thrown and the field data is not corrupted. + */ + public function assertNoImport() { + // Import the content of the staging directory. + try { + config_import(); + $this->fail('Exception thrown when attempting to import a field definition with a UUID that does not match the existing UUID.'); + } + catch (FieldException $e) { + $this->pass(format_string('Exception %e thrown when attempting to import a field definition with a UUID that does not match the existing UUID.', array('%e' => $e))); + } + + // Ensure that the field and instance were not corrupted. + $field = entity_load('field_entity', $this->field_id); + $instance = entity_load('field_instance', $this->instance_id); + $this->assertEqual($field, $this->original_field); + $this->assertEqual($instance, $this->original_instance); + } + +}