-
-
Save alexpott/8cd82bc75b88c01489d837cf8432451b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\Tests\my_project\Traits; | |
use Drupal\Component\Utility\Crypt; | |
use Drupal\Core\Command\DbDumpCommand; | |
use Drupal\Core\Database\Database; | |
use Drupal\user\Entity\User; | |
use Symfony\Component\Console\Tester\CommandTester; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Use for speedy test runs if you set MY_PROJECT_TEST_DB_PATH. | |
*/ | |
trait ProfileTestTrait { | |
/** | |
* Installs profile from db dump if available. | |
*/ | |
protected function doInstall() { | |
$path_to_db = getenv('MY_PROJECT_TEST_DB_PATH') ?: FALSE; | |
if ($path_to_db && strpos($path_to_db, 'COMMIT-HASH') !== FALSE) { | |
$rev = @exec('git rev-parse --short HEAD') ?: 'unknown-hash'; | |
$path_to_db = str_replace('COMMIT-HASH', $rev, $path_to_db); | |
} | |
// Load from DB dump if available. | |
if ($path_to_db && file_exists($path_to_db)) { | |
// Generate a hash salt. | |
$settings['settings']['hash_salt'] = (object) [ | |
'value' => Crypt::randomBytesBase64(55), | |
'required' => TRUE, | |
]; | |
// Since the installer isn't run, add the database settings here too. | |
$settings['databases']['default'] = (object) [ | |
'value' => Database::getConnectionInfo(), | |
'required' => TRUE, | |
]; | |
$this->writeSettings($settings); | |
mkdir($this->tempFilesDirectory); | |
require $path_to_db; | |
} | |
else { | |
parent::doInstall(); | |
if ($path_to_db) { | |
// Create DB dump. | |
$command = new DbDumpCommand(); | |
$command_tester = new CommandTester($command); | |
$command_tester->execute([]); | |
file_put_contents($path_to_db, $command_tester->getDisplay()); | |
} | |
} | |
} | |
/** | |
* Resets and rebuilds the environment after setup. | |
*/ | |
protected function rebuildAll() { | |
parent::rebuildAll(); | |
// Ensure the root user is valid. | |
$password = $this->randomMachineName(); | |
$this->rootUser = User::load(1); | |
$this->rootUser->setPassword($password); | |
$this->rootUser->passRaw = $password; | |
$this->rootUser->pass_raw = $password; | |
$this->rootUser->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment