- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
<?php
2009-05-13 19:42:18 +00:00
/**
* @file
* Install, update and uninstall functions for the simpletest module.
*/
2014-05-23 19:25:52 +00:00
use Drupal\Component\Utility\Environment;
2017-04-04 14:29:18 +00:00
use PHPUnit\Framework\TestCase;
2014-05-23 19:25:52 +00:00
2011-01-03 06:40:49 +00:00
/**
* Minimum value of PHP memory_limit for SimpleTest.
*/
2015-05-17 22:01:47 +00:00
const SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT = '128M';
2011-01-03 06:40:49 +00:00
2008-04-28 09:30:35 +00:00
/**
2010-10-15 23:40:21 +00:00
* Implements hook_requirements().
2008-04-28 09:30:35 +00:00
*/
function simpletest_requirements($phase) {
2017-03-04 01:20:24 +00:00
$requirements = [];
2008-04-28 09:30:35 +00:00
2017-04-04 14:29:18 +00:00
$has_phpunit = class_exists(TestCase::class);
2008-04-28 09:30:35 +00:00
$has_curl = function_exists('curl_init');
2010-01-07 05:34:21 +00:00
$open_basedir = ini_get('open_basedir');
2008-04-28 09:30:35 +00:00
2017-03-04 01:20:24 +00:00
$requirements['phpunit'] = [
2016-08-25 10:04:13 +00:00
'title' => t('PHPUnit dependency'),
'value' => $has_phpunit ? t('Found') : t('Not found'),
2017-03-04 01:20:24 +00:00
];
2016-08-25 10:04:13 +00:00
if (!$has_phpunit) {
$requirements['phpunit']['severity'] = REQUIREMENT_ERROR;
$requirements['phpunit']['description'] = t("The testing framework requires the PHPUnit package. Please run 'composer install --dev' to ensure it is present.");
}
2017-03-04 01:20:24 +00:00
$requirements['curl'] = [
2013-06-17 13:35:07 +00:00
'title' => t('cURL'),
'value' => $has_curl ? t('Enabled') : t('Not found'),
2017-03-04 01:20:24 +00:00
];
2009-02-01 17:11:00 +00:00
if (!$has_curl) {
$requirements['curl']['severity'] = REQUIREMENT_ERROR;
Issue #2474017 by pguillard, Cameron Tod, subhojit777, BQari, casivaagustin, colorfield, evgeny.chernyavskiy, wingmanjd, darol100, ifrik, jhodgdon, joshi.rohit100, Gábor Hojtsy, catch: Improve module description and error message about missing Curl library
2016-10-23 22:27:11 +00:00
$requirements['curl']['description'] = t('The testing framework requires the <a href="https://secure.php.net/manual/en/curl.setup.php">PHP cURL library</a>. For more information, see the <a href="https://www.drupal.org/requirements/php/curl">online information on installing the PHP cURL extension</a>.');
2009-02-01 17:11:00 +00:00
}
2010-01-07 05:34:21 +00:00
// SimpleTest currently needs 2 cURL options which are incompatible with
// having PHP's open_basedir restriction set.
2015-05-18 21:08:10 +00:00
// See https://www.drupal.org/node/674304.
2017-03-04 01:20:24 +00:00
$requirements['php_open_basedir'] = [
2013-06-17 13:35:07 +00:00
'title' => t('PHP open_basedir restriction'),
'value' => $open_basedir ? t('Enabled') : t('Disabled'),
2017-03-04 01:20:24 +00:00
];
2010-01-07 05:34:21 +00:00
if ($open_basedir) {
$requirements['php_open_basedir']['severity'] = REQUIREMENT_ERROR;
2016-01-05 14:31:39 +00:00
$requirements['php_open_basedir']['description'] = t('The testing framework requires the PHP <a href="http://php.net/manual/ini.core.php#ini.open-basedir">open_basedir</a> restriction to be disabled. Check your webserver configuration or contact your web host.');
2010-01-07 05:34:21 +00:00
}
2011-01-03 06:40:49 +00:00
// Check the current memory limit. If it is set too low, SimpleTest will fail
// to load all tests and throw a fatal error.
$memory_limit = ini_get('memory_limit');
2014-05-23 19:25:52 +00:00
if (!Environment::checkMemoryLimit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
2014-07-22 09:24:02 +00:00
$requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
2017-03-04 01:20:24 +00:00
$requirements['php_memory_limit']['description'] = t('The testing framework requires the PHP memory limit to be at least %memory_minimum_limit. The current value is %memory_limit. <a href=":url">Follow these steps to continue</a>.', ['%memory_limit' => $memory_limit, '%memory_minimum_limit' => SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, ':url' => 'https://www.drupal.org/node/207036']);
2011-01-03 06:40:49 +00:00
}
2014-02-10 17:31:31 +00:00
$site_directory = 'sites/simpletest';
2016-04-29 11:02:20 +00:00
if (!drupal_verify_install_file(\Drupal::root() . '/' . $site_directory, FILE_EXIST | FILE_READABLE | FILE_WRITABLE | FILE_EXECUTABLE, 'dir')) {
2017-03-04 01:20:24 +00:00
$requirements['simpletest_site_directory'] = [
2014-02-10 17:31:31 +00:00
'title' => t('Simpletest site directory'),
2014-11-17 12:20:57 +00:00
'value' => is_dir(\Drupal::root() . '/' . $site_directory) ? t('Not writable') : t('Missing'),
2014-02-10 17:31:31 +00:00
'severity' => REQUIREMENT_ERROR,
2017-03-04 01:20:24 +00:00
'description' => t('The testing framework requires the %sites-simpletest directory to exist and be writable in order to run tests.', [
2015-03-01 12:05:46 +00:00
'%sites-simpletest' => $site_directory,
2017-03-04 01:20:24 +00:00
]),
];
2014-02-10 17:31:31 +00:00
}
2014-11-17 12:20:57 +00:00
elseif (!file_save_htaccess(\Drupal::root() . '/' . $site_directory, FALSE)) {
2017-03-04 01:20:24 +00:00
$requirements['simpletest_site_directory'] = [
2014-02-10 17:31:31 +00:00
'title' => t('Simpletest site directory'),
'value' => t('Not protected'),
'severity' => REQUIREMENT_ERROR,
2017-03-04 01:20:24 +00:00
'description' => t('The file %file does not exist and could not be created automatically, which poses a security risk. Ensure that the directory is writable.', [
2015-03-01 12:05:46 +00:00
'%file' => $site_directory . '/.htaccess',
2017-03-04 01:20:24 +00:00
]),
];
2014-02-10 17:31:31 +00:00
}
2008-04-28 09:30:35 +00:00
return $requirements;
}
2008-06-24 21:51:03 +00:00
2010-10-15 23:40:21 +00:00
/**
* Implements hook_schema().
*/
2008-06-24 21:51:03 +00:00
function simpletest_schema() {
2017-03-04 01:20:24 +00:00
$schema['simpletest'] = [
2008-11-15 13:01:11 +00:00
'description' => 'Stores simpletest messages',
2017-03-04 01:20:24 +00:00
'fields' => [
'message_id' => [
2008-06-24 21:51:03 +00:00
'type' => 'serial',
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique simpletest message ID.',
2017-03-04 01:20:24 +00:00
],
'test_id' => [
2008-06-24 21:51:03 +00:00
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Test ID, messages belonging to the same ID are reported together',
2017-03-04 01:20:24 +00:00
],
'test_class' => [
2015-05-05 16:42:09 +00:00
'type' => 'varchar_ascii',
2008-06-24 21:51:03 +00:00
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The name of the class that created this message.',
2017-03-04 01:20:24 +00:00
],
'status' => [
2008-06-24 21:51:03 +00:00
'type' => 'varchar',
'length' => 9,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Message status. Core understands pass, fail, exception.',
2017-03-04 01:20:24 +00:00
],
'message' => [
2008-08-11 17:43:30 +00:00
'type' => 'text',
2008-06-24 21:51:03 +00:00
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'The message itself.',
2017-03-04 01:20:24 +00:00
],
'message_group' => [
2015-05-05 16:42:09 +00:00
'type' => 'varchar_ascii',
2008-06-24 21:51:03 +00:00
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The message group this message belongs to. For example: warning, browser, user.',
2017-03-04 01:20:24 +00:00
],
'function' => [
2015-05-05 16:42:09 +00:00
'type' => 'varchar_ascii',
2008-06-24 21:51:03 +00:00
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Name of the assertion function or method that created this message.',
2017-03-04 01:20:24 +00:00
],
'line' => [
2008-06-24 21:51:03 +00:00
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Line number on which the function is called.',
2017-03-04 01:20:24 +00:00
],
'file' => [
2008-06-24 21:51:03 +00:00
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Name of the file where the function is called.',
2017-03-04 01:20:24 +00:00
],
],
'primary key' => ['message_id'],
'indexes' => [
'reporter' => ['test_class', 'message_id'],
],
];
$schema['simpletest_test_id'] = [
2015-05-24 20:51:18 +00:00
'description' => 'Stores simpletest test IDs, used to auto-increment the test ID so that a fresh test ID is used.',
2017-03-04 01:20:24 +00:00
'fields' => [
'test_id' => [
2008-06-24 21:51:03 +00:00
'type' => 'serial',
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests
are run a new test ID is used.',
2017-03-04 01:20:24 +00:00
],
'last_prefix' => [
2009-07-07 07:50:53 +00:00
'type' => 'varchar',
'length' => 60,
'not null' => FALSE,
'default' => '',
'description' => 'The last database prefix used during testing.',
2017-03-04 01:20:24 +00:00
],
],
'primary key' => ['test_id'],
];
2008-06-24 21:51:03 +00:00
return $schema;
}
2010-11-12 03:06:52 +00:00
/**
* Implements hook_uninstall().
*/
function simpletest_uninstall() {
2014-02-10 17:31:31 +00:00
// Do not clean the environment in case the Simpletest module is uninstalled
// in a (recursive) test for itself, since simpletest_clean_environment()
// would also delete the test site of the parent test process.
2014-04-04 13:49:13 +00:00
if (!drupal_valid_test_ua()) {
2014-02-10 17:31:31 +00:00
simpletest_clean_environment();
}
// Delete verbose test output and any other testing framework files.
2010-11-12 03:06:52 +00:00
file_unmanaged_delete_recursive('public://simpletest');
}