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