Merge "(bug 43211) Remove unneeded noprint classes after CSS change."
[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 /** @dataProvider MediaWikiProvide::Days */
69 function testCurrentdayIsUnPadded( $day ) {
70 $this->assertUnPadded( 'currentday', $day );
71 }
72 /** @dataProvider MediaWikiProvide::Days */
73 function testCurrentdaytwoIsZeroPadded( $day ) {
74 $this->assertZeroPadded( 'currentday2', $day );
75 }
76 /** @dataProvider MediaWikiProvide::Days */
77 function testLocaldayIsUnPadded( $day ) {
78 $this->assertUnPadded( 'localday', $day );
79 }
80 /** @dataProvider MediaWikiProvide::Days */
81 function testLocaldaytwoIsZeroPadded( $day ) {
82 $this->assertZeroPadded( 'localday2', $day );
83 }
84
85 # month
86
87 /** @dataProvider MediaWikiProvide::Months */
88 function testCurrentmonthIsZeroPadded( $month ) {
89 $this->assertZeroPadded( 'currentmonth', $month );
90 }
91 /** @dataProvider MediaWikiProvide::Months */
92 function testCurrentmonthoneIsUnPadded( $month ) {
93 $this->assertUnPadded( 'currentmonth1', $month );
94 }
95 /** @dataProvider MediaWikiProvide::Months */
96 function testLocalmonthIsZeroPadded( $month ) {
97 $this->assertZeroPadded( 'localmonth', $month );
98 }
99 /** @dataProvider MediaWikiProvide::Months */
100 function testLocalmonthoneIsUnPadded( $month ) {
101 $this->assertUnPadded( 'localmonth1', $month );
102 }
103
104
105 # revision day
106
107 /** @dataProvider MediaWikiProvide::Days */
108 function testRevisiondayIsUnPadded( $day ) {
109 $this->assertUnPadded( 'revisionday', $day );
110 }
111 /** @dataProvider MediaWikiProvide::Days */
112 function testRevisiondaytwoIsZeroPadded( $day ) {
113 $this->assertZeroPadded( 'revisionday2', $day );
114 }
115
116 # revision month
117
118 /** @dataProvider MediaWikiProvide::Months */
119 function testRevisionmonthIsZeroPadded( $month ) {
120 $this->assertZeroPadded( 'revisionmonth', $month );
121 }
122 /** @dataProvider MediaWikiProvide::Months */
123 function testRevisionmonthoneIsUnPadded( $month ) {
124 $this->assertUnPadded( 'revisionmonth1', $month );
125 }
126
127 /**
128 * Rough tests for {{SERVERNAME}} magic word
129 * Bug 31176
130 */
131 function testServernameFromDifferentProtocols() {
132 global $wgServer;
133 $saved_wgServer= $wgServer;
134
135 $wgServer = 'http://localhost/';
136 $this->assertMagic( 'localhost', 'servername' );
137 $wgServer = 'https://localhost/';
138 $this->assertMagic( 'localhost', 'servername' );
139 $wgServer = '//localhost/'; # bug 31176
140 $this->assertMagic( 'localhost', 'servername' );
141
142 $wgServer = $saved_wgServer;
143 }
144
145 ############### HELPERS ############################################
146
147 /** assertion helper expecting a magic output which is zero padded */
148 PUBLIC function assertZeroPadded( $magic, $value ) {
149 $this->assertMagicPadding( $magic, $value, '%02d' );
150 }
151
152 /** assertion helper expecting a magic output which is unpadded */
153 PUBLIC function assertUnPadded( $magic, $value ) {
154 $this->assertMagicPadding( $magic, $value, '%d' );
155 }
156
157 /**
158 * Main assertion helper for magic variables padding
159 * @param $magic string Magic variable name
160 * @param $value mixed Month or day
161 * @param $format string sprintf format for $value
162 */
163 private function assertMagicPadding( $magic, $value, $format ) {
164 # Initialize parser timestamp as year 2010 at 12h34 56s.
165 # month and day are given by the caller ($value). Month < 12!
166 if( $value > 12 ) { $month = $value % 12; }
167 else { $month = $value; }
168
169 $this->setParserTS(
170 sprintf( '2010%02d%02d123456', $month, $value )
171 );
172
173 # please keep the following commented line of code. It helps debugging.
174 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
175
176 # format expectation and test it
177 $expected = sprintf( $format, $value );
178 $this->assertMagic( $expected, $magic );
179 }
180
181 /** helper to set the parser timestamp and revision timestamp */
182 private function setParserTS( $ts ) {
183 $this->testParser->Options()->setTimestamp( $ts );
184 $this->testParser->mRevisionTimestamp = $ts;
185 }
186
187 /**
188 * Assertion helper to test a magic variable output
189 */
190 private function assertMagic( $expected, $magic ) {
191 if( in_array( $magic, $this->expectedAsInteger ) ) {
192 $expected = (int)$expected;
193 }
194
195 # Generate a message for the assertion
196 $msg = sprintf( "Magic %s should be <%s:%s>",
197 $magic,
198 $expected,
199 gettype( $expected )
200 );
201
202 $this->assertSame(
203 $expected,
204 $this->testParser->getVariableValue( $magic ),
205 $msg
206 );
207 }
208 }