6 * Tests for sitealias.inc
10 class saCase extends CommandUnishTestCase {
12 * Covers the following responsibilities.
13 * - Dispatching a Drush command that uses strict option handling
14 * using a global option (e.g. --alias-path) places said global
15 * option BEFORE the command name.
16 * - Dispatching a Drush command that uses strict option handling
17 * using a site alias that contains a command-specific option
18 * places said option AFTER the command name.
20 function testDispatchStrictOptions() {
21 $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
22 file_exists($aliasPath) ?: mkdir($aliasPath);
23 $aliasFile = $aliasPath . '/bar.aliases.drushrc.php';
24 $aliasContents = <<<EOD
26 // Written by Unish. This file is safe to delete.
27 \$aliases['test'] = array(
28 'remote-host' => 'fake.remote-host.com',
29 'remote-user' => 'www-admin',
30 'root' => '/fake/path/to/root',
32 'command-specific' => array(
38 \$aliases['env-test'] = array(
39 'root' => '/fake/path/to/root',
41 'DRUSH_ENV_TEST' => 'WORKING_CASE',
42 'DRUSH_ENV_TEST2' => '{foo:[bar:{key:"val"},bar2:{key:"long val"}]}',
43 'DRUSH_ENV_TEST3' => "WORKING CASE = TRUE",
48 file_put_contents($aliasFile, $aliasContents);
50 'alias-path' => $aliasPath,
51 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
54 $this->drush('core-rsync', array('/a', '/b'), $options, '@test');
55 $output = $this->getOutput();
56 $command_position = strpos($output, 'core-rsync');
57 $global_option_position = strpos($output, '--alias-path=');
58 $command_specific_position = strpos($output, '--delete');
59 $this->assertTrue($command_position !== FALSE);
60 $this->assertTrue($global_option_position !== FALSE);
61 $this->assertTrue($command_specific_position !== FALSE);
62 $this->assertTrue($command_position > $global_option_position);
63 $this->assertTrue($command_position < $command_specific_position);
65 $eval = '$env_test = getenv("DRUSH_ENV_TEST");';
66 $eval .= '$env_test2 = getenv("DRUSH_ENV_TEST2");';
67 $eval .= 'print json_encode(get_defined_vars());';
68 $config = UNISH_SANDBOX . '/drushrc.php';
70 'alias-path' => $aliasPath,
71 'root' => $this->webroot(),
72 'uri' => key($this->getSites()),
73 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
75 $this->drush('unit-eval', array($eval), $options, '@env-test');
76 $output = $this->getOutput();
77 $actuals = json_decode(trim($output));
78 $this->assertEquals('WORKING_CASE', $actuals->env_test);
80 if ($this->is_windows()) {
81 $this->markTestSkipped('@todo. Needs quoting fix, and environment variables not widely used on Windows.');
84 $this->assertEquals('{foo:[bar:{key:"val"},bar2:{key:"long val"}]}', $actuals->env_test2);
85 $eval = 'print getenv("DRUSH_ENV_TEST3");';
86 $this->drush('unit-eval', array($eval), $options, '@env-test');
87 $output = $this->getOutput();
88 $this->assertEquals( "WORKING CASE = TRUE", $output);
93 * Test to see if rsync @site:%files calculates the %files path correctly.
94 * This tests the non-optimized code path in drush_sitealias_resolve_path_references.
96 * @todo This test does not appear to accomplish its goal.
98 function testRsyncBothRemote() {
99 $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
100 file_exists($aliasPath) ?: mkdir($aliasPath);
101 $aliasFile = $aliasPath . '/remote.aliases.drushrc.php';
102 $aliasContents = <<<EOD
104 // Written by Unish. This file is safe to delete.
105 \$aliases['one'] = array(
106 'remote-host' => 'fake.remote-host.com',
107 'remote-user' => 'www-admin',
108 'root' => '/fake/path/to/root',
111 \$aliases['two'] = array(
112 'remote-host' => 'other-fake.remote-host.com',
113 'remote-user' => 'www-admin',
114 'root' => '/other-fake/path/to/root',
118 file_put_contents($aliasFile, $aliasContents);
120 'alias-path' => $aliasPath,
124 $this->drush('core-rsync', array("@remote.one:files", "@remote.two:tmp"), $options, NULL, NULL, self::EXIT_SUCCESS, '2>&1;');
125 $output = $this->getOutput();
126 $level = $this->log_level();
127 $pattern = in_array($level, array('verbose', 'debug')) ? "Calling system(rsync -e 'ssh ' -akzv --stats --progress --yes %s /tmp);" : "Calling system(rsync -e 'ssh ' -akz --yes %s /tmp);";
128 $expected = sprintf($pattern, UNISH_SANDBOX . "/web/sites/default/files");
132 // Simulating backend invoke: /path/to/php -d sendmail_path='true' /path/to/drush.php --php=/path/to/php --php-options=' -d sendmail_path='\''true'\''' --backend=2 --alias-path=/path/to/site-alias-directory --nocolor --root=/fake/path/to/root --uri=default core-rsync '@remote.one:files' /path/to/tmpdir 2>&1
133 // Simulating backend invoke: /path/to/php -d sendmail_path='true' /path/to/drush.php --php=/path/to/php --php-options=' -d sendmail_path='\''true'\''' --backend=2 --alias-path=/path/to/site-alias-directory --nocolor --root=/fake/path/to/root --uri=default core-rsync /path/to/tmpdir/files '@remote.two:tmp' 2>&1'
134 // Since there are a lot of variable items in the output (e.g. path
135 // to a temporary folder), so we will use 'assertContains' to
136 // assert on portions of the output that does not vary.
137 $this->assertContains('Simulating backend invoke', $output);
138 $this->assertContains("core-rsync '@remote.one:files' /", $output);
139 $this->assertContains("/files '@remote.two:tmp'", $output);
143 * Assure that site lists work as expected.
144 * @todo Use --backend for structured return data. Depends on http://drupal.org/node/1043922
146 public function testSAList() {
147 $sites = $this->setUpDrupal(2);
148 $subdirs = array_keys($sites);
149 $eval = 'print "bon";';
153 'root' => $this->webroot(),
155 foreach ($subdirs as $dir) {
158 $this->drush('php-eval', array($eval), $options, implode(',', $dirs));
159 $output = $this->getOutputAsList();
160 $expected = "#stage >> bon
162 $actual = implode("\n", $output);
163 $actual = trim(preg_replace('/^#[a-z]* *>> *$/m', '', $actual)); // ignore blank lines
164 $this->assertEquals($expected, $actual);
168 * Ensure that requesting a non-existent alias throws an error.
170 public function testBadAlias() {
171 $this->drush('sa', array('@badalias'), array(), NULL, NULL, self::EXIT_ERROR);
175 * Ensure that a --uri on CLI overrides on provided by site alias during a backend invoke.
177 public function testBackendHonorsAliasOverride() {
178 if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
179 $this->markTestSkipped("Sites.php not available in Drupal 6 core.");
182 // Test a standard remote dispatch.
183 $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL), 'user@server/path/to/drupal#sitename');
184 $this->assertContains('--uri=http://example.com', $this->getOutput());
186 // Test a local-handling command which uses drush_redispatch_get_options().
187 $this->drush('browse', array(), array('uri' => 'http://example.com', 'simulate' => NULL), 'user@server/path/to/drupal#sitename');
188 $this->assertContains('--uri=http://example.com', $this->getOutput());
190 // Test a command which uses drush_invoke_process('@self') internally.
191 $sites = $this->setUpDrupal(1, TRUE);
193 $sites_php = "\n\$sites['example.com'] = '$name';";
194 file_put_contents($sites[$name]['root'] . '/sites/sites.php', $sites_php, FILE_APPEND);
195 $this->drush('pm-updatecode', array(), array('uri' => 'http://example.com', 'no' => NULL, 'no-core' => NULL, 'verbose' => NULL), '@' . $name);
196 $this->assertContains('--uri=http://example.com', $this->getErrorOutput());
198 // Test a remote alias that does not have a 'root' element
199 $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
201 $aliasContents = <<<EOD
203 // Written by Unish. This file is safe to delete.
204 \$aliases['rootlessremote'] = array(
205 'uri' => 'remoteuri',
206 'remote-host' => 'exampleisp.com',
207 'remote-user' => 'www-admin',
210 file_put_contents("$aliasPath/rootlessremote.aliases.drushrc.php", $aliasContents);
211 $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL, 'alias-path' => $aliasPath), '@rootlessremote');
212 $output = $this->getOutput();
213 $this->assertContains(' ssh ', $output);
214 $this->assertContains('--uri=http://example.com', $output);
216 // Test a remote alias that does not have a 'root' element with cwd inside a Drupal root directory
217 $root = $this->webroot();
218 $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL, 'alias-path' => $aliasPath), '@rootlessremote', $root);
219 $output = $this->getOutput();
220 $this->assertContains(' ssh ', $output);
221 $this->assertContains('--uri=http://example.com', $output);
225 * Test to see if we can access aliases defined inside of
226 * a provided Drupal root in various locations where they
229 public function testAliasFilesInDocroot() {
230 $root = $this->webroot();
232 $aliasContents = <<<EOD
234 // Written by Unish. This file is safe to delete.
235 \$aliases['atroot'] = array(
236 'root' => '/fake/path/to/othersite',
240 @mkdir($root . "/drush");
241 @mkdir($root . "/drush/site-aliases");
242 file_put_contents($root . "/drush/site-aliases/atroot.aliases.drushrc.php", $aliasContents);
244 $aliasContents = <<<EOD
246 // Written by Unish. This file is safe to delete.
247 \$aliases['insitefolder'] = array(
248 'root' => '/fake/path/to/othersite',
252 @mkdir($root . "/sites/all/drush");
253 @mkdir($root . "/sites/all/drush/site-aliases");
254 file_put_contents($root . "/sites/all/drush/site-aliases/sitefolder.aliases.drushrc.php", $aliasContents);
256 $aliasContents = <<<EOD
258 // Written by Unish. This file is safe to delete.
259 \$aliases['aboveroot'] = array(
260 'root' => '/fake/path/to/othersite',
264 @mkdir($root . "/../drush");
265 @mkdir($root . "/../drush/site-aliases");
266 file_put_contents($root . "/../drush/site-aliases/aboveroot.aliases.drushrc.php", $aliasContents);
268 // Ensure that none of these 'sa' commands return an error
269 $this->drush('sa', array('@atroot'), array(), '@dev');
270 $this->drush('sa', array('@insitefolder'), array(), '@dev');
271 $this->drush('sa', array('@aboveroot'), array(), '@dev');
276 * Ensure that Drush searches deep inside specified search locations
279 public function testDeepAliasSearching() {
280 $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
281 file_exists($aliasPath) ?: mkdir($aliasPath);
282 $deepPath = $aliasPath . '/deep';
283 file_exists($deepPath) ?: mkdir($deepPath);
284 $aliasFile = $deepPath . '/baz.aliases.drushrc.php';
285 $aliasContents = <<<EOD
287 // Written by Unish. This file is safe to delete.
288 \$aliases['deep'] = array(
289 'remote-host' => 'fake.remote-host.com',
290 'remote-user' => 'www-admin',
291 'root' => '/fake/path/to/root',
293 'command-specific' => array(
300 file_put_contents($aliasFile, $aliasContents);
302 'alias-path' => $aliasPath,
306 $this->drush('sa', array('@deep'), $options);
308 // Verify that the files directory is not recursed into.
309 $filesPath = $aliasPath . '/files';
310 file_exists($filesPath) ?: mkdir($filesPath);
311 $aliasFile = $filesPath . '/biz.aliases.drushrc.php';
312 $aliasContents = <<<EOD
314 // Written by unish. This file is safe to delete.
315 \$aliases['nope'] = array(
316 'remote-host' => 'fake.remote-host.com',
317 'remote-user' => 'www-admin',
318 'root' => '/fake/path/to/root',
320 'command-specific' => array(
327 file_put_contents($aliasFile, $aliasContents);
329 'alias-path' => $aliasPath,
333 // This should not find the '@nope' alias.
334 $this->drush('sa', array('@nope'), $options, NULL, NULL, self::EXIT_ERROR);