contextTest.php

  1. 8.0.x tests/contextTest.php
  2. 6.x tests/contextTest.php
  3. 7.x tests/contextTest.php
  4. 4.x tests/contextTest.php
  5. 5.x tests/contextTest.php
  6. master tests/contextTest.php
  • Assure that context API behaves as designed. Mostly implicitly tested, but we
  • do have some edges that need explicit testing.

*

  • *
  • @group base

See also

drush/includes/context.inc.

Classes

Namesort descending Description
contextCase

File

tests/contextTest.php
View source
  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. * @group base
  10. */
  11. class contextCase extends Drush_CommandTestCase {
  12. function setUpPaths() {
  13. $this->log("webroot: ".$this->webroot()."\n");
  14. $this->env = key($this->sites);
  15. $this->site = $this->webroot() . '/sites/' . $this->env;
  16. $this->home = UNISH_SANDBOX . '/home';
  17. $this->paths = array(
  18. 'custom' => UNISH_SANDBOX,
  19. 'site' => $this->site,
  20. 'drupal' => $this->webroot() . '/sites/all/drush',
  21. 'user' => $this->home,
  22. 'home.drush' => $this->home . '/.drush',
  23. 'system' => UNISH_SANDBOX . '/etc/drush',
  24. // We don't want to write a file into drush dir since it is not in the sandbox.
  25. // 'drush' => dirname(realpath(UNISH_DRUSH)),
  26. );
  27. // Run each path through realpath() since the paths we'll compare against
  28. // will have already run through drush_load_config_file().
  29. foreach ($this->paths as $key => $path) $this->paths[$key] = realpath($path);
  30. }
  31. /**
  32. * Try to write a tiny drushrc.php to each place that drush checks. Also
  33. * write a sites/dev/aliases.drushrc.php file to the sandbox.
  34. */
  35. function setUp() {
  36. parent::setUp();
  37. $this->setUpDrupal();
  38. $this->setUpPaths();
  39. // These files are only written to sandbox so get automatically cleaned up.
  40. foreach ($this->paths as $key => $path) {
  41. $contents = <<<EOD
  42. <?php
  43. // Written by Drush's contextCase::setUp(). This file is safe to delete.
  44. \$options['contextConfig'] = '$key';
  45. \$command_specific['unit-eval']['contextConfig'] = '$key-specific';
  46. EOD;
  47. $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
  48. if (file_put_contents($path, $contents)) {
  49. $this->written[] = $this->convert_path($path);
  50. }
  51. }
  52. // Also write a site alias so we can test its supremacy in context hierarchy.
  53. $path = $this->site . '/aliases.drushrc.php';
  54. $aliases['contextAlias'] = array(
  55. 'contextConfig' => 'alias1',
  56. 'command-specific' => array (
  57. 'unit-eval' => array (
  58. 'contextConfig' => 'alias-specific',
  59. ),
  60. ),
  61. );
  62. $contents = unish_file_aliases($aliases);
  63. $return = file_put_contents($path, $contents);
  64. }
  65. /**
  66. * These should be different tests but I could not work out how to do that
  67. * without calling setUp() twice. setUpBeforeClass() did not work out (for MW).
  68. */
  69. function testContext() {
  70. $this->ConfigSearchPaths();
  71. $this->ConfigVersionSpecific();
  72. $this->ContextHierarchy();
  73. }
  74. /**
  75. * Assure that all possible config files get loaded.
  76. */
  77. function ConfigSearchPaths() {
  78. $options = array(
  79. 'pipe' => NULL,
  80. 'config' => UNISH_SANDBOX,
  81. 'root' => $this->webroot(),
  82. 'uri' => $this->env
  83. );
  84. $this->drush('core-status', array('Drush configuration'), $options);
  85. $loaded = $this->getOutputFromJSON('drush-conf');
  86. $loaded = array_map(array(&$this, 'convert_path'), $loaded);
  87. $this->assertSame($this->written, $loaded);
  88. }
  89. /**
  90. * Assure that matching version-specific config files are loaded and others are ignored.
  91. */
  92. function ConfigVersionSpecific() {
  93. $major = $this->drush_major_version();
  94. // Arbitrarily choose the system search path.
  95. $path = realpath(UNISH_SANDBOX . '/etc/drush');
  96. $contents = <<<EOD
  97. <?php
  98. // Written by Unish. This file is safe to delete.
  99. \$options['unish_foo'] = 'bar';
  100. EOD;
  101. // Write matched and unmatched files to the system search path.
  102. $files = array(
  103. $path . '/drush' . $major . 'rc.php',
  104. $path . '/drush999' . 'rc.php',
  105. );
  106. mkdir($path . '/drush' . $major);
  107. mkdir($path . '/drush999');
  108. foreach ($files as $file) {
  109. file_put_contents($file, $contents);
  110. }
  111. $this->drush('core-status', array('Drush configuration'), array('pipe' => NULL));
  112. $loaded = $this->getOutputFromJSON('drush-conf');
  113. // Next 2 lines needed for Windows compatibility.
  114. $loaded = array_map(array(&$this, 'convert_path'), $loaded);
  115. $files = array_map(array(&$this, 'convert_path'), $files);
  116. $this->assertTrue(in_array($files[0], $loaded), 'Loaded a version-specific config file.');
  117. $this->assertFalse(in_array($files[1], $loaded), 'Did not load a mismatched version-specific config file.');
  118. }
  119. /**
  120. * Assure that options are loaded into right context and hierarchy is
  121. * respected by drush_get_option().
  122. *
  123. * Stdin context not exercised here. See backendCase::testTarget().
  124. */
  125. function ContextHierarchy() {
  126. // The 'custom' config file has higher priority than cli and regular config files.
  127. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  128. $eval .= '$cli1 = drush_get_option("cli1");';
  129. $eval .= 'print json_encode(get_defined_vars());';
  130. $config = UNISH_SANDBOX . '/drushrc.php';
  131. $options = array(
  132. 'cli1' => NULL,
  133. 'config' => $config,
  134. 'root' => $this->webroot(),
  135. 'uri' => $this->env,
  136. );
  137. $this->drush('php-eval', array($eval), $options);
  138. $output = $this->getOutput();
  139. $actuals = json_decode(trim($output));
  140. $this->assertEquals('custom', $actuals->contextConfig);
  141. $this->assertTrue($actuals->cli1);
  142. // Site alias trumps 'custom'.
  143. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  144. $eval .= 'print json_encode(get_defined_vars());';
  145. $options = array(
  146. 'config' => $config,
  147. 'root' => $this->webroot(),
  148. 'uri' => $this->env,
  149. );
  150. $this->drush('php-eval', array($eval), $options, '@contextAlias');
  151. $output = $this->getOutput();
  152. $actuals = json_decode(trim($output));
  153. $this->assertEquals('alias1', $actuals->contextConfig);
  154. // Command specific wins over non-specific. If it did not, $expected would
  155. // be 'site'. Note we call unit-eval command in order not to purturb
  156. // php-eval with options in config file.
  157. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  158. $eval .= 'print json_encode(get_defined_vars());';
  159. $options = array(
  160. 'root' => $this->webroot(),
  161. 'uri' => $this->env,
  162. 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
  163. );
  164. $this->drush('unit-eval', array($eval), $options);
  165. $output = $this->getOutput();
  166. $actuals = json_decode(trim($output));
  167. $this->assertEquals('site-specific', $actuals->contextConfig);
  168. }
  169. }