You are here

simpletest.module in SimpleTest 5

File

simpletest.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function simpletest_help($section) {
  $output = '';
  switch ($section) {
    case 'admin/modules#description':
      $output = t("Simple unit testing suite");
      break;
    case 'admin/help#simpletest':
      $output = '<p>' . t('The SimpleTest module is a framework for running automated unit tests in Drupal. It can be used to verify a working state of Drupal before and after any code changes, or as a means for developers to write and execute tests for their modules. It integrates with the <a href="@simpletest-official">SimpleTest PHP library</a>.', array(
        '@simpletest-official' => 'http://www.simpletest.org/',
      )) . '</p>';
      $output .= '<p>' . t('Visit <a href="@admin-simpletest">Administer >> Site building >> SimpleTest</a> to display a list of available tests. For comprehensive testing, select <em>all</em> tests, or individually select tests for more targeted testing. (Note: Selecting <em>all</em> tests may take several minutes to complete.)', array(
        '@admin-simpletest' => url('admin/build/simpletest'),
      )) . '</p>';
      $output .= '<p>' . t('After the tests have run, a message will be displayed next to each test group indicating whether tests within it passed, failed, or had exceptions. A pass means that a test returned the expected results, while fail means that it did not. An exception normally indicates an error outside of the test, such as a PHP warning or notice. If there were fails or exceptions, the results are expanded, and the tests that had issues will be indicated in red or pink rows. Use these results to refine your code and tests until all tests return a pass.') . '</p>';
      $output .= '<p>' . t('For more information on creating and modifying your own tests, see the <a href="@simpletest-api">SimpleTest API Documentation</a> in the Drupal handbook.', array(
        '@simpletest-api' => 'http://drupal.org/simpletest',
      )) . '</p>';
      $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@simpletest">SimpleTest module</a>.', array(
        '@simpletest' => 'http://drupal.org/handbook/modules/simpletest',
      )) . '</p>';
  }
  return $output;
}

/**
 * Implementation of hook_menu().
 */
function simpletest_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => 'admin/build/simpletest',
      'title' => 'Simpletest',
      'callback' => 'simpletest_entrypoint',
      'description' => t('Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.'),
      'access' => user_access('administer unit tests'),
    );
    $items[] = array(
      'path' => 'admin/settings/simpletest',
      'title' => 'Simpletest settings',
      'description' => t('Configure unit testing framework.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'simpletest_settings',
      'access' => user_access('administer unit tests'),
    );
    return $items;
  }
}

/**
 * Implementation of hook_perm().
 */
function simpletest_perm() {
  return array(
    'administer unit tests',
  );
}

/**
 * Try to load the simepletest
 * @return boolean TRUE if the load succeeded
 */
function simpletest_load() {
  static $loaded;
  if ($loaded) {
    return true;
  }
  global $user;
  if ($user->uid != 1) {
    drupal_set_message(t('We strongly suggest running the simpletests with uid=1!'));
  }
  $loaded = true;
  if (!defined('SIMPLE_TEST')) {
    define('SIMPLE_TEST', drupal_get_path('module', 'simpletest') . '/simpletest');
  }
  if (!is_dir(SIMPLE_TEST)) {
    $output = t('Sorry but the simpletest cannot be found in the current installation. Please notice that simpletest.module needs Simpletest framework. ' . 'Please download it from !simpletest_link and place it into the same directory as simpletest.module: %simpletest_directory', array(
      '!simpletest_link' => l('Simpletest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'),
      '%simpletest_directory' => SIMPLE_TEST,
    ));
    drupal_set_message($output, 'error');
    return false;
  }

  /* We currently use only the web tester that DrupalTestCase is built upon */
  require_once SIMPLE_TEST . '/web_tester.php';
  require_once SIMPLE_TEST . '/reporter.php';
  require_once 'drupal_reporter.php';
  if (version_compare(SimpleTest::getVersion(), '1.0.1beta2') < 0) {
    $output = t('Due to a lot of refactoring done on simpletest library side. Simpletest module is not compatible with simpeltest versions lower thab 1.0.1beta2. ' . 'Please download the latest version from !simpletest_link and place it into the same directory as simpletest.module: %simpletest_directory', array(
      '!simpletest_link' => l('Simpletest on SourceForge', 'https://sourceforge.net/project/showfiles.php?group_id=76550'),
      '%simpletest_directory' => SIMPLE_TEST,
    ));
    drupal_set_message($output, 'error');
    return false;
  }
  $path = drupal_get_path('module', 'simpletest') . '/';
  require_once $path . 'drupal_test_case.php';
  require_once $path . 'drupal_unit_tests.php';
  return true;
}

/**
 * Menu callback for both running tests and listing possible tests
 */
function simpletest_entrypoint() {
  if (!simpletest_load()) {

    /**
     * @todo find a better way for this return,
     * currently needed to show error, true leads to page not found
     */
    return '&nbsp;';
  }
  drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', 'module');
  $output = drupal_get_form('simpletest_overview_form');
  print theme('page', simpletest_running_output() . $output);
}
function simpletest_running_output($output = NULL) {
  static $o;
  if ($output != NULL) {
    $o = $output;
  }
  return $o;
}

/**
 * FAPI form submit for simpletest_overview_form
 *
 * @param $form_id 
 * @param $form_values
 */
