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