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 */
34 $wgContLang = Language
::factory( 'en' );
36 $this->testParser
= new Parser();
37 $this->testParser
->Options( new ParserOptions() );
39 # initialize parser output
40 $this->testParser
->clearState();
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)
46 $this->testParser
->setTitle( $title );
49 /** destroy parser (TODO: is it really neded?)*/
51 unset( $this->testParser
);
54 ############### TESTS #############################################
56 # - those got copy pasted, we can probably make them cleaner
57 # - tests are lacking useful messages
61 /** @dataProvider MediaWikiProvide::Days */
62 function testCurrentdayIsUnPadded( $day ) {
63 $this->assertUnPadded( 'currentday', $day );
65 /** @dataProvider MediaWikiProvide::Days */
66 function testCurrentdaytwoIsZeroPadded( $day ) {
67 $this->assertZeroPadded( 'currentday2', $day );
69 /** @dataProvider MediaWikiProvide::Days */
70 function testLocaldayIsUnPadded( $day ) {
71 $this->assertUnPadded( 'localday', $day );
73 /** @dataProvider MediaWikiProvide::Days */
74 function testLocaldaytwoIsZeroPadded( $day ) {
75 $this->assertZeroPadded( 'localday2', $day );
80 /** @dataProvider MediaWikiProvide::Months */
81 function testCurrentmonthIsZeroPadded( $month ) {
82 $this->assertZeroPadded( 'currentmonth', $month );
84 /** @dataProvider MediaWikiProvide::Months */
85 function testCurrentmonthoneIsUnPadded( $month ) {
86 $this->assertUnPadded( 'currentmonth1', $month );
88 /** @dataProvider MediaWikiProvide::Months */
89 function testLocalmonthIsZeroPadded( $month ) {
90 $this->assertZeroPadded( 'localmonth', $month );
92 /** @dataProvider MediaWikiProvide::Months */
93 function testLocalmonthoneIsUnPadded( $month ) {
94 $this->assertUnPadded( 'localmonth1', $month );
100 /** @dataProvider MediaWikiProvide::Days */
101 function testRevisiondayIsUnPadded( $day ) {
102 $this->assertUnPadded( 'revisionday', $day );
104 /** @dataProvider MediaWikiProvide::Days */
105 function testRevisiondaytwoIsZeroPadded( $day ) {
106 $this->assertZeroPadded( 'revisionday2', $day );
111 /** @dataProvider MediaWikiProvide::Months */
112 function testRevisionmonthIsZeroPadded( $month ) {
113 $this->assertZeroPadded( 'revisionmonth', $month );
115 /** @dataProvider MediaWikiProvide::Months */
116 function testRevisionmonthoneIsUnPadded( $month ) {
117 $this->assertUnPadded( 'revisionmonth1', $month );
121 * Rough tests for {{SERVERNAME}} magic word
124 function testServernameFromDifferentProtocols() {
126 $saved_wgServer= $wgServer;
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' );
135 $wgServer = $saved_wgServer;
138 ############### HELPERS ############################################
140 /** assertion helper expecting a magic output which is zero padded */
141 PUBLIC function assertZeroPadded( $magic, $value ) {
142 $this->assertMagicPadding( $magic, $value, '%02d' );
145 /** assertion helper expecting a magic output which is unpadded */
146 PUBLIC function assertUnPadded( $magic, $value ) {
147 $this->assertMagicPadding( $magic, $value, '%d' );
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
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; }
163 sprintf( '2010%02d%02d123456', $month, $value )
166 # please keep the following commented line of code. It helps debugging.
167 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
169 # format expectation and test it
170 $expected = sprintf( $format, $value );
171 $this->assertMagic( $expected, $magic );
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;
181 * Assertion helper to test a magic variable output
183 private function assertMagic( $expected, $magic ) {
184 if( in_array( $magic, $this->expectedAsInteger
) ) {
185 $expected = (int) $expected;
188 # Generate a message for the assertion
189 $msg = sprintf( "Magic %s should be <%s:%s>",
197 $this->testParser
->getVariableValue( $magic ),