siteAliasTest.php

  1. 8.0.x tests/siteAliasTest.php
  2. 6.x tests/siteAliasTest.php
  3. 7.x tests/siteAliasTest.php
  4. 4.x tests/siteAliasTest.php
  5. 5.x tests/siteAliasTest.php
  6. master tests/siteAliasTest.php

Namespace

Unish

Classes

Namesort descending Description
saCase Tests for sitealias.inc

File

tests/siteAliasTest.php
View source
  1. <?php
  2. namespace Unish;
  3. /**
  4. * Tests for sitealias.inc
  5. *
  6. * @group base
  7. */
  8. class saCase extends CommandUnishTestCase {
  9. /**
  10. * Covers the following responsibilities.
  11. * - Dispatching a Drush command that uses strict option handling
  12. * using a global option (e.g. --alias-path) places said global
  13. * option BEFORE the command name.
  14. * - Dispatching a Drush command that uses strict option handling
  15. * using a site alias that contains a command-specific option
  16. * places said option AFTER the command name.
  17. */
  18. function testDispatchStrictOptions() {
  19. $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
  20. file_exists($aliasPath) ?: mkdir($aliasPath);
  21. $aliasFile = $aliasPath . '/bar.aliases.drushrc.php';
  22. $aliasContents = <<<EOD
  23. <?php
  24. // Written by Unish. This file is safe to delete.
  25. \$aliases['test'] = array(
  26. 'remote-host' => 'fake.remote-host.com',
  27. 'remote-user' => 'www-admin',
  28. 'root' => '/fake/path/to/root',
  29. 'uri' => 'default',
  30. 'command-specific' => array(
  31. 'rsync' => array(
  32. 'delete' => TRUE,
  33. ),
  34. ),
  35. );
  36. \$aliases['env-test'] = array(
  37. 'root' => '/fake/path/to/root',
  38. '#env-vars' => array(
  39. 'DRUSH_ENV_TEST' => 'WORKING_CASE',
  40. 'DRUSH_ENV_TEST2' => '{foo:[bar:{key:"val"},bar2:{key:"long val"}]}',
  41. 'DRUSH_ENV_TEST3' => "WORKING CASE = TRUE",
  42. ),
  43. 'uri' => 'default',
  44. );
  45. EOD;
  46. file_put_contents($aliasFile, $aliasContents);
  47. $options = array(
  48. 'alias-path' => $aliasPath,
  49. 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
  50. 'simulate' => TRUE,
  51. );
  52. $this->drush('core-rsync', array('/a', '/b'), $options, '@test');
  53. $output = $this->getOutput();
  54. $command_position = strpos($output, 'core-rsync');
  55. $global_option_position = strpos($output, '--alias-path=');
  56. $command_specific_position = strpos($output, '--delete');
  57. $this->assertTrue($command_position !== FALSE);
  58. $this->assertTrue($global_option_position !== FALSE);
  59. $this->assertTrue($command_specific_position !== FALSE);
  60. $this->assertTrue($command_position > $global_option_position);
  61. $this->assertTrue($command_position < $command_specific_position);
  62. $eval = '$env_test = getenv("DRUSH_ENV_TEST");';
  63. $eval .= '$env_test2 = getenv("DRUSH_ENV_TEST2");';
  64. $eval .= 'print json_encode(get_defined_vars());';
  65. $config = UNISH_SANDBOX . '/drushrc.php';
  66. $options = array(
  67. 'alias-path' => $aliasPath,
  68. 'root' => $this->webroot(),
  69. 'uri' => key($this->getSites()),
  70. 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
  71. );
  72. $this->drush('unit-eval', array($eval), $options, '@env-test');
  73. $output = $this->getOutput();
  74. $actuals = json_decode(trim($output));
  75. $this->assertEquals('WORKING_CASE', $actuals->env_test);
  76. if ($this->is_windows()) {
  77. $this->markTestSkipped('@todo. Needs quoting fix, and environment variables not widely used on Windows.');
  78. }
  79. $this->assertEquals('{foo:[bar:{key:"val"},bar2:{key:"long val"}]}', $actuals->env_test2);
  80. $eval = 'print getenv("DRUSH_ENV_TEST3");';
  81. $this->drush('unit-eval', array($eval), $options, '@env-test');
  82. $output = $this->getOutput();
  83. $this->assertEquals( "WORKING CASE = TRUE", $output);
  84. }
  85. /**
  86. * Test to see if rsync @site:%files calculates the %files path correctly.
  87. * This tests the non-optimized code path in drush_sitealias_resolve_path_references.
  88. *
  89. * @todo This test does not appear to accomplish its goal.
  90. */
  91. function testRsyncBothRemote() {
  92. $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
  93. file_exists($aliasPath) ?: mkdir($aliasPath);
  94. $aliasFile = $aliasPath . '/remote.aliases.drushrc.php';
  95. $aliasContents = <<<EOD
  96. <?php
  97. // Written by Unish. This file is safe to delete.
  98. \$aliases['one'] = array(
  99. 'remote-host' => 'fake.remote-host.com',
  100. 'remote-user' => 'www-admin',
  101. 'root' => '/fake/path/to/root',
  102. 'uri' => 'default',
  103. );
  104. \$aliases['two'] = array(
  105. 'remote-host' => 'other-fake.remote-host.com',
  106. 'remote-user' => 'www-admin',
  107. 'root' => '/other-fake/path/to/root',
  108. 'uri' => 'default',
  109. );
  110. EOD;
  111. file_put_contents($aliasFile, $aliasContents);
  112. $options = array(
  113. 'alias-path' => $aliasPath,
  114. 'simulate' => TRUE,
  115. 'yes' => NULL,
  116. );
  117. $this->drush('core-rsync', array("@remote.one:files", "@remote.two:tmp"), $options, NULL, NULL, self::EXIT_SUCCESS, '2>&1;');
  118. $output = $this->getOutput();
  119. $level = $this->log_level();
  120. $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);";
  121. $expected = sprintf($pattern, UNISH_SANDBOX . "/web/sites/default/files");
  122. // Expected ouput:
  123. // 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
  124. // 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'
  125. // Since there are a lot of variable items in the output (e.g. path
  126. // to a temporary folder), so we will use 'assertContains' to
  127. // assert on portions of the output that does not vary.
  128. $this->assertContains('Simulating backend invoke', $output);
  129. $this->assertContains("core-rsync '@remote.one:files' /", $output);
  130. $this->assertContains("/files '@remote.two:tmp'", $output);
  131. }
  132. /**
  133. * Assure that site lists work as expected.
  134. * @todo Use --backend for structured return data. Depends on http://drupal.org/node/1043922
  135. */
  136. public function testSAList() {
  137. $sites = $this->setUpDrupal(2);
  138. $subdirs = array_keys($sites);
  139. $eval = 'print "bon";';
  140. $options = array(
  141. 'yes' => NULL,
  142. 'verbose' => NULL,
  143. 'root' => $this->webroot(),
  144. );
  145. foreach ($subdirs as $dir) {
  146. $dirs[] = "#$dir";
  147. }
  148. $this->drush('php-eval', array($eval), $options, implode(',', $dirs));
  149. $output = $this->getOutputAsList();
  150. $expected = "#stage >> bon
  151. #dev >> bon";
  152. $actual = implode("\n", $output);
  153. $actual = trim(preg_replace('/^#[a-z]* *>> *$/m', '', $actual)); // ignore blank lines
  154. $this->assertEquals($expected, $actual);
  155. }
  156. /**
  157. * Ensure that requesting a non-existent alias throws an error.
  158. */
  159. public function testBadAlias() {
  160. $this->drush('sa', array('@badalias'), array(), NULL, NULL, self::EXIT_ERROR);
  161. }
  162. /**
  163. * Ensure that a --uri on CLI overrides on provided by site alias during a backend invoke.
  164. */
  165. public function testBackendHonorsAliasOverride() {
  166. // Test a standard remote dispatch.
  167. $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL), 'user@server/path/to/drupal#sitename');
  168. $this->assertContains('--uri=http://example.com', $this->getOutput());
  169. // Test a local-handling command which uses drush_redispatch_get_options().
  170. $this->drush('browse', array(), array('uri' => 'http://example.com', 'simulate' => NULL), 'user@server/path/to/drupal#sitename');
  171. $this->assertContains('--uri=http://example.com', $this->getOutput());
  172. // Test a command which uses drush_invoke_process('@self') internally.
  173. $sites = $this->setUpDrupal(1, TRUE);
  174. $name = key($sites);
  175. $sites_php = "\n\$sites['example.com'] = '$name';";
  176. file_put_contents($sites[$name]['root'] . '/sites/sites.php', $sites_php, FILE_APPEND);
  177. $this->drush('pm-updatecode', array(), array('uri' => 'http://example.com', 'no' => NULL, 'no-core' => NULL, 'verbose' => NULL), '@' . $name);
  178. $this->assertContains('--uri=http://example.com', $this->getErrorOutput());
  179. // Test a remote alias that does not have a 'root' element
  180. $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
  181. @mkdir($aliasPath);
  182. $aliasContents = <<<EOD
  183. <?php
  184. // Written by Unish. This file is safe to delete.
  185. \$aliases['rootlessremote'] = array(
  186. 'uri' => 'remoteuri',
  187. 'remote-host' => 'exampleisp.com',
  188. 'remote-user' => 'www-admin',
  189. );
  190. EOD;
  191. file_put_contents("$aliasPath/rootlessremote.aliases.drushrc.php", $aliasContents);
  192. $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL, 'alias-path' => $aliasPath), '@rootlessremote');
  193. $output = $this->getOutput();
  194. $this->assertContains(' ssh ', $output);
  195. $this->assertContains('--uri=http://example.com', $output);
  196. // Test a remote alias that does not have a 'root' element with cwd inside a Drupal root directory
  197. $root = $this->webroot();
  198. $this->drush('core-status', array(), array('uri' => 'http://example.com', 'simulate' => NULL, 'alias-path' => $aliasPath), '@rootlessremote', $root);
  199. $output = $this->getOutput();
  200. $this->assertContains(' ssh ', $output);
  201. $this->assertContains('--uri=http://example.com', $output);
  202. }
  203. /**
  204. * Test to see if we can access aliases defined inside of
  205. * a provided Drupal root in various locations where they
  206. * may be stored.
  207. */
  208. public function testAliasFilesInDocroot() {
  209. $root = $this->webroot();
  210. $aliasContents = <<<EOD
  211. <?php
  212. // Written by Unish. This file is safe to delete.
  213. \$aliases['atroot'] = array(
  214. 'root' => '/fake/path/to/othersite',
  215. 'uri' => 'default',
  216. );
  217. EOD;
  218. @mkdir($root . "/drush");
  219. @mkdir($root . "/drush/site-aliases");
  220. file_put_contents($root . "/drush/site-aliases/atroot.aliases.drushrc.php", $aliasContents);
  221. $aliasContents = <<<EOD
  222. <?php
  223. // Written by Unish. This file is safe to delete.
  224. \$aliases['insitefolder'] = array(
  225. 'root' => '/fake/path/to/othersite',
  226. 'uri' => 'default',
  227. );
  228. EOD;
  229. @mkdir($root . "/sites/all/drush");
  230. @mkdir($root . "/sites/all/drush/site-aliases");
  231. file_put_contents($root . "/sites/all/drush/site-aliases/sitefolder.aliases.drushrc.php", $aliasContents);
  232. $aliasContents = <<<EOD
  233. <?php
  234. // Written by Unish. This file is safe to delete.
  235. \$aliases['aboveroot'] = 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/aboveroot.aliases.drushrc.php", $aliasContents);
  243. // Ensure that none of these 'sa' commands return an error
  244. $this->drush('sa', array('@atroot'), array(), '@dev');
  245. $this->drush('sa', array('@insitefolder'), array(), '@dev');
  246. $this->drush('sa', array('@aboveroot'), array(), '@dev');
  247. }
  248. /**
  249. * Ensure that Drush searches deep inside specified search locations
  250. * for alias files.
  251. */
  252. public function testDeepAliasSearching() {
  253. $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
  254. file_exists($aliasPath) ?: mkdir($aliasPath);
  255. $deepPath = $aliasPath . '/deep';
  256. file_exists($deepPath) ?: mkdir($deepPath);
  257. $aliasFile = $deepPath . '/baz.aliases.drushrc.php';
  258. $aliasContents = <<<EOD
  259. <?php
  260. // Written by Unish. This file is safe to delete.
  261. \$aliases['deep'] = array(
  262. 'remote-host' => 'fake.remote-host.com',
  263. 'remote-user' => 'www-admin',
  264. 'root' => '/fake/path/to/root',
  265. 'uri' => 'default',
  266. 'command-specific' => array(
  267. 'rsync' => array(
  268. 'delete' => TRUE,
  269. ),
  270. ),
  271. );
  272. EOD;
  273. file_put_contents($aliasFile, $aliasContents);
  274. $options = array(
  275. 'alias-path' => $aliasPath,
  276. 'simulate' => TRUE,
  277. );
  278. $this->drush('sa', array('@deep'), $options);
  279. // Verify that the files directory is not recursed into.
  280. $filesPath = $aliasPath . '/files';
  281. file_exists($filesPath) ?: mkdir($filesPath);
  282. $aliasFile = $filesPath . '/biz.aliases.drushrc.php';
  283. $aliasContents = <<<EOD
  284. <?php
  285. // Written by unish. This file is safe to delete.
  286. \$aliases['nope'] = array(
  287. 'remote-host' => 'fake.remote-host.com',
  288. 'remote-user' => 'www-admin',
  289. 'root' => '/fake/path/to/root',
  290. 'uri' => 'default',
  291. 'command-specific' => array(
  292. 'rsync' => array(
  293. 'delete' => TRUE,
  294. ),
  295. ),
  296. );
  297. EOD;
  298. file_put_contents($aliasFile, $aliasContents);
  299. $options = array(
  300. 'alias-path' => $aliasPath,
  301. 'simulate' => TRUE,
  302. );
  303. // This should not find the '@nope' alias.
  304. $this->drush('sa', array('@nope'), $options, NULL, NULL, self::EXIT_ERROR);
  305. }
  306. }