<?php
+use MediaWiki\Shell\Command;
use MediaWiki\Shell\Shell;
+use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Shell\Shell
* @group Shell
*/
-class ShellTest extends PHPUnit\Framework\TestCase {
+class ShellTest extends MediaWikiTestCase {
use MediaWikiCoversValidator;
'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->assertType( Command::class, $command );
+
+ $wrapper = TestingAccessWrapper::newFromObject( $command );
+ $this->assertEquals( $expected, $wrapper->command );
+ $this->assertEquals( 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' ],
+ ],
+ ];
+ }
}