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