Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / tests / AnnotatedCommandTest.php
1 <?php
2
3 namespace Unish;
4
5 use Webmozart\PathUtil\Path;
6
7 /**
8  * @group base
9  */
10 class AnnotatedCommandCase extends CommandUnishTestCase
11 {
12     use TestModuleHelperTrait;
13
14     public function testGlobal()
15     {
16         $globalIncludes = $targetDir = Path::join(__DIR__, 'resources/global-includes');
17
18         $options = [];
19
20         // We modified the set of available Drush commands; we need to clear the Drush command cache
21         $this->drush('cc', ['drush'], $options);
22
23         // drush foobar
24         $options['include'] = "$globalIncludes";
25         $this->drush('foobar', [], $options);
26         $output = $this->getOutput();
27         $this->assertEquals('baz', $output);
28
29         // Drush foobaz
30         $this->drush('foobaz', [], $options);
31         $output = $this->getOutput();
32         $this->assertEquals('bar', $output);
33
34         $options = [
35             'yes' => null,
36             'include' => $globalIncludes,
37             'directory' => self::getSandbox(),
38         ];
39
40         $original = getenv('SHELL_INTERACTIVE');
41         $this->setEnv(['SHELL_INTERACTIVE' => 1]);
42         $this->drush('generate', ['foo-example'], $options);
43         $this->setEnv(['SHELL_INTERACTIVE' => $original]);
44
45         $target = Path::join($this->getSandbox(), 'foo.php');
46         $actual = trim(file_get_contents($target));
47         $this->assertEquals('Foo.', $actual);
48         unlink($target);
49     }
50
51     public function testExecute()
52     {
53         $this->setUpDrupal(1, true);
54
55         // Copy the 'woot' module over to the Drupal site we just set up.
56         $this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
57
58         // Enable our module. This will also clear the commandfile cache.
59         $this->drush('pm-enable', ['woot']);
60
61         // In theory this is not necessary, but this test keeps failing.
62         // $this->drush('cc', array('drush'), $options);
63
64         // Make sure that modules can supply DCG Generators and they work.
65         $optionsExample['answers'] = json_encode([
66             'name' => 'foo',
67             'machine_name' => 'bar',
68         ]);
69         $optionsExample['directory'] = self::getSandbox();
70         $optionsExample['yes'] = null;
71         $original = getenv('SHELL_INTERACTIVE');
72         $this->setEnv(['SHELL_INTERACTIVE' => 1]);
73         $this->drush('generate', ['woot-example'], $optionsExample);
74         $this->setEnv(['SHELL_INTERACTIVE' => $original]);
75         $target = Path::join(self::getSandbox(), '/src/Commands/ExampleBarCommands.php');
76         $actual = trim(file_get_contents($target));
77         $this->assertEquals('ExampleBarCommands says Woot mightily.', $actual);
78
79         // drush woot
80         $this->drush('woot');
81         $output = $this->getOutput();
82         $this->assertEquals('Woot!', $output);
83
84         // drush my-cat bet alpha --flip
85         $this->drush('my-cat', ['bet', 'alpha'], ['flip' => null]);
86         $output = $this->getOutput();
87         $this->assertEquals('alphabet', $output);
88
89         // drush my-cat bet alpha --flip
90         $this->drush('my-cat', ['bet', 'alpha'], ['flip' => null, 'ignored-modules' => 'woot'], null, null, self::EXIT_ERROR);
91
92         $this->drush('try-formatters');
93         $output = $this->getOutput();
94         $expected = <<<EOT
95  ------ ------ -------
96   I      II     III
97  ------ ------ -------
98   One    Two    Three
99   Eins   Zwei   Drei
100   Ichi   Ni     San
101   Uno    Dos    Tres
102  ------ ------ -------
103 EOT;
104         $this->assertEquals(trim(preg_replace('#[ \n]+#', ' ', $expected)), trim(preg_replace('#[ \n]+#', ' ', $output)));
105
106         $this->drush('try-formatters --format=yaml --fields=III,II', [], [], null, null, self::EXIT_SUCCESS);
107         $output = $this->getOutput();
108         // TODO: If there are different versions of symfony/yaml in Drush and Drupal,
109         // then we can get indentation errors. Ignore that in these tests; this is not
110         // a problem with site-local Drush.
111         $output = str_replace('    ', '  ', $output);
112         $expected = <<<EOT
113 en:
114   third: Three
115   second: Two
116 de:
117   third: Drei
118   second: Zwei
119 jp:
120   third: San
121   second: Ni
122 es:
123   third: Tres
124   second: Dos
125 EOT;
126         $this->assertEquals($expected, $output);
127
128         $this->drush('try-formatters', [], ['format' => 'json']);
129         $data = $this->getOutput();
130         $expected = <<<EOT
131 {
132     "en": {
133         "first": "One",
134         "second": "Two",
135         "third": "Three"
136     },
137     "de": {
138         "first": "Eins",
139         "second": "Zwei",
140         "third": "Drei"
141     },
142     "jp": {
143         "first": "Ichi",
144         "second": "Ni",
145         "third": "San"
146     },
147     "es": {
148         "first": "Uno",
149         "second": "Dos",
150         "third": "Tres"
151     }
152 }
153 EOT;
154         $this->assertEquals($expected, $data);
155
156         // drush help my-cat
157         $this->drush('help', ['my-cat']);
158         $output = $this->getOutput();
159         $this->assertContains('bet alpha --flip Concatinate "alpha" and "bet".', $output);
160         $this->assertContains('Aliases: c', $output);
161
162         // drush help woot
163         $this->drush('help', ['woot']);
164         $output = $this->getOutput();
165         $this->assertContains('Woot mightily.', $output);
166
167         // TODO: support console.command commands
168         $this->drush('annotated:greet symfony');
169         $output = $this->getOutput();
170         $this->assertEquals('Hello symfony', $output);
171
172         $this->drush('demo:greet symfony');
173         $output = $this->getOutput();
174         $this->assertEquals('Hello symfony', $output);
175
176         $this->markTestSkipped('--help not working yet.');
177
178         // drush my-cat --help
179         $this->drush('my-cat', [], ['help' => null]);
180         $output = $this->getOutput();
181         $this->assertContains('my-cat bet alpha --flip', $output);
182         $this->assertContains('The first parameter', $output);
183         $this->assertContains('The other parameter', $output);
184         $this->assertContains('Whether or not the second parameter', $output);
185
186         // drush woot --help
187         $this->drush('woot', [], ['help' => null]);
188         $output = $this->getOutput();
189         $this->assertContains('Usage:', $output);
190         $this->assertContains('woot [options]', $output);
191         $this->assertContains('Woot mightily.', $output);
192
193         // drush try-formatters --help
194         $this->drush('try-formatters', [], ['help' => null]);
195         $output = $this->getOutput();
196         $this->assertContains('Demonstrate formatters', $output);
197         $this->assertContains('try:formatters --fields=first,third', $output);
198         $this->assertContains('try:formatters --fields=III,II', $output);
199         // $this->assertContains('--fields=<first, second, third>', $output);
200         $this->assertContains('Available fields:', $output);
201         $this->assertContains('[default: "table"]', $output);
202
203         $this->markTestSkipped('--ignored-modules not supported yet');
204
205         // TODO: Support --ignored-modules
206         // drush woot --help with the 'woot' module ignored
207         $this->drush('woot', [], ['help' => null, 'ignored-modules' => 'woot'], null, null, self::EXIT_ERROR);
208     }
209
210     public function setupGlobalExtensionsForTests()
211     {
212         $globalExtension = __DIR__ . '/resources/global-includes';
213         $targetDir = Path::join(self::getSandbox(), 'global-includes');
214         $this->mkdir($targetDir);
215         $this->recursiveCopy($globalExtension, $targetDir);
216         return $targetDir;
217     }
218 }