You are here

class EnvironmentCleanerService in Drupal 8

Uses containerized services to perform post-test cleanup.

Hierarchy

Expanded class hierarchy of EnvironmentCleanerService

1 string reference to 'EnvironmentCleanerService'
simpletest.services.yml in core/modules/simpletest/simpletest.services.yml
core/modules/simpletest/simpletest.services.yml
1 service uses EnvironmentCleanerService
environment_cleaner in core/modules/simpletest/simpletest.services.yml
Drupal\simpletest\EnvironmentCleanerService

File

core/modules/simpletest/src/EnvironmentCleanerService.php, line 16

Namespace

Drupal\simpletest
View source
class EnvironmentCleanerService extends EnvironmentCleaner {

  /**
   * Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The translation service.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translation;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Default cache.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheDefault;

  /**
   * Construct an environment cleaner.
   *
   * @param string $root
   *   The path to the root of the Drupal installation.
   * @param \Drupal\Core\Database\Connection $test_database
   *   Connection to the database against which tests were run.
   * @param \Drupal\Core\Database\Connection $results_database
   *   Connection to the database where test results were stored. This could be
   *   the same as $test_database, or it could be different.
   * @param \Drupal\Core\StringTranslation\TranslationInterface|null $translation
   *   (optional) The translation service. If none is supplied, this class will
   *   attempt to discover one using \Drupal.
   */
  public function __construct($root, Connection $test_database, Connection $results_database, MessengerInterface $messenger, TranslationInterface $translation, ConfigFactory $config, CacheBackendInterface $cache_default, FileSystem $file_system) {
    $this->root = $root;
    $this->testDatabase = $test_database;
    $this->resultsDatabase = $results_database;
    $this->messenger = $messenger;
    $this->translation = $translation;
    $this->configFactory = $config;
    $this->cacheDefault = $cache_default;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories = TRUE, $clear_database = TRUE) {
    $results_removed = 0;
    $clear_results = $this->configFactory
      ->get('simpletest.settings')
      ->get('clear_results');
    if ($clear_database) {
      $this
        ->cleanDatabase();
    }
    if ($clear_temp_directories) {
      $this
        ->cleanTemporaryDirectories();
    }
    if ($clear_results) {
      $results_removed = $this
        ->cleanResultsTable();
    }
    $this->cacheDefault
      ->delete('simpletest');
    $this->cacheDefault
      ->delete('simpletest_phpunit');
    if ($clear_results) {
      $this->messenger
        ->addMessage($this->translation
        ->formatPlural($results_removed, 'Removed 1 test result.', 'Removed @count test results.'));
    }
    else {
      $this->messenger
        ->addMessage($this->translation
        ->translate('Clear results is disabled and the test results table will not be cleared.'), 'warning');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function cleanDatabase() {
    $tables_removed = $this
      ->doCleanDatabase();
    if ($tables_removed > 0) {
      $this->messenger
        ->addMessage($this->translation
        ->formatPlural($tables_removed, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
    }
    else {
      $this->messenger
        ->addMessage($this->translation
        ->translate('No leftover tables to remove.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function cleanTemporaryDirectories() {
    $directories_removed = $this
      ->doCleanTemporaryDirectories();
    if ($directories_removed > 0) {
      $this->messenger
        ->addMessage($this->translation
        ->formatPlural($directories_removed, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
    }
    else {
      $this->messenger
        ->addMessage($this->translation
        ->translate('No temporary directories to remove.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EnvironmentCleaner::$fileSystem protected property The file system service.
EnvironmentCleaner::$output protected property Console output.
EnvironmentCleaner::$resultsDatabase protected property Connection to the database where test results are stored.
EnvironmentCleaner::$root protected property Path to Drupal root directory.
EnvironmentCleaner::$testDatabase protected property Connection to the database being used for tests.
EnvironmentCleaner::cleanResultsTable public function Clears test result tables from the results database. Overrides EnvironmentCleanerInterface::cleanResultsTable
EnvironmentCleaner::doCleanDatabase protected function Performs the fixture database cleanup.
EnvironmentCleaner::doCleanTemporaryDirectories protected function Performs the cleanup of temporary test directories.
EnvironmentCleanerService::$cacheDefault protected property Default cache.
EnvironmentCleanerService::$configFactory protected property The config factory.
EnvironmentCleanerService::$messenger protected property Messenger service.
EnvironmentCleanerService::$translation protected property The translation service.
EnvironmentCleanerService::cleanDatabase public function Remove database entries left over in the fixture database. Overrides EnvironmentCleaner::cleanDatabase
EnvironmentCleanerService::cleanEnvironment public function Removes all test-related database tables and directories. Overrides EnvironmentCleaner::cleanEnvironment
EnvironmentCleanerService::cleanTemporaryDirectories public function Finds all leftover fixture site directories and removes them. Overrides EnvironmentCleaner::cleanTemporaryDirectories
EnvironmentCleanerService::__construct public function Construct an environment cleaner. Overrides EnvironmentCleaner::__construct