5 * Assure that context API behaves as designed. Mostly implicitly tested, but we
6 * do have some edges that need explicit testing.
8 * @see drush/includes/context.inc.
15 class contextCase extends CommandUnishTestCase {
17 function setUpPaths() {
18 $this->log("webroot: " . $this->webroot() . "\n", 'warning');
19 $this->env = key($this->getSites());
20 $this->site = $this->webroot() . '/sites/' . $this->env;
21 $this->home = UNISH_SANDBOX . '/home';
23 'custom' => UNISH_SANDBOX,
24 'site' => $this->site,
25 'drupal' => $this->webroot() . '/sites/all/drush',
26 'drupal-parent' => dirname($this->webroot()) . '/drush',
27 'user' => $this->home,
28 'home.drush' => $this->home . '/.drush',
29 'system' => UNISH_SANDBOX . '/etc/drush',
30 // We don't want to write a file into drush dir since it is not in the sandbox.
31 // 'drush' => dirname(realpath(UNISH_DRUSH)),
33 // Run each path through realpath() since the paths we'll compare against
34 // will have already run through drush_load_config_file().
35 foreach ($this->paths as $key => $path) {
37 $this->paths[$key] = realpath($path);
42 * Try to write a tiny drushrc.php to each place that Drush checks. Also
43 * write a sites/dev/aliases.drushrc.php file to the sandbox.
48 if (!$this->getSites()) {
53 // These files are only written to sandbox so get automatically cleaned up.
54 foreach ($this->paths as $key => $path) {
57 // Written by Drush's contextCase::setUp(). This file is safe to delete.
58 \$options['contextConfig'] = '$key';
59 \$command_specific['unit-eval']['contextConfig'] = '$key-specific';
61 $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
62 if (file_put_contents($path, $contents)) {
63 $this->written[] = $this->convert_path($path);
67 // Also write a site alias so we can test its supremacy in context hierarchy.
68 $path = $this->webroot() . '/sites/' . $this->env . '/aliases.drushrc.php';
69 $aliases['contextAlias'] = array(
70 'contextConfig' => 'alias1',
71 'command-specific' => array (
72 'unit-eval' => array (
73 'contextConfig' => 'alias-specific',
77 $contents = $this->unish_file_aliases($aliases);
78 $return = file_put_contents($path, $contents);
82 * Assure that all possible config files get loaded.
84 function testConfigSearchPaths() {
87 'config' => UNISH_SANDBOX,
88 'root' => $this->webroot(),
89 'uri' => key($this->getSites())
91 $this->drush('core-status', array('Drush configuration'), $options);
92 $loaded = $this->getOutputFromJSON('drush-conf');
93 $loaded = array_map(array(&$this, 'convert_path'), $loaded);
94 $this->assertSame($this->written, $loaded);
98 * Assure that matching version-specific config files are loaded and others are ignored.
100 function testConfigVersionSpecific() {
101 $major = $this->drush_major_version();
102 // Arbitrarily choose the system search path.
103 $path = realpath(UNISH_SANDBOX . '/etc/drush');
106 // Written by Unish. This file is safe to delete.
107 \$options['unish_foo'] = 'bar';
110 // Write matched and unmatched files to the system search path.
112 $path . '/drush' . $major . 'rc.php',
113 $path . '/drush999' . 'rc.php',
115 mkdir($path . '/drush' . $major);
116 mkdir($path . '/drush999');
117 foreach ($files as $file) {
118 file_put_contents($file, $contents);
121 $this->drush('core-status', array('Drush configuration'), array('pipe' => NULL));
122 $loaded = $this->getOutputFromJSON('drush-conf');
123 // Next 2 lines needed for Windows compatibility.
124 $loaded = array_map(array(&$this, 'convert_path'), $loaded);
125 $files = array_map(array(&$this, 'convert_path'), $files);
126 $this->assertTrue(in_array($files[0], $loaded), 'Loaded a version-specific config file.');
127 $this->assertFalse(in_array($files[1], $loaded), 'Did not load a mismatched version-specific config file.');
131 * Assure that options are loaded into right context and hierarchy is
132 * respected by drush_get_option().
134 * Stdin context not exercised here. See backendCase::testTarget().
136 function testContextHierarchy() {
137 // The 'custom' config file has higher priority than cli and regular config files.
138 $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
139 $eval .= '$cli1 = drush_get_option("cli1");';
140 $eval .= 'print json_encode(get_defined_vars());';
141 $config = UNISH_SANDBOX . '/drushrc.php';
145 'root' => $this->webroot(),
146 'uri' => key($this->getSites()),
148 $this->drush('php-eval', array($eval), $options);
149 $output = $this->getOutput();
150 $actuals = json_decode(trim($output));
151 $this->assertEquals('custom', $actuals->contextConfig);
152 $this->assertTrue($actuals->cli1);
154 // Site alias trumps 'custom'.
155 $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
156 $eval .= 'print json_encode(get_defined_vars());';
159 'root' => $this->webroot(),
160 'uri' => key($this->getSites()),
162 $this->drush('php-eval', array($eval), $options, '@contextAlias');
163 $output = $this->getOutput();
164 $actuals = json_decode(trim($output));
165 $this->assertEquals('alias1', $actuals->contextConfig);
167 // Command specific wins over non-specific. If it did not, $expected would
168 // be 'site'. Note we call unit-eval command in order not to purturb
169 // php-eval with options in config file.
170 $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
171 $eval .= 'print json_encode(get_defined_vars());';
173 'root' => $this->webroot(),
174 'uri' => key($this->getSites()),
175 'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
177 $this->drush('unit-eval', array($eval), $options);
178 $output = $this->getOutput();
179 $actuals = json_decode(trim($output));
180 $this->assertEquals('site-specific', $actuals->contextConfig);