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

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 = $this->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. $output = trim($this->getOutput());
  86. $loaded = explode(' ', $output);
  87. $loaded = array_map(array(&$this, 'convert_path'), $loaded);
  88. $this->assertSame($this->written, $loaded);
  89. }
  90. /**
  91. * Assure that matching version-specific config files are loaded and others are ignored.
  92. */
  93. function ConfigVersionSpecific() {
  94. $major = $this->drush_major_version();
  95. // Arbitrarily choose the system search path.
  96. $path = realpath(UNISH_SANDBOX . '/etc/drush');
  97. $contents = <<<EOD
  98. <?php
  99. // Written by Unish. This file is safe to delete.
  100. \$options['unish_foo'] = 'bar';
  101. EOD;
  102. // Write matched and unmatched files to the system search path.
  103. $files = array(
  104. $path . '/drush' . $major . 'rc.php',
  105. $path . '/drush999' . 'rc.php',
  106. );
  107. mkdir($path . '/drush' . $major);
  108. mkdir($path . '/drush999');
  109. foreach ($files as $file) {
  110. file_put_contents($file, $contents);
  111. }
  112. $this->drush('core-status', array('Drush configuration'), array('pipe' => NULL));
  113. $output = trim($this->getOutput());
  114. $loaded = explode(' ', $output);
  115. // Next 2 lines needed for Windows compatibility.
  116. $loaded = array_map(array(&$this, 'convert_path'), $loaded);
  117. $files = array_map(array(&$this, 'convert_path'), $files);
  118. $this->assertTrue(in_array($files[0], $loaded), 'Loaded a version-specific config file.');
  119. $this->assertFalse(in_array($files[1], $loaded), 'Did not load a mismatched version-specific config file.');
  120. }
  121. /**
  122. * Assure that options are loaded into right context and hierarchy is
  123. * respected by drush_get_option().
  124. *
  125. * Stdin context not exercised here. See backendCase::testTarget().
  126. */
  127. function ContextHierarchy() {
  128. // The 'custom' config file has higher priority than cli and regular config files.
  129. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  130. $eval .= '$cli1 = drush_get_option("cli1");';
  131. $eval .= 'print json_encode(get_defined_vars());';
  132. $config = UNISH_SANDBOX . '/drushrc.php';
  133. $options = array(
  134. 'cli1' => NULL,
  135. 'config' => $config,
  136. 'root' => $this->webroot(),
  137. 'uri' => $this->env,
  138. );
  139. $this->drush('php-eval', array($eval), $options);
  140. $output = $this->getOutput();
  141. $actuals = json_decode(trim($output));
  142. $this->assertEquals('custom', $actuals->contextConfig);
  143. $this->assertTrue($actuals->cli1);
  144. // Site alias trumps 'custom'.
  145. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  146. $eval .= 'print json_encode(get_defined_vars());';
  147. $options = array(
  148. 'config' => $config,
  149. 'root' => $this->webroot(),
  150. 'uri' => $this->env,
  151. );
  152. $this->drush('php-eval', array($eval), $options, '@contextAlias');
  153. $output = $this->getOutput();
  154. $actuals = json_decode(trim($output));
  155. $this->assertEquals('alias1', $actuals->contextConfig);
  156. // Command specific wins over non-specific. If it did not, $expected would
  157. // be 'site'. Note we call unit-eval command in order not to purturb
  158. // php-eval with options in config file.
  159. $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
  160. $eval .= 'print json_encode(get_defined_vars());';
  161. $options = array(
  162. 'root' => $this->webroot(),
  163. 'uri' => $this->env,
  164. 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
  165. );
  166. $this->drush('unit-eval', array($eval), $options);
  167. $output = $this->getOutput();
  168. $actuals = json_decode(trim($output));
  169. $this->assertEquals('site-specific', $actuals->contextConfig);
  170. }
  171. }