View source
<?php
namespace Drupal\Tests\Scripts;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Core\Database\Database;
use Drupal\Core\Test\TestDatabase;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
class TestSiteApplicationTest extends UnitTestCase {
protected $php;
protected function setUp() : void {
parent::setUp();
$php_executable_finder = new PhpExecutableFinder();
$this->php = $php_executable_finder
->find();
$this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
}
public function testInstallWithNonExistingFile() {
$connection = Database::getConnection('default', $this
->addTestDatabase(''));
$table_count = count($connection
->schema()
->findTables('%'));
$command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->run();
$this
->assertStringContainsString('The file this-class-does-not-exist does not exist.', $process
->getErrorOutput());
$this
->assertSame(1, $process
->getExitCode());
$this
->assertCount($table_count, $connection
->schema()
->findTables('%'), 'No additional tables created in the database');
}
public function testInstallWithFileWithNoClass() {
$connection = Database::getConnection('default', $this
->addTestDatabase(''));
$table_count = count($connection
->schema()
->findTables('%'));
$command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/fixtures/empty_file.php.module --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->run();
$this
->assertStringContainsString('The file core/tests/fixtures/empty_file.php.module does not contain a class', $process
->getErrorOutput());
$this
->assertSame(1, $process
->getExitCode());
$this
->assertCount($table_count, $connection
->schema()
->findTables('%'), 'No additional tables created in the database');
}
public function testInstallWithNonSetupClass() {
$this
->markTestIncomplete('Fix this test in https://www.drupal.org/project/drupal/issues/2962157.');
$connection = Database::getConnection('default', $this
->addTestDatabase(''));
$table_count = count($connection
->schema()
->findTables('%'));
$command_line = $this->php . ' core/scripts/test-site.php install --setup-file "' . __FILE__ . '" --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root, [
'COLUMNS' => PHP_INT_MAX,
]);
$process
->run();
$this
->assertStringContainsString('The class Drupal\\Tests\\Scripts\\TestSiteApplicationTest contained in', $process
->getErrorOutput());
$this
->assertStringContainsString('needs to implement \\Drupal\\TestSite\\TestSetupInterface', $process
->getErrorOutput());
$this
->assertSame(1, $process
->getExitCode());
$this
->assertCount($table_count, $connection
->schema()
->findTables('%'), 'No additional tables created in the database');
}
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");
}
$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 = Process::fromShellCommandline($command_line, $this->root);
$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);
$this
->assertStringContainsString('Test page | Drupal', (string) $response
->getBody());
$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);
$this
->assertFileExists($this
->getTestLockFile($db_prefix));
$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);
$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
->assertMatchesRegularExpression($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('%')));
$this
->assertFileExists($this
->getTestLockFile($other_db_prefix));
$command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$this
->assertStringContainsString("Successfully uninstalled {$db_prefix} test site", $process
->getOutput());
$this
->assertCount(0, Database::getConnection('default', $key)
->schema()
->findTables('%'));
$this
->assertFileDoesNotExist($test_file);
$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);
$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 = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$this
->assertStringContainsString("Successfully uninstalled {$other_db_prefix} test site", $process
->getOutput());
$this
->assertCount(0, Database::getConnection('default', $other_key)
->schema()
->findTables('%'));
$this
->assertFileDoesNotExist($test_file);
$this
->assertFileExists($this
->getTestLockFile($db_prefix));
$this
->assertFileDoesNotExist($this
->getTestLockFile($other_db_prefix));
}
public function testInstallInDifferentLanguage() {
$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");
}
$command_line = $this->php . ' core/scripts/test-site.php install --json --langcode fr --setup-file core/tests/Drupal/TestSite/TestSiteMultilingualInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertEquals(0, $process
->getExitCode());
$result = json_decode($process
->getOutput(), TRUE);
$db_prefix = $result['db_prefix'];
$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);
$this
->assertStringContainsString('Test page | Drupal', (string) $response
->getBody());
$this
->assertStringContainsString('lang="fr"', (string) $response
->getBody());
$command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$this
->assertCount(0, Database::getConnection('default', $this
->addTestDatabase($db_prefix))
->schema()
->findTables('%'));
}
public function testTearDownDbPrefixValidation() {
$command_line = $this->php . ' core/scripts/test-site.php tear-down not-a-valid-prefix';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(1, $process
->getExitCode());
$this
->assertStringContainsString('Invalid database prefix: not-a-valid-prefix', $process
->getErrorOutput());
}
public function testUserLogin() {
$this
->markTestIncomplete('Fix this test in https://www.drupal.org/project/drupal/issues/2962157.');
$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");
}
$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 = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$result = json_decode($process
->getOutput(), TRUE);
$db_prefix = $result['db_prefix'];
$site_path = $result['site_path'];
$this
->assertSame('sites/simpletest/' . str_replace('test', '', $db_prefix), $site_path);
$command_line = $this->php . ' core/scripts/test-site.php user-login 1 --site-path ' . $site_path;
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$this
->assertStringContainsString('/user/reset/1/', $process
->getOutput());
$http_client = new Client();
$request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . trim($process
->getOutput())))
->withHeader('User-Agent', trim($result['user_agent']));
$response = $http_client
->send($request);
$this
->assertTrue($response
->getHeader('Set-Cookie'));
$command_line = $this->php . ' core/scripts/test-site.php user-login invalid-uid --site-path ' . $site_path;
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->run();
$this
->assertSame(1, $process
->getExitCode());
$this
->assertStringContainsString('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process
->getErrorOutput());
$command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
$process = Process::fromShellCommandline($command_line, $this->root);
$process
->setTimeout(500);
$process
->run();
$this
->assertSame(0, $process
->getExitCode());
$this
->assertStringContainsString("Successfully uninstalled {$db_prefix} test site", $process
->getOutput());
}
protected function addTestDatabase($db_prefix) {
$database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB'), $this->root);
$database['prefix'] = $db_prefix;
$target = __CLASS__ . $db_prefix;
Database::addConnectionInfo($target, 'default', $database);
return $target;
}
protected function getTestLockFile($db_prefix) {
$lock_id = str_replace('test', '', $db_prefix);
return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;
}
}