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( 'wgContLang', $contLang );
38 $this->testParser
= new Parser();
39 $this->testParser
->Options( ParserOptions
::newFromUserAndLang( new User
, $contLang ) );
41 # initialize parser output
42 $this->testParser
->clearState();
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)
48 $this->testParser
->setTitle( $title );
51 /** destroy parser (TODO: is it really neded?)*/
52 protected function tearDown() {
53 unset( $this->testParser
);
58 ############### TESTS #############################################
60 # - those got copy pasted, we can probably make them cleaner
61 # - tests are lacking useful messages
65 /** @dataProvider MediaWikiProvide::Days */
66 function testCurrentdayIsUnPadded( $day ) {
67 $this->assertUnPadded( 'currentday', $day );
69 /** @dataProvider MediaWikiProvide::Days */
70 function testCurrentdaytwoIsZeroPadded( $day ) {
71 $this->assertZeroPadded( 'currentday2', $day );
73 /** @dataProvider MediaWikiProvide::Days */
74 function testLocaldayIsUnPadded( $day ) {
75 $this->assertUnPadded( 'localday', $day );
77 /** @dataProvider MediaWikiProvide::Days */
78 function testLocaldaytwoIsZeroPadded( $day ) {
79 $this->assertZeroPadded( 'localday2', $day );
84 /** @dataProvider MediaWikiProvide::Months */
85 function testCurrentmonthIsZeroPadded( $month ) {
86 $this->assertZeroPadded( 'currentmonth', $month );
88 /** @dataProvider MediaWikiProvide::Months */
89 function testCurrentmonthoneIsUnPadded( $month ) {
90 $this->assertUnPadded( 'currentmonth1', $month );
92 /** @dataProvider MediaWikiProvide::Months */
93 function testLocalmonthIsZeroPadded( $month ) {
94 $this->assertZeroPadded( 'localmonth', $month );
96 /** @dataProvider MediaWikiProvide::Months */
97 function testLocalmonthoneIsUnPadded( $month ) {
98 $this->assertUnPadded( 'localmonth1', $month );
104 /** @dataProvider MediaWikiProvide::Days */
105 function testRevisiondayIsUnPadded( $day ) {
106 $this->assertUnPadded( 'revisionday', $day );
108 /** @dataProvider MediaWikiProvide::Days */
109 function testRevisiondaytwoIsZeroPadded( $day ) {
110 $this->assertZeroPadded( 'revisionday2', $day );
115 /** @dataProvider MediaWikiProvide::Months */
116 function testRevisionmonthIsZeroPadded( $month ) {
117 $this->assertZeroPadded( 'revisionmonth', $month );
119 /** @dataProvider MediaWikiProvide::Months */
120 function testRevisionmonthoneIsUnPadded( $month ) {
121 $this->assertUnPadded( 'revisionmonth1', $month );
125 * Rough tests for {{SERVERNAME}} magic word
128 function testServernameFromDifferentProtocols() {
130 $saved_wgServer= $wgServer;
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' );
139 $wgServer = $saved_wgServer;
142 ############### HELPERS ############################################
144 /** assertion helper expecting a magic output which is zero padded */
145 PUBLIC function assertZeroPadded( $magic, $value ) {
146 $this->assertMagicPadding( $magic, $value, '%02d' );
149 /** assertion helper expecting a magic output which is unpadded */
150 PUBLIC function assertUnPadded( $magic, $value ) {
151 $this->assertMagicPadding( $magic, $value, '%d' );
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
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; }
167 sprintf( '2010%02d%02d123456', $month, $value )
170 # please keep the following commented line of code. It helps debugging.
171 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
173 # format expectation and test it
174 $expected = sprintf( $format, $value );
175 $this->assertMagic( $expected, $magic );
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;
185 * Assertion helper to test a magic variable output
187 private function assertMagic( $expected, $magic ) {
188 if( in_array( $magic, $this->expectedAsInteger
) ) {
189 $expected = (int) $expected;
192 # Generate a message for the assertion
193 $msg = sprintf( "Magic %s should be <%s:%s>",
201 $this->testParser
->getVariableValue( $magic ),