4 use Webmozart\PathUtil\Path;
7 * Tests for the 'drush' script itself
9 class drushScriptCase extends CommandUnishTestCase {
12 * Test `PHP_OPTIONS=... drush`
14 public function testPhpOptionsTest() {
15 $this->markTestSkipped('Environment variables not yet passed along to Process by execute().');
18 // @todo: could probably run this test on mingw
19 if ($this->is_windows()) {
20 $this->markTestSkipped('Environment variable tests not currently functional on Windows.');
24 $env = array('PHP_OPTIONS' => '-d default_mimetype="text/drush"');
25 $this->drush('ev', array('print ini_get("default_mimetype");'), $options, NULL, NULL, self::EXIT_SUCCESS, NULL, $env);
26 $output = $this->getOutput();
27 $this->assertEquals('text/drush', $output);
30 public function testDrushFinder() {
31 // We don't really need a real Drupal site; we could
32 // create a fake site, as long as we had the right signature
33 // files to allow us to bootstrap to the DRUPAL_ROOT phase.
34 $this->setUpDrupal(1, TRUE);
36 $globalDrushDotPhp = Path::join(UNISH_DRUSH, '../drush.php');
38 // Control: test `drush --root ` ... with no site-local Drush
39 $drush_location = $this->getDrushLocation();
40 $this->assertEquals($globalDrushDotPhp, $drush_location);
42 // We will try copying a site-local Drush to
43 // all of the various locations the 'drush finder'
44 // might expect to find it.
45 $drush_locations = array(
52 foreach ($drush_locations as $drush_base) {
53 $drush_root = $this->create_site_local_drush($drush_base);
55 // Test `drush --root ` ... with a site-local Drush
56 $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
57 $this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
58 // Ensure that --local was NOT added
59 $result = $this->drush('ev', array('return drush_get_option("local");'), array('root' => $this->webroot()));
60 $output = $this->getOutput();
61 $this->assertEquals("", $output);
63 // Run the `drush --root` test again, this time with
64 // a drush.wrapper script in place.
65 $this->createDrushWrapper($drush_base);
66 $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
67 $this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
68 // Test to see if --local was added
69 $result = $this->drush('ev', array('return drush_get_option("local");'), array('root' => $this->webroot()));
70 $output = $this->getOutput();
71 $this->assertEquals("TRUE", $output);
73 // Get rid of the symlink and site-local Drush we created
74 $this->remove_site_local_drush($drush_base);
77 // Next, try again with a site-local Drush in a location
78 // that Drush does not search.
79 $mysterious_location = "path/drush/does/not/search";
80 $drush_root = $this->create_site_local_drush($mysterious_location);
81 // We should not find the site-local Drush without a Drush wrapper.
82 $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
83 $this->assertEquals($globalDrushDotPhp, $drush_location);
84 $this->createDrushWrapper($mysterious_location);
85 // Now that there is a Drush wrapper, we should be able to find the site-local Drush.
86 $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
87 $this->assertEquals(realpath($drush_root . '/drush.php'), $drush_location);
91 * Copy UNISH_DRUSH into the specified site-local location.
93 function create_site_local_drush($drush_base) {
94 $drush_root = $this->webroot() . '/' . $drush_base . '/drush/drush';
95 $bin_dir = $this->webroot() . '/' . $drush_base . '/bin';
97 $this->mkdir(dirname($drush_root));
98 $this->recursive_copy(dirname(UNISH_DRUSH), $drush_root);
99 @chmod($drush_root . '/drush', 0777);
100 @chmod($drush_root . '/drush.launcher', 0777);
101 $this->mkdir($bin_dir);
102 symlink($drush_root . '/drush', $bin_dir . '/drush');
107 function remove_site_local_drush($drush_base) {
108 // Get rid of the symlink and site-local Drush we created
109 unish_file_delete_recursive($this->webroot() . '/' . $drush_base . '/drush/drush');
110 unlink($this->webroot() . '/' . $drush_base . '/bin/drush');
111 if (file_exists($this->webroot() . '/drush.wrapper')) {
112 unlink($this->webroot() . '/drush.wrapper');
117 * TODO: Create a Drush wrapper script, and copy it to
118 * to the root of the fake Drupal site, and point it
119 * at the specified site-local Drush script.
121 function createDrushWrapper($drush_base) {
122 $drush_launcher = $drush_base . '/drush/drush/drush.launcher';
124 $drush_wrapper_src = dirname(UNISH_DRUSH) . '/examples/drush.wrapper';
125 $drush_wrapper_contents = file_get_contents($drush_wrapper_src);
126 $drush_wrapper_contents = preg_replace('#\.\./vendor/bin/drush.launcher#', $drush_launcher, $drush_wrapper_contents);
127 $drush_wrapper_target = $this->webroot() . '/drush.wrapper';
129 file_put_contents($drush_wrapper_target, $drush_wrapper_contents);
130 @chmod($drush_wrapper_target, 0777);
134 * Get the current location of the Drush script via
135 * `drush status 'Drush script' --format=yaml`. This
136 * will return results other than UNISH_DRUSH in the
137 * presence of a site-local Drush.
139 function getDrushLocation($options = array()) {
144 $result = $this->drush('status', array('Drush script'), $options);
146 $output = $this->getOutput();
147 list($key, $value) = explode(": ", $output);
148 return trim($value, "'");