Merge "Revert "Use display name in category page subheadings if provided""
[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 $warnings = [
76 'The value passed for \'foo\' contains invalid or non-normalized data. Textual data should ' .
77 'be valid, NFC-normalized Unicode without C0 control characters other than ' .
78 'HT (\\t), LF (\\n), and CR (\\r).'
79 ];
80
81 $c0 = '';
82 $enc = '';
83 for ( $i = 0; $i < 32; $i++ ) {
84 $c0 .= chr( $i );
85 $enc .= ( $i === 9 || $i === 10 || $i === 13 )
86 ? chr( $i )
87 : '�';
88 }
89
90 return [
91 'Basic param' => [ 'bar', null, 'bar', [] ],
92 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
93 'String param' => [ 'bar', '', 'bar', [] ],
94 'String param, defaulted' => [ null, '', '', [] ],
95 'String param, empty' => [ '', 'default', '', [] ],
96 'String param, required, empty' => [
97 '',
98 [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
99 new UsageException( 'The foo parameter must be set', 'nofoo' ),
100 []
101 ],
102 'Multi-valued parameter' => [
103 'a|b|c',
104 [ ApiBase::PARAM_ISMULTI => true ],
105 [ 'a', 'b', 'c' ],
106 []
107 ],
108 'Multi-valued parameter, alternative separator' => [
109 "\x1fa|b\x1fc|d",
110 [ ApiBase::PARAM_ISMULTI => true ],
111 [ 'a|b', 'c|d' ],
112 []
113 ],
114 'Multi-valued parameter, other C0 controls' => [
115 $c0,
116 [ ApiBase::PARAM_ISMULTI => true ],
117 [ $enc ],
118 $warnings
119 ],
120 'Multi-valued parameter, other C0 controls (2)' => [
121 "\x1f" . $c0,
122 [ ApiBase::PARAM_ISMULTI => true ],
123 [ substr( $enc, 0, -3 ), '' ],
124 $warnings
125 ],
126 ];
127 }
128
129 }