You are here

public function TestSiteApplicationTest::testInstallScript in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php \Drupal\Tests\Scripts\TestSiteApplicationTest::testInstallScript()

@coversNothing

File

core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php, line 108

Class

TestSiteApplicationTest
Tests core/scripts/test-site.php.

Namespace

Drupal\Tests\Scripts

Code

public function testInstallScript() {
  $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  if (!is_writable($simpletest_path)) {
    $this
      ->markTestSkipped("Requires the directory {$simpletest_path} to exist and be writable");
  }

  // Install a site using the JSON output.
  $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  $process = new Process($command_line, $this->root);

  // Set the timeout to a value that allows debugging.
  $process
    ->setTimeout(500);
  $process
    ->run();
  $this
    ->assertSame(0, $process
    ->getExitCode());
  $result = json_decode($process
    ->getOutput(), TRUE);
  $db_prefix = $result['db_prefix'];
  $this
    ->assertStringStartsWith('simpletest' . substr($db_prefix, 4) . ':', $result['user_agent']);
  $http_client = new Client();
  $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
    ->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 = new Process($command_line, $this->root);

  // Set the timeout to a value that allows debugging.
  $process
    ->setTimeout(500);
  $process
    ->run();
  $this
    ->assertStringContainsString('Successfully installed a test site', $process
    ->getOutput());
  $this
    ->assertSame(0, $process
    ->getExitCode());
  $regex = '/Database prefix\\s+([^\\s]*)/';
  $this
    ->assertRegExp($regex, $process
    ->getOutput());
  preg_match('/Database prefix\\s+([^\\s]*)/', $process
    ->getOutput(), $matches);
  $other_db_prefix = $matches[1];
  $other_key = $this
    ->addTestDatabase($other_db_prefix);
  $this
    ->assertGreaterThan(0, count(Database::getConnection('default', $other_key)
    ->schema()
    ->findTables('%')));

  // Ensure the lock file exists for the new install.
  $this
    ->assertFileExists($this
    ->getTestLockFile($other_db_prefix));

  // Now test the tear down process as well, but keep the lock.
  $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"';
  $process = new Process($command_line, $this->root);

  // Set the timeout to a value that allows debugging.
  $process
    ->setTimeout(500);
  $process
    ->run();
  $this
    ->assertSame(0, $process
    ->getExitCode());
  $this
    ->assertStringContainsString("Successfully uninstalled {$db_prefix} test site", $process
    ->getOutput());

  // Ensure that all the tables and files for this DB prefix are gone.
  $this
    ->assertCount(0, Database::getConnection('default', $key)
    ->schema()
    ->findTables('%'));
  $this
    ->assertFileNotExists($test_file);

  // Ensure the other site's tables and files still exist.
  $this
    ->assertGreaterThan(0, count(Database::getConnection('default', $other_key)
    ->schema()
    ->findTables('%')));
  $test_database = new TestDatabase($other_db_prefix);
  $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database
    ->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
  $this
    ->assertFileExists($test_file);

  // Tear down the other site. Tear down should work if the test site is
  // broken. Prove this by removing its settings.php.
  $test_site_settings = $this->root . DIRECTORY_SEPARATOR . $test_database
    ->getTestSitePath() . DIRECTORY_SEPARATOR . 'settings.php';
  $this
    ->assertTrue(unlink($test_site_settings));
  $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $other_db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  $process = new Process($command_line, $this->root);

  // Set the timeout to a value that allows debugging.
  $process
    ->setTimeout(500);
  $process
    ->run();
  $this
    ->assertSame(0, $process
    ->getExitCode());
  $this
    ->assertStringContainsString("Successfully uninstalled {$other_db_prefix} test site", $process
    ->getOutput());

  // Ensure that all the tables and files for this DB prefix are gone.
  $this
    ->assertCount(0, Database::getConnection('default', $other_key)
    ->schema()
    ->findTables('%'));
  $this
    ->assertFileNotExists($test_file);

  // The lock for the first site should still exist but the second site's lock
  // is released during tear down.
  $this
    ->assertFileExists($this
    ->getTestLockFile($db_prefix));
  $this
    ->assertFileNotExists($this
    ->getTestLockFile($other_db_prefix));
}