Add @covers for RemexStripTagHandler
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / MagicVariableTest.php
1 <?php
2 /**
3 * This file is intended to test magic variables in the parser
4 * It was inspired by Raymond & Matěj Grabovský commenting about r66200
5 *
6 * As of february 2011, it only tests some revisions and date related
7 * magic variables.
8 *
9 * @author Antoine Musso
10 * @copyright Copyright © 2011, Antoine Musso
11 * @file
12 *
13 * @group Database
14 */
15
16 /**
17 * @covers Parser::getVariableValue
18 */
19 class MagicVariableTest extends MediaWikiTestCase {
20 /**
21 * @var Parser
22 */
23 private $testParser = null;
24
25 /**
26 * An array of magicword returned as type integer by the parser
27 * They are usually returned as a string for i18n since we support
28 * persan numbers for example, but some magic explicitly return
29 * them as integer.
30 * @see MagicVariableTest::assertMagic()
31 */
32 private $expectedAsInteger = [
33 'revisionday',
34 'revisionmonth1',
35 ];
36
37 /** setup a basic parser object */
38 protected function setUp() {
39 parent::setUp();
40
41 $contLang = Language::factory( 'en' );
42 $this->setMwGlobals( [
43 'wgLanguageCode' => 'en',
44 'wgContLang' => $contLang,
45 ] );
46
47 $this->testParser = new Parser();
48 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
49
50 # initialize parser output
51 $this->testParser->clearState();
52
53 # Needs a title to do magic word stuff
54 $title = Title::newFromText( 'Tests' );
55 # Else it needs a db connection just to check if it's a redirect
56 # (when deciding the page language).
57 $title->mRedirect = false;
58
59 $this->testParser->setTitle( $title );
60 }
61
62 /**
63 * @param int $num Upper limit for numbers
64 * @return array Array of numbers from 1 up to $num
65 */
66 private static function createProviderUpTo( $num ) {
67 $ret = [];
68 for ( $i = 1; $i <= $num; $i++ ) {
69 $ret[] = [ $i ];
70 }
71
72 return $ret;
73 }
74
75 /**
76 * @return array Array of months numbers (as an integer)
77 */
78 public static function provideMonths() {
79 return self::createProviderUpTo( 12 );
80 }
81
82 /**
83 * @return array Array of days numbers (as an integer)
84 */
85 public static function provideDays() {
86 return self::createProviderUpTo( 31 );
87 }
88
89 # ############## TESTS #############################################
90 # @todo FIXME:
91 # - those got copy pasted, we can probably make them cleaner
92 # - tests are lacking useful messages
93
94 # day
95
96 /** @dataProvider provideDays */
97 public function testCurrentdayIsUnPadded( $day ) {
98 $this->assertUnPadded( 'currentday', $day );
99 }
100
101 /** @dataProvider provideDays */
102 public function testCurrentdaytwoIsZeroPadded( $day ) {
103 $this->assertZeroPadded( 'currentday2', $day );
104 }
105
106 /** @dataProvider provideDays */
107 public function testLocaldayIsUnPadded( $day ) {
108 $this->assertUnPadded( 'localday', $day );
109 }
110
111 /** @dataProvider provideDays */
112 public function testLocaldaytwoIsZeroPadded( $day ) {
113 $this->assertZeroPadded( 'localday2', $day );
114 }
115
116 # month
117
118 /** @dataProvider provideMonths */
119 public function testCurrentmonthIsZeroPadded( $month ) {
120 $this->assertZeroPadded( 'currentmonth', $month );
121 }
122
123 /** @dataProvider provideMonths */
124 public function testCurrentmonthoneIsUnPadded( $month ) {
125 $this->assertUnPadded( 'currentmonth1', $month );
126 }
127
128 /** @dataProvider provideMonths */
129 public function testLocalmonthIsZeroPadded( $month ) {
130 $this->assertZeroPadded( 'localmonth', $month );
131 }
132
133 /** @dataProvider provideMonths */
134 public function testLocalmonthoneIsUnPadded( $month ) {
135 $this->assertUnPadded( 'localmonth1', $month );
136 }
137
138 # revision day
139
140 /** @dataProvider provideDays */
141 public function testRevisiondayIsUnPadded( $day ) {
142 $this->assertUnPadded( 'revisionday', $day );
143 }
144
145 /** @dataProvider provideDays */
146 public function testRevisiondaytwoIsZeroPadded( $day ) {
147 $this->assertZeroPadded( 'revisionday2', $day );
148 }
149
150 # revision month
151
152 /** @dataProvider provideMonths */
153 public function testRevisionmonthIsZeroPadded( $month ) {
154 $this->assertZeroPadded( 'revisionmonth', $month );
155 }
156
157 /** @dataProvider provideMonths */
158 public function testRevisionmonthoneIsUnPadded( $month ) {
159 $this->assertUnPadded( 'revisionmonth1', $month );
160 }
161
162 # ############## HELPERS ############################################
163
164 /** assertion helper expecting a magic output which is zero padded */
165 public function assertZeroPadded( $magic, $value ) {
166 $this->assertMagicPadding( $magic, $value, '%02d' );
167 }
168
169 /** assertion helper expecting a magic output which is unpadded */
170 public function assertUnPadded( $magic, $value ) {
171 $this->assertMagicPadding( $magic, $value, '%d' );
172 }
173
174 /**
175 * Main assertion helper for magic variables padding
176 * @param string $magic Magic variable name
177 * @param mixed $value Month or day
178 * @param string $format Sprintf format for $value
179 */
180 private function assertMagicPadding( $magic, $value, $format ) {
181 # Initialize parser timestamp as year 2010 at 12h34 56s.
182 # month and day are given by the caller ($value). Month < 12!
183 if ( $value > 12 ) {
184 $month = $value % 12;
185 } else {
186 $month = $value;
187 }
188
189 $this->setParserTS(
190 sprintf( '2010%02d%02d123456', $month, $value )
191 );
192
193 # please keep the following commented line of code. It helps debugging.
194 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
195
196 # format expectation and test it
197 $expected = sprintf( $format, $value );
198 $this->assertMagic( $expected, $magic );
199 }
200
201 /**
202 * helper to set the parser timestamp and revision timestamp
203 * @param string $ts
204 */
205 private function setParserTS( $ts ) {
206 $this->testParser->Options()->setTimestamp( $ts );
207 $this->testParser->mRevisionTimestamp = $ts;
208 }
209
210 /**
211 * Assertion helper to test a magic variable output
212 * @param string|int $expected
213 * @param string $magic
214 */
215 private function assertMagic( $expected, $magic ) {
216 if ( in_array( $magic, $this->expectedAsInteger ) ) {
217 $expected = (int)$expected;
218 }
219
220 # Generate a message for the assertion
221 $msg = sprintf( "Magic %s should be <%s:%s>",
222 $magic,
223 $expected,
224 gettype( $expected )
225 );
226
227 $this->assertSame(
228 $expected,
229 $this->testParser->getVariableValue( $magic ),
230 $msg
231 );
232 }
233 }