wiki.techinc.nl/tests/phpunit/includes/shell/ShellTest.php
Thiemo Kreuz 6aa6d10e86 Replace all call_user_func(_array) in all tests
There is native support for all of this now in PHP, thanks to changes
and additions that have been made in later versions. There should be no
need any more to ever use call_user_func() or call_user_func_array().

Reviewing this should be fairly easy: Because this patch touches
exclusivly tests, but no production code, there is no such thing as
"insufficent test coverage". As long as CI goes green, this should be
fine.

Change-Id: Ib9690103687734bb5a85d3dab0e5642a07087bbc
2020-06-06 18:41:20 +02:00

104 lines
2.6 KiB
PHP

<?php
use MediaWiki\Shell\Command;
use MediaWiki\Shell\Shell;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Shell\Shell
* @group Shell
*/
class ShellTest extends MediaWikiTestCase {
public function testIsDisabled() {
$this->assertIsBool( Shell::isDisabled() ); // sanity
}
/**
* @dataProvider provideEscape
*/
public function testEscape( $args, $expected ) {
if ( wfIsWindows() ) {
$this->markTestSkipped( 'This test requires a POSIX environment.' );
}
$this->assertSame( $expected, Shell::escape( ...$args ) );
}
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'" ],
'skip nulls' => [ [ 'ls', null ], "'ls'" ],
];
}
/**
* @covers \MediaWiki\Shell\Shell::makeScriptCommand
* @dataProvider provideMakeScriptCommand
*
* @param string $expected
* @param string $script
* @param string[] $parameters
* @param string[] $options
* @param callable|null $hook
*/
public function testMakeScriptCommand(
$expected,
$script,
$parameters,
$options = [],
$hook = null
) {
// 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' );
$this->assertInstanceOf( Command::class, $command );
$wrapper = TestingAccessWrapper::newFromObject( $command );
$this->assertEquals( $expected, $wrapper->command );
$this->assertSame( 0, $wrapper->restrictions & Shell::NO_LOCALSETTINGS );
}
public function provideMakeScriptCommand() {
global $wgPhpCli;
return [
[
"'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
'maintenance/foobar.php',
[ 'bar\'"baz' ],
],
[
"'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
'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",
'maintenance/foobar.php',
[ 'bar\'"baz' ],
[ 'php' => '/bin/perl' ],
],
[
"'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
'maintenance/foobar.php',
[ 'bar\'"baz' ],
[ 'wrapper' => 'foobinize' ],
],
];
}
}