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 ascending 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. class contextCase extends Drush_TestCase {
  10. function setUpPaths() {
  11. $this->root = $this->sites[$this->env]['root'];
  12. $this->site = $this->root . '/sites/' . $this->env;
  13. $this->home = UNISH_SANDBOX . '/home';
  14. $this->paths = array(
  15. 'custom' => UNISH_SANDBOX,
  16. 'site' => $this->site,
  17. 'drupal' => $this->root,
  18. 'user' => $this->home,
  19. 'home.drush' => $this->home . '/.drush',
  20. 'system' => UNISH_SANDBOX . '/etc/drush',
  21. // We don't want to write a file into drush dir since it is not in the sandbox.
  22. // 'drush' => dirname(realpath(UNISH_DRUSH)),
  23. );
  24. // Run each path through realpath() since the paths we'll compare against
  25. // will have already run through drush_load_config_file().
  26. foreach ($this->paths as $key => $path) $this->paths[$key] = realpath($path);
  27. }
  28. /**
  29. * Try to write a tiny drushrc.php to each place that drush checks. Also
  30. * write a sites/dev/aliases.drushrc.php file to the sandbox.
  31. */
  32. function setup() {
  33. parent::setUp();
  34. $this->env = 'dev';
  35. $this->setUpDrupal($this->env, FALSE);
  36. $this->setUpPaths();
  37. // These files are only written to sandbox so get automatically cleaned up.
  38. foreach ($this->paths as $key => $path) {
  39. $contents = <<<EOD
  40. <?php
  41. // Written by Drush's contextCase::setup(). This file is safe to delete.
  42. \$options['contextConfig'] = '$key';
  43. \$command_specific['unit-eval']['contextConfig'] = '$key-specific';
  44. EOD;
  45. $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
  46. if (file_put_contents($path, $contents)) {
  47. $this->written[] = $path;
  48. }
  49. }
  50. // Also write a site alias so we can test its supremacy in context hierarchy.
  51. $path = $this->site . '/aliases.drushrc.php';
  52. $aliases['contextAlias'] = array(
  53. 'contextConfig' => 'alias1',
  54. 'command-specific' => array (
  55. 'unit-eval' => array (
  56. 'contextConfig' => 'alias-specific',
  57. ),
  58. ),
  59. );
  60. $contents = $this->file_aliases($aliases);
  61. $return = file_put_contents($path, $contents);
  62. }
  63. /**
  64. * These should be different tests but I could not work out how to do that
  65. * without calling setup() twice. setupBeforeClass() did not work out (for MW).
  66. */
  67. function testContext() {
  68. $this->ConfigSearchPaths();
  69. $this->ConfigVersionSpecific();
  70. $this->ContextHierarchy();
  71. }
  72. /**
  73. * Assure that all possible config files get loaded.
  74. */
  75. function ConfigSearchPaths() {
  76. $options = array(
  77. 'pipe' => NULL,
  78. 'config' => UNISH_SANDBOX,
  79. 'root' => $this->root,
  80. 'uri' => $this->env,
  81. );
  82. $this->drush('core-status', array('Drush configuration'), $options);
  83. $output = trim($this->getOutput());
  84. $loaded = explode(' ', $output);
  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 ConfigVersionSpecific() {
  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. $output = trim($this->getOutput());
  111. $loaded = explode(' ', $output);
  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 ContextHierarchy() {
  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->root,
  131. 'uri' => $this->env,
  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->root,
  144. 'uri' => $this->env,
  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->root,
  157. 'uri' => $this->env,
  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. }