3 namespace Drupal\Tests\Scripts;
5 use Drupal\Component\FileSystem\FileSystem;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Test\TestDatabase;
8 use Drupal\Tests\UnitTestCase;
10 use GuzzleHttp\Psr7\Request;
11 use Symfony\Component\Process\PhpExecutableFinder;
12 use Symfony\Component\Process\Process;
15 * Tests core/scripts/test-site.php.
19 * This test uses the Drupal\Core\Database\Database class which has a static.
20 * Therefore run in a separate process to avoid side effects.
22 * @runTestsInSeparateProcesses
23 * @preserveGlobalState disabled
25 * @see \Drupal\TestSite\TestSiteApplication
26 * @see \Drupal\TestSite\Commands\TestSiteInstallCommand
27 * @see \Drupal\TestSite\Commands\TestSiteTearDownCommand
29 class TestSiteApplicationTest extends UnitTestCase {
32 * The PHP executable path.
41 protected function setUp() {
43 $php_executable_finder = new PhpExecutableFinder();
44 $this->php = $php_executable_finder->find();
45 $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
51 public function testInstallWithNonExistingFile() {
53 // Create a connection to the DB configured in SIMPLETEST_DB.
54 $connection = Database::getConnection('default', $this->addTestDatabase(''));
55 $table_count = count($connection->schema()->findTables('%'));
57 $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"';
58 $process = new Process($command_line, $this->root);
61 $this->assertContains('The file this-class-does-not-exist does not exist.', $process->getErrorOutput());
62 $this->assertSame(1, $process->getExitCode());
63 $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
69 public function testInstallWithFileWithNoClass() {
71 // Create a connection to the DB configured in SIMPLETEST_DB.
72 $connection = Database::getConnection('default', $this->addTestDatabase(''));
73 $table_count = count($connection->schema()->findTables('%'));
75 $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/fixtures/empty_file.php.module --db-url "' . getenv('SIMPLETEST_DB') . '"';
76 $process = new Process($command_line, $this->root);
79 $this->assertContains('The file core/tests/fixtures/empty_file.php.module does not contain a class', $process->getErrorOutput());
80 $this->assertSame(1, $process->getExitCode());
81 $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
87 public function testInstallWithNonSetupClass() {
89 // Create a connection to the DB configured in SIMPLETEST_DB.
90 $connection = Database::getConnection('default', $this->addTestDatabase(''));
91 $table_count = count($connection->schema()->findTables('%'));
93 // Use __FILE__ to test absolute paths.
94 $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "' . __FILE__ . '" --db-url "' . getenv('SIMPLETEST_DB') . '"';
95 $process = new Process($command_line, $this->root, ['COLUMNS' => PHP_INT_MAX]);
98 $this->assertContains('The class Drupal\Tests\Scripts\TestSiteApplicationTest contained in', $process->getErrorOutput());
99 $this->assertContains('needs to implement \Drupal\TestSite\TestSetupInterface', $process->getErrorOutput());
100 $this->assertSame(1, $process->getExitCode());
101 $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
107 public function testInstallScript() {
108 $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
109 if (!is_writable($simpletest_path)) {
110 $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
113 // Install a site using the JSON output.
114 $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
115 $process = new Process($command_line, $this->root);
116 // Set the timeout to a value that allows debugging.
117 $process->setTimeout(500);
120 $this->assertSame(0, $process->getExitCode());
121 $result = json_decode($process->getOutput(), TRUE);
122 $db_prefix = $result['db_prefix'];
123 $this->assertStringStartsWith('simpletest' . substr($db_prefix, 4) . ':', $result['user_agent']);
125 $http_client = new Client();
126 $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
127 ->withHeader('User-Agent', trim($result['user_agent']));
129 $response = $http_client->send($request);
130 // Ensure the test_page_test module got installed.
131 $this->assertContains('Test page | Drupal', (string) $response->getBody());
133 // Ensure that there are files and database tables for the tear down command
135 $key = $this->addTestDatabase($db_prefix);
136 $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%')));
137 $test_database = new TestDatabase($db_prefix);
138 $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
139 $this->assertFileExists($test_file);
141 // Ensure the lock file exists.
142 $this->assertFileExists($this->getTestLockFile($db_prefix));
144 // Install another site so we can ensure the tear down command only removes
145 // one site at a time. Use the regular output.
146 $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
147 $process = new Process($command_line, $this->root);
148 // Set the timeout to a value that allows debugging.
149 $process->setTimeout(500);
151 $this->assertContains('Successfully installed a test site', $process->getOutput());
152 $this->assertSame(0, $process->getExitCode());
153 $regex = '/Database prefix\s+([^\s]*)/';
154 $this->assertRegExp($regex, $process->getOutput());
155 preg_match('/Database prefix\s+([^\s]*)/', $process->getOutput(), $matches);
156 $other_db_prefix = $matches[1];
157 $other_key = $this->addTestDatabase($other_db_prefix);
158 $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
160 // Ensure the lock file exists for the new install.
161 $this->assertFileExists($this->getTestLockFile($other_db_prefix));
163 // Now test the tear down process as well, but keep the lock.
164 $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"';
165 $process = new Process($command_line, $this->root);
166 // Set the timeout to a value that allows debugging.
167 $process->setTimeout(500);
169 $this->assertSame(0, $process->getExitCode());
170 $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
172 // Ensure that all the tables and files for this DB prefix are gone.
173 $this->assertCount(0, Database::getConnection('default', $key)->schema()->findTables('%'));
174 $this->assertFileNotExists($test_file);
176 // Ensure the other site's tables and files still exist.
177 $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
178 $test_database = new TestDatabase($other_db_prefix);
179 $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
180 $this->assertFileExists($test_file);
182 // Tear down the other site. Tear down should work if the test site is
183 // broken. Prove this by removing its settings.php.
184 $test_site_settings = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . 'settings.php';
185 $this->assertTrue(unlink($test_site_settings));
186 $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $other_db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
187 $process = new Process($command_line, $this->root);
188 // Set the timeout to a value that allows debugging.
189 $process->setTimeout(500);
191 $this->assertSame(0, $process->getExitCode());
192 $this->assertContains("Successfully uninstalled $other_db_prefix test site", $process->getOutput());
194 // Ensure that all the tables and files for this DB prefix are gone.
195 $this->assertCount(0, Database::getConnection('default', $other_key)->schema()->findTables('%'));
196 $this->assertFileNotExists($test_file);
198 // The lock for the first site should still exist but the second site's lock
199 // is released during tear down.
200 $this->assertFileExists($this->getTestLockFile($db_prefix));
201 $this->assertFileNotExists($this->getTestLockFile($other_db_prefix));
207 public function testInstallInDifferentLanguage() {
208 $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
209 if (!is_writable($simpletest_path)) {
210 $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
213 $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') . '"';
214 $process = new Process($command_line, $this->root);
215 $process->setTimeout(500);
217 $this->assertEquals(0, $process->getExitCode());
219 $result = json_decode($process->getOutput(), TRUE);
220 $db_prefix = $result['db_prefix'];
221 $http_client = new Client();
222 $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
223 ->withHeader('User-Agent', trim($result['user_agent']));
225 $response = $http_client->send($request);
226 // Ensure the test_page_test module got installed.
227 $this->assertContains('Test page | Drupal', (string) $response->getBody());
228 $this->assertContains('lang="fr"', (string) $response->getBody());
230 // Now test the tear down process as well.
231 $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
232 $process = new Process($command_line, $this->root);
233 $process->setTimeout(500);
235 $this->assertSame(0, $process->getExitCode());
237 // Ensure that all the tables for this DB prefix are gone.
238 $this->assertCount(0, Database::getConnection('default', $this->addTestDatabase($db_prefix))->schema()->findTables('%'));
244 public function testTearDownDbPrefixValidation() {
245 $command_line = $this->php . ' core/scripts/test-site.php tear-down not-a-valid-prefix';
246 $process = new Process($command_line, $this->root);
247 $process->setTimeout(500);
249 $this->assertSame(1, $process->getExitCode());
250 $this->assertContains('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput());
256 public function testUserLogin() {
257 $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
258 if (!is_writable($simpletest_path)) {
259 $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
262 // Install a site using the JSON output.
263 $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
264 $process = new Process($command_line, $this->root);
265 // Set the timeout to a value that allows debugging.
266 $process->setTimeout(500);
269 $this->assertSame(0, $process->getExitCode());
270 $result = json_decode($process->getOutput(), TRUE);
271 $db_prefix = $result['db_prefix'];
272 $site_path = $result['site_path'];
273 $this->assertSame('sites/simpletest/' . str_replace('test', '', $db_prefix), $site_path);
275 // Test the user login command with valid uid.
276 $command_line = $this->php . ' core/scripts/test-site.php user-login 1 --site-path ' . $site_path;
277 $process = new Process($command_line, $this->root);
279 $this->assertSame(0, $process->getExitCode());
280 $this->assertContains('/user/reset/1/', $process->getOutput());
282 $http_client = new Client();
283 $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . trim($process->getOutput())))
284 ->withHeader('User-Agent', trim($result['user_agent']));
286 $response = $http_client->send($request);
288 // Ensure the response sets a new session.
289 $this->assertTrue($response->getHeader('Set-Cookie'));
291 // Test the user login command with invalid uid.
292 $command_line = $this->php . ' core/scripts/test-site.php user-login invalid-uid --site-path ' . $site_path;
293 $process = new Process($command_line, $this->root);
295 $this->assertSame(1, $process->getExitCode());
296 $this->assertContains('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process->getErrorOutput());
298 // Now tear down the test site.
299 $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
300 $process = new Process($command_line, $this->root);
301 // Set the timeout to a value that allows debugging.
302 $process->setTimeout(500);
304 $this->assertSame(0, $process->getExitCode());
305 $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
309 * Adds the installed test site to the database connection info.
311 * @param string $db_prefix
312 * The prefix of the installed test site.
315 * The database key of the added connection.
317 protected function addTestDatabase($db_prefix) {
318 $database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB'), $this->root);
319 $database['prefix'] = ['default' => $db_prefix];
320 $target = __CLASS__ . $db_prefix;
321 Database::addConnectionInfo($target, 'default', $database);
326 * Gets the lock file path.
328 * @param string $db_prefix
329 * The prefix of the installed test site.
332 * The lock file path.
334 protected function getTestLockFile($db_prefix) {
335 $lock_id = str_replace('test', '', $db_prefix);
336 return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;