Skip to content
Snippets Groups Projects

Improve performance of functional tests by caching Drupal installations

Open Dave Reid requested to merge issue/drupal-2900208:2900208-cache-installs into 11.x
10 unresolved threads
Files
3
@@ -69,6 +69,13 @@ trait FunctionalTestSetupTrait {
*/
protected bool $usesSuperUserAccessPolicy;
/**
* Path to SQL dump file.
*
* @var string
*/
protected $dumpFile;
Please register or sign in to reply
/**
* Prepares site settings and services before installation.
*/
@@ -753,4 +760,115 @@ protected function getDatabaseTypes() {
return $database_types;
}
/**
* Installs Drupal using SQL dump.
*/
protected function installDrupalFromDump() {
Please register or sign in to reply
$this->restoreDatabase();
$this->initUserSession();
$this->prepareSettings();
$connection_info = Database::getConnectionInfo('default');
$databases['default']['default'] = (object) [
'value' => $connection_info['default'],
'required' => TRUE,
];
$settings['databases'] = $databases;
$settings['settings']['hash_salt'] = (object) [
'value' => $this->databasePrefix,
'required' => TRUE,
];
$settings['settings']['hash_salt'] = (object) [
'value' => $this->databasePrefix,
'required' => TRUE,
];
$settings['settings']['file_public_path'] = (object) [
'value' => $this->publicFilesDirectory,
'required' => TRUE,
];
$settings['settings']['file_private_path'] = (object) [
'value' => $this->privateFilesDirectory,
'required' => TRUE,
];
$settings['settings']['file_temp_path'] = (object) [
'value' => $this->tempFilesDirectory,
'required' => TRUE,
];
$this->writeSettings($settings);
// During tests, cacheable responses should get the debugging cacheability
// headers by default.
$this->setContainerParameter('http.response.debug_cacheability_headers', TRUE);
$autoloader = require './autoload.php';
$this->kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
DrupalKernel::bootEnvironment();
$site_path = DrupalKernel::findSitePath($request);
$this->kernel->setSitePath($site_path);
Settings::initialize($this->kernel->getAppRoot(), $this->kernel->getSitePath(), $autoloader);
$this->kernel->boot()->preHandle($request);
$this->container = $this->kernel->getContainer();
\Drupal::service('file_system')->prepareDirectory($this->tempFilesDirectory, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY);
// Manually create the private directory.
\Drupal::service('file_system')->prepareDirectory($this->privateFilesDirectory, FileSystemInterface::CREATE_DIRECTORY);
}
/**
* Dumps database structure and contents of test site.
*/
protected function dumpDatabase() {
Please register or sign in to reply
$connection_info = Database::getConnectionInfo('default');
$user = $connection_info['default']['username'];
$pass = $connection_info['default']['password'];
$db = $connection_info['default']['database'];
$host = $connection_info['default']['host'];
switch ($connection_info['default']['driver']) {
case 'mysql':
case 'Drupal\mysql\Driver\Database\mysql':
$tables = \Drupal::database()
->query("SHOW TABLES LIKE '$this->databasePrefix%'")
->fetchCol();
$tables_param = implode(' ', $tables);
exec("mysqldump -u$user -p$pass -h $host $db $tables_param | sed 's/$this->databasePrefix/default_db_prefix_/' > {$this->dumpFile}");
break;
default:
throw new \LogicException("The database driver {$connection_info['default']['driver']} is not supported yet.");
Please register or sign in to reply
}
}
/**
* Restores database structure and contents of test site.
*/
protected function restoreDatabase() {
Please register or sign in to reply
$connection_info = Database::getConnectionInfo('default');
$user = $connection_info['default']['username'];
$pass = $connection_info['default']['password'];
$db = $connection_info['default']['database'];
$host = $connection_info['default']['host'];
switch ($connection_info['default']['driver']) {
case 'mysql':
case 'Drupal\mysql\Driver\Database\mysql':
exec("sed 's/default_db_prefix_/$this->databasePrefix/' $this->dumpFile | mysql -u$user -p$pass -h $host $db");
break;
default:
throw new \LogicException("The database driver {$connection_info['default']['driver']} is not supported yet.");
Please register or sign in to reply
}
}
}
Loading