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 Ashar Voultoiz
10 * @copyright Copyright © 2011, Ashar Voultoiz
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 );
120 ############### HELPERS ############################################
122 /** assertion helper expecting a magic output which is zero padded */
123 PUBLIC function assertZeroPadded( $magic, $value ) {
124 $this->assertMagicPadding( $magic, $value, '%02d' );
127 /** assertion helper expecting a magic output which is unpadded */
128 PUBLIC function assertUnPadded( $magic, $value ) {
129 $this->assertMagicPadding( $magic, $value, '%d' );
133 * Main assertion helper for magic variables padding
134 * @param $magic string Magic variable name
135 * @param $value mixed Month or day
136 * @param $format string sprintf format for $value
138 private function assertMagicPadding( $magic, $value, $format ) {
139 # Initialize parser timestamp as year 2010 at 12h34 56s.
140 # month and day are given by the caller ($value). Month < 12!
141 if( $value > 12 ) { $month = $value %
12; }
142 else { $month = $value; }
145 sprintf( '2010%02d%02d123456', $month, $value )
148 # please keep the following commented line of code. It helps debugging.
149 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
151 # format expectation and test it
152 $expected = sprintf( $format, $value );
153 $this->assertMagic( $expected, $magic );
156 /** helper to set the parser timestamp and revision timestamp */
157 private function setParserTS( $ts ) {
158 $this->testParser
->Options()->setTimestamp( $ts );
159 $this->testParser
->mRevisionTimestamp
= $ts;
163 * Assertion helper to test a magic variable output
165 private function assertMagic( $expected, $magic ) {
166 if( in_array( $magic, $this->expectedAsInteger
) ) {
167 $expected = (int) $expected;
170 # Generate a message for the assertion
171 $msg = sprintf( "Magic %s should be <%s:%s>",
179 $this->testParser
->getVariableValue( $magic ),