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