More tidying.
[yaffs-website] / vendor / drush / drush / tests / contextTest.php
1 <?php
2
3 /**
4 * @file
5 *  Assure that context API behaves as designed. Mostly implicitly tested, but we
6 *  do have some edges that need explicit testing.
7 *
8 *  @see drush/includes/context.inc.
9 *
10 *  @group base
11 */
12
13 namespace Unish;
14
15 class contextCase extends CommandUnishTestCase {
16
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';
22     $this->paths = array(
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)),
32     );
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) {
36       @mkdir($path);
37       $this->paths[$key] = realpath($path);
38     }
39   }
40
41   /**
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.
44    */
45   function setUp() {
46     parent::setUp();
47
48     if (!$this->getSites()) {
49       $this->setUpDrupal();
50     }
51     $this->setUpPaths();
52
53     // These files are only written to sandbox so get automatically cleaned up.
54     foreach ($this->paths as $key => $path) {
55       $contents = <<<EOD
56 <?php
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';
60 EOD;
61       $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
62       if (file_put_contents($path, $contents)) {
63         $this->written[] = $this->convert_path($path);
64       }
65     }
66
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',
74         ),
75       ),
76     );
77     $contents = $this->unish_file_aliases($aliases);
78     $return = file_put_contents($path, $contents);
79   }
80
81   /**
82    * Assure that all possible config files get loaded.
83    */
84   function testConfigSearchPaths() {
85     $options = array(
86       'pipe' => NULL,
87       'config' => UNISH_SANDBOX,
88       'root' => $this->webroot(),
89       'uri' => key($this->getSites())
90     );
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);
95   }
96
97   /**
98    * Assure that matching version-specific config files are loaded and others are ignored.
99    */
100   function testConfigVersionSpecific() {
101     $major = $this->drush_major_version();
102     // Arbitrarily choose the system search path.
103     $path = realpath(UNISH_SANDBOX . '/etc/drush');
104     $contents = <<<EOD
105 <?php
106 // Written by Unish. This file is safe to delete.
107 \$options['unish_foo'] = 'bar';
108 EOD;
109
110     // Write matched and unmatched files to the system search path.
111     $files = array(
112       $path .  '/drush' . $major . 'rc.php',
113       $path .  '/drush999' . 'rc.php',
114     );
115     mkdir($path . '/drush' . $major);
116     mkdir($path . '/drush999');
117     foreach ($files as $file) {
118       file_put_contents($file, $contents);
119     }
120
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.');
128   }
129
130   /**
131    * Assure that options are loaded into right context and hierarchy is
132    * respected by drush_get_option().
133    *
134    * Stdin context not exercised here. See backendCase::testTarget().
135    */
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';
142     $options = array(
143       'cli1' => NULL,
144       'config' => $config,
145       'root' => $this->webroot(),
146       'uri' => key($this->getSites()),
147     );
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);
153
154     // Site alias trumps 'custom'.
155     $eval =  '$contextConfig = drush_get_option("contextConfig", "n/a");';
156     $eval .= 'print json_encode(get_defined_vars());';
157     $options = array(
158       'config' => $config,
159       'root' => $this->webroot(),
160       'uri' => key($this->getSites()),
161     );
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);
166
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());';
172     $options = array(
173       'root' => $this->webroot(),
174       'uri' => key($this->getSites()),
175       'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
176     );
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);
181   }
182 }