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. $this->assertEquals('{foo:[bar:{key:"val"},bar2:{key:"long val"}]}', $actuals->env_test2);
  77. $eval = 'print getenv("DRUSH_ENV_TEST3");';
  78. $this->drush('unit-eval', array($eval), $options, '@env-test');
  79. $output = $this->getOutput();
  80. $this->assertEquals( "WORKING CASE = TRUE", $output);
  81. }
  82. /**
  83. * Assure that site lists work as expected.
  84. * @todo Use --backend for structured return data. Depends on http://drupal.org/node/1043922
  85. */
  86. public function testSAList() {
  87. $sites = $this->setUpDrupal(2);
  88. $subdirs = array_keys($sites);
  89. $eval = 'print "bon";';
  90. $options = array(
  91. 'yes' => NULL,
  92. 'verbose' => NULL,
  93. 'root' => $this->webroot(),
  94. );
  95. foreach ($subdirs as $dir) {
  96. $dirs[] = "#$dir";
  97. }
  98. $this->drush('php-eval', array($eval), $options, implode(',', $dirs));
  99. $output = $this->getOutputAsList();
  100. $expected = "#stage >> bon
  101. #dev >> bon";
  102. $actual = implode("\n", $output);
  103. $actual = trim(preg_replace('/^#[a-z]* *>> *$/m', '', $actual)); // ignore blank lines
  104. $this->assertEquals($expected, $actual);
  105. }
  106. /**
  107. * Ensure that requesting a non-existent alias throws an error.
  108. */
  109. public function testBadAlias() {
  110. $this->drush('sa', array('@badalias'), array(), NULL, NULL, self::EXIT_ERROR);
  111. }
  112. /**
  113. * Test to see if we can access aliases defined inside of
  114. * a provided Drupal root in various locations where they
  115. * may be stored.
  116. */
  117. public function testAliasFilesInDocroot() {
  118. $root = $this->webroot();
  119. $aliasContents = <<<EOD
  120. <?php
  121. // Written by Unish. This file is safe to delete.
  122. \$aliases['atroot'] = array(
  123. 'root' => '/fake/path/to/othersite',
  124. 'uri' => 'default',
  125. );
  126. EOD;
  127. @mkdir($root . "/drush");
  128. @mkdir($root . "/drush/site-aliases");
  129. file_put_contents($root . "/drush/site-aliases/atroot.aliases.drushrc.php", $aliasContents);
  130. $aliasContents = <<<EOD
  131. <?php
  132. // Written by Unish. This file is safe to delete.
  133. \$aliases['insitefolder'] = array(
  134. 'root' => '/fake/path/to/othersite',
  135. 'uri' => 'default',
  136. );
  137. EOD;
  138. @mkdir($root . "/sites/all/drush");
  139. @mkdir($root . "/sites/all/drush/site-aliases");
  140. file_put_contents($root . "/sites/all/drush/site-aliases/sitefolder.aliases.drushrc.php", $aliasContents);
  141. $aliasContents = <<<EOD
  142. <?php
  143. // Written by Unish. This file is safe to delete.
  144. \$aliases['aboveroot'] = array(
  145. 'root' => '/fake/path/to/othersite',
  146. 'uri' => 'default',
  147. );
  148. EOD;
  149. @mkdir($root . "/../drush");
  150. @mkdir($root . "/../drush/site-aliases");
  151. file_put_contents($root . "/../drush/site-aliases/aboveroot.aliases.drushrc.php", $aliasContents);
  152. // Ensure that none of these 'sa' commands return an error
  153. $this->drush('sa', array('@atroot'), array(), '@dev');
  154. $this->drush('sa', array('@insitefolder'), array(), '@dev');
  155. $this->drush('sa', array('@aboveroot'), array(), '@dev');
  156. }
  157. /**
  158. * Ensure that Drush searches deep inside specified search locations
  159. * for alias files.
  160. */
  161. public function testDeepAliasSearching() {
  162. $aliasPath = UNISH_SANDBOX . '/site-alias-directory';
  163. file_exists($aliasPath) ?: mkdir($aliasPath);
  164. $deepPath = $aliasPath . '/deep';
  165. file_exists($deepPath) ?: mkdir($deepPath);
  166. $aliasFile = $deepPath . '/baz.aliases.drushrc.php';
  167. $aliasContents = <<<EOD
  168. <?php
  169. // Written by Unish. This file is safe to delete.
  170. \$aliases['deep'] = array(
  171. 'remote-host' => 'fake.remote-host.com',
  172. 'remote-user' => 'www-admin',
  173. 'root' => '/fake/path/to/root',
  174. 'uri' => 'default',
  175. 'command-specific' => array(
  176. 'rsync' => array(
  177. 'delete' => TRUE,
  178. ),
  179. ),
  180. );
  181. EOD;
  182. file_put_contents($aliasFile, $aliasContents);
  183. $options = array(
  184. 'alias-path' => $aliasPath,
  185. 'simulate' => TRUE,
  186. );
  187. $this->drush('sa', array('@deep'), $options);
  188. // Verify that the files directory is not recursed into.
  189. $filesPath = $aliasPath . '/files';
  190. file_exists($filesPath) ?: mkdir($filesPath);
  191. $aliasFile = $filesPath . '/biz.aliases.drushrc.php';
  192. $aliasContents = <<<EOD
  193. <?php
  194. // Written by unish. This file is safe to delete.
  195. \$aliases['nope'] = array(
  196. 'remote-host' => 'fake.remote-host.com',
  197. 'remote-user' => 'www-admin',
  198. 'root' => '/fake/path/to/root',
  199. 'uri' => 'default',
  200. 'command-specific' => array(
  201. 'rsync' => array(
  202. 'delete' => TRUE,
  203. ),
  204. ),
  205. );
  206. EOD;
  207. file_put_contents($aliasFile, $aliasContents);
  208. $options = array(
  209. 'alias-path' => $aliasPath,
  210. 'simulate' => TRUE,
  211. );
  212. // This should not find the '@nope' alias.
  213. $this->drush('sa', array('@nope'), $options, NULL, NULL, self::EXIT_ERROR);
  214. }
  215. }