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

Namespace

Unish

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