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