simpletest.install 7.2 KB

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