simpletest.install 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the simpletest module.
  5. */
  6. use Drupal\Component\FileSecurity\FileSecurity;
  7. use Drupal\Component\Utility\Environment;
  8. use Drupal\Core\File\Exception\FileException;
  9. use Drupal\Core\Test\TestDatabase;
  10. use PHPUnit\Framework\TestCase;
  11. /**
  12. * Minimum value of PHP memory_limit for SimpleTest.
  13. */
  14. const SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT = '128M';
  15. /**
  16. * Implements hook_requirements().
  17. */
  18. function simpletest_requirements($phase) {
  19. $requirements = [];
  20. $requirements['deprecation'] = [
  21. 'title' => t('Testing (SimpleTest)'),
  22. 'value' => t('The <em>Testing</em> (SimpleTest) module is deprecated for removal in Drupal 9. It should not be enabled on production sites. Read <a href="https://www.drupal.org/node/3091784">The Drupal core SimpleTest module is deprecated</a> for alternative ways to run tests during development.'),
  23. 'severity' => $phase === 'runtime' ? REQUIREMENT_WARNING : REQUIREMENT_INFO,
  24. ];
  25. $has_phpunit = class_exists(TestCase::class);
  26. $has_curl = function_exists('curl_init');
  27. $requirements['phpunit'] = [
  28. 'title' => t('PHPUnit dependency'),
  29. 'value' => $has_phpunit ? t('Found') : t('Not found'),
  30. ];
  31. if (!$has_phpunit) {
  32. $requirements['phpunit']['severity'] = REQUIREMENT_ERROR;
  33. $requirements['phpunit']['description'] = t("The testing framework requires the PHPUnit package. Please run 'composer install' to ensure it is present.");
  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. // Check the current memory limit. If it is set too low, SimpleTest will fail
  44. // to load all tests and throw a fatal error.
  45. $memory_limit = ini_get('memory_limit');
  46. if (!Environment::checkMemoryLimit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
  47. $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
  48. $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']);
  49. }
  50. $site_directory = 'sites/simpletest';
  51. if (!drupal_verify_install_file(\Drupal::root() . '/' . $site_directory, FILE_EXIST | FILE_READABLE | FILE_WRITABLE | FILE_EXECUTABLE, 'dir')) {
  52. $requirements['simpletest_site_directory'] = [
  53. 'title' => t('Simpletest site directory'),
  54. 'value' => is_dir(\Drupal::root() . '/' . $site_directory) ? t('Not writable') : t('Missing'),
  55. 'severity' => REQUIREMENT_ERROR,
  56. 'description' => t('The testing framework requires the %sites-simpletest directory to exist and be writable in order to run tests.', [
  57. '%sites-simpletest' => $site_directory,
  58. ]),
  59. ];
  60. }
  61. elseif (!FileSecurity::writeHtaccess(\Drupal::root() . '/' . $site_directory, FALSE)) {
  62. $requirements['simpletest_site_directory'] = [
  63. 'title' => t('Simpletest site directory'),
  64. 'value' => t('Not protected'),
  65. 'severity' => REQUIREMENT_ERROR,
  66. '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.', [
  67. '%file' => $site_directory . '/.htaccess',
  68. ]),
  69. ];
  70. }
  71. return $requirements;
  72. }
  73. /**
  74. * Implements hook_schema().
  75. */
  76. function simpletest_schema() {
  77. return TestDatabase::testingSchema();
  78. }
  79. /**
  80. * Implements hook_uninstall().
  81. */
  82. function simpletest_uninstall() {
  83. // Do not clean the environment in case the Simpletest module is uninstalled
  84. // in a (recursive) test for itself, since simpletest_clean_environment()
  85. // would also delete the test site of the parent test process.
  86. if (!drupal_valid_test_ua()) {
  87. \Drupal::service('environment_cleaner')->cleanEnvironment();
  88. }
  89. // Delete verbose test output and any other testing framework files.
  90. try {
  91. \Drupal::service('file_system')->deleteRecursive('public://simpletest');
  92. }
  93. catch (FileException $e) {
  94. // Ignore.
  95. }
  96. }