From add237018837920d626e6b07278f7e3010668f85 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Mon, 14 May 2018 09:28:54 +1000 Subject: [PATCH] Issue #2972003 by alexpott: Verify that the file name matches the ID when installing configuration --- core/lib/Drupal/Core/Config/ConfigInstaller.php | 6 +++++- ....default.yml => config_test.dynamic.isinstallable.yml} | 0 .../config/install/config_test.dynamic.no_id_match.yml | 5 +++++ .../config_test_id_mismatch.info.yml | 7 +++++++ .../Drupal/KernelTests/Core/Config/ConfigInstallTest.php | 8 ++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) rename core/modules/config/tests/config_test/config/install/{config_test.dynamic.isinstallable.default.yml => config_test.dynamic.isinstallable.yml} (100%) create mode 100644 core/modules/config/tests/config_test_id_mismatch/config/install/config_test.dynamic.no_id_match.yml create mode 100644 core/modules/config/tests/config_test_id_mismatch/config_test_id_mismatch.info.yml diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php index d7788161cba..24d735b99d5 100644 --- a/core/lib/Drupal/Core/Config/ConfigInstaller.php +++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php @@ -315,10 +315,11 @@ class ConfigInstaller implements ConfigInstallerInterface { $entity_storage = $this->configManager ->getEntityManager() ->getStorage($entity_type); + + $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); // It is possible that secondary writes can occur during configuration // creation. Updates of such configuration are allowed. if ($this->getActiveStorages($collection)->exists($name)) { - $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get()); } @@ -327,6 +328,9 @@ class ConfigInstaller implements ConfigInstallerInterface { } if ($entity->isInstallable()) { $entity->trustData()->save(); + if ($id !== $entity->id()) { + trigger_error(sprintf('The configuration name "%s" does not match the ID "%s"', $name, $entity->id()), E_USER_WARNING); + } } } else { diff --git a/core/modules/config/tests/config_test/config/install/config_test.dynamic.isinstallable.default.yml b/core/modules/config/tests/config_test/config/install/config_test.dynamic.isinstallable.yml similarity index 100% rename from core/modules/config/tests/config_test/config/install/config_test.dynamic.isinstallable.default.yml rename to core/modules/config/tests/config_test/config/install/config_test.dynamic.isinstallable.yml diff --git a/core/modules/config/tests/config_test_id_mismatch/config/install/config_test.dynamic.no_id_match.yml b/core/modules/config/tests/config_test_id_mismatch/config/install/config_test.dynamic.no_id_match.yml new file mode 100644 index 00000000000..879a53b5b90 --- /dev/null +++ b/core/modules/config/tests/config_test_id_mismatch/config/install/config_test.dynamic.no_id_match.yml @@ -0,0 +1,5 @@ +id: does_not_match +label: Default +weight: 0 +protected_property: Default +langcode: en diff --git a/core/modules/config/tests/config_test_id_mismatch/config_test_id_mismatch.info.yml b/core/modules/config/tests/config_test_id_mismatch/config_test_id_mismatch.info.yml new file mode 100644 index 00000000000..7cd2e0926a8 --- /dev/null +++ b/core/modules/config/tests/config_test_id_mismatch/config_test_id_mismatch.info.yml @@ -0,0 +1,7 @@ +name: 'Configuration test ID mismatch' +type: module +package: Testing +version: VERSION +core: 8.x +dependencies: + - drupal:config_test diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php index 9e473c29ff6..5efd20bbb15 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php @@ -253,6 +253,14 @@ class ConfigInstallTest extends KernelTestBase { ); } + /** + * Tests installing configuration where the filename and ID do not match. + */ + public function testIdMisMatch() { + $this->setExpectedException(\PHPUnit_Framework_Error_Warning::class, 'The configuration name "config_test.dynamic.no_id_match" does not match the ID "does_not_match"'); + $this->installModules(['config_test_id_mismatch']); + } + /** * Installs a module. *