You are here

variablecheck.test in Variable Check 7

Tests for Variablecheck.

File

variablecheck.test
View source
<?php

/**
 * @file
 * Tests for Variablecheck.
 */

/**
 * Functional tests for the variablecheck module.
 *
 * @see DrupalWebTestCase
 */
class VariableCheckTestCase extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Variablecheck functional tests',
      'description' => 'Test that variablecheck works properly.',
      'group' => 'Variablecheck',
    );
  }

  /**
   * {@inheritdoc}
   *
   * Stop our expected unserialize() notices from failing tests.
   */
  protected function error($message = '', $group = 'Other', array $caller = NULL) {
    if ($group == 'Notice' && $message == 'unserialize(): Error at offset 0 of 9 bytes') {
      return $this
        ->assert('debug', 'Suppressed notice <em>' . $message . '</em>', 'Debug', $caller);
    }
    return parent::error($message, $group, $caller);
  }

  /**
   * Enable module and store a good and bad value in the variables table.
   */
  function setUp() {
    parent::setUp('variablecheck');

    // Add a known good serialized value to the variables table.
    variable_set('variablecheck_good', 'good value');

    // Add a known bad naugty value to the variables table.
    db_insert('variable')
      ->fields(array(
      'name' => 'variablecheck_bad',
      'value' => 'bad value',
    ))
      ->execute();
  }

  /**
   * Check for the dreaded notices.
   */
  function testVariableNotice() {
    $this
      ->drupalGet('');
    $this
      ->assertRaw('unserialize(): Error at offset 0 of 9 bytes');
  }

  /**
   * Access the admin page and verify the good variable is not shown whilst
   * the bad variable is.
   */
  function testVariableAdminPage() {
    $account = $this
      ->drupalCreateUser(array(
      'access site reports',
      'check variables',
    ));
    $this
      ->drupalLogin($account);
    $this
      ->drupalGet('admin/reports/variablecheck');
    $this
      ->assertText('Found one invalid variable', 'Detected an invalid variable.');
    $this
      ->assertPattern("/variablecheck_bad.*bad value/", "Detected the correct bad variable and its value.");
  }

  /**
   * Access the admin page to delete the bad variable, ensure it is then
   * no longer shown.
   */
  function testVariableAdminPageDelete() {

    // TODO.
  }

}

/**
 * Unit tests for the variablecheck module.
 *
 * This module relies on the database, so really the only thing we can
 * really check here is whether all required functions and hooks exist
 * and return data. Running them would just result in errors.
 * @see DrupalUnitTestCase
 */
class VariableCheckUnitTestCase extends DrupalUnitTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Variablecheck unit tests',
      'description' => 'Test that variablecheck works properly.',
      'group' => 'Variablecheck',
    );
  }

  /**
   * Set up the test environment.
   */
  public function setUp() {
    drupal_load('module', 'variablecheck');
    parent::setUp();
  }

  /**
   * Check that the hooks return non-empty arrays.
   */
  public function testVariableCheckHooks() {

    // Ensure the hooks exist and return non-empty arrays.
    $hooks = array(
      'menu' => TRUE,
      'permission' => TRUE,
      'requirements' => FALSE,
    );
    foreach ($hooks as $hook => $test) {
      $callback = 'variablecheck_' . $hook;
      $this
        ->assertTrue(is_callable($callback), 'The ' . $callback . ' hook can be called.');
      if ($test) {
        $result = call_user_func($callback);
        $this
          ->assertTrue(is_array($result), 'The ' . $callback . ' hook returns an array.');
        $this
          ->assertFalse(empty($result), 'The ' . $callback . ' hook does not return an empty array.');
      }
    }
  }

  /**
   * And we can check functions too.
   */
  public function testVariableCheckFunctions() {

    // Ensure these functions exist.
    $functions = array(
      'check_variables',
      'check_variables_form',
      'check_variables_form_validate',
      'check_variables_form_submit',
      'delete_confirm',
      'delete_confirm_submit',
    );
    foreach ($functions as $function) {
      $callback = 'variablecheck_' . $function;
      $this
        ->assertTrue(is_callable($callback), 'The ' . $callback . ' function exists and is callable.');
    }
  }

}

Classes

Namesort descending Description
VariableCheckTestCase Functional tests for the variablecheck module.
VariableCheckUnitTestCase Unit tests for the variablecheck module.