function simpletest_overview_form_submit($form_id, $form_values) {
  switch ($form_values['running_options']) {
    case 'all_tests':
      $output = simpletest_run_tests();
      break;
    case 'selected_tests':
      $test_list = array();
      foreach ($form_values as $item => $value) {
        if ($value === 1 && strpos($item, 'selectall') === FALSE) {
          $tests_list[] = $item;
        }
      }
      if (count($tests_list) > 0) {
        $output = simpletest_run_tests($tests_list);
      }
      else {

        // no test has been selected
        drupal_set_message(t('No test has been selected.'), 'error');
      }
      break;
    default:
      drupal_set_message(t('No test has been selected.'), 'error');
  }
  simpletest_running_output($output);
  return FALSE;
}

/**
 * Create simpletest_overview_form 
 */
function simpletest_overview_form() {
  $output = array();
  $output[] = array();
  $total_test =& simpletest_get_total_test();
  $test_instances = $total_test
    ->getTestInstances();
  foreach ($test_instances as $group_test) {
    $group = array();
    $tests = $group_test
      ->getTestInstances();
    $group_class = str_replace(' ', '-', strtolower($group_test
      ->getLabel()));
    $group['tests'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => 'Tests',
      '#attributes' => array(
        'class' => $group_class,
      ),
    );
    foreach ($tests as $test) {
      $test_info = $test
        ->get_info();
      $desc = $test_info['desc'];
      $group['tests'][get_class($test)] = array(
        '#type' => 'checkbox',
        '#title' => $test_info['name'],
        '#default_value' => 0,
        '#description' => $desc,
      );
    }
    $output[] = array_merge($group, array(
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#title' => $group_test
        ->getLabel(),
      '#attributes' => array(
        'class' => 'select_all',
      ),
    ));
  }
  $submit['running_options'] = array(
    '#type' => 'radios',
    '#default_value' => 'selected_tests',
    '#options' => array(
      'all_tests' => t('Run all tests (WARNING, this may take a long time)'),
      'selected_tests' => t('Run selected tests'),
    ),
  );
  $submit['op'] = array(
    '#type' => 'submit',
    '#value' => t('Begin'),
  );
  $output[] = array_merge($submit, array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#title' => 'Run tests',
  ));
  return $output;
}

/**
 * Actually runs tests
 * @param array $testlist list of tests to run or DEFAULT NULL run all tests
 * @param boolean $html_reporter true if you want results in simple html, FALSE for full drupal page
 */
function simpletest_run_tests($testlist = NULL, $reporter = 'drupal') {
  global $test_running;
  if (!$test_running) {
    $test_running = TRUE;
    $test = simpletest_get_total_test($testlist);
    if (gettype($reporter) == 'string') {
      switch ($reporter) {
        case 'text':
          $reporter =& new TextReporter();
          break;
        case 'xml':
          $reporter =& new XMLReporter();
          break;
        case 'html':
          $reporter =& new HtmlReporter();
          break;
        case 'drupal':
          $reporter =& new DrupalReporter();
          break;
      }
    }
    cache_clear_all();
    $results = $test
      ->run($reporter);
    switch (get_class($reporter)) {
      default:
        return $results;
      case 'DrupalReporter':
        return $reporter
          ->getOutput();
    }
  }
}

/**
 * This function makes sure no unnecessary copies of the DrupalUnitTests object are instantiated
 * @param  array $classes list of all classes the test should concern or
 *                        DEFAULT NULL
 * @return DrupalUnitTests object
 */
function &simpletest_get_total_test($classes = NULL) {
  static $total_test;
  if (!$total_test) {
    if (!simpletest_load()) {
      return FALSE;
    }
    $total_test =& new DrupalUnitTests();
  }
  if (!is_null($classes)) {
    return new DrupalUnitTests($classes);
  }
  return $total_test;
}
function simpletest_settings() {
  $from = array();
  $form['http_auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('HTTP authentication'),
    '#description' => t('If needed, enter a username and password for reaching your web site. This is not a drupal username/password. This
    is a login presented by your web server. Most sites may leave section empty.'),
  );
  $form['http_auth']['simpletest_httpauth'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use http authentication'),
    '#default_value' => variable_get('simpletest_httpauth', false),
  );
  $form['http_auth']['simpletest_httpauth_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => variable_get('simpletest_httpauth_username', ''),
  );
  $form['http_auth']['simpletest_httpauth_pass'] = array(
    '#title' => t('Password'),
    '#type' => 'password',
    '#default_value' => variable_get('simpletest_httpauth_pass', ''),
  );
  $form['devel'] = array(
    '#type' => 'fieldset',
    '#title' => t('Devel module settings'),
    '#description' => t('Devel module can cause problems if you have query log enabled. It will output a few thousand queries and crash your browser'),
  );
  $form['devel']['simpletest_devel'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use devel query log on test result pages'),
    '#default_value' => variable_get('simpletest_devel', false),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
simpletest_entrypoint Menu callback for both running tests and listing possible tests
simpletest_get_total_test This function makes sure no unnecessary copies of the DrupalUnitTests object are instantiated
simpletest_help Implementation of hook_help().
simpletest_load Try to load the simepletest
simpletest_menu Implementation of hook_menu().
simpletest_overview_form Create simpletest_overview_form
simpletest_overview_form_submit FAPI form submit for simpletest_overview_form
simpletest_perm Implementation of hook_perm().
simpletest_running_output
simpletest_run_tests Actually runs tests
simpletest_settings