simpletest.install 6.7 KB

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