TestSiteApplicationTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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->assertContains('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->assertContains('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. // Create a connection to the DB configured in SIMPLETEST_DB.
  75. $connection = Database::getConnection('default', $this->addTestDatabase(''));
  76. $table_count = count($connection->schema()->findTables('%'));
  77. // Use __FILE__ to test absolute paths.
  78. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file "' . __FILE__ . '" --db-url "' . getenv('SIMPLETEST_DB') . '"';
  79. $process = new Process($command_line, $this->root, ['COLUMNS' => PHP_INT_MAX]);
  80. $process->run();
  81. $this->assertContains('The class Drupal\Tests\Scripts\TestSiteApplicationTest contained in', $process->getErrorOutput());
  82. $this->assertContains('needs to implement \Drupal\TestSite\TestSetupInterface', $process->getErrorOutput());
  83. $this->assertSame(1, $process->getExitCode());
  84. $this->assertCount($table_count, $connection->schema()->findTables('%'), 'No additional tables created in the database');
  85. }
  86. /**
  87. * @coversNothing
  88. */
  89. public function testInstallScript() {
  90. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  91. if (!is_writable($simpletest_path)) {
  92. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  93. }
  94. // Install a site using the JSON output.
  95. $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  96. $process = new Process($command_line, $this->root);
  97. // Set the timeout to a value that allows debugging.
  98. $process->setTimeout(500);
  99. $process->run();
  100. $this->assertSame(0, $process->getExitCode());
  101. $result = json_decode($process->getOutput(), TRUE);
  102. $db_prefix = $result['db_prefix'];
  103. $this->assertStringStartsWith('simpletest' . substr($db_prefix, 4) . ':', $result['user_agent']);
  104. $http_client = new Client();
  105. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
  106. ->withHeader('User-Agent', trim($result['user_agent']));
  107. $response = $http_client->send($request);
  108. // Ensure the test_page_test module got installed.
  109. $this->assertContains('Test page | Drupal', (string) $response->getBody());
  110. // Ensure that there are files and database tables for the tear down command
  111. // to clean up.
  112. $key = $this->addTestDatabase($db_prefix);
  113. $this->assertGreaterThan(0, count(Database::getConnection('default', $key)->schema()->findTables('%')));
  114. $test_database = new TestDatabase($db_prefix);
  115. $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
  116. $this->assertFileExists($test_file);
  117. // Ensure the lock file exists.
  118. $this->assertFileExists($this->getTestLockFile($db_prefix));
  119. // Install another site so we can ensure the tear down command only removes
  120. // one site at a time. Use the regular output.
  121. $command_line = $this->php . ' core/scripts/test-site.php install --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  122. $process = new Process($command_line, $this->root);
  123. // Set the timeout to a value that allows debugging.
  124. $process->setTimeout(500);
  125. $process->run();
  126. $this->assertContains('Successfully installed a test site', $process->getOutput());
  127. $this->assertSame(0, $process->getExitCode());
  128. $regex = '/Database prefix\s+([^\s]*)/';
  129. $this->assertRegExp($regex, $process->getOutput());
  130. preg_match('/Database prefix\s+([^\s]*)/', $process->getOutput(), $matches);
  131. $other_db_prefix = $matches[1];
  132. $other_key = $this->addTestDatabase($other_db_prefix);
  133. $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
  134. // Ensure the lock file exists for the new install.
  135. $this->assertFileExists($this->getTestLockFile($other_db_prefix));
  136. // Now test the tear down process as well, but keep the lock.
  137. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --keep-lock --db-url "' . getenv('SIMPLETEST_DB') . '"';
  138. $process = new Process($command_line, $this->root);
  139. // Set the timeout to a value that allows debugging.
  140. $process->setTimeout(500);
  141. $process->run();
  142. $this->assertSame(0, $process->getExitCode());
  143. $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
  144. // Ensure that all the tables and files for this DB prefix are gone.
  145. $this->assertCount(0, Database::getConnection('default', $key)->schema()->findTables('%'));
  146. $this->assertFileNotExists($test_file);
  147. // Ensure the other site's tables and files still exist.
  148. $this->assertGreaterThan(0, count(Database::getConnection('default', $other_key)->schema()->findTables('%')));
  149. $test_database = new TestDatabase($other_db_prefix);
  150. $test_file = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . '.htkey';
  151. $this->assertFileExists($test_file);
  152. // Tear down the other site. Tear down should work if the test site is
  153. // broken. Prove this by removing its settings.php.
  154. $test_site_settings = $this->root . DIRECTORY_SEPARATOR . $test_database->getTestSitePath() . DIRECTORY_SEPARATOR . 'settings.php';
  155. $this->assertTrue(unlink($test_site_settings));
  156. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $other_db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  157. $process = new Process($command_line, $this->root);
  158. // Set the timeout to a value that allows debugging.
  159. $process->setTimeout(500);
  160. $process->run();
  161. $this->assertSame(0, $process->getExitCode());
  162. $this->assertContains("Successfully uninstalled $other_db_prefix test site", $process->getOutput());
  163. // Ensure that all the tables and files for this DB prefix are gone.
  164. $this->assertCount(0, Database::getConnection('default', $other_key)->schema()->findTables('%'));
  165. $this->assertFileNotExists($test_file);
  166. // The lock for the first site should still exist but the second site's lock
  167. // is released during tear down.
  168. $this->assertFileExists($this->getTestLockFile($db_prefix));
  169. $this->assertFileNotExists($this->getTestLockFile($other_db_prefix));
  170. }
  171. /**
  172. * @coversNothing
  173. */
  174. public function testInstallInDifferentLanguage() {
  175. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  176. if (!is_writable($simpletest_path)) {
  177. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  178. }
  179. $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') . '"';
  180. $process = new Process($command_line, $this->root);
  181. $process->setTimeout(500);
  182. $process->run();
  183. $this->assertEquals(0, $process->getExitCode());
  184. $result = json_decode($process->getOutput(), TRUE);
  185. $db_prefix = $result['db_prefix'];
  186. $http_client = new Client();
  187. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
  188. ->withHeader('User-Agent', trim($result['user_agent']));
  189. $response = $http_client->send($request);
  190. // Ensure the test_page_test module got installed.
  191. $this->assertContains('Test page | Drupal', (string) $response->getBody());
  192. $this->assertContains('lang="fr"', (string) $response->getBody());
  193. // Now test the tear down process as well.
  194. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  195. $process = new Process($command_line, $this->root);
  196. $process->setTimeout(500);
  197. $process->run();
  198. $this->assertSame(0, $process->getExitCode());
  199. // Ensure that all the tables for this DB prefix are gone.
  200. $this->assertCount(0, Database::getConnection('default', $this->addTestDatabase($db_prefix))->schema()->findTables('%'));
  201. }
  202. /**
  203. * @coversNothing
  204. */
  205. public function testTearDownDbPrefixValidation() {
  206. $command_line = $this->php . ' core/scripts/test-site.php tear-down not-a-valid-prefix';
  207. $process = new Process($command_line, $this->root);
  208. $process->setTimeout(500);
  209. $process->run();
  210. $this->assertSame(1, $process->getExitCode());
  211. $this->assertContains('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput());
  212. }
  213. /**
  214. * @coversNothing
  215. */
  216. public function testUserLogin() {
  217. $simpletest_path = $this->root . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . 'simpletest';
  218. if (!is_writable($simpletest_path)) {
  219. $this->markTestSkipped("Requires the directory $simpletest_path to exist and be writable");
  220. }
  221. // Install a site using the JSON output.
  222. $command_line = $this->php . ' core/scripts/test-site.php install --json --setup-file core/tests/Drupal/TestSite/TestSiteInstallTestScript.php --db-url "' . getenv('SIMPLETEST_DB') . '"';
  223. $process = new Process($command_line, $this->root);
  224. // Set the timeout to a value that allows debugging.
  225. $process->setTimeout(500);
  226. $process->run();
  227. $this->assertSame(0, $process->getExitCode());
  228. $result = json_decode($process->getOutput(), TRUE);
  229. $db_prefix = $result['db_prefix'];
  230. $site_path = $result['site_path'];
  231. $this->assertSame('sites/simpletest/' . str_replace('test', '', $db_prefix), $site_path);
  232. // Test the user login command with valid uid.
  233. $command_line = $this->php . ' core/scripts/test-site.php user-login 1 --site-path ' . $site_path;
  234. $process = new Process($command_line, $this->root);
  235. $process->run();
  236. $this->assertSame(0, $process->getExitCode());
  237. $this->assertContains('/user/reset/1/', $process->getOutput());
  238. $http_client = new Client();
  239. $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . trim($process->getOutput())))
  240. ->withHeader('User-Agent', trim($result['user_agent']));
  241. $response = $http_client->send($request);
  242. // Ensure the response sets a new session.
  243. $this->assertTrue($response->getHeader('Set-Cookie'));
  244. // Test the user login command with invalid uid.
  245. $command_line = $this->php . ' core/scripts/test-site.php user-login invalid-uid --site-path ' . $site_path;
  246. $process = new Process($command_line, $this->root);
  247. $process->run();
  248. $this->assertSame(1, $process->getExitCode());
  249. $this->assertContains('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process->getErrorOutput());
  250. // Now tear down the test site.
  251. $command_line = $this->php . ' core/scripts/test-site.php tear-down ' . $db_prefix . ' --db-url "' . getenv('SIMPLETEST_DB') . '"';
  252. $process = new Process($command_line, $this->root);
  253. // Set the timeout to a value that allows debugging.
  254. $process->setTimeout(500);
  255. $process->run();
  256. $this->assertSame(0, $process->getExitCode());
  257. $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
  258. }
  259. /**
  260. * Adds the installed test site to the database connection info.
  261. *
  262. * @param string $db_prefix
  263. * The prefix of the installed test site.
  264. *
  265. * @return string
  266. * The database key of the added connection.
  267. */
  268. protected function addTestDatabase($db_prefix) {
  269. $database = Database::convertDbUrlToConnectionInfo(getenv('SIMPLETEST_DB'), $this->root);
  270. $database['prefix'] = ['default' => $db_prefix];
  271. $target = __CLASS__ . $db_prefix;
  272. Database::addConnectionInfo($target, 'default', $database);
  273. return $target;
  274. }
  275. /**
  276. * Gets the lock file path.
  277. *
  278. * @param string $db_prefix
  279. * The prefix of the installed test site.
  280. *
  281. * @return string
  282. * The lock file path.
  283. */
  284. protected function getTestLockFile($db_prefix) {
  285. $lock_id = str_replace('test', '', $db_prefix);
  286. return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;
  287. }
  288. }