1 simpletest.module | simpletest_test_get_all() |
Get a list of all of the tests provided by the system.
The list of test classes is loaded from each module's *.tests.info file. Once loaded, the test list is cached and stored in a static variable.
Return value
An array of tests keyed with the groups specified by each owning module's: .tests.info file and then keyed by the test class. An example of the array structure is provided below.
$groups['Block'] => array(
'BlockTestCase' => array(
'name' => 'Block functionality',
'description' => 'Add, edit and delete custom block...',
'group' => 'Block',
'file' => block.test,
'file path' => 'core/modules/block',
),
);
See also
simpletest_registry_files_alter()
File
- core/
modules/ simpletest/ simpletest.module, line 354 - Provides testing functionality.
Code
function simpletest_test_get_all() {
$groups = &backdrop_static(__FUNCTION__);
if (!$groups) {
// Load test information from cache if available, otherwise retrieve the
// information from each module's *.tests.info file.
if ($cache = cache()->get('simpletest')) {
$groups = $cache->data;
}
else {
$files = backdrop_system_listing('/^' . BACKDROP_PHP_FUNCTION_PATTERN . '\.tests\.info$/', 'modules', 'name', 0);
$groups = array();
$check_keys = array('name', 'description', 'group', 'file');
foreach ($files as $file) {
$classes = backdrop_parse_info_file($file->uri, TRUE);
// Error messages are translated as they are created below, but the
// variable substitutions are not passed in. This is done so that the
// same error message can to be output to watchdog(), which needs its
// variables separately, and to backdrop_set_message(), which needs a
// translated string with substitutions already made.
$error = '';
$variables = array('@uri' => $file->uri);
if (empty($classes)) {
$error = t('File @uri contains no testing classes.');
}
else {
// If the .tests.info file is malformed, it puts a slew of
// hard-to-diagnose errors into the watchdog log when the testing page
// is loaded. So we'll check for various malformations and put a more
// informative error.
foreach ($classes as $class => $info) {
if (!is_array($info) || empty($info)) {
$error = t('File @uri contains a definition for @class that is not an array of testing class fields.');
$variables += array('@class' => $class);
continue;
}
else {
$missing_keys = array();
foreach ($check_keys as $key) {
if (!isset($info[$key])) {
$missing_keys[] = $key;
}
}
if (!empty($missing_keys)) {
$keys = implode(', ', $missing_keys);
$error = format_plural(count($missing_keys), 'File @uri contains a definition for @class that is missing field @keys.', 'File @uri contains a definition for @class that is missing fields @keys.');
$variables += array('@class' => $class, '@keys' => $keys);
continue;
}
}
// Verify that the test file is present in the same directory as
// specified in the .tests.info file.
if (empty($error)) {
$info['file path'] = dirname($file->uri);
$test_file_full = BACKDROP_ROOT . '/' . $info['file path'] . '/' . $info['file'];
if (!file_exists($test_file_full)) {
$error = t('File @uri specifies a non-existent file @test_file_full. Please verify that the test file exists in the location specified in @uri.');
$variables += array('@test_file_full' => $test_file_full);
continue;
}
}
if (empty($error)) {
// If the format passed, build the rest of the groups information.
// If this test class requires a non-existing module, skip it.
if (!empty($info['dependencies'])) {
foreach ($info['dependencies'] as $module) {
if (!backdrop_get_filename('module', $module)) {
continue 2;
}
}
}
$groups[$info['group']][$class] = $info;
}
}
}
if (!empty($error)) {
if (backdrop_is_cli()) {
// If running in CLI, then we're probably about to run some
// automated tests, so we need a hard fail.
print format_string($error, $variables) . "\n";
exit(1);
}
else {
// Pass the substitution variables into the already-translated
// string for backdrop_set_message().
backdrop_set_message(format_string($error, $variables), 'error');
watchdog('simpletest', $error, $variables, WATCHDOG_WARNING, l(t('See the Testing Framework documentation.'), 'https://docs.backdropcms.org/documentation/testing-framework'));
continue;
}
}
}
// Sort the groups and tests within the groups by name.
uksort($groups, 'strnatcasecmp');
foreach ($groups as &$tests) {
uksort($tests, 'strnatcasecmp');
}
// Allow modules extending core tests to disable originals.
backdrop_alter('simpletest', $groups);
cache()->set('simpletest', $groups);
}
}
return $groups;
}