| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Shell\Command; |
| 5 |
use MediaWiki\Shell\Shell; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ShellTest extends MediaWikiIntegrationTestCase { |
| 12 |
|
| 13 |
public function testIsDisabled() { |
| 14 |
$this->assertIsBool( Shell::isDisabled() ); |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testEscape( $args, $expected ) { |
| 21 |
if ( wfIsWindows() ) { |
| 22 |
$this->markTestSkipped( 'This test requires a POSIX environment.' ); |
| 23 |
} |
| 24 |
$this->assertSame( $expected, Shell::escape( ...$args ) ); |
| 25 |
} |
| 26 |
|
| 27 |
public static function provideEscape() { |
| 28 |
return [ |
| 29 |
'simple' => [ [ 'true' ], "'true'" ], |
| 30 |
'with args' => [ [ 'convert', '-font', 'font name' ], "'convert' '-font' 'font name'" ], |
| 31 |
'array' => [ [ [ 'convert', '-font', 'font name' ] ], "'convert' '-font' 'font name'" ], |
| 32 |
'skip nulls' => [ [ 'ls', null ], "'ls'" ], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function testMakeScriptCommand( |
| 48 |
$expected, |
| 49 |
$expectedWin, |
| 50 |
$script, |
| 51 |
$parameters, |
| 52 |
$options = [], |
| 53 |
$hook = null |
| 54 |
) { |
| 55 |
|
| 56 |
$this->overrideConfigValue( MainConfigNames::Hooks, [] ); |
| 57 |
|
| 58 |
if ( $hook ) { |
| 59 |
$this->setTemporaryHook( 'wfShellWikiCmd', $hook ); |
| 60 |
} |
| 61 |
|
| 62 |
$command = Shell::makeScriptCommand( $script, $parameters, $options ); |
| 63 |
$command->params( 'safe' ) |
| 64 |
->unsafeParams( 'unsafe' ); |
| 65 |
|
| 66 |
$this->assertInstanceOf( Command::class, $command ); |
| 67 |
|
| 68 |
if ( wfIsWindows() ) { |
| 69 |
$this->assertEquals( $expectedWin, $command->getCommandString() ); |
| 70 |
} else { |
| 71 |
$this->assertEquals( $expected, $command->getCommandString() ); |
| 72 |
} |
| 73 |
$this->assertSame( [], $command->getDisallowedPaths() ); |
| 74 |
} |
| 75 |
|
| 76 |
public static function provideMakeScriptCommand() { |
| 77 |
global $wgPhpCli; |
| 78 |
|
| 79 |
$IP = MW_INSTALL_PATH; |
| 80 |
|
| 81 |
return [ |
| 82 |
'no option' => [ |
| 83 |
"'$wgPhpCli' '$IP/maintenance/run.php' '$IP/maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe", |
| 84 |
"\"$wgPhpCli\" \"$IP/maintenance/run.php\" \"$IP/maintenance/foobar.php\" \"bar'\\\"baz\" \"safe\" unsafe", |
| 85 |
"$IP/maintenance/foobar.php", |
| 86 |
[ 'bar\'"baz' ], |
| 87 |
], |
| 88 |
'hook' => [ |
| 89 |
"'$wgPhpCli' '$IP/maintenance/run.php' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe", |
| 90 |
"\"$wgPhpCli\" \"$IP/maintenance/run.php\" \"changed.php\" \"--wiki=somewiki\" \"bar'\\\"baz\" \"safe\" unsafe", |
| 91 |
'maintenance/foobar.php', |
| 92 |
[ 'bar\'"baz' ], |
| 93 |
[], |
| 94 |
static function ( &$script, array &$parameters ) { |
| 95 |
$script = 'changed.php'; |
| 96 |
array_unshift( $parameters, '--wiki=somewiki' ); |
| 97 |
} |
| 98 |
], |
| 99 |
'php option' => [ |
| 100 |
"'/bin/perl' '$IP/maintenance/run.php' 'maintenance/foobar.php' 'safe' unsafe", |
| 101 |
"\"/bin/perl\" \"$IP/maintenance/run.php\" \"maintenance/foobar.php\" \"safe\" unsafe", |
| 102 |
"maintenance/foobar.php", |
| 103 |
[], |
| 104 |
[ 'php' => '/bin/perl' ], |
| 105 |
], |
| 106 |
'wrapper option' => [ |
| 107 |
"'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'safe' unsafe", |
| 108 |
"\"$wgPhpCli\" \"foobinize\" \"maintenance/foobar.php\" \"safe\" unsafe", |
| 109 |
"maintenance/foobar.php", |
| 110 |
[], |
| 111 |
[ 'wrapper' => 'foobinize' ], |
| 112 |
], |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
} |