diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index 458d765..824d18e 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Config\Entity; +use Drupal\Component\Utility\String; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityStorageControllerBase; @@ -14,6 +15,7 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Component\Uuid\UuidInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -346,6 +348,12 @@ public function save(EntityInterface $entity) { $config = $this->configFactory->get($prefix . $id); $is_new = $config->isNew(); + // If the entity is new but a config file for it already exists, prevent + // it to be overwritten. + if ($entity->isNew() && !$config->isNew()) { + throw new EntityStorageException(String::format('@type entity with ID @id already exists.', array('@type' => $this->entityType, '@id' => $id))); + } + if (!$is_new && !isset($entity->original)) { $this->resetCache(array($id)); $entity->original = $this->load($id); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php index 260da45..e72ed99 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentPreviewTest.php @@ -31,28 +31,6 @@ public static function getInfo() { ); } - function setUp() { - parent::setUp(); - - // Add the basic_html filter format from the standard install profile. - $filter_format_storage_controller = $this->container->get('entity.manager')->getStorageController('filter_format'); - $filter_format = $filter_format_storage_controller->create(array( - 'format' => 'basic_html', - 'name' => 'Basic HTML', - 'status' => TRUE, - 'roles' => array('authenticated'), - ), 'filter_format'); - - $filter_format->setFilterConfig('filter_html', array( - 'module' => 'filter', - 'status' => TRUE, - 'settings' => array( - 'allowed_html' => '
    1. ', - ), - )); - $filter_format->save(); - } - /** * Tests comment preview. */ diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php index 9acca96..955177e 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php @@ -8,6 +8,7 @@ namespace Drupal\config\Tests; use Drupal\Core\Entity\EntityMalformedException; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Language\Language; use Drupal\simpletest\WebTestBase; @@ -135,24 +136,18 @@ function testCRUD() { $this->assertIdentical($config_test->isNew(), FALSE); $this->assertIdentical($config_test->getOriginalId(), $expected['id']); - // Re-create the entity with the same ID and verify updated status. + // Make sure that is not possible to overwrite an existing entity with + // a new one. $same_id = entity_create('config_test', array( 'id' => $config_test->id(), )); $this->assertIdentical($same_id->isNew(), TRUE); - $status = $same_id->save(); - $this->assertIdentical($status, SAVED_UPDATED); - - // Verify that the entity was overwritten. - $same_id = entity_load('config_test', $config_test->id()); - $this->assertIdentical($same_id->id(), $config_test->id()); - $this->assertIdentical($same_id->label(), NULL); - $this->assertNotEqual($same_id->uuid(), $config_test->uuid()); - - // Delete the overridden entity first. - $same_id->delete(); - // Revert to previous state. - $config_test->save(); + try { + $same_id->save(); + $this->fail('Not possible to overwrite an entity entity.'); + } catch (EntityStorageException $e) { + $this->pass('Not possible to overwrite an entity entity.'); + } // Verify that renaming the ID returns correct status and properties. $ids = array($expected['id'], 'second_' . $this->randomName(4), 'third_' . $this->randomName(4)); diff --git a/core/modules/field/lib/Drupal/field/Entity/Field.php b/core/modules/field/lib/Drupal/field/Entity/Field.php index b127979..6d5e68a 100644 --- a/core/modules/field/lib/Drupal/field/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Entity/Field.php @@ -316,12 +316,6 @@ protected function preSaveNew(EntityStorageControllerInterface $storage_controll )); } - // Ensure the field name is unique (we do not care about deleted fields). - if ($prior_field = $storage_controller->load($this->id)) { - $message = 'Attempt to create field name %name which already exists.'; - throw new FieldException(format_string($message, array('%name' => $this->name))); - } - // Disallow reserved field names. This can't prevent all field name // collisions with existing entity properties, but some is better than // none. diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index 7c2e381..51eebbd 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -346,10 +346,6 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); if ($this->isNew()) { - // Ensure the field instance is unique within the bundle. - if ($prior_instance = $storage_controller->load($this->id())) { - throw new FieldException(format_string('Attempt to create an instance of field %name on bundle @bundle that already has an instance of that field.', array('%name' => $this->field->name, '@bundle' => $this->bundle))); - } // Set the default instance settings. $this->settings += $field_type_manager->getDefaultInstanceSettings($this->field->type); // Notify the entity storage controller. diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index 73ee608..b7e9209 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -7,6 +7,7 @@ namespace Drupal\field\Tests; +use Drupal\Core\Entity\EntityStorageException; use Drupal\field\FieldException; class CrudTest extends FieldUnitTestBase { @@ -70,7 +71,7 @@ function testCreateField() { entity_create('field_entity', $field_definition)->save(); $this->fail(t('Cannot create two fields with the same name.')); } - catch (FieldException $e) { + catch (EntityStorageException $e) { $this->pass(t('Cannot create two fields with the same name.')); } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index e05baac..9eaa8d0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -7,6 +7,7 @@ namespace Drupal\field\Tests; +use Drupal\Core\Entity\EntityStorageException; use Drupal\field\FieldException; class FieldInstanceCrudTest extends FieldUnitTestBase { @@ -90,7 +91,7 @@ function testCreateFieldInstance() { entity_create('field_instance', $this->instance_definition)->save(); $this->fail(t('Cannot create two instances with the same field / bundle combination.')); } - catch (FieldException $e) { + catch (EntityStorageException $e) { $this->pass(t('Cannot create two instances with the same field / bundle combination.')); } diff --git a/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_field_type.yml b/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_field_type.yml index e6382e4..6113c52 100644 --- a/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_field_type.yml +++ b/core/modules/node/tests/modules/node_test_views/test_views/views.view.test_field_type.yml @@ -10,6 +10,7 @@ display: field: type id: type table: node_field_data + plugin_id: node_type provider: node display_plugin: default display_title: Master diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php index 70df481..4fc2f5c 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewStorageTest.php @@ -142,6 +142,8 @@ protected function createTests() { // Create a new View instance with config values. $values = \Drupal::config('views.view.test_view_storage')->get(); + $values['id'] = 'test_view_storage_new'; + unset($values['uuid']); $created = $this->controller->create($values); $this->assertTrue($created instanceof View, 'Created object is a View.'); @@ -157,7 +159,6 @@ protected function createTests() { } // Check the UUID of the loaded View. - $created->set('id', 'test_view_storage_new'); $created->save(); $created_loaded = entity_load('view', 'test_view_storage_new'); $this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created UUID has been saved correctly.'); diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_field_type.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_field_type.yml deleted file mode 100644 index 6113c52..0000000 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_field_type.yml +++ /dev/null @@ -1,21 +0,0 @@ -base_table: node -core: '8' -description: '' -status: '1' -display: - default: - display_options: - fields: - type: - field: type - id: type - table: node_field_data - plugin_id: node_type - provider: node - display_plugin: default - display_title: Master - id: default - position: '0' -label: '' -id: test_field_type -tag: ''