simpletest.install 7.65 KB
Newer Older
1
2
<?php

3
4
5
6
7
/**
 * @file
 * Install, update and uninstall functions for the simpletest module.
 */

8
use Drupal\Component\Utility\Environment;
9
use Drupal\Component\Utility\String;
10

11
12
/**
 * Minimum value of PHP memory_limit for SimpleTest.
13
14
15
16
 *
 * @todo Reduce the memory required to use SimpleTest on some environments in
 *   https://www.drupal.org/node/2289201 and
 *   https://www.drupal.org/node/2307163 and then decrease this limit.
17
 */
18
const SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT = '320M';
19

20
/**
21
 * Implements hook_requirements().
22
23
24
25
26
 */
function simpletest_requirements($phase) {
  $requirements = array();

  $has_curl = function_exists('curl_init');
27
  $has_domdocument = method_exists('DOMDocument', 'loadHTML');
28
  $open_basedir = ini_get('open_basedir');
29

30
  $requirements['curl'] = array(
31
32
    'title' => t('cURL'),
    'value' => $has_curl ? t('Enabled') : t('Not found'),
33
34
35
  );
  if (!$has_curl) {
    $requirements['curl']['severity'] = REQUIREMENT_ERROR;
36
    $requirements['curl']['description'] = t('The testing framework could not be installed because the PHP <a href="@curl_url">cURL</a> library is not available.', array('@curl_url' => 'http://php.net/manual/curl.setup.php'));
37
38
39
  }

  $requirements['php_domdocument'] = array(
40
41
    'title' => t('PHP DOMDocument class'),
    'value' => $has_domdocument ? t('Enabled') : t('Not found'),
42
43
44
  );
  if (!$has_domdocument) {
    $requirements['php_domdocument']['severity'] = REQUIREMENT_ERROR;
45
    $requirements['php_domdocument']['description'] = t('The testing framework requires the DOMDocument class to be available. Check the configure command at the <a href="@link-phpinfo">PHP info page</a>.', array('@link-phpinfo' => url('admin/reports/status/php')));
46
47
  }

48
49
  // SimpleTest currently needs 2 cURL options which are incompatible with
  // having PHP's open_basedir restriction set.
50
  // See http://drupal.org/node/674304.
51
  $requirements['php_open_basedir'] = array(
52
53
    'title' => t('PHP open_basedir restriction'),
    'value' => $open_basedir ? t('Enabled') : t('Disabled'),
54
55
56
  );
  if ($open_basedir) {
    $requirements['php_open_basedir']['severity'] = REQUIREMENT_ERROR;
57
    $requirements['php_open_basedir']['description'] = t('The testing framework requires the PHP <a href="@open_basedir-url">open_basedir</a> restriction to be disabled. Check your webserver configuration or contact your web host.', array('@open_basedir-url' => 'http://php.net/manual/ini.core.php#ini.open-basedir'));
58
59
  }

60
61
62
  // 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');
63
  if (!Environment::checkMemoryLimit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
64
    $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
65
    $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>.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, '@url' => 'http://drupal.org/node/207036'));
66
67
  }

68
69
70
71
72
73
74
  $site_directory = 'sites/simpletest';
  if (!drupal_verify_install_file(DRUPAL_ROOT . '/' . $site_directory, FILE_EXIST|FILE_READABLE|FILE_WRITABLE|FILE_EXECUTABLE, 'dir')) {
    $requirements['simpletest_site_directory'] = array(
      'title' => t('Simpletest site directory'),
      'value' => is_dir(DRUPAL_ROOT . '/' . $site_directory) ? t('Not writable') : t('Missing'),
      'severity' => REQUIREMENT_ERROR,
      'description' => t('The testing framework requires the !sites-simpletest directory to exist and be writable in order to run tests.', array(
75
        '!sites-simpletest' => '<code>./' . String::checkPlain($site_directory) . '</code>',
76
77
78
79
80
81
82
83
84
      )),
    );
  }
  elseif (!file_save_htaccess(DRUPAL_ROOT . '/' . $site_directory, FALSE)) {
    $requirements['simpletest_site_directory'] = array(
      'title' => t('Simpletest site directory'),
      'value' => t('Not protected'),
      'severity' => REQUIREMENT_ERROR,
      '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.', array(
85
        '!file' => '<code>./' . String::checkPlain($site_directory) . '/.htaccess</code>',
86
87
88
89
      )),
    );
  }

90
91
  return $requirements;
}
92

93
94
95
/**
 * Implements hook_schema().
 */
96
97
function simpletest_schema() {
  $schema['simpletest'] = array(
98
    'description' => 'Stores simpletest messages',
99
100
101
102
    'fields' => array(
      'message_id'  => array(
        'type' => 'serial',
        'not null' => TRUE,
103
        'description' => 'Primary Key: Unique simpletest message ID.',
104
105
106
107
108
      ),
      'test_id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
109
        'description' => 'Test ID, messages belonging to the same ID are reported together',
110
111
112
113
114
115
      ),
      'test_class' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
116
        'description' => 'The name of the class that created this message.',
117
118
119
120
121
122
      ),
      'status' => array(
        'type' => 'varchar',
        'length' => 9,
        'not null' => TRUE,
        'default' => '',
123
        'description' => 'Message status. Core understands pass, fail, exception.',
124
125
      ),
      'message' => array(
126
        'type' => 'text',
127
        'not null' => TRUE,
128
        'description' => 'The message itself.',
129
130
131
132
133
134
      ),
      'message_group' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
135
        'description' => 'The message group this message belongs to. For example: warning, browser, user.',
136
      ),
137
      'function' => array(
138
139
140
141
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
142
        'description' => 'Name of the assertion function or method that created this message.',
143
144
145
146
147
      ),
      'line' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
148
        'description' => 'Line number on which the function is called.',
149
150
151
152
153
154
      ),
      'file' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
155
        'description' => 'Name of the file where the function is called.',
156
157
158
159
      ),
    ),
    'primary key' => array('message_id'),
    'indexes' => array(
160
      'reporter' => array('test_class', 'message_id'),
161
162
163
    ),
  );
  $schema['simpletest_test_id'] = array(
164
    'description' => 'Stores simpletest test IDs, used to auto-incrament the test ID so that a fresh test ID is used.',
165
    'fields' => array(
166
      'test_id'  => array(
167
168
        'type' => 'serial',
        'not null' => TRUE,
169
170
        '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.',
171
      ),
172
173
174
175
176
177
178
      'last_prefix' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => FALSE,
        'default' => '',
        'description' => 'The last database prefix used during testing.',
      ),
179
    ),
180
    'primary key' => array('test_id'),
181
182
183
  );
  return $schema;
}
184
185
186
187
188

/**
 * Implements hook_uninstall().
 */
function simpletest_uninstall() {
189
190
191
  // 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.
192
  if (!drupal_valid_test_ua()) {
193
194
195
    simpletest_clean_environment();
  }
  // Delete verbose test output and any other testing framework files.
196
197
  file_unmanaged_delete_recursive('public://simpletest');
}