You are here

encryptfapi.test in Encrypt Form API 7.2

Tests for encrypt.module

File

encryptfapi.test
View source
<?php

/**
 * @file
 * Tests for encrypt.module
 */

/**
 * Test basic encryption and decryption.
 */
class EncryptFAPITest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Encrypt Form API Test',
      'description' => 'Test the #encrypt Form API property',
      'group' => 'Encrypt Form API',
    );
  }

  /**
   * Returns list of element types to test.
   */
  public static function elementTypes() {
    return _encryptfapi_test_form_element_types();
  }

  /**
   * Returns list of options for element types that support options.
   */
  public static function elementOptions() {
    return _encryptfapi_test_form_element_options();
  }

  /**
   * Returns list of types of tests to perform.
   */
  public static function testTypes() {
    return _encryptfapi_test_test_types();
  }

  /**
   * Gets a variable.
   */
  public static function variableGet($variable_name, $test_type) {
    if ($test_type == 'nested') {
      $variable = variable_get("{$variable_name}_container", array());
      $variable = isset($variable[$variable_name]) ? $variable[$variable_name] : '';
    }
    else {
      $variable = variable_get($variable_name, '');
    }
    return $variable;
  }

  /**
   * Decrypts a variable.
   */
  public static function decrypt($variable) {
    $decrypted = decrypt($variable);

    // If the decrypted text is serialized, unserialize it.
    $unserialized = @unserialize($decrypted);
    if ($unserialized !== FALSE) {
      foreach ($unserialized as $key => $value) {
        if ($value) {
          $decrypted = $unserialized[$key];
          break;
        }
      }
    }
    return $decrypted;
  }

  /**
   * Enable encryptfapi and encryptfapi_test modules.
   */
  function setUp() {
    parent::setUp('encryptfapi', 'encryptfapi_test');
  }

  /**
   * Tests #encrypt property on all supported form element types.
   */
  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));
      }
    }
  }

}

Classes

Namesort descending Description
EncryptFAPITest Test basic encryption and decryption.