Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / drush / drush / tests / shellAliasTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * Tests for Shell aliases.
7  *
8  * @group base
9  */
10 class shellAliasesCase extends CommandUnishTestCase {
11
12   /**
13    * Write a config file that contains the shell-aliases array.
14    */
15   function setUp() {
16     parent::setUp();
17     $contents = "
18       <?php
19
20       \$options['shell-aliases'] = array(
21         'glopts' => 'topic core-global-options',
22         'pull' => '!git pull',
23         'echosimple' => '!echo {{@target}}',
24         'echotest' => '!echo {{@target}} {{%root}} {{%mypath}}',
25         'compound-command' => '!cd {{%sandbox}} && echo second',
26       );
27     ";
28     file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
29     if (!file_exists(UNISH_SANDBOX . '/b')) {
30       mkdir(UNISH_SANDBOX . '/b');
31     }
32     $contents = "
33       <?php
34
35       \$options['shell-aliases'] = array(
36         'also' => '!echo alternate config file included too',
37       );
38     ";
39     file_put_contents(UNISH_SANDBOX . '/b/drushrc.php', trim($contents));
40     $aliases['myalias'] = array(
41       'root' => '/path/to/drupal',
42       'uri' => 'mysite.org',
43       '#peer' => '@live',
44       'path-aliases' => array (
45         '%mypath' => '/srv/data/mypath',
46         '%sandbox' => UNISH_SANDBOX,
47       ),
48     );
49     $contents = $this->unish_file_aliases($aliases);
50     file_put_contents(UNISH_SANDBOX . '/aliases.drushrc.php', $contents);
51   }
52
53   /**
54    * Test shell aliases to Drush commands.
55    */
56   public function testShellAliasDrushLocal() {
57     $options = array(
58       'config' => UNISH_SANDBOX,
59     );
60     $this->drush('glopts', array(), $options);
61     $output = $this->getOutput();
62     $this->assertContains('These options are applicable to most drush commands.', $output, 'Successfully executed local shell alias to drush command');
63   }
64
65   /**
66    * Test shell aliases to Bash commands. Assure we pass along extra arguments
67    * and options.
68    */
69   public function testShellAliasBashLocal() {
70     $options = array(
71       'config' => UNISH_SANDBOX,
72       'simulate' => NULL,
73       'rebase' => NULL,
74     );
75     $this->drush('pull', array('origin'), $options, NULL, NULL, self::EXIT_SUCCESS, '2>&1');
76     $output = $this->getOutput();
77     $this->assertContains('Calling proc_open(git pull origin --rebase);', $output);
78   }
79
80   public function testShellAliasDrushRemote() {
81     $options = array(
82       'config' => UNISH_SANDBOX,
83       'simulate' => NULL,
84       'ssh-options' => '',
85     );
86     $this->drush('glopts', array(), $options, 'user@server/path/to/drupal#sitename');
87     // $expected might be different on non unix platforms. We shall see.
88     // n.b. --config is not included in calls to remote systems.
89     $bash = $this->escapeshellarg('drush  --config=drush-sandbox --nocolor --uri=sitename --root=/path/to/drupal  core-topic core-global-options 2>&1');
90     $expected = "Simulating backend invoke: ssh -t user@server $bash 2>&1";
91     $output = $this->getOutput();
92     // Remove any coverage arguments. The filename changes, so it's not possible
93     // to create a string for assertEquals, and the need for both shell escaping
94     // and regexp escaping different parts of the expected output for
95     // assertRegexp makes it easier just to remove the argument before checking
96     // the output.
97     $output = preg_replace('{--drush-coverage=[^ ]+ }', '', $output);
98     $output = preg_replace('{--config=[^ ]+ +}', '--config=drush-sandbox ', $output);
99     $this->assertEquals($expected, $output, 'Expected remote shell alias to a drush command was built');
100   }
101
102   public function testShellAliasBashRemote() {
103     $options = array(
104       'config' => UNISH_SANDBOX,
105       'simulate' => NULL,
106       'ssh-options' => '',
107       'rebase' => NULL,
108     );
109     $this->drush('pull', array('origin'), $options, 'user@server/path/to/drupal#sitename', NULL, self::EXIT_SUCCESS, '2>&1');
110     // $expected might be different on non unix platforms. We shall see.
111     $exec = self::escapeshellarg('cd /path/to/drupal && git pull origin --rebase');
112     $expected = "Calling proc_open(ssh  user@server $exec);";
113     $output = $this->getOutput();
114     $this->assertEquals($expected, $output, 'Expected remote shell alias to a bash command was built');
115   }
116
117   /**
118    * Test shell aliases with simple replacements -- no alias.
119    */
120   public function testShellAliasSimpleReplacement() {
121     $options = array(
122       'config' => UNISH_SANDBOX,
123     );
124     $this->drush('echosimple', array(), $options);
125     // Windows command shell prints quotes (but not always?). See http://drupal.org/node/1452944.
126     $expected = '@none';
127     $output = $this->getOutput();
128     $this->assertEquals($expected, $output);
129   }
130
131   /**
132    * Test shell aliases with complex replacements -- no alias.
133    */
134   public function testShellAliasReplacementNoAlias() {
135     $options = array(
136       'config' => UNISH_SANDBOX,
137     );
138     // echo test has replacements that are not satisfied, so this is expected to return an error.
139     $this->drush('echotest', array(), $options, NULL, NULL, self::EXIT_ERROR);
140   }
141
142   /**
143    * Test shell aliases with replacements -- alias.
144    */
145   public function testShellAliasReplacementWithAlias() {
146     $options = array(
147       'config' => UNISH_SANDBOX,
148       'alias-path' => UNISH_SANDBOX,
149     );
150     $this->drush('echotest', array(), $options, '@myalias');
151     // Windows command shell prints quotes (not always?). See http://drupal.org/node/1452944.
152     $expected = '@myalias';
153     $expected .= ' /path/to/drupal /srv/data/mypath';
154     $output = $this->getOutput();
155     $this->assertEquals($expected, $output);
156   }
157
158   /**
159    * Test shell aliases with replacements and compound commands.
160    */
161   public function testShellAliasCompoundCommands() {
162     $options = array(
163       'config' => UNISH_SANDBOX,
164       'alias-path' => UNISH_SANDBOX,
165     );
166     $this->drush('compound-command', array(), $options, '@myalias');
167     $expected = 'second';
168     $output = $this->getOutput();
169     $this->assertEquals($expected, $output);
170   }
171
172
173   /**
174    * Test shell aliases with multiple config files.
175    */
176   public function testShellAliasMultipleConfigFiles() {
177     $options = array(
178       'config' => UNISH_SANDBOX . "/b" . PATH_SEPARATOR . UNISH_SANDBOX,
179       'alias-path' => UNISH_SANDBOX,
180     );
181     $this->drush('also', array(), $options);
182     $expected = "alternate config file included too";
183     $output = $this->getOutput();
184     $this->assertEquals($expected, $output);
185   }
186
187 }