Merge "Fix for Ic74c7ba7 (c8c50eb): there's no reason to pass $this by reference."
[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 function setUp() {
33 global $wgContLang;
34 $wgContLang = Language::factory( 'en' );
35
36 $this->testParser = new Parser();
37 $this->testParser->Options( new ParserOptions() );
38
39 # initialize parser output
40 $this->testParser->clearState();
41
42 # Needs a title to do magic word stuff
43 $title = Title::newFromText( 'Tests' );
44 $title->mRedirect = false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language)
45
46 $this->testParser->setTitle( $title );
47 }
48
49 /** destroy parser (TODO: is it really neded?)*/
50 function tearDown() {
51 unset( $this->testParser );
52 }
53
54 ############### TESTS #############################################
55 # @todo FIXME:
56 # - those got copy pasted, we can probably make them cleaner
57 # - tests are lacking useful messages
58
59 # day
60
61 /** @dataProvider MediaWikiProvide::Days */
62 function testCurrentdayIsUnPadded( $day ) {
63 $this->assertUnPadded( 'currentday', $day );
64 }
65 /** @dataProvider MediaWikiProvide::Days */
66 function testCurrentdaytwoIsZeroPadded( $day ) {
67 $this->assertZeroPadded( 'currentday2', $day );
68 }
69 /** @dataProvider MediaWikiProvide::Days */
70 function testLocaldayIsUnPadded( $day ) {
71 $this->assertUnPadded( 'localday', $day );
72 }
73 /** @dataProvider MediaWikiProvide::Days */
74 function testLocaldaytwoIsZeroPadded( $day ) {
75 $this->assertZeroPadded( 'localday2', $day );
76 }
77
78 # month
79
80 /** @dataProvider MediaWikiProvide::Months */
81 function testCurrentmonthIsZeroPadded( $month ) {
82 $this->assertZeroPadded( 'currentmonth', $month );
83 }
84 /** @dataProvider MediaWikiProvide::Months */
85 function testCurrentmonthoneIsUnPadded( $month ) {
86 $this->assertUnPadded( 'currentmonth1', $month );
87 }
88 /** @dataProvider MediaWikiProvide::Months */
89 function testLocalmonthIsZeroPadded( $month ) {
90 $this->assertZeroPadded( 'localmonth', $month );
91 }
92 /** @dataProvider MediaWikiProvide::Months */
93 function testLocalmonthoneIsUnPadded( $month ) {
94 $this->assertUnPadded( 'localmonth1', $month );
95 }
96
97
98 # revision day
99
100 /** @dataProvider MediaWikiProvide::Days */
101 function testRevisiondayIsUnPadded( $day ) {
102 $this->assertUnPadded( 'revisionday', $day );
103 }
104 /** @dataProvider MediaWikiProvide::Days */
105 function testRevisiondaytwoIsZeroPadded( $day ) {
106 $this->assertZeroPadded( 'revisionday2', $day );
107 }
108
109 # revision month
110
111 /** @dataProvider MediaWikiProvide::Months */
112 function testRevisionmonthIsZeroPadded( $month ) {
113 $this->assertZeroPadded( 'revisionmonth', $month );
114 }
115 /** @dataProvider MediaWikiProvide::Months */
116 function testRevisionmonthoneIsUnPadded( $month ) {
117 $this->assertUnPadded( 'revisionmonth1', $month );
118 }
119
120 /**
121 * Rough tests for {{SERVERNAME}} magic word
122 * Bug 31176
123 */
124 function testServernameFromDifferentProtocols() {
125 global $wgServer;
126 $saved_wgServer= $wgServer;
127
128 $wgServer = 'http://localhost/';
129 $this->assertMagic( 'localhost', 'servername' );
130 $wgServer = 'https://localhost/';
131 $this->assertMagic( 'localhost', 'servername' );
132 $wgServer = '//localhost/'; # bug 31176
133 $this->assertMagic( 'localhost', 'servername' );
134
135 $wgServer = $saved_wgServer;
136 }
137
138 ############### HELPERS ############################################
139
140 /** assertion helper expecting a magic output which is zero padded */
141 PUBLIC function assertZeroPadded( $magic, $value ) {
142 $this->assertMagicPadding( $magic, $value, '%02d' );
143 }
144
145 /** assertion helper expecting a magic output which is unpadded */
146 PUBLIC function assertUnPadded( $magic, $value ) {
147 $this->assertMagicPadding( $magic, $value, '%d' );
148 }
149
150 /**
151 * Main assertion helper for magic variables padding
152 * @param $magic string Magic variable name
153 * @param $value mixed Month or day
154 * @param $format string sprintf format for $value
155 */
156 private function assertMagicPadding( $magic, $value, $format ) {
157 # Initialize parser timestamp as year 2010 at 12h34 56s.
158 # month and day are given by the caller ($value). Month < 12!
159 if( $value > 12 ) { $month = $value % 12; }
160 else { $month = $value; }
161
162 $this->setParserTS(
163 sprintf( '2010%02d%02d123456', $month, $value )
164 );
165
166 # please keep the following commented line of code. It helps debugging.
167 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
168
169 # format expectation and test it
170 $expected = sprintf( $format, $value );
171 $this->assertMagic( $expected, $magic );
172 }
173
174 /** helper to set the parser timestamp and revision timestamp */
175 private function setParserTS( $ts ) {
176 $this->testParser->Options()->setTimestamp( $ts );
177 $this->testParser->mRevisionTimestamp = $ts;
178 }
179
180 /**
181 * Assertion helper to test a magic variable output
182 */
183 private function assertMagic( $expected, $magic ) {
184 if( in_array( $magic, $this->expectedAsInteger ) ) {
185 $expected = (int) $expected;
186 }
187
188 # Generate a message for the assertion
189 $msg = sprintf( "Magic %s should be <%s:%s>",
190 $magic,
191 $expected,
192 gettype( $expected )
193 );
194
195 $this->assertSame(
196 $expected,
197 $this->testParser->getVariableValue( $magic ),
198 $msg
199 );
200 }
201 }