Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / tests / siteAliasTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * Tests for sitealias.inc
7  *
8  * @group base
9  */
10 class saCase extends CommandUnishTestCase {
11   /**
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.
19    */
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
25   <?php
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',
31     'uri' => 'default',
32     'command-specific' => array(
33       'rsync' => array(
34         'delete' => TRUE,
35       ),
36     ),
37   );
38   \$aliases['env-test'] = array(
39     'root' => '/fake/path/to/root',
40     '#env-vars' => array(
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",
44     ),
45     'uri' => 'default',
46   );
47 EOD;
48     file_put_contents($aliasFile, $aliasContents);
49     $options = array(
50       'alias-path' => $aliasPath,
51       'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
52       'simulate' => TRUE,
53     );
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);
64
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';
69     $options = array(
70       'alias-path' => $aliasPath,
71       'root' => $this->webroot(),
72       'uri' => key($this->getSites()),
73       'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
74     );
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);
79
80     if ($this->is_windows()) {
81       $this->markTestSkipped('@todo. Needs quoting fix, and environment variables not widely used on Windows.');
82     }
83
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);
89   }
90
91
92   /**
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.
95    *
96    * @todo This test does not appear to accomplish its goal.
97    */
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
103   <?php
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',
109     'uri' => 'default',
110   );
111   \$aliases['two'] = array(
112     'remote-host' => 'other-fake.remote-host.com',
113     'remote-user' => 'www-admin',
114     'root' => '/other-fake/path/to/root',
115     'uri' => 'default',
116   );
117 EOD;
118     file_put_contents($aliasFile, $aliasContents);
119     $options = array(
120       'alias-path' => $aliasPath,
121       'simulate' => TRUE,
122       'yes' => NULL,
123     );
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");
129
130
131     // Expected ouput:
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);
140   }
141
142   /**
143    * Assure that site lists work as expected.
144    * @todo Use --backend for structured return data. Depends on http://drupal.org/node/1043922
145    */
146   public function testSAList() {
147     $sites = $this->setUpDrupal(2);
148     $subdirs = array_keys($sites);
149     $eval = 'print "bon";';
150     $options = array(
151       'yes' => NULL,
152       'verbose' => NULL,
153       'root' => $this->webroot(),
154     );
155     foreach ($subdirs as $dir) {
156       $dirs[] = "#$dir";
157     }
158     $this->drush('php-eval', array($eval), $options, implode(',', $dirs));
159     $output = $this->getOutputAsList();
160     $expected = "#stage >> bon
161 #dev   >> bon";
162     $actual = implode("\n", $output);
163     $actual = trim(preg_replace('/^#[a-z]* *>> *$/m', '', $actual)); // ignore blank lines
164     $this->assertEquals($expected, $actual);
165   }
166
167   /**
168    * Ensure that requesting a non-existent alias throws an error.
169    */
170   public function testBadAlias() {
171     $this->drush('sa', array('@badalias'), array(), NULL, NULL, self::EXIT_ERROR);
172   }
173
174   /**
175    * Ensure that a --uri on CLI overrides on provided by site alias during a backend invoke.
176    */
177   public function testBackendHonorsAliasOverride() {
178     if (UNISH_DRUPAL_MAJOR_VERSION == 6) {
179       $this->markTestSkipped("Sites.php not available in Drupal 6 core.");
180     }
181
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());
185
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());
189
190     // Test a command which uses drush_invoke_process('@self') internally.
191     $sites = $this->setUpDrupal(1, TRUE);
192     $name = key($sites);
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());
197
198     // Test a remote alias that does not have a 'root' element
199     $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
200     @mkdir($aliasPath);
201     $aliasContents = <<<EOD
202   <?php
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',
208   );
209 EOD;
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);
215
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);
222   }
223
224   /**
225    * Test to see if we can access aliases defined inside of
226    * a provided Drupal root in various locations where they
227    * may be stored.
228    */
229   public function testAliasFilesInDocroot() {
230     $root = $this->webroot();
231
232     $aliasContents = <<<EOD
233   <?php
234   // Written by Unish. This file is safe to delete.
235   \$aliases['atroot'] = array(
236     'root' => '/fake/path/to/othersite',
237     'uri' => 'default',
238   );
239 EOD;
240     @mkdir($root . "/drush");
241     @mkdir($root . "/drush/site-aliases");
242     file_put_contents($root . "/drush/site-aliases/atroot.aliases.drushrc.php", $aliasContents);
243
244     $aliasContents = <<<EOD
245   <?php
246   // Written by Unish. This file is safe to delete.
247   \$aliases['insitefolder'] = array(
248     'root' => '/fake/path/to/othersite',
249     'uri' => 'default',
250   );
251 EOD;
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);
255
256     $aliasContents = <<<EOD
257   <?php
258   // Written by Unish. This file is safe to delete.
259   \$aliases['aboveroot'] = array(
260     'root' => '/fake/path/to/othersite',
261     'uri' => 'default',
262   );
263 EOD;
264     @mkdir($root . "/../drush");
265     @mkdir($root . "/../drush/site-aliases");
266     file_put_contents($root . "/../drush/site-aliases/aboveroot.aliases.drushrc.php", $aliasContents);
267
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');
272   }
273
274
275   /**
276    * Ensure that Drush searches deep inside specified search locations
277    * for alias files.
278    */
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
286   <?php
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',
292     'uri' => 'default',
293     'command-specific' => array(
294       'rsync' => array(
295         'delete' => TRUE,
296       ),
297     ),
298   );
299 EOD;
300     file_put_contents($aliasFile, $aliasContents);
301     $options = array(
302       'alias-path' => $aliasPath,
303       'simulate' => TRUE,
304     );
305
306     $this->drush('sa', array('@deep'), $options);
307
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
313     <?php
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',
319     'uri' => 'default',
320     'command-specific' => array(
321       'rsync' => array(
322         'delete' => TRUE,
323       ),
324     ),
325   );
326 EOD;
327     file_put_contents($aliasFile, $aliasContents);
328     $options = array(
329       'alias-path' => $aliasPath,
330       'simulate' => TRUE,
331     );
332
333     // This should not find the '@nope' alias.
334     $this->drush('sa', array('@nope'), $options, NULL, NULL, self::EXIT_ERROR);
335   }
336 }