b6cfc80e100c204a4372b6fdecb5bf2cb2578359
[yaffs-website] / web / core / modules / simpletest / simpletest.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for the simpletest module.
6  */
7
8 use Drupal\Component\Utility\Environment;
9
10 /**
11  * Minimum value of PHP memory_limit for SimpleTest.
12  */
13 const SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT = '128M';
14
15 /**
16  * Implements hook_requirements().
17  */
18 function simpletest_requirements($phase) {
19   $requirements = [];
20
21   $has_phpunit = class_exists('\PHPUnit_Framework_TestCase');
22   $has_curl = function_exists('curl_init');
23   $open_basedir = ini_get('open_basedir');
24
25   $requirements['phpunit'] = [
26     'title' => t('PHPUnit dependency'),
27     'value' => $has_phpunit ? t('Found') : t('Not found'),
28   ];
29   if (!$has_phpunit) {
30     $requirements['phpunit']['severity'] = REQUIREMENT_ERROR;
31     $requirements['phpunit']['description'] = t("The testing framework requires the PHPUnit package. Please run 'composer install --dev' to ensure it is present.");
32   }
33
34   $requirements['curl'] = [
35     'title' => t('cURL'),
36     'value' => $has_curl ? t('Enabled') : t('Not found'),
37   ];
38   if (!$has_curl) {
39     $requirements['curl']['severity'] = REQUIREMENT_ERROR;
40     $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>.');
41   }
42
43   // SimpleTest currently needs 2 cURL options which are incompatible with
44   // having PHP's open_basedir restriction set.
45   // See https://www.drupal.org/node/674304.
46   $requirements['php_open_basedir'] = [
47     'title' => t('PHP open_basedir restriction'),
48     'value' => $open_basedir ? t('Enabled') : t('Disabled'),
49   ];
50   if ($open_basedir) {
51     $requirements['php_open_basedir']['severity'] = REQUIREMENT_ERROR;
52     $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.');
53   }
54
55   // Check the current memory limit. If it is set too low, SimpleTest will fail
56   // to load all tests and throw a fatal error.
57   $memory_limit = ini_get('memory_limit');
58   if (!Environment::checkMemoryLimit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
59     $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
60     $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']);
61   }
62
63   $site_directory = 'sites/simpletest';
64   if (!drupal_verify_install_file(\Drupal::root() . '/' . $site_directory, FILE_EXIST | FILE_READABLE | FILE_WRITABLE | FILE_EXECUTABLE, 'dir')) {
65     $requirements['simpletest_site_directory'] = [
66       'title' => t('Simpletest site directory'),
67       'value' => is_dir(\Drupal::root() . '/' . $site_directory) ? t('Not writable') : t('Missing'),
68       'severity' => REQUIREMENT_ERROR,
69       'description' => t('The testing framework requires the %sites-simpletest directory to exist and be writable in order to run tests.', [
70         '%sites-simpletest' => $site_directory,
71       ]),
72     ];
73   }
74   elseif (!file_save_htaccess(\Drupal::root() . '/' . $site_directory, FALSE)) {
75     $requirements['simpletest_site_directory'] = [
76       'title' => t('Simpletest site directory'),
77       'value' => t('Not protected'),
78       'severity' => REQUIREMENT_ERROR,
79       '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.', [
80         '%file' => $site_directory . '/.htaccess',
81       ]),
82     ];
83   }
84
85   return $requirements;
86 }
87
88 /**
89  * Implements hook_schema().
90  */
91 function simpletest_schema() {
92   $schema['simpletest'] = [
93     'description' => 'Stores simpletest messages',
94     'fields' => [
95       'message_id'  => [
96         'type' => 'serial',
97         'not null' => TRUE,
98         'description' => 'Primary Key: Unique simpletest message ID.',
99       ],
100       'test_id' => [
101         'type' => 'int',
102         'not null' => TRUE,
103         'default' => 0,
104         'description' => 'Test ID, messages belonging to the same ID are reported together',
105       ],
106       'test_class' => [
107         'type' => 'varchar_ascii',
108         'length' => 255,
109         'not null' => TRUE,
110         'default' => '',
111         'description' => 'The name of the class that created this message.',
112       ],
113       'status' => [
114         'type' => 'varchar',
115         'length' => 9,
116         'not null' => TRUE,
117         'default' => '',
118         'description' => 'Message status. Core understands pass, fail, exception.',
119       ],
120       'message' => [
121         'type' => 'text',
122         'not null' => TRUE,
123         'description' => 'The message itself.',
124       ],
125       'message_group' => [
126         'type' => 'varchar_ascii',
127         'length' => 255,
128         'not null' => TRUE,
129         'default' => '',
130         'description' => 'The message group this message belongs to. For example: warning, browser, user.',
131       ],
132       'function' => [
133         'type' => 'varchar_ascii',
134         'length' => 255,
135         'not null' => TRUE,
136         'default' => '',
137         'description' => 'Name of the assertion function or method that created this message.',
138       ],
139       'line' => [
140         'type' => 'int',
141         'not null' => TRUE,
142         'default' => 0,
143         'description' => 'Line number on which the function is called.',
144       ],
145       'file' => [
146         'type' => 'varchar',
147         'length' => 255,
148         'not null' => TRUE,
149         'default' => '',
150         'description' => 'Name of the file where the function is called.',
151       ],
152     ],
153     'primary key' => ['message_id'],
154     'indexes' => [
155       'reporter' => ['test_class', 'message_id'],
156     ],
157   ];
158   $schema['simpletest_test_id'] = [
159     'description' => 'Stores simpletest test IDs, used to auto-increment the test ID so that a fresh test ID is used.',
160     'fields' => [
161       'test_id'  => [
162         'type' => 'serial',
163         'not null' => TRUE,
164         'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests
165                             are run a new test ID is used.',
166       ],
167       'last_prefix' => [
168         'type' => 'varchar',
169         'length' => 60,
170         'not null' => FALSE,
171         'default' => '',
172         'description' => 'The last database prefix used during testing.',
173       ],
174     ],
175     'primary key' => ['test_id'],
176   ];
177   return $schema;
178 }
179
180 /**
181  * Implements hook_uninstall().
182  */
183 function simpletest_uninstall() {
184   // Do not clean the environment in case the Simpletest module is uninstalled
185   // in a (recursive) test for itself, since simpletest_clean_environment()
186   // would also delete the test site of the parent test process.
187   if (!drupal_valid_test_ua()) {
188     simpletest_clean_environment();
189   }
190   // Delete verbose test output and any other testing framework files.
191   file_unmanaged_delete_recursive('public://simpletest');
192 }