3 use MediaWiki\Shell\Command
;
4 use MediaWiki\Shell\Shell
;
5 use Wikimedia\TestingAccessWrapper
;
8 * @covers \MediaWiki\Shell\Shell
11 class ShellTest
extends MediaWikiTestCase
{
13 use MediaWikiCoversValidator
;
15 public function testIsDisabled() {
16 $this->assertInternalType( 'bool', Shell
::isDisabled() ); // sanity
20 * @dataProvider provideEscape
22 public function testEscape( $args, $expected ) {
23 if ( wfIsWindows() ) {
24 $this->markTestSkipped( 'This test requires a POSIX environment.' );
26 $this->assertSame( $expected, call_user_func_array( [ Shell
::class, 'escape' ], $args ) );
29 public function provideEscape() {
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'" ],
39 * @covers \MediaWiki\Shell\Shell::makeScriptCommand
40 * @dataProvider provideMakeScriptCommand
42 * @param string $expected
43 * @param string $script
44 * @param string[] $parameters
45 * @param string[] $options
46 * @param callable|null $hook
48 public function testMakeScriptCommand( $expected,
54 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
55 $this->setMwGlobals( 'wgHooks', [] );
58 $this->setTemporaryHook( 'wfShellWikiCmd', $hook );
61 $command = Shell
::makeScriptCommand( $script, $parameters, $options );
62 $command->params( 'safe' )
63 ->unsafeParams( 'unsafe' );
65 $this->assertType( Command
::class, $command );
67 $wrapper = TestingAccessWrapper
::newFromObject( $command );
68 $this->assertEquals( $expected, $wrapper->command
);
69 $this->assertEquals( 0, $wrapper->restrictions
& Shell
::NO_LOCALSETTINGS
);
72 public function provideMakeScriptCommand() {
77 "'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
78 'maintenance/foobar.php',
82 "'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
83 'maintenance/foobar.php',
86 function ( &$script, array &$parameters ) {
87 $script = 'changed.php';
88 array_unshift( $parameters, '--wiki=somewiki' );
92 "'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
93 'maintenance/foobar.php',
95 [ 'php' => '/bin/perl' ],
98 "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
99 'maintenance/foobar.php',
101 [ 'wrapper' => 'foobinize' ],