contextTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * @file
  4. * Assure that context API behaves as designed. Mostly implicitly tested, but we
  5. * do have some edges that need explicit testing.
  6. *
  7. * @see drush/includes/context.inc.
  8. */
  9. class contextCase extends Drush_TestCase {
  10. function setUpPaths() {
  11. $this->root = $this->sites[$this->env]['root'];
  12. $this->site = $this->root . '/sites/' . $this->env;
  13. $this->home = UNISH_SANDBOX . '/home';
  14. $this->paths = array(
  15. 'custom' => UNISH_SANDBOX,
  16. 'site' => $this->site,
  17. 'drupal' => $this->root,
  18. 'user' => $this->home,
  19. 'home.drush' => $this->home . '/.drush',
  20. 'system' => UNISH_SANDBOX . '/etc/drush',
  21. // We don't want to write a file into drush dir since it is not in the sandbox.
  22. // 'drush' => dirname(realpath(UNISH_DRUSH)),
  23. );
  24. // Run each path through realpath() since the paths we'll compare against
  25. // will have already run through drush_load_config_file().
  26. foreach ($this->paths as $key => $path) $this->paths[$key] = realpath($path);
  27. }
  28. /*
  29. * Try to write a tiny drushrc.php to each place that drush checks. Also
  30. * write a sites/dev/aliases.drushrc.php file to the sandbox.
  31. */
  32. function setup() {
  33. parent::setUp();
  34. $this->env = 'dev';
  35. $this->setUpDrupal($this->env, FALSE);
  36. $this->setUpPaths();
  37. // These files are only written to sandbox so get automatically cleaned up.
  38. foreach ($this->paths as $key => $path) {
  39. $contents = <<<EOD
  40. <?php
  41. // Written by Drush's contextCase::setup(). This file is safe to delete.
  42. \$options['contextConfig'] = '$key';
  43. \$command_specific['unit-eval']['contextConfig'] = '$key-specific';
  44. EOD;
  45. $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
  46. if (file_put_contents($path, $contents)) {
  47. $this->written[] = $path;
  48. }
  49. }
  50. // Also write a site alias so we can test its supremacy in context hierarchy.
  51. $path = $this->site . '/aliases.drushrc.php';
  52. $aliases['contextAlias'] = array(
  53. 'contextConfig' => 'alias1',
  54. 'command-specific' => array (
  55. 'unit-eval' => array (
  56. 'contextConfig' => 'alias-specific',
  57. ),
  58. ),
  59. );
  60. $contents = $this->file_aliases($aliases);
  61. $return = file_put_contents($path, $contents);
  62. }
  63. /*
  64. * These should be two different tests but I could not work out how to do that
  65. * without calling setup() twice. setupBeforeClass() did not work out (for MW).
  66. */
  67. function testContext() {
  68. $this->ConfigFile();
  69. $this->ContextHierarchy();
  70. }
  71. /*
  72. * Assure that all possible config files get loaded.
  73. */
  74. function ConfigFile() {
  75. $options = array(
  76. 'pipe' => NULL,
  77. 'config' => UNISH_SANDBOX,
  78. 'root' => $this->root,
  79. 'uri' => $this->env,
  80. );
  81. $this->drush('core-status', array('Drush configuration'), $options);
  82. $output = trim($this->getOutput());
  83. $loaded = explode(' ', $output);
  84. $this->assertSame($this->written, $loaded);
  85. }
  86. /*
  87. * Assure that options are loaded into right context and hierarchy is
  88. * respected by drush_get_option().
  89. *
  90. * Stdin context not exercised here. See backendCase::testTarget().
  91. */
  92. function ContextHierarchy() {
  93. // The 'custom' config file has higher priority than cli and regular config files.
  94. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  95. $eval .= '$cli1 = drush_get_option("cli1");';
  96. $eval .= 'print json_encode(get_defined_vars());';
  97. $config = UNISH_SANDBOX . '/drushrc.php';
  98. $options = array(
  99. 'cli1' => NULL,
  100. 'config' => $config,
  101. 'root' => $this->root,
  102. 'uri' => $this->env,
  103. );
  104. $this->drush('php-eval', array($eval), $options);
  105. $output = $this->getOutput();
  106. $actuals = json_decode(trim($output));
  107. $this->assertEquals('custom', $actuals->contextConfig);
  108. $this->assertTrue($actuals->cli1);
  109. // Site alias trumps 'custom'.
  110. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  111. $eval .= 'print json_encode(get_defined_vars());';
  112. $options = array(
  113. 'config' => $config,
  114. 'root' => $this->root,
  115. 'uri' => $this->env,
  116. );
  117. $this->drush('php-eval', array($eval), $options, '@contextAlias');
  118. $output = $this->getOutput();
  119. $actuals = json_decode(trim($output));
  120. $this->assertEquals('alias1', $actuals->contextConfig);
  121. // Command specific wins over non-specific. If it did not, $expected would
  122. // be 'site'. Note we call unit-eval command in order not to purturb
  123. // php-eval with options in config file.
  124. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  125. $eval .= 'print json_encode(get_defined_vars());';
  126. $options = array(
  127. 'root' => $this->root,
  128. 'uri' => $this->env,
  129. 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
  130. );
  131. $this->drush('unit-eval', array($eval), $options);
  132. $output = $this->getOutput();
  133. $actuals = json_decode(trim($output));
  134. $this->assertEquals('site-specific', $actuals->contextConfig);
  135. }
  136. }