From 6f716eb1ed739962df12878ec3df00c4c044b75a Mon Sep 17 00:00:00 2001 From: Herb v/d Dool Date: Wed, 20 Sep 2023 13:10:33 -0400 Subject: [PATCH] Fixes so tests pass. --- drush/key_delete.inc | 4 ++-- drush/key_list.inc | 2 +- drush/key_save.inc | 4 ++-- drush/key_value_get.inc | 2 +- key.module | 53 +++++++++++++---------------------------- tests/key.test | 12 +++++----- 6 files changed, 28 insertions(+), 49 deletions(-) diff --git a/drush/key_delete.inc b/drush/key_delete.inc index 70303b6..a37caf5 100644 --- a/drush/key_delete.inc +++ b/drush/key_delete.inc @@ -14,7 +14,7 @@ use Drush\Log\LogLevel; function _drush_key_delete($id) { // Look for a key with the specified ID. If one does not exist, set an // error and abort. - $key = key_get_key($id, TRUE); + $key = key_get_key($id); if (!$key) { return drush_set_error('DRUSH_KEY_DOES_NOT_EXIST', dt('Key !id does not exist.', array('!id' => $id))); } @@ -30,7 +30,7 @@ function _drush_key_delete($id) { key_delete_key($id, FALSE); // Try to load the key to confirm that it was deleted. - $key_check = key_get_key($id, TRUE); + $key_check = key_get_key($id); // If the key still exists, set an error and abort. if ($key_check) { diff --git a/drush/key_list.inc b/drush/key_list.inc index 2ba8994..c17d805 100644 --- a/drush/key_list.inc +++ b/drush/key_list.inc @@ -6,7 +6,7 @@ function _drush_key_list() { $result = array(); - $keys = key_get_keys(TRUE); + $keys = key_get_keys(); $key_types = key_get_plugins_as_options('key_type', TRUE, TRUE); $key_providers = key_get_plugins_as_options('key_provider', TRUE, TRUE); diff --git a/drush/key_save.inc b/drush/key_save.inc index c58a8f0..9c3b35c 100644 --- a/drush/key_save.inc +++ b/drush/key_save.inc @@ -20,7 +20,7 @@ function _drush_key_save($id, $key_value = NULL) { // Look for a key with the specified ID. If one exists, and the overwrite // option was not specified, set an error and abort. - $existing_key = key_get_key($id, TRUE); + $existing_key = key_get_key($id); $overwrite = drush_get_option('overwrite'); if ($existing_key && !$overwrite) { return drush_set_error('DRUSH_KEY_EXISTS', dt('Key !id exists; specify --overwrite to overwrite.', array('!id' => $values['id']))); @@ -101,7 +101,7 @@ function _drush_key_save($id, $key_value = NULL) { key_save_key($values, $existing_key, FALSE); // Load the key to confirm that it was saved. - $key_check = key_get_key($values['id'], TRUE); + $key_check = key_get_key($values['id']); if (!$key_check) { return drush_set_error('DRUSH_KEY_NOT_SAVED', dt('Key !id was not saved.', array('!id' => $values['id']))); diff --git a/drush/key_value_get.inc b/drush/key_value_get.inc index 723289e..ffb5b86 100644 --- a/drush/key_value_get.inc +++ b/drush/key_value_get.inc @@ -13,7 +13,7 @@ function _drush_key_value_get($id) { // Look for a key with the specified ID. If one does not exist, set an // error and abort. - $key = key_get_key($id, TRUE); + $key = key_get_key($id); if (!$key) { return drush_set_error('DRUSH_KEY_DOES_NOT_EXIST', dt('Key !id does not exist.', array('!id' => $id))); } diff --git a/key.module b/key.module index 0a7d141..f474faf 100644 --- a/key.module +++ b/key.module @@ -349,24 +349,17 @@ function key_get_plugin($type, $plugin_id, $reset = FALSE) { /** * Gets information about all key configurations. * - * @param bool $reset - * A flag to force the configurations to be retrieved from the database. - * * @return array * An array of configurations. */ -function key_get_keys($reset = FALSE) { - $configs = &backdrop_static(__FUNCTION__); - - if (!isset($configs) || $reset) { - $config_keys = config_get_names_with_prefix('key.key'); - foreach ($config_keys as $config_key) { - $key_config = config_get($config_key); - $configs[$key_config['id']] = $key_config; - } +function key_get_keys() { + $config_keys = config_get_names_with_prefix('key.key.'); + foreach ($config_keys as $config_key) { + $key_config = config_get($config_key); + $configs[$key_config['id']] = $key_config; } - return $configs; + return is_array($configs) ? $configs : array(); } /** @@ -382,7 +375,7 @@ function key_get_keys($reset = FALSE) { */ function key_get_keys_by_provider($key_provider_id, $reset = FALSE) { $filtered_keys = array(); - $keys = key_get_keys($reset); + $keys = key_get_keys(); foreach ($keys as $id => $key) { if ($key['key_provider'] == $key_provider_id) { @@ -406,7 +399,7 @@ function key_get_keys_by_provider($key_provider_id, $reset = FALSE) { */ function key_get_keys_by_type($key_type_id, $reset = FALSE) { $filtered_keys = array(); - $keys = key_get_keys($reset); + $keys = key_get_keys(); foreach ($keys as $id => $key) { if ($key['key_type'] == $key_type_id) { @@ -430,7 +423,7 @@ function key_get_keys_by_type($key_type_id, $reset = FALSE) { */ function key_get_keys_by_storage_method($storage_method, $reset = FALSE) { $filtered_keys = array(); - $keys = key_get_keys($reset); + $keys = key_get_keys(); $providers = key_get_plugins('key_provider', TRUE, $reset); foreach ($keys as $id => $key) { @@ -455,7 +448,7 @@ function key_get_keys_by_storage_method($storage_method, $reset = FALSE) { */ function key_get_keys_by_type_group($type_group, $reset = FALSE) { $filtered_keys = array(); - $keys = key_get_keys($reset); + $keys = key_get_keys(); $types = key_get_plugins('key_type', TRUE, $reset); foreach ($keys as $id => $key) { @@ -481,7 +474,7 @@ function key_get_keys_by_type_group($type_group, $reset = FALSE) { function key_get_key_names_as_options($filters = array(), $reset = FALSE) { $options = array(); - $keys = key_get_keys($reset); + $keys = key_get_keys(); foreach ($filters as $index => $filter) { switch ($index) { @@ -511,27 +504,12 @@ function key_get_key_names_as_options($filters = array(), $reset = FALSE) { * * @param string $id * The machine name of the configuration to get. - * @param bool $reset - * A flag to force the configuration to be retrieved from the database. * * @return array * A key configuration. */ -function key_get_key($id, $reset = FALSE) { - $configs = &backdrop_static(__FUNCTION__); - - if (!isset($configs) || $reset) { - $configs = key_get_keys($reset); - } - - if (array_key_exists($id, $configs)) { - $config = $configs[$id]; - } - else { - $config = NULL; - } - - return $config; +function key_get_key($id) { + return config_get('key.key.' . $id); } /** @@ -550,10 +528,11 @@ function key_get_key($id, $reset = FALSE) { function key_save_key($fields, $original_config = array(), $messages = TRUE) { $save_config = config('key.key.' . $fields['id']); $save_config->setMultiple($fields); + $config_is_new = $save_config->isNew(); $save_config->save(); // Load the configuration to make sure it was saved. - $key_config = key_get_key($fields['id'], TRUE); + $key_config = key_get_key($fields['id']); if (empty($key_config)) { $key_config = NULL; } @@ -571,7 +550,7 @@ function key_save_key($fields, $original_config = array(), $messages = TRUE) { if ($messages) { $t_args = array('%label' => $fields['label']); - switch ($save_config->isNew()) { + switch ($config_is_new) { case TRUE: backdrop_set_message(t('The key %label has been added.', $t_args)); watchdog('key', 'Added key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list')); diff --git a/tests/key.test b/tests/key.test index 29c4f02..15c2cfa 100644 --- a/tests/key.test +++ b/tests/key.test @@ -110,7 +110,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $this->assertText($original_label, 'The configuration/authentication key still appears on the key listing page.'); // Confirm that the key was edited successfully. - $config = key_get_key($original_id, TRUE); + $config = key_get_key($original_id); $this->assertEqual($config, $altered_config, 'The configuration/authentication key was edited successfully.'); // Confirm that the key value was set successfully. @@ -125,7 +125,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $this->assertText(t('The key @label has been deleted.', array('@label' => $original_label)), 'Confirmation text appears after deleting the configuration/authentication key.'); // Confirm that the key was deleted. - $config = key_get_key($original_id, TRUE); + $config = key_get_key($original_id); $this->assertFalse($config, 'The configuration/authentication key was deleted successfully.'); // Confirm that the key no longer appears on the listing page. @@ -142,7 +142,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $original_id = strtolower($original_label); // Define a file location for the test key. - $original_file_location = __DIR__ . '/tests/keys/256-bit.key'; + $original_file_location = __DIR__ . '/keys/256-bit.key'; // Define a configuration. $original_config = array( @@ -196,7 +196,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $this->assertEqual($key_value, file_get_contents($original_file_location), 'The file/encryption key value was retrieved successfully.'); // Define a new file location. - $new_file_location = __DIR__ . '/tests/keys/256-bit_base64-encoded.key'; + $new_file_location = __DIR__ . '/keys/256-bit_base64-encoded.key'; // Define an altered configuration. $altered_config = $config; @@ -233,7 +233,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $this->assertText($original_label, 'The file/encryption key still appears on the key listing page.'); // Confirm that the key was edited successfully. - $config = key_get_key($original_id, TRUE); + $config = key_get_key($original_id); $this->assertEqual($config, $altered_config, 'The file/encryption key was edited successfully.'); // Confirm that the key value can be retrieved successfully. @@ -248,7 +248,7 @@ class KeyBasicAdminTest extends BackdropWebTestCase { $this->assertText(t('The key @label has been deleted.', array('@label' => $original_label)), 'Confirmation text appears after deleting the file/encryption key.'); // Confirm that the key was deleted. - $config = key_get_key($original_id, TRUE); + $config = key_get_key($original_id); $this->assertFalse($config, 'The file/encryption key was deleted successfully.'); // Confirm that the key no longer appears on the listing page.