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 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
37 $this->testParser
= new Parser();
38 $this->testParser
->Options( new ParserOptions() );
40 # initialize parser output
41 $this->testParser
->clearState();
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)
47 $this->testParser
->setTitle( $title );
50 /** destroy parser (TODO: is it really neded?)*/
51 protected function tearDown() {
52 unset( $this->testParser
);
57 ############### TESTS #############################################
59 # - those got copy pasted, we can probably make them cleaner
60 # - tests are lacking useful messages
64 /** @dataProvider MediaWikiProvide::Days */
65 function testCurrentdayIsUnPadded( $day ) {
66 $this->assertUnPadded( 'currentday', $day );
68 /** @dataProvider MediaWikiProvide::Days */
69 function testCurrentdaytwoIsZeroPadded( $day ) {
70 $this->assertZeroPadded( 'currentday2', $day );
72 /** @dataProvider MediaWikiProvide::Days */
73 function testLocaldayIsUnPadded( $day ) {
74 $this->assertUnPadded( 'localday', $day );
76 /** @dataProvider MediaWikiProvide::Days */
77 function testLocaldaytwoIsZeroPadded( $day ) {
78 $this->assertZeroPadded( 'localday2', $day );
83 /** @dataProvider MediaWikiProvide::Months */
84 function testCurrentmonthIsZeroPadded( $month ) {
85 $this->assertZeroPadded( 'currentmonth', $month );
87 /** @dataProvider MediaWikiProvide::Months */
88 function testCurrentmonthoneIsUnPadded( $month ) {
89 $this->assertUnPadded( 'currentmonth1', $month );
91 /** @dataProvider MediaWikiProvide::Months */
92 function testLocalmonthIsZeroPadded( $month ) {
93 $this->assertZeroPadded( 'localmonth', $month );
95 /** @dataProvider MediaWikiProvide::Months */
96 function testLocalmonthoneIsUnPadded( $month ) {
97 $this->assertUnPadded( 'localmonth1', $month );
103 /** @dataProvider MediaWikiProvide::Days */
104 function testRevisiondayIsUnPadded( $day ) {
105 $this->assertUnPadded( 'revisionday', $day );
107 /** @dataProvider MediaWikiProvide::Days */
108 function testRevisiondaytwoIsZeroPadded( $day ) {
109 $this->assertZeroPadded( 'revisionday2', $day );
114 /** @dataProvider MediaWikiProvide::Months */
115 function testRevisionmonthIsZeroPadded( $month ) {
116 $this->assertZeroPadded( 'revisionmonth', $month );
118 /** @dataProvider MediaWikiProvide::Months */
119 function testRevisionmonthoneIsUnPadded( $month ) {
120 $this->assertUnPadded( 'revisionmonth1', $month );
124 * Rough tests for {{SERVERNAME}} magic word
127 function testServernameFromDifferentProtocols() {
129 $saved_wgServer= $wgServer;
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' );
138 $wgServer = $saved_wgServer;
141 ############### HELPERS ############################################
143 /** assertion helper expecting a magic output which is zero padded */
144 PUBLIC function assertZeroPadded( $magic, $value ) {
145 $this->assertMagicPadding( $magic, $value, '%02d' );
148 /** assertion helper expecting a magic output which is unpadded */
149 PUBLIC function assertUnPadded( $magic, $value ) {
150 $this->assertMagicPadding( $magic, $value, '%d' );
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
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; }
166 sprintf( '2010%02d%02d123456', $month, $value )
169 # please keep the following commented line of code. It helps debugging.
170 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
172 # format expectation and test it
173 $expected = sprintf( $format, $value );
174 $this->assertMagic( $expected, $magic );
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;
184 * Assertion helper to test a magic variable output
186 private function assertMagic( $expected, $magic ) {
187 if( in_array( $magic, $this->expectedAsInteger
) ) {
188 $expected = (int) $expected;
191 # Generate a message for the assertion
192 $msg = sprintf( "Magic %s should be <%s:%s>",
200 $this->testParser
->getVariableValue( $magic ),