Version 1
[yaffs-website] / vendor / drush / drush / tests / annotatedCommandTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * @group base
7  */
8 class annotatedCommandCase extends CommandUnishTestCase {
9
10   public function testGlobal() {
11     $globalExtensions = $this->setupGlobalExtensionsForTests();
12
13     $options = [];
14
15     // We modified the set of available Drush commands; we need to clear the Drush command cache
16     $this->drush('cc', array('drush'), $options);
17
18     // drush foobar
19     $options['include'] = "$globalExtensions";
20     $this->drush('foobar', array(), $options);
21     $output = $this->getOutput();
22     $this->assertEquals('baz', $output);
23
24     // Clear the Drush command cache again and test again with new includes
25     $this->drush('cc', array('drush'), $options);
26
27     // drush foobar again, except include the 'Commands' folder when passing --include
28     $options['include'] = "$globalExtensions/Commands";
29     $this->drush('foobar', array(), $options);
30     $output = $this->getOutput();
31     $this->assertEquals('baz', $output);
32   }
33
34   public function testExecute() {
35     $sites = $this->setUpDrupal(1, TRUE);
36     $uri = key($sites);
37     $root = $this->webroot();
38     $options = array(
39       'root' => $root,
40       'uri' => $uri,
41       'yes' => NULL,
42     );
43
44     // Copy the 'woot' module over to the Drupal site we just set up.
45     $this->setupModulesForTests($root);
46
47     // These are not good asserts, but for the purposes of isolation....
48     $targetDir = $root . DIRECTORY_SEPARATOR . $this->drupalSitewideDirectory() . '/modules/woot';
49     if (UNISH_DRUPAL_MAJOR_VERSION == 8) {
50         $commandFile = $targetDir . "/src/Commands/WootCommands.php";
51     } else {
52         $commandFile = $targetDir . "/Commands/WootCommands.php";
53     }
54     $this->assertFileExists(dirname(dirname(dirname($commandFile))));
55     $this->assertFileExists(dirname(dirname($commandFile)));
56     $this->assertFileExists(dirname($commandFile));
57     $this->assertFileExists($commandFile);
58
59     // Enable out module. This will also clear the commandfile cache.
60     $this->drush('pm-enable', array('woot'), $options);
61
62     // In theory this is not necessary, but this test keeps failing.
63     $this->drush('cc', array('drush'), $options);
64
65     // drush woot --help
66     $this->drush('woot', array(), $options + ['help' => NULL]);
67     $output = $this->getOutput();
68     $this->assertContains('Woot mightily.', $output);
69     $this->assertContains('Aliases: wt', $output);
70
71     // drush help woot
72     $this->drush('help', array('woot'), $options);
73     $output = $this->getOutput();
74     $this->assertContains('Woot mightily.', $output);
75
76     // drush woot
77     $this->drush('woot', array(), $options);
78     $output = $this->getOutput();
79     $this->assertEquals('Woot!', $output);
80
81     // drush my-cat --help
82     $this->drush('my-cat', array(), $options + ['help' => NULL]);
83     $output = $this->getOutput();
84     $this->assertContains('This is the my-cat command', $output);
85     $this->assertContains('bet alpha --flip', $output);
86     $this->assertContains('The first parameter', $output);
87     $this->assertContains('The other parameter', $output);
88     $this->assertContains('Whether or not the second parameter', $output);
89     $this->assertContains('Aliases: c', $output);
90
91     // drush help my-cat
92     $this->drush('help', array('my-cat'), $options);
93     $output = $this->getOutput();
94     $this->assertContains('This is the my-cat command', $output);
95
96     // drush my-cat bet alpha --flip
97     $this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL]);
98     $output = $this->getOutput();
99     $this->assertEquals('alphabet', $output);
100
101     // drush woot --help with the 'woot' module ignored
102     $this->drush('woot', array(), $options + ['help' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR);
103
104     // drush my-cat bet alpha --flip
105     $this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR);
106
107     $this->drush('try-formatters', array(), $options);
108     $output = $this->getOutput();
109     $expected = <<<EOT
110  ------ ------ -------
111   I      II     III
112  ------ ------ -------
113   One    Two    Three
114   Eins   Zwei   Drei
115   Ichi   Ni     San
116   Uno    Dos    Tres
117  ------ ------ -------
118 EOT;
119     $this->assertEquals(trim(preg_replace('#[ \n]+#', ' ', $expected)), trim(preg_replace('#[ \n]+#', ' ', $output)));
120
121     $this->drush('try-formatters --format=yaml --fields=III,II', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
122     $output = $this->getOutput();
123     $expected = <<<EOT
124 en:
125   third: Three
126   second: Two
127 de:
128   third: Drei
129   second: Zwei
130 jp:
131   third: San
132   second: Ni
133 es:
134   third: Tres
135   second: Dos
136 EOT;
137     $this->assertEquals($expected, $output);
138
139     $this->drush('try-formatters', array(), $options + ['backend' => NULL]);
140     $parsed = $this->parse_backend_output($this->getOutput());
141     $data = $parsed['object'];
142     $expected = <<<EOT
143 {"en":{"first":"One","second":"Two","third":"Three"},"de":{"first":"Eins","second":"Zwei","third":"Drei"},"jp":{"first":"Ichi","second":"Ni","third":"San"},"es":{"first":"Uno","second":"Dos","third":"Tres"}}
144 EOT;
145     $this->assertEquals($expected, json_encode($data));
146
147     // drush try-formatters --help
148     $this->drush('try-formatters', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS);
149     $output = $this->getOutput();
150     $this->assertContains('Demonstrate formatters', $output);
151     $this->assertContains('try:formatters --fields=first,third', $output);
152     $this->assertContains('try:formatters --fields=III,II', $output);
153     $this->assertContains('--fields=<first, second, third>', $output);
154     $this->assertContains('Fields to output. All available', $output);
155     $this->assertContains('--format=<table>', $output);
156     $this->assertContains('Select output format. Available:', $output);
157     $this->assertContains('Aliases: try-formatters', $output);
158
159     // If we are running Drupal version 8 or later, then also check to
160     // see if the demo:greet and annotated:greet commands are available.
161     if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
162         $this->drush('demo:greet symfony', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
163         $output = $this->getOutput();
164         $this->assertEquals('Hello symfony', $output);
165
166         $this->drush('annotated:greet symfony', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
167         $output = $this->getOutput();
168         $this->assertEquals('Hello symfony', $output);
169     }
170
171     // Flush the Drush cache so that our 'woot' command is not cached.
172     $this->drush('cache-clear', array('drush'), $options, NULL, NULL, self::EXIT_SUCCESS);
173   }
174
175   public function setupGlobalExtensionsForTests() {
176     $globalExtension = __DIR__ . '/resources/global-includes';
177     $targetDir = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'global-includes';
178     $this->mkdir($targetDir);
179     $this->recursive_copy($globalExtension, $targetDir);
180     return $targetDir;
181   }
182
183   public function setupModulesForTests($root) {
184     $wootModule = __DIR__ . '/resources/modules/d' . UNISH_DRUPAL_MAJOR_VERSION . '/woot';
185     $targetDir = $root . DIRECTORY_SEPARATOR . $this->drupalSitewideDirectory() . '/modules/woot';
186     $this->mkdir($targetDir);
187     $this->recursive_copy($wootModule, $targetDir);
188   }
189 }