Issue #1887654 by tim.plunkett, damiankloip, Berdir, marthinal, xjm: Creating a config entity with an existing machine name shouldn't work.

8.0.x
Alex Pott 2014-01-07 10:21:48 +00:00
parent 36adc62b7e
commit 261fb6483f
10 changed files with 25 additions and 73 deletions

View File

@ -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\Config;
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;
@ -344,9 +346,13 @@ class ConfigStorageController extends EntityStorageControllerBase {
$id = $entity->getOriginalId();
}
$config = $this->configFactory->get($prefix . $id);
$is_new = $config->isNew();
if (!$is_new && !isset($entity->original)) {
// Prevent overwriting an existing configuration file if the entity is new.
if ($entity->isNew() && !$config->isNew()) {
throw new EntityStorageException(String::format('@type entity with ID @id already exists.', array('@type' => $this->entityType, '@id' => $id)));
}
if (!$config->isNew() && !isset($entity->original)) {
$this->resetCache(array($id));
$entity->original = $this->load($id);
}
@ -372,7 +378,7 @@ class ConfigStorageController extends EntityStorageControllerBase {
$config->set($key, $value);
}
if (!$is_new) {
if (!$config->isNew()) {
$return = SAVED_UPDATED;
$config->save();
$entity->postSave($this, TRUE);

View File

@ -31,28 +31,6 @@ class CommentPreviewTest extends CommentTestBase {
);
}
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' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6> <p> <span> <img>',
),
));
$filter_format->save();
}
/**
* Tests comment preview.
*/

View File

@ -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 @@ class ConfigEntityTest extends WebTestBase {
$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.
// Ensure that creating an entity with the same id as an existing one is not
// possible.
$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));

View File

@ -316,12 +316,6 @@ class Field extends ConfigEntityBase implements FieldInterface {
));
}
// 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.

View File

@ -346,10 +346,6 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface {
$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.

View File

@ -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 @@ class CrudTest extends FieldUnitTestBase {
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.'));
}

View File

@ -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 @@ class FieldInstanceCrudTest extends FieldUnitTestBase {
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.'));
}

View File

@ -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

View File

@ -142,6 +142,8 @@ class ViewStorageTest extends ViewUnitTestBase {
// 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 @@ class ViewStorageTest extends ViewUnitTestBase {
}
// 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.');

View File

@ -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: ''