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
{
19 private $testParser = null;
22 * An array of magicword returned as type integer by the parser
23 * They are usually returned as a string for i18n since we support
24 * persan numbers for example, but some magic explicitly return
26 * @see MagicVariableTest::assertMagic()
28 private $expectedAsInteger = array(
33 /** setup a basic parser object */
34 protected function setUp() {
37 $contLang = Language
::factory( 'en' );
38 $this->setMwGlobals( array(
39 'wgLanguageCode' => 'en',
40 'wgContLang' => $contLang,
43 $this->testParser
= new Parser();
44 $this->testParser
->Options( ParserOptions
::newFromUserAndLang( new User
, $contLang ) );
46 # initialize parser output
47 $this->testParser
->clearState();
49 # Needs a title to do magic word stuff
50 $title = Title
::newFromText( 'Tests' );
51 # Else it needs a db connection just to check if it's a redirect
52 # (when deciding the page language).
53 $title->mRedirect
= false;
55 $this->testParser
->setTitle( $title );
59 * @param int $num Upper limit for numbers
60 * @return array Array of numbers from 1 up to $num
62 private static function createProviderUpTo( $num ) {
64 for ( $i = 1; $i <= $num; $i++
) {
72 * @return array Array of months numbers (as an integer)
74 public static function provideMonths() {
75 return self
::createProviderUpTo( 12 );
79 * @return array Array of days numbers (as an integer)
81 public static function provideDays() {
82 return self
::createProviderUpTo( 31 );
85 ############### TESTS #############################################
87 # - those got copy pasted, we can probably make them cleaner
88 # - tests are lacking useful messages
92 /** @dataProvider provideDays */
93 public function testCurrentdayIsUnPadded( $day ) {
94 $this->assertUnPadded( 'currentday', $day );
97 /** @dataProvider provideDays */
98 public function testCurrentdaytwoIsZeroPadded( $day ) {
99 $this->assertZeroPadded( 'currentday2', $day );
102 /** @dataProvider provideDays */
103 public function testLocaldayIsUnPadded( $day ) {
104 $this->assertUnPadded( 'localday', $day );
107 /** @dataProvider provideDays */
108 public function testLocaldaytwoIsZeroPadded( $day ) {
109 $this->assertZeroPadded( 'localday2', $day );
114 /** @dataProvider provideMonths */
115 public function testCurrentmonthIsZeroPadded( $month ) {
116 $this->assertZeroPadded( 'currentmonth', $month );
119 /** @dataProvider provideMonths */
120 public function testCurrentmonthoneIsUnPadded( $month ) {
121 $this->assertUnPadded( 'currentmonth1', $month );
124 /** @dataProvider provideMonths */
125 public function testLocalmonthIsZeroPadded( $month ) {
126 $this->assertZeroPadded( 'localmonth', $month );
129 /** @dataProvider provideMonths */
130 public function testLocalmonthoneIsUnPadded( $month ) {
131 $this->assertUnPadded( 'localmonth1', $month );
136 /** @dataProvider provideDays */
137 public function testRevisiondayIsUnPadded( $day ) {
138 $this->assertUnPadded( 'revisionday', $day );
141 /** @dataProvider provideDays */
142 public function testRevisiondaytwoIsZeroPadded( $day ) {
143 $this->assertZeroPadded( 'revisionday2', $day );
148 /** @dataProvider provideMonths */
149 public function testRevisionmonthIsZeroPadded( $month ) {
150 $this->assertZeroPadded( 'revisionmonth', $month );
153 /** @dataProvider provideMonths */
154 public function testRevisionmonthoneIsUnPadded( $month ) {
155 $this->assertUnPadded( 'revisionmonth1', $month );
158 ############### HELPERS ############################################
160 /** assertion helper expecting a magic output which is zero padded */
161 public function assertZeroPadded( $magic, $value ) {
162 $this->assertMagicPadding( $magic, $value, '%02d' );
165 /** assertion helper expecting a magic output which is unpadded */
166 public function assertUnPadded( $magic, $value ) {
167 $this->assertMagicPadding( $magic, $value, '%d' );
171 * Main assertion helper for magic variables padding
172 * @param string $magic Magic variable name
173 * @param mixed $value Month or day
174 * @param string $format Sprintf format for $value
176 private function assertMagicPadding( $magic, $value, $format ) {
177 # Initialize parser timestamp as year 2010 at 12h34 56s.
178 # month and day are given by the caller ($value). Month < 12!
180 $month = $value %
12;
186 sprintf( '2010%02d%02d123456', $month, $value )
189 # please keep the following commented line of code. It helps debugging.
190 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
192 # format expectation and test it
193 $expected = sprintf( $format, $value );
194 $this->assertMagic( $expected, $magic );
198 * helper to set the parser timestamp and revision timestamp
201 private function setParserTS( $ts ) {
202 $this->testParser
->Options()->setTimestamp( $ts );
203 $this->testParser
->mRevisionTimestamp
= $ts;
207 * Assertion helper to test a magic variable output
208 * @param string|int $expected
209 * @param string $magic
211 private function assertMagic( $expected, $magic ) {
212 if ( in_array( $magic, $this->expectedAsInteger
) ) {
213 $expected = (int)$expected;
216 # Generate a message for the assertion
217 $msg = sprintf( "Magic %s should be <%s:%s>",
225 $this->testParser
->getVariableValue( $magic ),