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