You are here

public function EncryptFAPITest::testEncryption in Encrypt Form API 7.2

Tests #encrypt property on all supported form element types.

File

./encryptfapi.test, line 87
Tests for encrypt.module

Class

EncryptFAPITest
Test basic encryption and decryption.

Code

public function testEncryption() {
  $this
    ->drupalGet('encryptfapi_test');

  // Set up the test definitions and test values.
  $tests = array();
  $test_values = array();
  foreach ($this
    ->elementTypes() as $name => $element_type) {
    foreach ($this
      ->testTypes() as $test_type) {

      // Choose a random option, if the element supports options.
      if (isset($element_type['options']) && $element_type['options'] === TRUE) {
        $random_option = array_rand($this
          ->elementOptions());
      }

      // Define the field name.
      $field_name = "encryptfapi_test_{$name}_{$test_type}";
      $variable_name = "encryptfapi_test_{$name}_{$test_type}";
      if ($test_type == 'nested') {
        $field_name = "{$field_name}_container[{$field_name}]";
      }
      if ($name == 'checkboxes') {
        $field_name = "{$field_name}[{$random_option}]";
      }

      // Set the test value
      if (isset($element_type['options']) && $element_type['options'] === TRUE) {
        $test_value = $random_option;
      }
      elseif (in_array($name, array(
        'checkbox',
        'radio',
      ))) {
        $test_value = 1;
      }
      else {
        $test_value = $this
          ->randomName(10);
      }

      // Add this value to the list of values.
      if ($name == 'password_confirm') {
        $test_values["{$field_name}[pass1]"] = $test_value;
        $test_values["{$field_name}[pass2]"] = $test_value;
      }
      else {
        $test_values[$field_name] = $test_value;
      }

      // Add this item to the list of tests.
      $tests[$field_name] = array(
        'field_name' => $field_name,
        'variable_name' => $variable_name,
        'label' => ucwords($element_type['label']) . ' (' . ucwords($test_type) . ')',
        'element_type' => $element_type,
        'test_type' => $test_type,
        'value' => $test_value,
      );
    }
  }

  // Submit the form with the test value.
  $this
    ->drupalPost('encryptfapi_test', $test_values, 'Save configuration');

  // Perform the tests.
  foreach ($tests as $field_name => $test) {
    $t_args = array(
      '%label' => $test['label'],
    );
    $variable = $this
      ->variableGet($test['variable_name'], $test['test_type']);
    $this
      ->assertNotEqual($test['value'], $variable, t('%label: The value stored does not equal the value submitted.', $t_args));
    $this
      ->assertEqual($test['value'], $this
      ->decrypt($variable), t('%label: The stored value, when decrypted, equals the original, submitted value.', $t_args));

    // For all fields except ones that don't allow a default value
    // (e.g., password, password_confirm).
    if (!isset($test['element_type']['default_value']) || $test['element_type']['default_value'] == TRUE) {
      $this
        ->assertFieldByName($field_name, $test['value'], t('%label: The value is unencrypted in the form.', $t_args));
    }
  }
}