TestSiteApplicationTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace Drupal\Tests\Scripts;
  3. use Drupal\Component\FileSystem\FileSystem;
  4. use Drupal\Core\Database\Database;
  5. use Drupal\Core\Test\TestDatabase;
  6. use Drupal\Tests\UnitTestCase;
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Psr7\Request;
  9. use Symfony\Component\Process\PhpExecutableFinder;
  10. use Symfony\Component\Process\Process;
  11. /**
  12. * Tests core/scripts/test-site.php.
  13. *
  14. * @group Setup
  15. *
  16. * This test uses the Drupal\Core\Database\Database class which has a static.
  17. * Therefore run in a separate process to avoid side effects.
  18. *
  19. * @runTestsInSeparateProcesses
  20. * @preserveGlobalState disabled
  21. *
  22. * @see \Drupal\TestSite\TestSiteApplication
  23. * @see \Drupal\TestSite\Commands\TestSiteInstallCommand
  24. * @see \Drupal\TestSite\Commands\TestSiteTearDownCommand
  25. */
  26. class TestSiteApplicationTest extends UnitTestCase {
  27. /**
  28. * The PHP executable path.
  29. *
  30. * @var string
  31. */
  32. protected $php;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function setUp() {
  37. parent::setUp();
  38. $php_executable_finder = new PhpExecutableFinder();
  39. $this->php = $php_executable_finder->find();
  40. $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
  41. }
  42. /**
  43. * @coversNothing
  44. */
  45. public function testInstallWithNonExistingFile() {
  46. // Create a connection to the DB configured in SIMPLETEST_DB.
  47. $connection = Database::getConnection('default', $this->addTestDatabase(''));
  48. $table_count = count($connection->schema()->findTables('%'));
  49. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "this-class-does-not-exist" --db-url "' . getenv('SIMPLETEST_DB') . '"';
  50. $process = new Process($command_line, $this->root);
  51. $process->run();
  52. $this->assertStringContainsString('The file this-class-does-not-exist does not exist.', $process->getErrorOutput());
  53. $this->assertSame(1, $process->getExitCode());
  54. $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
  55. }
  56. /**
  57. * @coversNothing
  58. */
  59. public function testInstallWithFileWithNoClass() {
  60. // Create a connection to the DB configured in SIMPLETEST_DB.
  61. $connection = Database::getConnection('default', $this->addTestDatabase(''));
  62. $table_count = count($connection->schema()->findTables('%'));
  63. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/fixtures/empty_file.php.module --db-url "' . getenv('SIMPLETEST_DB') . '"';
  64. $process = new Process($command_line, $this->root);
  65. $process->run();
  66. $this->assertStringContainsString('The file core/tests/fixtures/empty_file.php.module does not contain a class', $process->getErrorOutput());
  67. $this->assertSame(1, $process->getExitCode());
  68. $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
  69. }
  70. /**
  71. * @coversNothing
  72. */
  73. public function testInstallWithNonSetupClass() {
  74. $this->markTestIncomplete('Fix this test in https://www.drupal.org/project/drupal/issues/2962157.');
  75. // Create a connection to the DB configured in SIMPLETEST_DB.
  76. $connection = Database::getConnection('default', $this->addTestDatabase(''));
  77. $table_count = count($connection->schema()->findTables('%'));
  78. // Use __FILE__ to test absolute paths.
  79. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "' . __FILE__ . '" --db-url "' . getenv('SIMPLETEST_DB') . '"';
  80. $process = new Process($command_line, $this->root, ['COLUMNS' => PHP_INT_MAX]);
  81. $process->run();
  82. $this->assertStringContainsString('The class Drupal\Tests\Scripts\TestSiteApplicationTest contained in', $process->getErrorOutput());
  83. $this->assertStringContainsString('needs to implement \Drupal\TestSite\TestSetupInterface', $process->getErrorOutput());
  84. $this->assertSame(1, $process->getExitCode());
  85. $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
  86. }
  87. /**
  88. * @coversNothing
  89. */
  90. public function testInstallScript() {
  91. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  92. if (!is_writable($simpletest_path)) {
  93. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  94. }
  95. // Install a site using the JSON output.
  96. $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  97. $process = new Process($command_line, $this->root);
  98. // Set the timeout to a value that allows debugging.
  99. $process->setTimeout(500);
  100. $process->run();
  101. $this->assertSame(0, $process->getExitCode());
  102. $result = json_decode($process->getOutput(), TRUE);
  103. $db_prefix = $result['db_prefix'];
  104. $this->assertStringStartsWith('simpletest' . substr($db_prefix, 4) . ':', $result['user_agent']);
  105. $http_client = new Client();
  106. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
  107. ->withHeader('User-Agent', trim($result['user_agent']));
  108. $response = $http_client->send($request);
  109. // Ensure the test_page_test module got installed.
  110. $this->assertStringContainsString('Test page | Drupal', (string) $response->getBody());
  111. // Ensure that there are files and database tables for the tear down command
  112. // to clean up.
  113. $key = $this->addTestDatabase($db_prefix);
  114. $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%')));
  115. $test_database = new TestDatabase($db_prefix);
  116. $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
  117. $this->assertFileExists($test_file);
  118. // Ensure the lock file exists.
  119. $this->assertFileExists($this->getTestLockFile($db_prefix));
  120. // Install another site so we can ensure the tear down command only removes
  121. // one site at a time. Use the regular output.
  122. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  123. $process = new Process($command_line, $this->root);
  124. // Set the timeout to a value that allows debugging.
  125. $process->setTimeout(500);
  126. $process->run();
  127. $this->assertStringContainsString('Successfully installed a test site', $process->getOutput());
  128. $this->assertSame(0, $process->getExitCode());
  129. $regex = '/Database prefix\s+([^\s]*)/';
  130. $this->assertRegExp($regex, $process->getOutput());
  131. preg_match('/Database prefix\s+([^\s]*)/', $process->getOutput(), $matches);
  132. $other_db_prefix = $matches[1];
  133. $other_key = $this->addTestDatabase($other_db_prefix);
  134. $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
  135. // Ensure the lock file exists for the new install.
  136. $this->assertFileExists($this->getTestLockFile($other_db_prefix));
  137. // Now test the tear down process as well, but keep the lock.
  138. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"';
  139. $process = new Process($command_line, $this->root);
  140. // Set the timeout to a value that allows debugging.
  141. $process->setTimeout(500);
  142. $process->run();
  143. $this->assertSame(0, $process->getExitCode());
  144. $this->assertStringContainsString("Successfully uninstalled $db_prefix test site", $process->getOutput());
  145. // Ensure that all the tables and files for this DB prefix are gone.
  146. $this->assertCount(0, Database::getConnection('default', $key)->schema()->findTables('%'));
  147. $this->assertFileNotExists($test_file);
  148. // Ensure the other site's tables and files still exist.
  149. $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
  150. $test_database = new TestDatabase($other_db_prefix);
  151. $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
  152. $this->assertFileExists($test_file);
  153. // Tear down the other site. Tear down should work if the test site is
  154. // broken. Prove this by removing its settings.php.
  155. $test_site_settings = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . 'settings.php';
  156. $this->assertTrue(unlink($test_site_settings));
  157. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $other_db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  158. $process = new Process($command_line, $this->root);
  159. // Set the timeout to a value that allows debugging.
  160. $process->setTimeout(500);
  161. $process->run();
  162. $this->assertSame(0, $process->getExitCode());
  163. $this->assertStringContainsString("Successfully uninstalled $other_db_prefix test site", $process->getOutput());
  164. // Ensure that all the tables and files for this DB prefix are gone.
  165. $this->assertCount(0, Database::getConnection('default', $other_key)->schema()->findTables('%'));
  166. $this->assertFileNotExists($test_file);
  167. // The lock for the first site should still exist but the second site's lock
  168. // is released during tear down.
  169. $this->assertFileExists($this->getTestLockFile($db_prefix));
  170. $this->assertFileNotExists($this->getTestLockFile($other_db_prefix));
  171. }
  172. /**
  173. * @coversNothing
  174. */
  175. public function testInstallInDifferentLanguage() {
  176. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  177. if (!is_writable($simpletest_path)) {
  178. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  179. }
  180. $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') . '"';
  181. $process = new Process($command_line, $this->root);
  182. $process->setTimeout(500);
  183. $process->run();
  184. $this->assertEquals(0, $process->getExitCode());
  185. $result = json_decode($process->getOutput(), TRUE);
  186. $db_prefix = $result['db_prefix'];
  187. $http_client = new Client();
  188. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
  189. ->withHeader('User-Agent', trim($result['user_agent']));
  190. $response = $http_client->send($request);
  191. // Ensure the test_page_test module got installed.
  192. $this->assertStringContainsString('Test page | Drupal', (string) $response->getBody());
  193. $this->assertStringContainsString('lang="fr"', (string) $response->getBody());
  194. // Now test the tear down process as well.
  195. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  196. $process = new Process($command_line, $this->root);
  197. $process->setTimeout(500);
  198. $process->run();
  199. $this->assertSame(0, $process->getExitCode());
  200. // Ensure that all the tables for this DB prefix are gone.
  201. $this->assertCount(0, Database::getConnection('default', $this->addTestDatabase($db_prefix))->schema()->findTables('%'));
  202. }
  203. /**
  204. * @coversNothing
  205. */
  206. public function testTearDownDbPrefixValidation() {
  207. $command_line = $this->php . ' core/scripts/test-site.php tear-down not-a-valid-prefix';
  208. $process = new Process($command_line, $this->root);
  209. $process->setTimeout(500);
  210. $process->run();
  211. $this->assertSame(1, $process->getExitCode());
  212. $this->assertStringContainsString('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput());
  213. }
  214. /**
  215. * @coversNothing
  216. */
  217. public function testUserLogin() {
  218. $this->markTestIncomplete('Fix this test in https://www.drupal.org/project/drupal/issues/2962157.');
  219. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  220. if (!is_writable($simpletest_path)) {
  221. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  222. }
  223. // Install a site using the JSON output.
  224. $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  225. $process = new Process($command_line, $this->root);
  226. // Set the timeout to a value that allows debugging.
  227. $process->setTimeout(500);
  228. $process->run();
  229. $this->assertSame(0, $process->getExitCode());
  230. $result = json_decode($process->getOutput(), TRUE);
  231. $db_prefix = $result['db_prefix'];
  232. $site_path = $result['site_path'];
  233. $this->assertSame('sites/simpletest/' . str_replace('test', '', $db_prefix), $site_path);
  234. // Test the user login command with valid uid.
  235. $command_line = $this->php . ' core/scripts/test-site.php user-login 1 --site-path ' . $site_path;
  236. $process = new Process($command_line, $this->root);
  237. $process->run();
  238. $this->assertSame(0, $process->getExitCode());
  239. $this->assertStringContainsString('/user/reset/1/', $process->getOutput());
  240. $http_client = new Client();
  241. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . trim($process->getOutput())))
  242. ->withHeader('User-Agent', trim($result['user_agent']));
  243. $response = $http_client->send($request);
  244. // Ensure the response sets a new session.
  245. $this->assertTrue($response->getHeader('Set-Cookie'));
  246. // Test the user login command with invalid uid.
  247. $command_line = $this->php . ' core/scripts/test-site.php user-login invalid-uid --site-path ' . $site_path;
  248. $process = new Process($command_line, $this->root);
  249. $process->run();
  250. $this->assertSame(1, $process->getExitCode());
  251. $this->assertStringContainsString('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process->getErrorOutput());
  252. // Now tear down the test site.
  253. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  254. $process = new Process($command_line, $this->root);
  255. // Set the timeout to a value that allows debugging.
  256. $process->setTimeout(500);
  257. $process->run();
  258. $this->assertSame(0, $process->getExitCode());
  259. $this->assertStringContainsString("Successfully uninstalled $db_prefix test site", $process->getOutput());
  260. }
  261. /**
  262. * Adds the installed test site to the database connection info.
  263. *
  264. * @param string $db_prefix
  265. * The prefix of the installed test site.
  266. *
  267. * @return string
  268. * The database key of the added connection.
  269. */
  270. protected function addTestDatabase($db_prefix) {
  271. $database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB'), $this->root);
  272. $database['prefix'] = ['default' => $db_prefix];
  273. $target = __CLASS__ . $db_prefix;
  274. Database::addConnectionInfo($target, 'default', $database);
  275. return $target;
  276. }
  277. /**
  278. * Gets the lock file path.
  279. *
  280. * @param string $db_prefix
  281. * The prefix of the installed test site.
  282. *
  283. * @return string
  284. * The lock file path.
  285. */
  286. protected function getTestLockFile($db_prefix) {
  287. $lock_id = str_replace('test', '', $db_prefix);
  288. return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;
  289. }
  290. }