You are here

public function KeyBasicAdminTest::testConfigKeyProvider in Key 7.3

Test the Configuration key provider.

File

./key.test, line 47
Tests for key administration and usage.

Class

KeyBasicAdminTest
Test basic key administration.

Code

public function testConfigKeyProvider() {

  // Create a random label.
  $original_label = DrupalWebTestCase::randomName(8);
  $original_id = strtolower($original_label);

  // Create a random key value.
  $original_key_value = DrupalWebTestCase::randomString(32);

  // Define a configuration.
  $original_config = array(
    'id' => $original_id,
    'label' => $original_label,
    'description' => 'A test key.',
    'key_type' => 'authentication',
    'key_type_settings' => array(),
    'key_provider' => 'config',
    'key_provider_settings' => array(
      'key_value' => $original_key_value,
    ),
    'key_input' => 'text_field',
    'key_input_settings' => array(),
  );

  // Set the data to save.
  $data = $original_config;
  unset($data['key_type_settings']);
  unset($data['key_provider_settings']);
  unset($data['key_input_settings']);
  unset($data['key_input']);
  $data['key_input_settings[key_value]'] = $original_key_value;

  // Add the configuration.
  $this
    ->drupalGet(KEY_MENU_PATH . '/add');
  $this
    ->drupalPostAJAX(NULL, array(
    'key_type' => $data['key_type'],
  ), 'key_type');
  $this
    ->drupalPostAJAX(NULL, array(
    'key_provider' => $data['key_provider'],
  ), 'key_provider');
  $this
    ->drupalPost(NULL, $data, t('Save key'));

  // Confirm that the confirmation text appears.
  $this
    ->assertText(t('The key @label has been added.', array(
    '@label' => $original_label,
  )), 'Confirmation text appears after adding the configuration/authentication key.');

  // Confirm that the key appears on the listing page.
  $this
    ->drupalGet(KEY_MENU_PATH);
  $this
    ->assertText($original_label, 'The configuration/authentication key appears on the key listing page.');

  // Confirm that the key was added successfully.
  $config = key_get_key($original_id);
  $this
    ->assertEqual($config, $original_config, 'The configuration/authentication key was added successfully.');

  // Confirm that the key value was set successfully.
  $key_value = key_get_key_value($original_id);
  $this
    ->assertEqual($key_value, $original_key_value, 'The configuration/authentication key value was set successfully.');

  // Define an altered configuration.
  $altered_config = $config;
  $altered_config['description'] = 'A test key that has been altered.';

  // Set the data to save.
  $data = $altered_config;
  unset($data['id']);
  unset($data['key_type_settings']);
  unset($data['key_provider_settings']);
  unset($data['key_input_settings']);
  unset($data['key_input']);
  $data['key_input_settings[key_value]'] = $original_key_value;

  // Edit the configuration.
  $this
    ->drupalGet(KEY_MENU_PATH . "/manage/{$original_id}");
  $this
    ->drupalPostAJAX(NULL, array(
    'key_type' => $data['key_type'],
  ), 'key_type');
  $this
    ->drupalPostAJAX(NULL, array(
    'key_provider' => $data['key_provider'],
  ), 'key_provider');
  $this
    ->drupalPost(NULL, $data, t('Save key'));

  // Confirm that the confirmation text appears.
  $this
    ->assertText(t('The key @label has been updated.', array(
    '@label' => $original_label,
  )), 'Confirmation text appears after editing the configuration/authentication key.');

  // Confirm that the key still appears on the listing page.
  $this
    ->drupalGet(KEY_MENU_PATH);
  $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);
  $this
    ->assertEqual($config, $altered_config, 'The configuration/authentication key was edited successfully.');

  // Confirm that the key value was set successfully.
  $key_value = key_get_key_value($original_id);
  $this
    ->assertEqual($key_value, $original_key_value, 'The configuration/authentication key value was set successfully.');

  // Delete the key.
  $this
    ->drupalGet(KEY_MENU_PATH . "/manage/{$original_id}/delete");
  $this
    ->drupalPost(NULL, NULL, t('Delete'));

  // Confirm that the confirmation text appears.
  $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);
  $this
    ->assertFalse($config, 'The configuration/authentication key was deleted successfully.');

  // Confirm that the key no longer appears on the listing page.
  $this
    ->drupalGet(KEY_MENU_PATH);
  $this
    ->assertNoText($original_label, 'The configuration/authentication key no longer appears on the key listing page.');
}