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