Debian Codegraph @

tests/phpunit/includes/shell/ShellTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Shell\Command;
5 use MediaWiki\Shell\Shell;
6
7 /**
8 * @covers \MediaWiki\Shell\Shell
9 * @group Shell
10 */
11 class ShellTest extends MediaWikiIntegrationTestCase {
12
13 public function testIsDisabled() {
14 $this->assertIsBool( Shell::isDisabled() );
15 }
16
17 /**
18 * @dataProvider provideEscape
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 * @covers \MediaWiki\Shell\Shell::makeScriptCommand
38 * @dataProvider provideMakeScriptCommand
39 *
40 * @param string $expected expected in POSIX
41 * @param string $expectedWin expected in Windows
42 * @param string $script
43 * @param string[] $parameters
44 * @param string[] $options
45 * @param callable|null $hook
46 */
47 public function testMakeScriptCommand(
48 $expected,
49 $expectedWin,
50 $script,
51 $parameters,
52 $options = [],
53 $hook = null
54 ) {
55 // Running tests under Vagrant involves MWMultiVersion that uses the below hook
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 }