getDatabasePrefix example


  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.

  public function testCreateAndGet(): void {
    // Test ::createNew.     $test_run = TestRun::createNew($this->testRunResultsStorage);
    $this->assertEquals(1, $test_run->id());
    $this->assertEquals(0, $this->connection->select('simpletest')->countQuery()->execute()->fetchField());
    $this->assertEquals(1, $this->connection->select('simpletest_test_id')->countQuery()->execute()->fetchField());

    $test_run->setDatabasePrefix('oddity1234');
    $this->assertEquals('oddity1234', $test_run->getDatabasePrefix());
    $this->assertEquals('oddity1234', $this->connection->select('simpletest_test_id', 's')->fields('s', ['last_prefix'])->execute()->fetchField());

    $this->assertEquals(1, $test_run->insertLogEntry($this->getTestLogEntry('Test\GroundControl')));
    $this->assertEquals('oddity1234', $test_run->getDatabasePrefix());
    $this->assertEquals('Test\GroundControl', $test_run->getTestClass());
    $this->assertEquals(1, $this->connection->select('simpletest')->countQuery()->execute()->fetchField());
    $this->assertEquals(1, $this->connection->select('simpletest_test_id')->countQuery()->execute()->fetchField());

    // Explicitly void the $test_run variable.     $test_run = NULL;

    

  protected function tearDown(TestDatabase $test_database$db_url): void {
    // Connect to the test database.     $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']);
  }

  
protected function checkSequenceRenaming(string $tableName): void {
    // For PostgreSQL, we also need to check that the sequence has been renamed.     // The initial name of the sequence has been generated automatically by     // PostgreSQL when the table was created, however, on subsequent table     // renames the name is generated by Drupal and can not be easily     // re-constructed. Hence we can only check that we still have a sequence on     // the new table name.     $sequenceExists = (bool) $this->connection->query("SELECT pg_get_serial_sequence('{" . $tableName . "}', 'id')")->fetchField();
    $this->assertTrue($sequenceExists, 'Sequence was renamed.');

    // Rename the table again and repeat the check.     $anotherTableName = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->schema->renameTable($tableName$anotherTableName);

    $sequenceExists = (bool) $this->connection->query("SELECT pg_get_serial_sequence('{" . $anotherTableName . "}', 'id')")->fetchField();
    $this->assertTrue($sequenceExists, 'Sequence was renamed.');
  }

  /** * {@inheritdoc} */
  public function testTableWithSpecificDataType(): void {
    $table_specification = [
      
$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.       'twig_cache' => FALSE,
      // @see \Drupal\KernelTests\KernelTestBase::register()     ];
    new Settings($settings);

    
/** * @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'],
    ];
    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();
  }

}

      'primary key' => ['id'],
      'unique keys' => [
        'test_field' => ['test_field'],
      ],
    ];

    // PostgreSQL has a max identifier length of 63 characters, MySQL has 64 and     // SQLite does not have any limit. Use the lowest common value and create a     // table name as long as possible in order to cover edge cases around     // identifier names for the table's primary or unique key constraints.     $table_name = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->schema->createTable($table_name$table_specification);

    $this->assertIndexOnColumns($table_name['id'], 'primary');
    $this->assertIndexOnColumns($table_name['test_field'], 'unique');

    $new_table_name = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->assertNull($this->schema->renameTable($table_name$new_table_name));

    // Test for renamed primary and unique keys.     $this->assertIndexOnColumns($new_table_name['id'], 'primary');
    $this->assertIndexOnColumns($new_table_name['test_field'], 'unique');

    
$this->markTestSkipped();
    }

    // Install a site using the standard profile to ensure the one time login     // link generation works.
    $install_command = [
      $this->php,
      'core/scripts/drupal',
      'quick-start',
      'standard',
      "--site-name='Test site {$this->testDb->getDatabasePrefix()}'",
      '--suppress-login',
    ];
    $process = new Process($install_command, NULL, ['DRUPAL_DEV_SITE_PATH' => $this->testDb->getTestSitePath()]);
    $process->setTimeout(500);
    $process->start();
    $guzzle = new Client();
    $port = FALSE;
    $process->waitUntil(function D$type$output) use (&$port) {
      if (preg_match('/127.0.0.1:(\d+)/', $output$match)) {
        $port = $match[1];
        return TRUE;
      }
Home | Imprint | This part of the site doesn't use cookies.