diff --git a/core/includes/theme.inc b/core/includes/theme.inc index db6314d..ead0b95 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1134,7 +1134,7 @@ function theme_enable($theme_list) { $theme_config = \Drupal::config('system.theme'); $disabled_themes = \Drupal::config('system.theme.disabled'); $creation_id_generator = \Drupal::service('config.creation_id_generator'); - $creation_id_generator->setPrefix('theme_enable'); + $creation_id_generator->useStaticPrefix(); foreach ($theme_list as $key) { // Throw an exception if the theme name is too long. if (strlen($key) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { @@ -1162,7 +1162,7 @@ function theme_enable($theme_list) { // Invoke hook_themes_enabled() after the themes have been enabled. \Drupal::moduleHandler()->invokeAll('themes_enabled', array($theme_list)); - $creation_id_generator->unsetPrefix(); + $creation_id_generator->unsetStaticPrefix(); } /** diff --git a/core/lib/Drupal/Core/Config/CreationId.php b/core/lib/Drupal/Core/Config/CreationId.php index 1474275..9a7407f 100644 --- a/core/lib/Drupal/Core/Config/CreationId.php +++ b/core/lib/Drupal/Core/Config/CreationId.php @@ -18,9 +18,12 @@ * configuration files can be synchronised without issue. During usual runtime * the UUID generator is used to create a unique ID. * - * Prefixes are stacked so that a module's or profile's hook_install can call - * theme_enable() and the generated creation ID's remain consistent. Therefore, - * there should be no difference between a configuration entity created by a theme being enabled in the UI + * The number of times useStaticPrefix() is called is stored so that a module's + * or profile's hook_install() can call theme_enable() or + * \Drupal\Core\Extension\ModuleHandler::install() and the generated creation + * ID's will remain consistent. Therefore, there should be no difference + * between a configuration entity created by a theme being enabled in the UI or + * by calling theme_enable() from hook_install(). * * @see theme_enable() * @see \Drupal\Core\Extension\ModuleHandler::install() @@ -29,11 +32,14 @@ class CreationId implements CreationIdInterface { /** - * The stack of prefixes. + * The static prefix nesting level. * - * @var array + * If this is 0 then the static prefix should not be used. Values greater than + * 0 mean that static prefixing will be used. + * + * @var int */ - protected $prefixes = array(); + protected $staticPrefixNestingLevel = 0; /** * The UUID generator. @@ -53,19 +59,19 @@ public function __construct(UuidInterface $uuid_generator) { /** * {@inheritdoc} */ - public function setPrefix($prefix) { - $this->prefixes[] = $prefix; + public function useStaticPrefix() { + $this->staticPrefixNestingLevel++; } /** * {@inheritdoc} */ public function getPrefix() { - if (empty($this->prefixes)) { - $prefix = $this->uuidGenerator->generate(); + if ($this->staticPrefixNestingLevel > 0) { + $prefix = $this::STATIC_PREFIX; } else { - $prefix = end($this->prefixes); + $prefix = $this->uuidGenerator->generate(); } return $prefix; } @@ -83,10 +89,11 @@ public function generate($id) { /** * {@inheritdoc} */ - public function unsetPrefix() { - if (empty($this->prefixes)) { - throw new ConfigException('Cannot unset prefix when the CreationId::$prefixes array is empty.'); + public function unsetStaticPrefix() { + $this->staticPrefixNestingLevel--; + if ($this->staticPrefixNestingLevel < 0) { + throw new ConfigException('Cannot use this method when \Drupal\Core\Config\CreationId::useStaticPrefix() has not been called or \Drupal\Core\Config\CreationId::unsetStaticPrefix() has been called so many times the nesting level is now negative.'); } - return array_pop($this->prefixes); + return $this->staticPrefixNestingLevel; } } diff --git a/core/lib/Drupal/Core/Config/CreationIdInterface.php b/core/lib/Drupal/Core/Config/CreationIdInterface.php index 827a636..ee8857a 100644 --- a/core/lib/Drupal/Core/Config/CreationIdInterface.php +++ b/core/lib/Drupal/Core/Config/CreationIdInterface.php @@ -12,12 +12,14 @@ */ interface CreationIdInterface { + const STATIC_PREFIX = 'static-creation-id'; + /** - * Sets the creation id prefix. + * Makes the generator use a static prefix instead of a UUID. * * @param string $prefix */ - public function setPrefix($prefix); + public function useStaticPrefix(); /** * Gets the creation id prefix. @@ -36,14 +38,15 @@ public function getPrefix(); public function generate($id); /** - * Removes the current prefix from the prefixes array. + * Stops using the static prefix. * * @throws ConfigException - * If the prefixes array is empty an exception is thrown. + * If not use the static prefix an exception is thrown. * - * @return string - * The prefix that was removed from the prefixes array. + * @return int + * The current nesting level. If this is greater than 0 static prefixing + * will still be used. */ - public function unsetPrefix(); + public function unsetStaticPrefix(); } diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index 4a66a40..f0efed8 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -503,7 +503,7 @@ public static function parseDependency($dependency) { */ public function install(array $module_list, $enable_dependencies = TRUE) { $creation_id_generator = \Drupal::service('config.creation_id_generator'); - $creation_id_generator->setPrefix('ModuleHandler::install'); + $creation_id_generator->useStaticPrefix(); $module_config = \Drupal::config('system.module'); if ($enable_dependencies) { // Get all module data so we can find dependencies and sort. @@ -660,7 +660,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) { $this->invokeAll('modules_installed', array($modules_installed)); } - $creation_id_generator->unsetPrefix(); + $creation_id_generator->unsetStaticPrefix(); return TRUE; } diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallWebTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallWebTest.php index 3934f60..fcd425f 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigInstallWebTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigInstallWebTest.php @@ -98,7 +98,7 @@ protected function testCreationIdGeneration() { drupal_flush_all_caches(); $default_config_entity = entity_load('config_test', 'dotted.default'); $default_config_entity_creation_id = $default_config_entity->uuid(); - $this->assertEqual($default_config_entity_creation_id, 'ModuleHandler::install__dotted__default'); + $this->assertEqual($default_config_entity_creation_id, 'static-creation-id__dotted__default'); $edit = array( 'id' => 'test_creation_id', @@ -107,7 +107,7 @@ protected function testCreationIdGeneration() { $this->drupalPostForm('admin/structure/config_test/add', $edit, 'Save'); $custom_config_entity = entity_load('config_test', 'test_creation_id'); $custom_config_entity_creation_id = $custom_config_entity->uuid(); - $this->assertIdentical(FALSE, strpos($custom_config_entity_creation_id, 'ModuleHandler::install'), "Runtime generated configuration creation hash does not contain ModuleHandler::install"); + $this->assertIdentical(FALSE, strpos($custom_config_entity_creation_id, 'static-creation-id'), "Runtime generated configuration creation hash does not contain ModuleHandler::install"); \Drupal::moduleHandler()->uninstall(array('config_test')); diff --git a/core/profiles/standard/lib/Drupal/standard/Tests/StandardTest.php b/core/profiles/standard/lib/Drupal/standard/Tests/StandardTest.php index 89d40ba..ec257b6 100644 --- a/core/profiles/standard/lib/Drupal/standard/Tests/StandardTest.php +++ b/core/profiles/standard/lib/Drupal/standard/Tests/StandardTest.php @@ -67,11 +67,10 @@ function testStandard() { $this->drupalLogout(); $this->assertText('Main navigation'); - // Verify the configuration entity creation IDs are as expected. - $this->assertEqual(entity_load('view', 'frontpage')->uuid(), 'ModuleHandler::install__frontpage'); - // Once breakpoints are being handled correctly we will be able to assert - // that they are created during theme_enable() and have the expected - // creation ID. + // Verify the configuration entity creation IDs are as expected when + // created by enabling modules and themes during installation. + $this->assertEqual(entity_load('view', 'frontpage')->uuid(), 'static-creation-id__frontpage'); + $this->assertEqual(entity_load('breakpoint', 'theme.bartik.mobile')->uuid(), 'static-creation-id__theme__bartik__mobile'); } } diff --git a/core/tests/Drupal/Tests/Core/Config/CreationIdTest.php b/core/tests/Drupal/Tests/Core/Config/CreationIdTest.php index aa394aa..bde46ab 100644 --- a/core/tests/Drupal/Tests/Core/Config/CreationIdTest.php +++ b/core/tests/Drupal/Tests/Core/Config/CreationIdTest.php @@ -15,12 +15,12 @@ class CreationIdTest extends UnitTestCase { public static function getInfo() { return array( 'name' => 'Config creation ID test', - 'description' => 'Tests the use of prefixes by \Drupal\Core\Config\CreationId.', + 'description' => 'Tests the use of static and UUID prefixes by \Drupal\Core\Config\CreationId.', 'group' => 'Configuration' ); } - public function testCreationIdNoPrefix() { + public function testCreationIdNoStaticPrefix() { $uuid = $this->getMock('Drupal\Component\Uuid\UuidInterface'); $uuid->expects($this->once()) ->method('generate') @@ -30,28 +30,42 @@ public function testCreationIdNoPrefix() { $this->assertEquals('generated-uuid__test__id', $creationIdGenerator->generate('test.id')); } - public function testCreationIdPrefix() { + public function testCreationIdStaticPrefix() { $uuid = $this->getMock('Drupal\Component\Uuid\UuidInterface'); $uuid->expects($this->once()) ->method('generate') ->will($this->returnValue('generated-uuid')); $creationIdGenerator = new CreationId($uuid); - $creationIdGenerator->setPrefix('CreationIdTest'); - $this->assertEquals('CreationIdTest__test__id', $creationIdGenerator->generate('test.id')); + $creationIdGenerator->useStaticPrefix(); + $this->assertEquals('static-creation-id__test__id', $creationIdGenerator->generate('test.id')); - // Add a new prefix to the prefix stack and ensure that it is used. - $creationIdGenerator->setPrefix('AnotherPrefix'); - $this->assertEquals('AnotherPrefix__test__id', $creationIdGenerator->generate('test.id')); + // Unset the current prefix and ensure that we fallback to using UUID generator. + $creationIdGenerator->unsetStaticPrefix(); + $this->assertEquals('generated-uuid__test__id', $creationIdGenerator->generate('test.id')); + } + + public function testCreationIdNestingStaticPrefix() { + $uuid = $this->getMock('Drupal\Component\Uuid\UuidInterface'); + $uuid->expects($this->once()) + ->method('generate') + ->will($this->returnValue('generated-uuid')); + + $creationIdGenerator = new CreationId($uuid); + $creationIdGenerator->useStaticPrefix(); + $this->assertEquals('static-creation-id__test__id', $creationIdGenerator->generate('test.id')); + + $creationIdGenerator->useStaticPrefix(); + $this->assertEquals('static-creation-id__test__id', $creationIdGenerator->generate('test.id')); - // Unset the current prefix and ensure that the old prefix is used. - $creationIdGenerator->unsetPrefix(); - $this->assertEquals('CreationIdTest__test__id', $creationIdGenerator->generate('test.id')); + $nesting_level = $creationIdGenerator->unsetStaticPrefix(); + $this->assertEquals(1, $nesting_level); + $this->assertEquals('static-creation-id__test__id', $creationIdGenerator->generate('test.id')); // Unset the current prefix and ensure that we fallback to using UUID generator. - $creationIdGenerator->unsetPrefix(); + $nesting_level = $creationIdGenerator->unsetStaticPrefix(); + $this->assertEquals(0, $nesting_level); $this->assertEquals('generated-uuid__test__id', $creationIdGenerator->generate('test.id')); - } /** @@ -64,7 +78,7 @@ public function testUnsetPrefixException () { ->will($this->returnValue('generated-uuid')); $creationIdGenerator = new CreationId($uuid); - $creationIdGenerator->unsetPrefix(); + $creationIdGenerator->unsetStaticPrefix(); } } diff --git a/core/themes/bartik/config/breakpoint.breakpoint.theme.bartik.mobile.yml b/core/themes/bartik/config/breakpoint.breakpoint.theme.bartik.mobile.yml index 99740ce..7add4c0 100644 --- a/core/themes/bartik/config/breakpoint.breakpoint.theme.bartik.mobile.yml +++ b/core/themes/bartik/config/breakpoint.breakpoint.theme.bartik.mobile.yml @@ -1,5 +1,4 @@ id: theme.bartik.mobile -uuid: 676e11ac-c254-415a-881c-28786167ed69 name: mobile label: mobile mediaQuery: '(min-width: 0px)'