<?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',
    );
  }

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

  /**
   * Tests #encrypt property for the simple case of #encrypt => TRUE.
   */
  public function testEncryptionTextfield() {
    $this->drupalGet('encryptfapi_test');

    $data = array(
      'encryptfapi_textfield_simple' => $this->randomName(10),
      'encryptfapi_textfield_complex' => $this->randomName(10),
      'encrypt_tree[encryptfapi_textfield_nested]' => $this->randomName(10),
    );
    $this->drupalPost('encryptfapi_test', $data, 'Save configuration');

    // First, test the simple one.
    $this->assertFieldByName('encryptfapi_textfield_simple', $data['encryptfapi_textfield_simple'], t('Simple: The value appears unencrypted in the textfield.'));
    $encrypted_value = variable_get('encryptfapi_textfield_simple', 'A Test Value');
    $this->assertNotEqual($data['encryptfapi_textfield_simple'], $encrypted_value['text'], t('Simple: The value stored does not equal the value shown.'));
    $this->assertEqual($data['encryptfapi_textfield_simple'], decrypt($encrypted_value, array('base64' => TRUE)), t('Simple: The stored value, when decrypted, equals the displayed/original value.'));

    // Then, the complex one.
    $this->assertFieldByName('encryptfapi_textfield_complex', $data['encryptfapi_textfield_complex'], t('Complex: The value appears unencrypted in the textfield.'));
    $encrypted_value = variable_get('encryptfapi_textfield_complex', 'A Test Value');
    $unserialized = unserialize($encrypted_value);
    $this->assertNotEqual($data['encryptfapi_textfield_complex'], $encrypted_value['text'], t('Complex: The value stored does not equal the value shown.'));
    $this->assertEqual($data['encryptfapi_textfield_complex'], decrypt($encrypted_value, array('base64' => TRUE)), t('Complex: The stored value, when decrypted, equals the displayed/original value.'));
    $this->assertEqual('mcrypt_rij_256', $unserialized['method'], t('The correct encryption method was used.'));

    // Then, a nested one.
    $nested = variable_get('encrypt_tree', array());
    $this->assertFieldByName('encrypt_tree[encryptfapi_textfield_nested]', $data['encrypt_tree[encryptfapi_textfield_nested]'], t('Nested: The value appears unencrypted in the textfield.'));
    $encrypted_value = variable_get('encrypt_tree', array());
    $unserialized = unserialize($encrypted_value['encryptfapi_textfield_nested']);
    $this->assertNotEqual($data['encrypt_tree[encryptfapi_textfield_nested]'], $unserialized['text'], t('Nested: The value stored does not equal the value shown.'));
    $this->assertEqual($data['encrypt_tree[encryptfapi_textfield_nested]'], decrypt($encrypted_value['encryptfapi_textfield_nested'], array('base64' => TRUE)), t('Nested: The stored value, when decrypted, equals the displayed/original value.'));
  }

}
