diff --git a/core/includes/update.inc b/core/includes/update.inc index fe8034c..bd13dc7 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -1183,6 +1183,33 @@ function update_variables_to_config($config_name, array $variable_map) { } /** + * Updates 7.x variables to state records. + * + * Provides a generalized method to migrate variables from 7.x to 8.x's + * state() system. + * + * @param array $variable_map + * An associative array that maps old variables names to new state record + * names; e.g.: + * @code + * array('old_variable' => 'extension.new_name') + * @endcode + * This would migrate the value contained in variable name 'old_variable' into + * the state item 'extension.new_name'. + * Non-existing variables and variables with NULL values are omitted. + */ +function update_variables_to_state(array $variable_map) { + foreach ($variable_map as $variable_name => $state_name) { + if (NULL !== $value = update_variable_get($variable_name)) { + state()->set($state_name, $value); + } + } + + // Delete the migrated variables. + db_delete('variable')->condition('name', array_keys($variable_map), 'IN')->execute(); +} + +/** * Helper function to update entities with uuid. * * @param array $sandbox diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php new file mode 100644 index 0000000..280faad --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php @@ -0,0 +1,59 @@ + 'State system upgrade test', + 'description' => 'Tests upgrade of system variables to the state system.', + 'group' => 'Upgrade path', + ); + } + + public function setUp() { + $this->databaseDumpFiles = array( + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.bare.standard_all.database.php.gz', + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.state.system.database.php', + ); + parent::setUp(); + } + + /** + * Tests upgrade of system variables to state system. + */ + public function testSystemVariableUpgrade() { + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + $expected_state = array(); + + $expected_state['system.install_time'] = array( + 'value' => 1304208000, + 'variable_name' => 'install_time', + ); + $expected_state['system.install_task'] = array( + 'value' => 'done', + 'variable_name' => 'install_task', + ); + $expected_state['system.path_alias_whitelist'] = array( + 'value' => array( + + ), + 'variable_name' => 'path_alias_whitelist', + ); + + foreach ($expected_state as $name => $data) { + $this->assertIdentical(state()->get($name), $data['value']); + $deleted = !db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => $data['variable_name']))->fetchField(); + $this->assertTrue($deleted, format_string('Variable !name has been deleted.', array('!name' => $data['variable_name']))); + } + } +} diff --git a/core/modules/system/system.install b/core/modules/system/system.install index a8819c1..4c827e9 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2132,17 +2132,15 @@ function system_update_8025() { } /** - * Convert install_task and install_time variables to state api values. + * Migrates install_task and install_time variables to State API. + * + * @ingroup config_upgrade */ function system_update_8026() { - $variables = array('system.install_task', 'system.install_time'); - - foreach ($variables as $variable) { - if ($value = update_variable_get($variable, FALSE)) { - state()->set($variable, $value); - } - update_variable_del($variable); - } + update_variables_to_state(array( + 'install_task' => 'system.install_task', + 'install_time' => 'system.install_time', + )); } /** diff --git a/core/modules/system/tests/upgrade/drupal-7.state.system.database.php b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php new file mode 100644 index 0000000..54676b9 --- /dev/null +++ b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php @@ -0,0 +1,18 @@ +key(array('name' => 'install_time'))->fields(array('value' => serialize(1304208000))) + ->execute(); + +// Add non-default system settings.