EnvironmentCleanerService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\simpletest;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Messenger\MessengerInterface;
  5. use Drupal\Core\StringTranslation\TranslationInterface;
  6. use Drupal\Core\Config\ConfigFactory;
  7. use Drupal\Core\Cache\CacheBackendInterface;
  8. use Drupal\Core\File\FileSystem;
  9. use Drupal\Core\Test\EnvironmentCleaner;
  10. /**
  11. * Uses containerized services to perform post-test cleanup.
  12. */
  13. class EnvironmentCleanerService extends EnvironmentCleaner {
  14. /**
  15. * Messenger service.
  16. *
  17. * @var \Drupal\Core\Messenger\MessengerInterface
  18. */
  19. protected $messenger;
  20. /**
  21. * The translation service.
  22. *
  23. * @var \Drupal\Core\StringTranslation\TranslationInterface
  24. */
  25. protected $translation;
  26. /**
  27. * The config factory.
  28. *
  29. * @var \Drupal\Core\Config\ConfigFactory
  30. */
  31. protected $configFactory;
  32. /**
  33. * Default cache.
  34. *
  35. * @var \Drupal\Core\Cache\CacheBackendInterface
  36. */
  37. protected $cacheDefault;
  38. /**
  39. * Construct an environment cleaner.
  40. *
  41. * @param string $root
  42. * The path to the root of the Drupal installation.
  43. * @param \Drupal\Core\Database\Connection $test_database
  44. * Connection to the database against which tests were run.
  45. * @param \Drupal\Core\Database\Connection $results_database
  46. * Connection to the database where test results were stored. This could be
  47. * the same as $test_database, or it could be different.
  48. * @param \Drupal\Core\StringTranslation\TranslationInterface|null $translation
  49. * (optional) The translation service. If none is supplied, this class will
  50. * attempt to discover one using \Drupal.
  51. */
  52. public function __construct($root, Connection $test_database, Connection $results_database, MessengerInterface $messenger, TranslationInterface $translation, ConfigFactory $config, CacheBackendInterface $cache_default, FileSystem $file_system) {
  53. $this->root = $root;
  54. $this->testDatabase = $test_database;
  55. $this->resultsDatabase = $results_database;
  56. $this->messenger = $messenger;
  57. $this->translation = $translation;
  58. $this->configFactory = $config;
  59. $this->cacheDefault = $cache_default;
  60. $this->fileSystem = $file_system;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories = TRUE, $clear_database = TRUE) {
  66. $results_removed = 0;
  67. $clear_results = $this->configFactory->get('simpletest.settings')->get('clear_results');
  68. if ($clear_database) {
  69. $this->cleanDatabase();
  70. }
  71. if ($clear_temp_directories) {
  72. $this->cleanTemporaryDirectories();
  73. }
  74. if ($clear_results) {
  75. $results_removed = $this->cleanResultsTable();
  76. }
  77. $this->cacheDefault->delete('simpletest');
  78. $this->cacheDefault->delete('simpletest_phpunit');
  79. if ($clear_results) {
  80. $this->messenger->addMessage($this->translation->formatPlural($results_removed, 'Removed 1 test result.', 'Removed @count test results.'));
  81. }
  82. else {
  83. $this->messenger->addMessage($this->translation->translate('Clear results is disabled and the test results table will not be cleared.'), 'warning');
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function cleanDatabase() {
  90. $tables_removed = $this->doCleanDatabase();
  91. if ($tables_removed > 0) {
  92. $this->messenger->addMessage($this->translation->formatPlural($tables_removed, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
  93. }
  94. else {
  95. $this->messenger->addMessage($this->translation->translate('No leftover tables to remove.'));
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function cleanTemporaryDirectories() {
  102. $directories_removed = $this->doCleanTemporaryDirectories();
  103. if ($directories_removed > 0) {
  104. $this->messenger->addMessage($this->translation->formatPlural($directories_removed, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
  105. }
  106. else {
  107. $this->messenger->addMessage($this->translation->translate('No temporary directories to remove.'));
  108. }
  109. }
  110. }