MediaWiki REL1_31
ShellTest.php
Go to the documentation of this file.
1<?php
2
5use Wikimedia\TestingAccessWrapper;
6
12
13 use MediaWikiCoversValidator;
14
15 public function testIsDisabled() {
16 $this->assertInternalType( 'bool', Shell::isDisabled() ); // sanity
17 }
18
22 public function testEscape( $args, $expected ) {
23 if ( wfIsWindows() ) {
24 $this->markTestSkipped( 'This test requires a POSIX environment.' );
25 }
26 $this->assertSame( $expected, call_user_func_array( [ Shell::class, 'escape' ], $args ) );
27 }
28
29 public function provideEscape() {
30 return [
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'" ],
35 ];
36 }
37
49 public function testMakeScriptCommand(
50 $expected,
51 $expectedWin,
52 $script,
53 $parameters,
54 $options = [],
55 $hook = null
56 ) {
57 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
58 $this->setMwGlobals( 'wgHooks', [] );
59
60 if ( $hook ) {
61 $this->setTemporaryHook( 'wfShellWikiCmd', $hook );
62 }
63
64 $command = Shell::makeScriptCommand( $script, $parameters, $options );
65 $command->params( 'safe' )
66 ->unsafeParams( 'unsafe' );
67
68 $this->assertType( Command::class, $command );
69
70 $wrapper = TestingAccessWrapper::newFromObject( $command );
71
72 if ( wfIsWindows() ) {
73 $this->assertEquals( $expectedWin, $wrapper->command );
74 } else {
75 $this->assertEquals( $expected, $wrapper->command );
76 }
77 $this->assertSame( 0, $wrapper->restrictions & Shell::NO_LOCALSETTINGS );
78 }
79
80 public function provideMakeScriptCommand() {
81 global $wgPhpCli;
82
83 return [
84 [
85 "'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
86 '"' . $wgPhpCli . '" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
87 'maintenance/foobar.php',
88 [ 'bar\'"baz' ],
89 ],
90 [
91 "'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
92 '"' . $wgPhpCli . '" "changed.php" "--wiki=somewiki" "bar\'\\"baz" "safe" unsafe',
93 'maintenance/foobar.php',
94 [ 'bar\'"baz' ],
95 [],
96 function ( &$script, array &$parameters ) {
97 $script = 'changed.php';
98 array_unshift( $parameters, '--wiki=somewiki' );
99 }
100 ],
101 [
102 "'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
103 '"/bin/perl" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
104 'maintenance/foobar.php',
105 [ 'bar\'"baz' ],
106 [ 'php' => '/bin/perl' ],
107 ],
108 [
109 "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
110 '"' . $wgPhpCli . '" "foobinize" "maintenance/foobar.php" "bar\'\\"baz" "safe" unsafe',
111 'maintenance/foobar.php',
112 [ 'bar\'"baz' ],
113 [ 'wrapper' => 'foobinize' ],
114 ],
115 ];
116 }
117}
$wgPhpCli
Executable path of the PHP cli binary.
wfIsWindows()
Check if the operating system is Windows.
$command
Definition cdb.php:65
if( $line===false) $args
Definition cdb.php:64
assertType( $type, $actual, $message='')
Asserts the type of the provided value.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setTemporaryHook( $hookName, $handler)
Create a temporary hook handler which will be reset by tearDown.
Class used for executing shell commands.
Definition Command.php:35
Executes shell commands.
Definition Shell.php:44
\MediaWiki\Shell\Shell Shell
Definition ShellTest.php:11
provideEscape()
Definition ShellTest.php:29
testIsDisabled()
Definition ShellTest.php:15
testEscape( $args, $expected)
provideEscape
Definition ShellTest.php:22
testMakeScriptCommand( $expected, $expectedWin, $script, $parameters, $options=[], $hook=null)
\MediaWiki\Shell\Shell::makeScriptCommand provideMakeScriptCommand
Definition ShellTest.php:49
provideMakeScriptCommand()
Definition ShellTest.php:80
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001