3 * This file is intended to test magic variables in the parser
4 * It was inspired by Raymond & Matěj Grabovský commenting about r66200
6 * As of february 2011, it only tests some revisions and date related
9 * @author Antoine Musso
10 * @copyright Copyright © 2011, Antoine Musso
15 class MagicVariableTest
extends MediaWikiTestCase
{
16 /** Will contains a parser object*/
17 private $testParser = null;
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
24 * @see MagicVariableTest::assertMagic()
26 private $expectedAsInteger = array(
31 /** setup a basic parser object */
32 protected function setUp() {
35 $contLang = Language
::factory( 'en' );
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => 'en',
38 'wgContLang' => $contLang,
41 $this->testParser
= new Parser();
42 $this->testParser
->Options( ParserOptions
::newFromUserAndLang( new User
, $contLang ) );
44 # initialize parser output
45 $this->testParser
->clearState();
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)
51 $this->testParser
->setTitle( $title );
54 /** destroy parser (TODO: is it really neded?)*/
55 protected function tearDown() {
56 unset( $this->testParser
);
61 ############### TESTS #############################################
63 # - those got copy pasted, we can probably make them cleaner
64 # - tests are lacking useful messages
69 * @dataProvider MediaWikiProvide::Days
72 function testCurrentdayIsUnPadded( $day ) {
73 $this->assertUnPadded( 'currentday', $day );
77 * @dataProvider MediaWikiProvide::Days
80 function testCurrentdaytwoIsZeroPadded( $day ) {
81 $this->assertZeroPadded( 'currentday2', $day );
85 * @dataProvider MediaWikiProvide::Days
88 function testLocaldayIsUnPadded( $day ) {
89 $this->assertUnPadded( 'localday', $day );
93 * @dataProvider MediaWikiProvide::Days
96 function testLocaldaytwoIsZeroPadded( $day ) {
97 $this->assertZeroPadded( 'localday2', $day );
103 * @dataProvider MediaWikiProvide::Months
106 function testCurrentmonthIsZeroPadded( $month ) {
107 $this->assertZeroPadded( 'currentmonth', $month );
111 * @dataProvider MediaWikiProvide::Months
114 function testCurrentmonthoneIsUnPadded( $month ) {
115 $this->assertUnPadded( 'currentmonth1', $month );
119 * @dataProvider MediaWikiProvide::Months
122 function testLocalmonthIsZeroPadded( $month ) {
123 $this->assertZeroPadded( 'localmonth', $month );
127 * @dataProvider MediaWikiProvide::Months
130 function testLocalmonthoneIsUnPadded( $month ) {
131 $this->assertUnPadded( 'localmonth1', $month );
138 * @dataProvider MediaWikiProvide::Days
141 function testRevisiondayIsUnPadded( $day ) {
142 $this->assertUnPadded( 'revisionday', $day );
146 * @dataProvider MediaWikiProvide::Days
149 function testRevisiondaytwoIsZeroPadded( $day ) {
150 $this->assertZeroPadded( 'revisionday2', $day );
156 * @dataProvider MediaWikiProvide::Months
159 function testRevisionmonthIsZeroPadded( $month ) {
160 $this->assertZeroPadded( 'revisionmonth', $month );
164 * @dataProvider MediaWikiProvide::Months
167 function testRevisionmonthoneIsUnPadded( $month ) {
168 $this->assertUnPadded( 'revisionmonth1', $month );
172 * Rough tests for {{SERVERNAME}} magic word
176 function testServernameFromDifferentProtocols() {
178 $saved_wgServer = $wgServer;
180 $wgServer = 'http://localhost/';
181 $this->assertMagic( 'localhost', 'servername' );
182 $wgServer = 'https://localhost/';
183 $this->assertMagic( 'localhost', 'servername' );
184 $wgServer = '//localhost/'; # bug 31176
185 $this->assertMagic( 'localhost', 'servername' );
187 $wgServer = $saved_wgServer;
190 ############### HELPERS ############################################
192 /** assertion helper expecting a magic output which is zero padded */
193 PUBLIC function assertZeroPadded( $magic, $value ) {
194 $this->assertMagicPadding( $magic, $value, '%02d' );
197 /** assertion helper expecting a magic output which is unpadded */
198 PUBLIC function assertUnPadded( $magic, $value ) {
199 $this->assertMagicPadding( $magic, $value, '%d' );
203 * Main assertion helper for magic variables padding
204 * @param $magic string Magic variable name
205 * @param $value mixed Month or day
206 * @param $format string sprintf format for $value
208 private function assertMagicPadding( $magic, $value, $format ) {
209 # Initialize parser timestamp as year 2010 at 12h34 56s.
210 # month and day are given by the caller ($value). Month < 12!
212 $month = $value %
12;
218 sprintf( '2010%02d%02d123456', $month, $value )
221 # please keep the following commented line of code. It helps debugging.
222 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
224 # format expectation and test it
225 $expected = sprintf( $format, $value );
226 $this->assertMagic( $expected, $magic );
229 /** helper to set the parser timestamp and revision timestamp */
230 private function setParserTS( $ts ) {
231 $this->testParser
->Options()->setTimestamp( $ts );
232 $this->testParser
->mRevisionTimestamp
= $ts;
236 * Assertion helper to test a magic variable output
238 private function assertMagic( $expected, $magic ) {
239 if ( in_array( $magic, $this->expectedAsInteger
) ) {
240 $expected = (int)$expected;
243 # Generate a message for the assertion
244 $msg = sprintf( "Magic %s should be <%s:%s>",
252 $this->testParser
->getVariableValue( $magic ),