Merge "registration: Only allow one extension to set a specific config setting"
[lhc/web/wiklou.git] / tests / phpunit / includes / shell / CommandTest.php
1 <?php
2
3 use MediaWiki\Shell\Command;
4
5 /**
6 * @group Shell
7 */
8 class CommandTest extends PHPUnit_Framework_TestCase {
9 private function requirePosix() {
10 if ( wfIsWindows() ) {
11 $this->markTestSkipped( 'This test requires a POSIX environment.' );
12 }
13 }
14
15 /**
16 * @dataProvider provideExecute
17 */
18 public function testExecute( $commandInput, $expectedExitCode, $expectedOutput ) {
19 $this->requirePosix();
20
21 $command = new Command();
22 $result = $command
23 ->params( $commandInput )
24 ->execute();
25
26 $this->assertSame( $expectedExitCode, $result->getExitCode() );
27 $this->assertSame( $expectedOutput, $result->getStdout() );
28 }
29
30 public function provideExecute() {
31 return [
32 'success status' => [ 'true', 0, '' ],
33 'failure status' => [ 'false', 1, '' ],
34 'output' => [ [ 'echo', '-n', 'x', '>', 'y' ], 0, 'x > y' ],
35 ];
36 }
37
38 public function testEnvironment() {
39 $this->requirePosix();
40
41 $command = new Command();
42 $result = $command
43 ->params( [ 'printenv', 'FOO' ] )
44 ->environment( [ 'FOO' => 'bar' ] )
45 ->execute();
46 $this->assertSame( "bar\n", $result->getStdout() );
47 }
48
49 public function testStdout() {
50 $this->requirePosix();
51
52 $command = new Command();
53
54 $result = $command
55 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
56 ->execute();
57
58 $this->assertNotContains( 'ThisIsStderr', $result->getStdout() );
59 $this->assertEquals( "ThisIsStderr\n", $result->getStderr() );
60 }
61
62 public function testStdoutRedirection() {
63 $this->requirePosix();
64
65 $command = new Command();
66
67 $result = $command
68 ->params( 'bash', '-c', 'echo ThisIsStderr 1>&2' )
69 ->includeStderr( true )
70 ->execute();
71
72 $this->assertEquals( "ThisIsStderr\n", $result->getStdout() );
73 $this->assertNull( $result->getStderr() );
74 }
75
76 public function testOutput() {
77 global $IP;
78
79 $this->requirePosix();
80 chdir( $IP );
81
82 $command = new Command();
83 $result = $command
84 ->params( [ 'ls', 'index.php' ] )
85 ->execute();
86 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
87 $this->assertSame( null, $result->getStderr() );
88
89 $command = new Command();
90 $result = $command
91 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
92 ->includeStderr()
93 ->execute();
94 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
95 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStdout() );
96 $this->assertSame( null, $result->getStderr() );
97
98 $command = new Command();
99 $result = $command
100 ->params( [ 'ls', 'index.php', 'no-such-file' ] )
101 ->execute();
102 $this->assertRegExp( '/^index.php$/m', $result->getStdout() );
103 $this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
104 }
105
106 public function testT69870() {
107 $commandLine = wfIsWindows()
108 // 333 = 331 + CRLF
109 ? ( 'for /l %i in (1, 1, 1001) do @echo ' . str_repeat( '*', 331 ) )
110 : 'printf "%-333333s" "*"';
111
112 // Test several times because it involves a race condition that may randomly succeed or fail
113 for ( $i = 0; $i < 10; $i++ ) {
114 $command = new Command();
115 $output = $command->unsafeParams( $commandLine )
116 ->execute()
117 ->getStdout();
118 $this->assertEquals( 333333, strlen( $output ) );
119 }
120 }
121 }