ThunderTestTrait.php in Thunder 8.3
File
tests/src/Traits/ThunderTestTrait.php
View source
<?php
namespace Drupal\Tests\thunder\Traits;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Database;
use Drupal\dblog\Controller\DbLogController;
use Symfony\Component\HttpFoundation\Request;
trait ThunderTestTrait {
protected function installParameters() {
$parameters = parent::installParameters();
$parameters['forms']['thunder_module_configure_form'] = [
'install_modules_thunder_demo' => NULL,
];
return $parameters;
}
public function installDrupal() {
$this
->initUserSession();
$this
->prepareSettings();
$this
->doInstall();
$this
->initSettings();
$request = Request::createFromGlobals();
$container = $this
->initKernel($request);
$this
->initConfig($container);
$this
->installModulesFromClassProperty($container);
$this
->rebuildAll();
}
protected function prepareSettings() {
parent::prepareSettings();
$settings['settings']['install_profile'] = (object) [
'value' => $this->profile,
'required' => TRUE,
];
$settings['settings']['hash_salt'] = (object) [
'value' => Crypt::randomBytesBase64(55),
'required' => TRUE,
];
$settings['databases']['default'] = (object) [
'value' => Database::getConnectionInfo(),
'required' => TRUE,
];
$this
->writeSettings($settings);
}
protected function doInstall() {
if (!empty($_SERVER['thunderDumpFile'])) {
$file = $_SERVER['thunderDumpFile'];
if (substr($file, -3) == '.gz') {
$file = "compress.zlib://{$file}";
}
require $file;
}
else {
parent::doInstall();
}
}
protected function logWithRole($role) {
$editor = $this
->drupalCreateUser();
$editor
->addRole($role);
$editor
->save();
$this
->drupalLogin($editor);
return $editor;
}
protected function tearDown() {
$query = \Drupal::database()
->select('watchdog', 'w')
->fields('w', [
'message',
'variables',
]);
$andGroup = $query
->andConditionGroup()
->condition('severity', 5, '<')
->condition('type', 'php');
$group = $query
->orConditionGroup()
->condition('severity', 4, '<')
->condition($andGroup);
$query
->condition($group);
$query
->groupBy('w.message');
$query
->groupBy('w.variables');
$controller = DbLogController::create($this->container);
if ($query
->countQuery()
->execute()
->fetchField()) {
$errors = [];
foreach ($query
->execute()
->fetchAll() as $row) {
$errors[] = Unicode::truncate(Html::decodeEntities(strip_tags($controller
->formatMessage($row))), 256, TRUE, TRUE);
}
throw new \Exception(print_r($errors, TRUE));
}
parent::tearDown();
}
}