prepareDatabasePrefix example

/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUpAppRoot();

    $this->isInstalled = FALSE;

    $this->setupBaseUrl();

    $this->prepareDatabasePrefix();

    // Install Drupal test site.     $this->prepareEnvironment();

    // Define information about the user 1 account.     $this->rootUser = new UserSession([
      'uid' => 1,
      'name' => 'admin',
      'mail' => 'admin@example.com',
      'pass_raw' => $this->randomMachineName(),
    ]);

    

  protected function prepareEnvironment() {
    // Bootstrap Drupal so we can use Drupal's built in functions.     $this->classLoader = require __DIR__ . '/../../../../../autoload.php';
    $request = Request::createFromGlobals();
    $kernel = TestRunnerKernel::createFromRequest($request$this->classLoader);
    // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.     chdir(DRUPAL_ROOT);
    $kernel->boot();
    $kernel->preHandle($request);
    $this->prepareDatabasePrefix();

    $this->originalSite = $kernel->findSitePath($request);

    // Create test directory ahead of installation so fatal errors and debug     // information can be logged during installation process.     \Drupal::service('file_system')->prepareDirectory($this->siteDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);

    // Prepare filesystem directory paths.     $this->publicFilesDirectory = $this->siteDirectory . '/files';
    $this->privateFilesDirectory = $this->siteDirectory . '/private';
    $this->tempFilesDirectory = $this->siteDirectory . '/temp';
    
protected function prepareDatabasePrefix() {
    $test_db = new TestDatabase();
    $this->siteDirectory = $test_db->getTestSitePath();
    $this->databasePrefix = $test_db->getDatabasePrefix();
  }

  /** * Changes the database connection to the prefixed one. */
  protected function changeDatabasePrefix() {
    if (empty($this->databasePrefix)) {
      $this->prepareDatabasePrefix();
    }

    // If the test is run with argument dburl then use it.     $db_url = getenv('SIMPLETEST_DB');
    if (!empty($db_url)) {
      // Ensure no existing database gets in the way. If a default database       // exists already it must be removed.       Database::removeConnection('default');
      $database = Database::convertDbUrlToConnectionInfo($db_url$this->root ?? DRUPAL_ROOT, TRUE);
      Database::addConnectionInfo('default', 'default', $database);
    }

    
Home | Imprint | This part of the site doesn't use cookies.