summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/shell/ShellTest.php
blob: bf46f44b0c674b74b6ad6307f732dbdf2c6bd919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

use MediaWiki\Shell\Command;
use MediaWiki\Shell\Shell;
use Wikimedia\TestingAccessWrapper;

/**
 * @covers \MediaWiki\Shell\Shell
 * @group Shell
 */
class ShellTest extends MediaWikiTestCase {

	use MediaWikiCoversValidator;

	public function testIsDisabled() {
		$this->assertInternalType( 'bool', Shell::isDisabled() ); // sanity
	}

	/**
	 * @dataProvider provideEscape
	 */
	public function testEscape( $args, $expected ) {
		if ( wfIsWindows() ) {
			$this->markTestSkipped( 'This test requires a POSIX environment.' );
		}
		$this->assertSame( $expected, call_user_func_array( [ Shell::class, '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->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' ],
			],
		];
	}
}