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