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