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