getTestSitePath example

/** * @covers ::__construct * @covers ::getDatabasePrefix * @covers ::getTestSitePath * @covers ::getPhpErrorLogPath * * @dataProvider providerTestConstructor */
  public function testConstructor($db_prefix$expected_db_prefix$expected_site_path) {
    $test_db = new TestDatabase($db_prefix);
    $this->assertEquals($expected_db_prefix$test_db->getDatabasePrefix());
    $this->assertEquals($expected_site_path$test_db->getTestSitePath());
    $this->assertEquals($expected_site_path . '/error.log', $test_db->getPhpErrorLogPath());
  }

  /** * Data provider for self::testConstructor() */
  public function providerTestConstructor() {
    return [
      ['test1234', 'test1234', 'sites/simpletest/1234'],
      ['test123456test234567', 'test123456test234567', 'sites/simpletest/234567'],
    ];
  }
protected function getLockFile(int $lock_id): string {
    return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;
  }

  /** * Gets the file path of the PHP error log of the test. * * @return string * The relative path to the test site PHP error log file. */
  public function getPhpErrorLogPath(): string {
    return $this->getTestSitePath() . '/error.log';
  }

}
    $root = dirname(__DIR__, 5);
    $database = Database::convertDbUrlToConnectionInfo($db_url$root);
    $database['prefix'] = $test_database->getDatabasePrefix();
    Database::addConnectionInfo(__CLASS__, 'default', $database);

    // Remove all the tables.     $schema = Database::getConnection('default', __CLASS__)->schema();
    $tables = $schema->findTables('%');
    array_walk($tables[$schema, 'dropTable']);

    // Delete test site directory.     $this->fileUnmanagedDeleteRecursive($root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath()[BrowserTestBase::class, 'filePreDeleteCallback']);
  }

  /** * Deletes all files and directories in the specified path recursively. * * Note this method has no dependencies on Drupal core to ensure that the * test site can be torn down even if something in the test site is broken. * * @param string $path * A string containing either a URI or a file or directory path. * @param callable $callback * (optional) Callback function to run on each file prior to deleting it and * on each directory prior to traversing it. For example, can be used to * modify permissions. * * @return bool * TRUE for success or if path does not exist, FALSE in the event of an * error. * * @see \Drupal\Core\File\FileSystemInterface::deleteRecursive() */
$this->markTestSkipped('This test requires a writable sites/simpletest directory');
    }
    // Get a lock and a valid site path.     $this->testDb = new TestDatabase();
  }

  /** * {@inheritdoc} */
  protected function tearDown(): void {
    if ($this->testDb) {
      $test_site_directory = $this->root . DIRECTORY_SEPARATOR . $this->testDb->getTestSitePath();
      if (file_exists($test_site_directory)) {
        // @todo use the tear down command from         // https://www.drupal.org/project/drupal/issues/2926633         // Delete test site directory.         $this->fileUnmanagedDeleteRecursive($test_site_directory[
          BrowserTestBase::class,
          'filePreDeleteCallback',
        ]);
      }
    }
    parent::tearDown();
  }

  protected function bootEnvironment() {
    \Drupal::unsetContainer();

    $this->classLoader = require $this->root . '/autoload.php';

    // Set up virtual filesystem.     Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
    $test_db = new TestDatabase();
    $this->siteDirectory = $test_db->getTestSitePath();

    // Ensure that all code that relies on drupal_valid_test_ua() can still be     // safely executed. This primarily affects the (test) site directory     // resolution (used by e.g. LocalStream and PhpStorage).     $this->databasePrefix = $test_db->getDatabasePrefix();
    drupal_valid_test_ua($this->databasePrefix);

    $settings = [
      'hash_salt' => static::class,
      'file_public_path' => $this->siteDirectory . '/files',
      // Disable Twig template caching/dumping.
->withHeader('User-Agent', trim($result['user_agent']));

    $response = $http_client->send($request);
    // Ensure the test_page_test module got installed.     $this->assertStringContainsString('Test page | Drupal', (string) $response->getBody());

    // Ensure that there are files and database tables for the tear down command     // to clean up.     $key = $this->addTestDatabase($db_prefix);
    $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%')));
    $test_database = new TestDatabase($db_prefix);
    $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
    $this->assertFileExists($test_file);

    // Ensure the lock file exists.     $this->assertFileExists($this->getTestLockFile($db_prefix));

    // Install another site so we can ensure the tear down command only removes     // one site at a time. Use the regular output.     $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
    $process = Process::fromShellCommandline($command_line$this->root);
    // Set the timeout to a value that allows debugging.     $process->setTimeout(500);
    

  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 (static::validateHostname($request) === FALSE) {
      throw new BadRequestHttpException();
    }

    if ($app_root === NULL) {
      $app_root = static::guessApplicationRoot();
    }

    // Check for a test override.     if ($test_prefix = drupal_valid_test_ua()) {
      $test_db = new TestDatabase($test_prefix);
      return $test_db->getTestSitePath();
    }

    // Determine whether multi-site functionality is enabled. If not, return     // the default directory.     if (!file_exists($app_root . '/sites/sites.php')) {
      return 'sites/default';
    }

    // Pre-populate host and script variables, then include sites.php which may     // populate $sites with a site-directory mapping.     $script_name = $request->server->get('SCRIPT_NAME');
    
// Ensure that we use the database from SIMPLETEST_DB environment variable.     Database::removeConnection('default');
    $this->changeDatabasePrefixTrait();
  }

  /** * {@inheritdoc} */
  protected function prepareDatabasePrefix() {
    // Override this method so that we can force a lock to be created.     $test_db = new TestDatabase(NULL, TRUE);
    $this->siteDirectory = $test_db->getTestSitePath();
    $this->databasePrefix = $test_db->getDatabasePrefix();
  }

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