Merge "docs: Fix typo"
[mediawiki.git] / tests / phpunit / includes / shell / ShellTest.php
blob8c2c29ffb5b482bcfa1185ec01cadc1932ef6f66
1 <?php
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Shell\Command;
5 use MediaWiki\Shell\Shell;
7 /**
8 * @covers \MediaWiki\Shell\Shell
9 * @group Shell
11 class ShellTest extends MediaWikiIntegrationTestCase {
13 public function testIsDisabled() {
14 $this->assertIsBool( Shell::isDisabled() );
17 /**
18 * @dataProvider provideEscape
20 public function testEscape( $args, $expected ) {
21 if ( wfIsWindows() ) {
22 $this->markTestSkipped( 'This test requires a POSIX environment.' );
24 $this->assertSame( $expected, Shell::escape( ...$args ) );
27 public static 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'" ],
36 /**
37 * @covers \MediaWiki\Shell\Shell::makeScriptCommand
38 * @dataProvider provideMakeScriptCommand
40 * @param string $expected expected in POSIX
41 * @param string $expectedWin expected in Windows
42 * @param string $script
43 * @param string[] $parameters
44 * @param string[] $options
45 * @param callable|null $hook
47 public function testMakeScriptCommand(
48 $expected,
49 $expectedWin,
50 $script,
51 $parameters,
52 $options = [],
53 $hook = null
54 ) {
55 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
56 $this->overrideConfigValue( MainConfigNames::Hooks, [] );
58 if ( $hook ) {
59 $this->setTemporaryHook( 'wfShellWikiCmd', $hook );
62 $command = Shell::makeScriptCommand( $script, $parameters, $options );
63 $command->params( 'safe' )
64 ->unsafeParams( 'unsafe' );
66 $this->assertInstanceOf( Command::class, $command );
68 if ( wfIsWindows() ) {
69 $this->assertEquals( $expectedWin, $command->getCommandString() );
70 } else {
71 $this->assertEquals( $expected, $command->getCommandString() );
73 $this->assertSame( [], $command->getDisallowedPaths() );
76 public static function provideMakeScriptCommand() {
77 global $wgPhpCli;
79 $IP = MW_INSTALL_PATH;
81 return [
82 'no option' => [
83 "'$wgPhpCli' '$IP/maintenance/run.php' '$IP/maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
84 "\"$wgPhpCli\" \"$IP/maintenance/run.php\" \"$IP/maintenance/foobar.php\" \"bar'\\\"baz\" \"safe\" unsafe",
85 "$IP/maintenance/foobar.php",
86 [ 'bar\'"baz' ],
88 'hook' => [
89 "'$wgPhpCli' '$IP/maintenance/run.php' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
90 "\"$wgPhpCli\" \"$IP/maintenance/run.php\" \"changed.php\" \"--wiki=somewiki\" \"bar'\\\"baz\" \"safe\" unsafe",
91 'maintenance/foobar.php',
92 [ 'bar\'"baz' ],
93 [],
94 static function ( &$script, array &$parameters ) {
95 $script = 'changed.php';
96 array_unshift( $parameters, '--wiki=somewiki' );
99 'php option' => [
100 "'/bin/perl' '$IP/maintenance/run.php' 'maintenance/foobar.php' 'safe' unsafe",
101 "\"/bin/perl\" \"$IP/maintenance/run.php\" \"maintenance/foobar.php\" \"safe\" unsafe",
102 "maintenance/foobar.php",
104 [ 'php' => '/bin/perl' ],
106 'wrapper option' => [
107 "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'safe' unsafe",
108 "\"$wgPhpCli\" \"foobinize\" \"maintenance/foobar.php\" \"safe\" unsafe",
109 "maintenance/foobar.php",
111 [ 'wrapper' => 'foobinize' ],