Skip to content

Commit

Permalink
Fixes so tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
herbdool committed Sep 20, 2023
1 parent 198a457 commit 6f716eb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 49 deletions.
4 changes: 2 additions & 2 deletions drush/key_delete.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion drush/key_list.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions drush/key_save.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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'])));
Expand Down Expand Up @@ -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'])));
Expand Down
2 changes: 1 addition & 1 deletion drush/key_value_get.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
53 changes: 16 additions & 37 deletions key.module
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -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'));
Expand Down
12 changes: 6 additions & 6 deletions tests/key.test
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 6f716eb

Please sign in to comment.