simpletest.install 7.2 KB

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