You are here

function xautoload_simpletest_alter in X Autoload 7.2

Same name and namespace in other branches
  1. 7.3 xautoload.module \xautoload_simpletest_alter()

Implements hook_simpletest_alter().

File

./xautoload.module, line 111

Code

function xautoload_simpletest_alter(&$groups) {
  if (version_compare(PHP_VERSION, '5.3') < 0) {

    // Namespaces only exist since PHP 5.3.
    return;
  }

  // Select all PSR-0 classes in the Tests namespace of all modules.
  // This does include disabled modules.
  $system_list = db_query("SELECT name, filename FROM {system}")
    ->fetchAllKeyed();
  foreach ($system_list as $name => $filename) {

    // Build directory in which the test files would reside.
    $tests_dir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/Drupal/' . $name . '/Tests';

    // Scan it for test files if it exists.
    if (is_dir($tests_dir)) {
      $files = file_scan_directory($tests_dir, '/.*\\.php/');
      if (!empty($files)) {
        $basedir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/';
        foreach ($files as $file) {

          // Convert the file name into the namespaced class name.
          $replacements = array(
            '/' => '\\',
            $basedir => '',
            '.php' => '',
          );
          $classes[] = strtr($file->uri, $replacements);
        }
      }
    }
  }

  // Check that each class has a getInfo() method and store the information
  // in an array keyed with the group specified in the test information.
  foreach ($classes as $class) {

    // Test classes need to implement getInfo() to be valid.
    if (class_exists($class) && method_exists($class, 'getInfo')) {
      $info = call_user_func(array(
        $class,
        'getInfo',
      ));

      // If this test class requires a non-existing module, skip it.
      if (!empty($info['dependencies'])) {
        foreach ($info['dependencies'] as $module) {
          if (!drupal_get_filename('module', $module)) {
            continue 2;
          }
        }
      }
      $groups[$info['group']][$class] = $info;
    }
  }

  // Sort the groups and tests within the groups by name.
  uksort($groups, 'strnatcasecmp');
  foreach ($groups as $group => &$tests) {
    uksort($tests, 'strnatcasecmp');
  }
}