API: Use U+001F (Unit Separator) for separating multi-valued parameters
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBaseTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8 class ApiBaseTest extends ApiTestCase {
9
10 /**
11 * @covers ApiBase::requireOnlyOneParameter
12 */
13 public function testRequireOnlyOneParameterDefault() {
14 $mock = new MockApi();
15 $mock->requireOnlyOneParameter(
16 [ "filename" => "foo.txt", "enablechunks" => false ],
17 "filename", "enablechunks"
18 );
19 $this->assertTrue( true );
20 }
21
22 /**
23 * @expectedException UsageException
24 * @covers ApiBase::requireOnlyOneParameter
25 */
26 public function testRequireOnlyOneParameterZero() {
27 $mock = new MockApi();
28 $mock->requireOnlyOneParameter(
29 [ "filename" => "foo.txt", "enablechunks" => 0 ],
30 "filename", "enablechunks"
31 );
32 }
33
34 /**
35 * @expectedException UsageException
36 * @covers ApiBase::requireOnlyOneParameter
37 */
38 public function testRequireOnlyOneParameterTrue() {
39 $mock = new MockApi();
40 $mock->requireOnlyOneParameter(
41 [ "filename" => "foo.txt", "enablechunks" => true ],
42 "filename", "enablechunks"
43 );
44 }
45
46 /**
47 * @dataProvider provideGetParameterFromSettings
48 * @param string|null $input
49 * @param array $paramSettings
50 * @param mixed $expected
51 * @param string[] $warnings
52 */
53 public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
54 $mock = new MockApi();
55 $wrapper = TestingAccessWrapper::newFromObject( $mock );
56
57 $context = new DerivativeContext( $mock );
58 $context->setRequest( new FauxRequest( $input !== null ? [ 'foo' => $input ] : [] ) );
59 $wrapper->mMainModule = new ApiMain( $context );
60
61 if ( $expected instanceof UsageException ) {
62 try {
63 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
64 } catch ( UsageException $ex ) {
65 $this->assertEquals( $expected, $ex );
66 }
67 } else {
68 $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
69 $this->assertSame( $expected, $result );
70 $this->assertSame( $warnings, $mock->warnings );
71 }
72 }
73
74 public static function provideGetParameterFromSettings() {
75 $c0 = '';
76 $enc = '';
77 for ( $i = 0; $i < 32; $i++ ) {
78 $c0 .= chr( $i );
79 $enc .= ( $i === 9 || $i === 10 || $i === 13 )
80 ? chr( $i )
81 : '�';
82 }
83
84 return [
85 'Basic param' => [ 'bar', null, 'bar', [] ],
86 'String param' => [ 'bar', '', 'bar', [] ],
87 'String param, defaulted' => [ null, '', '', [] ],
88 'String param, empty' => [ '', 'default', '', [] ],
89 'String param, required, empty' => [
90 '',
91 [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
92 new UsageException( 'The foo parameter must be set', 'nofoo' ),
93 []
94 ],
95 'Multi-valued parameter' => [
96 'a|b|c',
97 [ ApiBase::PARAM_ISMULTI => true ],
98 [ 'a', 'b', 'c' ],
99 []
100 ],
101 'Multi-valued parameter, alternative separator' => [
102 "\x1fa|b\x1fc|d",
103 [ ApiBase::PARAM_ISMULTI => true ],
104 [ 'a|b', 'c|d' ],
105 []
106 ],
107 'Multi-valued parameter, other C0 controls' => [
108 $c0,
109 [ ApiBase::PARAM_ISMULTI => true ],
110 [ $enc ],
111 []
112 ],
113 'Multi-valued parameter, other C0 controls (2)' => [
114 "\x1f" . $c0,
115 [ ApiBase::PARAM_ISMULTI => true ],
116 [ substr( $enc, 0, -3 ), '' ],
117 []
118 ],
119 ];
120 }
121
122 }