Chris@17: php = $php_executable_finder->find(); Chris@17: $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testInstallWithNonExistingFile() { Chris@17: Chris@17: // Create a connection to the DB configured in SIMPLETEST_DB. Chris@17: $connection = Database::getConnection('default', $this->addTestDatabase('')); Chris@17: $table_count = count($connection->schema()->findTables('%')); Chris@17: Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->run(); Chris@17: Chris@17: $this->assertContains('The file this-class-does-not-exist does not exist.', $process->getErrorOutput()); Chris@17: $this->assertSame(1, $process->getExitCode()); Chris@17: $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testInstallWithFileWithNoClass() { Chris@17: Chris@17: // Create a connection to the DB configured in SIMPLETEST_DB. Chris@17: $connection = Database::getConnection('default', $this->addTestDatabase('')); Chris@17: $table_count = count($connection->schema()->findTables('%')); Chris@17: Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/fixtures/empty_file.php.module --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->run(); Chris@17: Chris@17: $this->assertContains('The file core/tests/fixtures/empty_file.php.module does not contain a class', $process->getErrorOutput()); Chris@17: $this->assertSame(1, $process->getExitCode()); Chris@17: $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testInstallWithNonSetupClass() { Chris@17: Chris@17: // Create a connection to the DB configured in SIMPLETEST_DB. Chris@17: $connection = Database::getConnection('default', $this->addTestDatabase('')); Chris@17: $table_count = count($connection->schema()->findTables('%')); Chris@17: Chris@17: // Use __FILE__ to test absolute paths. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "' . __FILE__ . '" --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root, ['COLUMNS' => PHP_INT_MAX]); Chris@17: $process->run(); Chris@17: Chris@17: $this->assertContains('The class Drupal\Tests\Scripts\TestSiteApplicationTest contained in', $process->getErrorOutput()); Chris@17: $this->assertContains('needs to implement \Drupal\TestSite\TestSetupInterface', $process->getErrorOutput()); Chris@17: $this->assertSame(1, $process->getExitCode()); Chris@17: $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testInstallScript() { Chris@17: $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest'; Chris@17: if (!is_writable($simpletest_path)) { Chris@17: $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable"); Chris@17: } Chris@17: Chris@17: // Install a site using the JSON output. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $result = json_decode($process->getOutput(), TRUE); Chris@17: $db_prefix = $result['db_prefix']; Chris@17: $this->assertStringStartsWith('simpletest' . substr($db_prefix, 4) . ':', $result['user_agent']); Chris@17: Chris@17: $http_client = new Client(); Chris@17: $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page')) Chris@17: ->withHeader('User-Agent', trim($result['user_agent'])); Chris@17: Chris@17: $response = $http_client->send($request); Chris@17: // Ensure the test_page_test module got installed. Chris@17: $this->assertContains('Test page | Drupal', (string) $response->getBody()); Chris@17: Chris@17: // Ensure that there are files and database tables for the tear down command Chris@17: // to clean up. Chris@17: $key = $this->addTestDatabase($db_prefix); Chris@17: $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%'))); Chris@17: $test_database = new TestDatabase($db_prefix); Chris@17: $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey'; Chris@17: $this->assertFileExists($test_file); Chris@17: Chris@17: // Ensure the lock file exists. Chris@17: $this->assertFileExists($this->getTestLockFile($db_prefix)); Chris@17: Chris@17: // Install another site so we can ensure the tear down command only removes Chris@17: // one site at a time. Use the regular output. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertContains('Successfully installed a test site', $process->getOutput()); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $regex = '/Database prefix\s+([^\s]*)/'; Chris@17: $this->assertRegExp($regex, $process->getOutput()); Chris@17: preg_match('/Database prefix\s+([^\s]*)/', $process->getOutput(), $matches); Chris@17: $other_db_prefix = $matches[1]; Chris@17: $other_key = $this->addTestDatabase($other_db_prefix); Chris@17: $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%'))); Chris@17: Chris@17: // Ensure the lock file exists for the new install. Chris@17: $this->assertFileExists($this->getTestLockFile($other_db_prefix)); Chris@17: Chris@17: // Now test the tear down process as well, but keep the lock. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput()); Chris@17: Chris@17: // Ensure that all the tables and files for this DB prefix are gone. Chris@17: $this->assertCount(0, Database::getConnection('default', $key)->schema()->findTables('%')); Chris@17: $this->assertFileNotExists($test_file); Chris@17: Chris@17: // Ensure the other site's tables and files still exist. Chris@17: $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%'))); Chris@17: $test_database = new TestDatabase($other_db_prefix); Chris@17: $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey'; Chris@17: $this->assertFileExists($test_file); Chris@17: Chris@17: // Tear down the other site. Tear down should work if the test site is Chris@17: // broken. Prove this by removing its settings.php. Chris@17: $test_site_settings = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . 'settings.php'; Chris@17: $this->assertTrue(unlink($test_site_settings)); Chris@17: $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $other_db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $this->assertContains("Successfully uninstalled $other_db_prefix test site", $process->getOutput()); Chris@17: Chris@17: // Ensure that all the tables and files for this DB prefix are gone. Chris@17: $this->assertCount(0, Database::getConnection('default', $other_key)->schema()->findTables('%')); Chris@17: $this->assertFileNotExists($test_file); Chris@17: Chris@17: // The lock for the first site should still exist but the second site's lock Chris@17: // is released during tear down. Chris@17: $this->assertFileExists($this->getTestLockFile($db_prefix)); Chris@17: $this->assertFileNotExists($this->getTestLockFile($other_db_prefix)); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testInstallInDifferentLanguage() { Chris@17: $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest'; Chris@17: if (!is_writable($simpletest_path)) { Chris@17: $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable"); Chris@17: } Chris@17: Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --json --langcode fr --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertEquals(0, $process->getExitCode()); Chris@17: Chris@17: $result = json_decode($process->getOutput(), TRUE); Chris@17: $db_prefix = $result['db_prefix']; Chris@17: $http_client = new Client(); Chris@17: $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page')) Chris@17: ->withHeader('User-Agent', trim($result['user_agent'])); Chris@17: Chris@17: $response = $http_client->send($request); Chris@17: // Ensure the test_page_test module got installed. Chris@17: $this->assertContains('Test page | Drupal', (string) $response->getBody()); Chris@17: $this->assertContains('lang="fr"', (string) $response->getBody()); Chris@17: Chris@17: // Now test the tear down process as well. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: Chris@17: // Ensure that all the tables for this DB prefix are gone. Chris@17: $this->assertCount(0, Database::getConnection('default', $this->addTestDatabase($db_prefix))->schema()->findTables('%')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testTearDownDbPrefixValidation() { Chris@17: $command_line = $this->php . ' core/scripts/test-site.php tear-down not-a-valid-prefix'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertSame(1, $process->getExitCode()); Chris@17: $this->assertContains('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @coversNothing Chris@17: */ Chris@17: public function testUserLogin() { Chris@17: $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest'; Chris@17: if (!is_writable($simpletest_path)) { Chris@17: $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable"); Chris@17: } Chris@17: Chris@17: // Install a site using the JSON output. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $result = json_decode($process->getOutput(), TRUE); Chris@17: $db_prefix = $result['db_prefix']; Chris@17: $site_path = $result['site_path']; Chris@17: $this->assertSame('sites/simpletest/' . str_replace('test', '', $db_prefix), $site_path); Chris@17: Chris@17: // Test the user login command with valid uid. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php user-login 1 --site-path ' . $site_path; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->run(); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $this->assertContains('/user/reset/1/', $process->getOutput()); Chris@17: Chris@17: $http_client = new Client(); Chris@17: $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . trim($process->getOutput()))) Chris@17: ->withHeader('User-Agent', trim($result['user_agent'])); Chris@17: Chris@17: $response = $http_client->send($request); Chris@17: Chris@17: // Ensure the response sets a new session. Chris@17: $this->assertTrue($response->getHeader('Set-Cookie')); Chris@17: Chris@17: // Test the user login command with invalid uid. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php user-login invalid-uid --site-path ' . $site_path; Chris@17: $process = new Process($command_line, $this->root); Chris@17: $process->run(); Chris@17: $this->assertSame(1, $process->getExitCode()); Chris@17: $this->assertContains('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process->getErrorOutput()); Chris@17: Chris@17: // Now tear down the test site. Chris@17: $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"'; Chris@17: $process = new Process($command_line, $this->root); Chris@17: // Set the timeout to a value that allows debugging. Chris@17: $process->setTimeout(500); Chris@17: $process->run(); Chris@17: $this->assertSame(0, $process->getExitCode()); Chris@17: $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Adds the installed test site to the database connection info. Chris@17: * Chris@17: * @param string $db_prefix Chris@17: * The prefix of the installed test site. Chris@17: * Chris@17: * @return string Chris@17: * The database key of the added connection. Chris@17: */ Chris@17: protected function addTestDatabase($db_prefix) { Chris@17: $database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB'), $this->root); Chris@17: $database['prefix'] = ['default' => $db_prefix]; Chris@17: $target = __CLASS__ . $db_prefix; Chris@17: Database::addConnectionInfo($target, 'default', $database); Chris@17: return $target; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets the lock file path. Chris@17: * Chris@17: * @param string $db_prefix Chris@17: * The prefix of the installed test site. Chris@17: * Chris@17: * @return string Chris@17: * The lock file path. Chris@17: */ Chris@17: protected function getTestLockFile($db_prefix) { Chris@17: $lock_id = str_replace('test', '', $db_prefix); Chris@17: return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id; Chris@17: } Chris@17: Chris@17: }