<?php
// $Id: captcha.admin.test,v 1.2 2008/06/04 08:24:38 soxofaan Exp $

/**
* @file Tests for the CAPTCHA administration.
*/
class CaptchaAdminTest extends DrupalTestCase {

  /**
   * User with CAPTCHA administration permission
   */
  var $admin_user;

  /**
   * User without CAPTCHA administration permission
   */
  var $normal_user;

  /**
   * Drupal SimpleTest method: return metadata about the test.
   */
  function get_info() {
    return array(
      'name'  => t('CAPTCHA administration'),
      'desc'  => t('Test the CAPTCHA administration.'),
      'group' => t('CAPTCHA module'),
    );
  }

  /**
   * SimpleTest core method: code run before each and every test method.
   *
   * Optional. You only need this if you have setup tasks.
   */
  function setUp() {
    // Always call the setUp() function from the parent class.
    parent::setUp();

    // Make sure that CAPTCHA module is enabled.
    $this->drupalModuleEnable('captcha');

    // Create a filter admin user
    $permissions = array('administer CAPTCHA settings');
    $this->admin_user = $this->drupalCreateUserRolePerm($permissions);

    // Create a normal user for page creation
    $permissions = array();
    $this->normal_user = $this->drupalCreateUserRolePerm($permissions);

    // because this is about CAPTCHA admin testing: log in as admin by default
    $this->drupalLoginUser($this->admin_user);
  }

  /**
   * SimpleTest core method: code run after each and every test method.
   *
   * Optional. You only need this if you have setup tasks.
   */
  function tearDown() {
      // log out the admin (we logged in in setUp())
      $this->drupalGet(url('logout', array('absolute' => TRUE)));

    // Always call the tearDown() function from the parent class.
    parent::tearDown();
  }

