3 use MediaWiki\MainConfigNames
;
4 use MediaWiki\Shell\Command
;
5 use MediaWiki\Shell\Shell
;
8 * @covers \MediaWiki\Shell\Shell
11 class ShellTest
extends MediaWikiIntegrationTestCase
{
13 public function testIsDisabled() {
14 $this->assertIsBool( Shell
::isDisabled() );
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() {
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'" ],
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(
55 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
56 $this->overrideConfigValue( MainConfigNames
::Hooks
, [] );
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() );
71 $this->assertEquals( $expected, $command->getCommandString() );
73 $this->assertSame( [], $command->getDisallowedPaths() );
76 public static function provideMakeScriptCommand() {
79 $IP = MW_INSTALL_PATH
;
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",
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',
94 static function ( &$script, array &$parameters ) {
95 $script = 'changed.php';
96 array_unshift( $parameters, '--wiki=somewiki' );
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' ],