bf46f44b0c674b74b6ad6307f732dbdf2c6bd919
[lhc/web/wiklou.git] / tests / phpunit / includes / shell / ShellTest.php
1 <?php
2
3 use MediaWiki\Shell\Command;
4 use MediaWiki\Shell\Shell;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @covers \MediaWiki\Shell\Shell
9 * @group Shell
10 */
11 class ShellTest extends MediaWikiTestCase {
12
13 use MediaWikiCoversValidator;
14
15 public function testIsDisabled() {
16 $this->assertInternalType( 'bool', Shell::isDisabled() ); // sanity
17 }
18
19 /**
20 * @dataProvider provideEscape
21 */
22 public function testEscape( $args, $expected ) {
23 if ( wfIsWindows() ) {
24 $this->markTestSkipped( 'This test requires a POSIX environment.' );
25 }
26 $this->assertSame( $expected, call_user_func_array( [ Shell::class, 'escape' ], $args ) );
27 }
28
29 public function provideEscape() {
30 return [
31 'simple' => [ [ 'true' ], "'true'" ],
32 'with args' => [ [ 'convert', '-font', 'font name' ], "'convert' '-font' 'font name'" ],
33 'array' => [ [ [ 'convert', '-font', 'font name' ] ], "'convert' '-font' 'font name'" ],
34 'skip nulls' => [ [ 'ls', null ], "'ls'" ],
35 ];
36 }
37
38 /**
39 * @covers \MediaWiki\Shell\Shell::makeScriptCommand
40 * @dataProvider provideMakeScriptCommand
41 *
42 * @param string $expected
43 * @param string $script
44 * @param string[] $parameters
45 * @param string[] $options
46 * @param callable|null $hook
47 */
48 public function testMakeScriptCommand( $expected,
49 $script,
50 $parameters,
51 $options = [],
52 $hook = null
53 ) {
54 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
55 $this->setMwGlobals( 'wgHooks', [] );
56
57 if ( $hook ) {
58 $this->setTemporaryHook( 'wfShellWikiCmd', $hook );
59 }
60
61 $command = Shell::makeScriptCommand( $script, $parameters, $options );
62 $command->params( 'safe' )
63 ->unsafeParams( 'unsafe' );
64
65 $this->assertType( Command::class, $command );
66
67 $wrapper = TestingAccessWrapper::newFromObject( $command );
68 $this->assertEquals( $expected, $wrapper->command );
69 $this->assertEquals( 0, $wrapper->restrictions & Shell::NO_LOCALSETTINGS );
70 }
71
72 public function provideMakeScriptCommand() {
73 global $wgPhpCli;
74
75 return [
76 [
77 "'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
78 'maintenance/foobar.php',
79 [ 'bar\'"baz' ],
80 ],
81 [
82 "'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
83 'maintenance/foobar.php',
84 [ 'bar\'"baz' ],
85 [],
86 function ( &$script, array &$parameters ) {
87 $script = 'changed.php';
88 array_unshift( $parameters, '--wiki=somewiki' );
89 }
90 ],
91 [
92 "'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
93 'maintenance/foobar.php',
94 [ 'bar\'"baz' ],
95 [ 'php' => '/bin/perl' ],
96 ],
97 [
98 "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
99 'maintenance/foobar.php',
100 [ 'bar\'"baz' ],
101 [ 'wrapper' => 'foobinize' ],
102 ],
103 ];
104 }
105 }