  /**
   * Method for testing the CAPTCHA point administration
   */
  function test_captcha_point_administration() {
    // Generate CAPTCHA point data:
    // Drupal form ID should consist of lowercase alphanumerics and underscore)
    $captcha_point_form_id = strtolower($this->randomName(32, 'a_form_id'));
    // the Math CAPTCHA by the CAPTCHA module is always available, so let's use it
    $captcha_point_module = 'captcha';
    $captcha_point_type = 'Math';

    // Set CAPTCHA point through admin/user/captcha/captcha/captcha_point
    $form_values = array(
      'captcha_point_form_id' => $captcha_point_form_id,
      'captcha_type' => $captcha_point_module .'/'. $captcha_point_type,
    );
    $this->drupalPost('admin/user/captcha/captcha/captcha_point', $form_values, t('Save'));
    $this->assertText(t('Saved CAPTCHA point settings.'),
      'Saving of CAPTCHA point settings');

    // Check in database
    $result = db_fetch_object(db_query("SELECT * FROM {captcha_points} WHERE form_id='%s'", array($captcha_point_form_id)));
    $this->assertEqual($result->module, $captcha_point_module,
      'Enabled CAPTCHA point should have module set');
    $this->assertEqual($result->type, $captcha_point_type,
      'Enabled CAPTCHA point should have type set');

    // Disable CAPTCHA point again
    $this->drupalPost('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id .'/disable', array(), t('Disable'));
    $this->assertWantedRaw(t('Disabled CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id)), 'Disabling of CAPTCHA point');

    // Check in database
    $result = db_fetch_object(db_query("SELECT * FROM {captcha_points} WHERE form_id='%s'", array($captcha_point_form_id)));
    $this->assertNull($result->module,
      'Disabled CAPTCHA point should have NULL as module');
    $this->assertNull($result->type,
      'Disabled CAPTCHA point should have NULL as type');

    // Set CAPTCHA point through admin/user/captcha/captcha/captcha_point/$form_id
    $form_values = array(
      'captcha_type' => $captcha_point_module .'/'. $captcha_point_type,
    );
    $this->drupalPost('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id, $form_values, t('Save'));
    $this->assertText(t('Saved CAPTCHA point settings.'),
      'Saving of CAPTCHA point settings');

    // Check in database
    $result = db_fetch_object(db_query("SELECT * FROM {captcha_points} WHERE form_id='%s'", array($captcha_point_form_id)));
    $this->assertEqual($result->module, $captcha_point_module,
      'Enabled CAPTCHA point should have module set');
    $this->assertEqual($result->type, $captcha_point_type,
      'Enabled CAPTCHA point should have type set');

    // Delete CAPTCHA point
    $this->drupalPost('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id .'/delete', array(), t('Delete'));
    $this->assertWantedRaw(t('Deleted CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id)),
      'Deleting of CAPTCHA point');

    // Check in database
    $result = db_fetch_object(db_query("SELECT * FROM {captcha_points} WHERE form_id='%s'", array($captcha_point_form_id)));
    $this->assertFalse($result, 'Deleted CAPTCHA point should be in database');
  }

  /**
   * Method for testing the CAPTCHA point administration
   */
  function test_captcha_point_administration_by_non_admin() {
    // First add a CAPTCHA point (as admin)
    $captcha_point_form_id = strtolower($this->randomName(32, 'a_form_id'));
    $captcha_point_module = 'captcha';
    $captcha_point_type = 'Math';
    $form_values = array(
      'captcha_point_form_id' => $captcha_point_form_id,
      'captcha_type' => $captcha_point_module .'/'. $captcha_point_type,
    );
    $this->drupalPost('admin/user/captcha/captcha/captcha_point', $form_values, t('Save'));
    $this->assertText(t('Saved CAPTCHA point settings.'),
      'Saving of CAPTCHA point settings');

    // Switch from admin to nonadmin
    $this->drupalGet(url('logout', array('absolute' => TRUE)));
    $this->drupalLoginUser($this->normal_user);

    // Try to set CAPTCHA point through admin/user/captcha/captcha/captcha_point
    $this->drupalGet('admin/user/captcha/captcha/captcha_point');
    $this->assertText(t('You are not authorized to access this page.'),
      'Non admin should not be able to set a CAPTCHA point');

    // Try to set CAPTCHA point through admin/user/captcha/captcha/captcha_point/$form_id
    $this->drupalGet('admin/user/captcha/captcha/captcha_point/'. strtolower($this->randomName(32, 'a_form_id')));
    $this->assertText(t('You are not authorized to access this page.'),
      'Non admin should not be able to set a CAPTCHA point');

    // Try to disable the CAPTCHA point
    $this->drupalGet('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id .'/disable');
    $this->assertText(t('You are not authorized to access this page.'),
      'Non admin should not be able to disable a CAPTCHA point');

    // Try to delete the CAPTCHA point
    $this->drupalGet('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id .'/delete');
    $this->assertText(t('You are not authorized to access this page.'),
      'Non admin should not be able to delete a CAPTCHA point');

    // Switch from nonadmin to admin again
    $this->drupalGet(url('logout', array('absolute' => TRUE)));
    $this->drupalLoginUser($this->admin_user);

    // Check if original CAPTCHA point still exists in database
    $result = db_fetch_object(db_query("SELECT * FROM {captcha_points} WHERE form_id='%s'", array($captcha_point_form_id)));
    $this->assertEqual($result->module, $captcha_point_module,
      'Enabled CAPTCHA point should still have module set');
    $this->assertEqual($result->type, $captcha_point_type,
      'Enabled CAPTCHA point should still have type set');

    // Delete CAPTCHA point
    $this->drupalPost('admin/user/captcha/captcha/captcha_point/'. $captcha_point_form_id .'/delete', array(), t('Delete'));
    $this->assertWantedRaw(t('Deleted CAPTCHA for form %form_id.', array('%form_id' => $captcha_point_form_id)),
      'Deleting of CAPTCHA point');
  }

}
