Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Scripts / TestSiteApplicationTest.php
1 <?php
2
3 namespace Drupal\Tests\Scripts;
4
5 use Drupal\Component\FileSystem\FileSystem;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Test\TestDatabase;
8 use Drupal\Tests\UnitTestCase;
9 use GuzzleHttp\Client;
10 use GuzzleHttp\Psr7\Request;
11 use Symfony\Component\Process\PhpExecutableFinder;
12 use Symfony\Component\Process\Process;
13
14 /**
15  * Tests core/scripts/test-site.php.
16  *
17  * @group Setup
18  *
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.
21  *
22  * @runTestsInSeparateProcesses
23  * @preserveGlobalState disabled
24  *
25  * @see \Drupal\TestSite\TestSiteApplication
26  * @see \Drupal\TestSite\Commands\TestSiteInstallCommand
27  * @see \Drupal\TestSite\Commands\TestSiteTearDownCommand
28  */
29 class TestSiteApplicationTest extends UnitTestCase {
30
31   /**
32    * The PHP executable path.
33    *
34    * @var string
35    */
36   protected $php;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43     $php_executable_finder = new PhpExecutableFinder();
44     $this->php = $php_executable_finder->find();
45     $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
46   }
47
48   /**
49    * @coversNothing
50    */
51   public function testInstallWithNonExistingFile() {
52
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('%'));
56
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);
59     $process->run();
60
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');
64   }
65
66   /**
67    * @coversNothing
68    */
69   public function testInstallWithFileWithNoClass() {
70
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('%'));
74
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);
77     $process->run();
78
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');
82   }
83
84   /**
85    * @coversNothing
86    */
87   public function testInstallWithNonSetupClass() {
88
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('%'));
92
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]);
96     $process->run();
97
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');
102   }
103
104   /**
105    * @coversNothing
106    */
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");
111     }
112
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);
118     $process->run();
119
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']);
124
125     $http_client = new Client();
126     $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page'))
127       ->withHeader('User-Agent', trim($result['user_agent']));
128
129     $response = $http_client->send($request);
130     // Ensure the test_page_test module got installed.
131     $this->assertContains('Test page | Drupal', (string) $response->getBody());
132
133     // Ensure that there are files and database tables for the tear down command
134     // to clean up.
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);
140
141     // Ensure the lock file exists.
142     $this->assertFileExists($this->getTestLockFile($db_prefix));
143
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);
150     $process->run();
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('%')));
159
160     // Ensure the lock file exists for the new install.
161     $this->assertFileExists($this->getTestLockFile($other_db_prefix));
162
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);
168     $process->run();
169     $this->assertSame(0, $process->getExitCode());
170     $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
171
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);
175
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);
181
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);
190     $process->run();
191     $this->assertSame(0, $process->getExitCode());
192     $this->assertContains("Successfully uninstalled $other_db_prefix test site", $process->getOutput());
193
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);
197
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));
202   }
203
204   /**
205    * @coversNothing
206    */
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");
211     }
212
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);
216     $process->run();
217     $this->assertEquals(0, $process->getExitCode());
218
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']));
224
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());
229
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);
234     $process->run();
235     $this->assertSame(0, $process->getExitCode());
236
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('%'));
239   }
240
241   /**
242    * @coversNothing
243    */
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);
248     $process->run();
249     $this->assertSame(1, $process->getExitCode());
250     $this->assertContains('Invalid database prefix: not-a-valid-prefix', $process->getErrorOutput());
251   }
252
253   /**
254    * @coversNothing
255    */
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");
260     }
261
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);
267     $process->run();
268
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);
274
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);
278     $process->run();
279     $this->assertSame(0, $process->getExitCode());
280     $this->assertContains('/user/reset/1/', $process->getOutput());
281
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']));
285
286     $response = $http_client->send($request);
287
288     // Ensure the response sets a new session.
289     $this->assertTrue($response->getHeader('Set-Cookie'));
290
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);
294     $process->run();
295     $this->assertSame(1, $process->getExitCode());
296     $this->assertContains('The "uid" argument needs to be an integer, but it is "invalid-uid".', $process->getErrorOutput());
297
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);
303     $process->run();
304     $this->assertSame(0, $process->getExitCode());
305     $this->assertContains("Successfully uninstalled $db_prefix test site", $process->getOutput());
306   }
307
308   /**
309    * Adds the installed test site to the database connection info.
310    *
311    * @param string $db_prefix
312    *   The prefix of the installed test site.
313    *
314    * @return string
315    *   The database key of the added connection.
316    */
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);
322     return $target;
323   }
324
325   /**
326    * Gets the lock file path.
327    *
328    * @param string $db_prefix
329    *   The prefix of the installed test site.
330    *
331    * @return string
332    *   The lock file path.
333    */
334   protected function getTestLockFile($db_prefix) {
335     $lock_id = str_replace('test', '', $db_prefix);
336     return FileSystem::getOsTemporaryDirectory() . '/test_' . $lock_id;
337   }
338
339 }