2016-11-03 00:27:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-01-19 01:44:03 +00:00
|
|
|
use MediaWiki\Shell\Command;
|
2016-11-03 00:27:15 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-09 06:08:28 +00:00
|
|
|
* @covers \MediaWiki\Shell\Shell
|
2016-11-03 00:27:15 +00:00
|
|
|
* @group Shell
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ShellTest extends MediaWikiIntegrationTestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2016-11-03 00:27:15 +00:00
|
|
|
public function testIsDisabled() {
|
2019-12-13 14:29:10 +00:00
|
|
|
$this->assertIsBool( Shell::isDisabled() ); // sanity
|
2016-11-03 00:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideEscape
|
|
|
|
|
*/
|
|
|
|
|
public function testEscape( $args, $expected ) {
|
|
|
|
|
if ( wfIsWindows() ) {
|
|
|
|
|
$this->markTestSkipped( 'This test requires a POSIX environment.' );
|
|
|
|
|
}
|
2020-05-29 06:46:30 +00:00
|
|
|
$this->assertSame( $expected, Shell::escape( ...$args ) );
|
2016-11-03 00:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideEscape() {
|
|
|
|
|
return [
|
|
|
|
|
'simple' => [ [ 'true' ], "'true'" ],
|
|
|
|
|
'with args' => [ [ 'convert', '-font', 'font name' ], "'convert' '-font' 'font name'" ],
|
|
|
|
|
'array' => [ [ [ 'convert', '-font', 'font name' ] ], "'convert' '-font' 'font name'" ],
|
2017-11-29 02:51:25 +00:00
|
|
|
'skip nulls' => [ [ 'ls', null ], "'ls'" ],
|
2016-11-03 00:27:15 +00:00
|
|
|
];
|
|
|
|
|
}
|
2018-01-19 01:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Shell\Shell::makeScriptCommand
|
|
|
|
|
* @dataProvider provideMakeScriptCommand
|
|
|
|
|
*
|
2021-01-09 11:03:10 +00:00
|
|
|
* @param string $expected expected in POSIX
|
2018-11-06 00:36:37 +00:00
|
|
|
* @param string $expectedWin expected in Windows
|
2018-01-19 01:44:03 +00:00
|
|
|
* @param string $script
|
|
|
|
|
* @param string[] $parameters
|
|
|
|
|
* @param string[] $options
|
|
|
|
|
* @param callable|null $hook
|
|
|
|
|
*/
|
2018-11-06 10:57:17 +00:00
|
|
|
public function testMakeScriptCommand(
|
|
|
|
|
$expected,
|
2018-11-06 00:36:37 +00:00
|
|
|
$expectedWin,
|
2018-11-06 10:57:17 +00:00
|
|
|
$script,
|
|
|
|
|
$parameters,
|
|
|
|
|
$options = [],
|
|
|
|
|
$hook = null
|
2018-01-19 01:44:03 +00:00
|
|
|
) {
|
|
|
|
|
// Running tests under Vagrant involves MWMultiVersion that uses the below hook
|
|
|
|
|
$this->setMwGlobals( 'wgHooks', [] );
|
|
|
|
|
|
|
|
|
|
if ( $hook ) {
|
|
|
|
|
$this->setTemporaryHook( 'wfShellWikiCmd', $hook );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$command = Shell::makeScriptCommand( $script, $parameters, $options );
|
|
|
|
|
$command->params( 'safe' )
|
|
|
|
|
->unsafeParams( 'unsafe' );
|
|
|
|
|
|
2019-12-13 18:21:35 +00:00
|
|
|
$this->assertInstanceOf( Command::class, $command );
|
2018-01-19 01:44:03 +00:00
|
|
|
|
2018-11-06 00:36:37 +00:00
|
|
|
if ( wfIsWindows() ) {
|
2020-09-11 03:21:24 +00:00
|
|
|
$this->assertEquals( $expectedWin, $command->getCommandString() );
|
2018-11-06 00:36:37 +00:00
|
|
|
} else {
|
2020-09-11 03:21:24 +00:00
|
|
|
$this->assertEquals( $expected, $command->getCommandString() );
|
2018-11-06 00:36:37 +00:00
|
|
|
}
|
2020-09-11 03:21:24 +00:00
|
|
|
$this->assertSame( [], $command->getDisallowedPaths() );
|
2018-01-19 01:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideMakeScriptCommand() {
|
|
|
|
|
global $wgPhpCli;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
"'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
|
2018-11-06 00:36:37 +00:00
|
|
|
'"' . $wgPhpCli . '" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
|
2018-01-19 01:44:03 +00:00
|
|
|
'maintenance/foobar.php',
|
|
|
|
|
[ 'bar\'"baz' ],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
|
2018-11-06 00:36:37 +00:00
|
|
|
'"' . $wgPhpCli . '" "changed.php" "--wiki=somewiki" "bar\'\\"baz" "safe" unsafe',
|
2018-01-19 01:44:03 +00:00
|
|
|
'maintenance/foobar.php',
|
|
|
|
|
[ 'bar\'"baz' ],
|
|
|
|
|
[],
|
|
|
|
|
function ( &$script, array &$parameters ) {
|
|
|
|
|
$script = 'changed.php';
|
|
|
|
|
array_unshift( $parameters, '--wiki=somewiki' );
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
|
2018-11-06 00:36:37 +00:00
|
|
|
'"/bin/perl" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
|
2018-01-19 01:44:03 +00:00
|
|
|
'maintenance/foobar.php',
|
|
|
|
|
[ 'bar\'"baz' ],
|
|
|
|
|
[ 'php' => '/bin/perl' ],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
|
2018-11-06 00:36:37 +00:00
|
|
|
'"' . $wgPhpCli . '" "foobinize" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
|
2018-01-19 01:44:03 +00:00
|
|
|
'maintenance/foobar.php',
|
|
|
|
|
[ 'bar\'"baz' ],
|
|
|
|
|
[ 'wrapper' => 'foobinize' ],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2016-11-03 00:27:15 +00:00
|
|
|
}
|