EnvironmentCleaner.php in Drupal 10
File
core/lib/Drupal/Core/Test/EnvironmentCleaner.php
View source
<?php
namespace Drupal\Core\Test;
use Drupal\Core\Database\Connection;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\Console\Output\OutputInterface;
class EnvironmentCleaner implements EnvironmentCleanerInterface {
protected $root;
protected $testDatabase;
protected $resultsDatabase;
protected $fileSystem;
protected $output;
public function __construct($root, Connection $test_database, Connection $results_database, OutputInterface $output, FileSystemInterface $file_system) {
$this->root = $root;
$this->testDatabase = $test_database;
$this->resultsDatabase = $results_database;
$this->output = $output;
$this->fileSystem = $file_system;
}
public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories = TRUE, $clear_database = TRUE) {
$count = 0;
if ($clear_database) {
$this
->doCleanDatabase();
}
if ($clear_temp_directories) {
$this
->doCleanTemporaryDirectories();
}
if ($clear_results) {
$count = $this
->cleanResultsTable();
$this->output
->write('Test results removed: ' . $count);
}
else {
$this->output
->write('Test results were not removed.');
}
}
public function cleanDatabase() {
$count = $this
->doCleanDatabase();
if ($count > 0) {
$this->output
->write('Leftover tables removed: ' . $count);
}
else {
$this->output
->write('No leftover tables to remove.');
}
}
protected function doCleanDatabase() {
$schema = $this->testDatabase
->schema();
$tables = $schema
->findTables('test%');
$count = 0;
foreach ($tables as $table) {
if (preg_match('/^test\\d+.*/', $table, $matches)) {
$schema
->dropTable($matches[0]);
$count++;
}
}
return $count;
}
public function cleanTemporaryDirectories() {
$count = $this
->doCleanTemporaryDirectories();
if ($count > 0) {
$this->output
->write('Temporary directories removed: ' . $count);
}
else {
$this->output
->write('No temporary directories to remove.');
}
}
protected function doCleanTemporaryDirectories() {
$count = 0;
$simpletest_dir = $this->root . '/sites/simpletest';
if (is_dir($simpletest_dir)) {
$files = scandir($simpletest_dir);
foreach ($files as $file) {
if ($file[0] != '.') {
$path = $simpletest_dir . '/' . $file;
$this->fileSystem
->deleteRecursive($path, function ($any_path) {
@chmod($any_path, 0700);
});
$count++;
}
}
}
return $count;
}
public function cleanResultsTable($test_id = NULL) {
$count = 0;
if ($test_id) {
$count = $this->resultsDatabase
->query('SELECT COUNT([test_id]) FROM {simpletest_test_id} WHERE [test_id] = :test_id', [
':test_id' => $test_id,
])
->fetchField();
$this->resultsDatabase
->delete('simpletest')
->condition('test_id', $test_id)
->execute();
$this->resultsDatabase
->delete('simpletest_test_id')
->condition('test_id', $test_id)
->execute();
}
else {
$count = $this->resultsDatabase
->query('SELECT COUNT([test_id]) FROM {simpletest_test_id}')
->fetchField();
$this->resultsDatabase
->delete('simpletest')
->execute();
$this->resultsDatabase
->delete('simpletest_test_id')
->execute();
}
return $count;
}
}