You are here

public function KeyBasicAdminTest::testFileKeyProvider in Key 7.3

Test the File key provider.

File

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

Class

KeyBasicAdminTest
Test basic key administration.

Code

public function testFileKeyProvider() {

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

  // Define a file location for the test key.
  $original_file_location = __DIR__ . '/tests/keys/256-bit.key';

  // Define a configuration.
  $original_config = array(
    'id' => $original_id,
    'label' => $original_label,
    'description' => 'A test key.',
    'key_type' => 'encryption',
    'key_type_settings' => array(
      'key_size' => 256,
    ),
    'key_provider' => 'file',
    'key_provider_settings' => array(
      'file_location' => $original_file_location,
      'base64_encoded' => 0,
    ),
    'key_input' => 'none',
    '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']);
  foreach ($original_config['key_type_settings'] as $index => $value) {
    $data["key_type_settings[{$index}]"] = $value;
  }
  foreach ($original_config['key_provider_settings'] as $index => $value) {
    $data["key_provider_settings[{$index}]"] = $value;
  }
  unset($data['key_provider_settings[base64_encoded]']);

  // 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 file/encryption key.');

  // Confirm that the key appears on the listing page.
  $this
    ->drupalGet(KEY_MENU_PATH);
  $this
    ->assertText($original_label, 'The file/encryption 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 file/encryption key was added successfully.');

  // Confirm that the key value can be retrieved successfully.
  $key_value = key_get_key_value($original_id);
  $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';

  // Define an altered configuration.
  $altered_config = $config;
  $altered_config['key_provider_settings'] = array(
    'file_location' => $new_file_location,
    'base64_encoded' => 1,
  );

  // 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']);
  foreach ($altered_config['key_type_settings'] as $index => $value) {
    $data["key_type_settings[{$index}]"] = $value;
  }
  foreach ($altered_config['key_provider_settings'] as $index => $value) {
    $data["key_provider_settings[{$index}]"] = $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 file/encryption key.');

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

  // Confirm that the key value can be retrieved successfully.
  $key_value = key_get_key_value($original_id);
  $this
    ->assertEqual($key_value, base64_decode(file_get_contents($new_file_location)), 'The file/encryption key value was retrieved 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 file/encryption key.');

  // Confirm that the key was deleted.
  $config = key_get_key($original_id, TRUE);
  $this
    ->assertFalse($config, 'The file/encryption 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 file/encryption key no longer appears on the key listing page.');
}