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