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 $title->mRedirect
= false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language)
53 $this->testParser
->setTitle( $title );
57 * @param int $num upper limit for numbers
58 * @return array of numbers from 1 up to $num
60 private static function createProviderUpTo( $num ) {
62 for ( $i = 1; $i <= $num; $i++
) {
70 * @return array of months numbers (as an integer)
72 public static function provideMonths() {
73 return self
::createProviderUpTo( 12 );
77 * @return array of days numbers (as an integer)
79 public static function provideDays() {
80 return self
::createProviderUpTo( 31 );
83 ############### TESTS #############################################
85 # - those got copy pasted, we can probably make them cleaner
86 # - tests are lacking useful messages
90 /** @dataProvider provideDays */
91 public function testCurrentdayIsUnPadded( $day ) {
92 $this->assertUnPadded( 'currentday', $day );
95 /** @dataProvider provideDays */
96 public function testCurrentdaytwoIsZeroPadded( $day ) {
97 $this->assertZeroPadded( 'currentday2', $day );
100 /** @dataProvider provideDays */
101 public function testLocaldayIsUnPadded( $day ) {
102 $this->assertUnPadded( 'localday', $day );
105 /** @dataProvider provideDays */
106 public function testLocaldaytwoIsZeroPadded( $day ) {
107 $this->assertZeroPadded( 'localday2', $day );
112 /** @dataProvider provideMonths */
113 public function testCurrentmonthIsZeroPadded( $month ) {
114 $this->assertZeroPadded( 'currentmonth', $month );
117 /** @dataProvider provideMonths */
118 public function testCurrentmonthoneIsUnPadded( $month ) {
119 $this->assertUnPadded( 'currentmonth1', $month );
122 /** @dataProvider provideMonths */
123 public function testLocalmonthIsZeroPadded( $month ) {
124 $this->assertZeroPadded( 'localmonth', $month );
127 /** @dataProvider provideMonths */
128 public function testLocalmonthoneIsUnPadded( $month ) {
129 $this->assertUnPadded( 'localmonth1', $month );
134 /** @dataProvider provideDays */
135 public function testRevisiondayIsUnPadded( $day ) {
136 $this->assertUnPadded( 'revisionday', $day );
139 /** @dataProvider provideDays */
140 public function testRevisiondaytwoIsZeroPadded( $day ) {
141 $this->assertZeroPadded( 'revisionday2', $day );
146 /** @dataProvider provideMonths */
147 public function testRevisionmonthIsZeroPadded( $month ) {
148 $this->assertZeroPadded( 'revisionmonth', $month );
151 /** @dataProvider provideMonths */
152 public function testRevisionmonthoneIsUnPadded( $month ) {
153 $this->assertUnPadded( 'revisionmonth1', $month );
157 * Rough tests for {{SERVERNAME}} magic word
160 * @dataProvider provideDataServernameFromDifferentProtocols
162 public function testServernameFromDifferentProtocols( $server ) {
163 $this->setMwGlobals( 'wgServer', $server );
165 $this->assertMagic( 'localhost', 'servername' );
168 public static function provideDataServernameFromDifferentProtocols() {
170 array( 'http://localhost/' ),
171 array( 'https://localhost/' ),
172 array( '//localhost/' ), # bug 31176
176 ############### HELPERS ############################################
178 /** assertion helper expecting a magic output which is zero padded */
179 public function assertZeroPadded( $magic, $value ) {
180 $this->assertMagicPadding( $magic, $value, '%02d' );
183 /** assertion helper expecting a magic output which is unpadded */
184 public function assertUnPadded( $magic, $value ) {
185 $this->assertMagicPadding( $magic, $value, '%d' );
189 * Main assertion helper for magic variables padding
190 * @param $magic string Magic variable name
191 * @param $value mixed Month or day
192 * @param $format string sprintf format for $value
194 private function assertMagicPadding( $magic, $value, $format ) {
195 # Initialize parser timestamp as year 2010 at 12h34 56s.
196 # month and day are given by the caller ($value). Month < 12!
198 $month = $value %
12;
204 sprintf( '2010%02d%02d123456', $month, $value )
207 # please keep the following commented line of code. It helps debugging.
208 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
210 # format expectation and test it
211 $expected = sprintf( $format, $value );
212 $this->assertMagic( $expected, $magic );
215 /** helper to set the parser timestamp and revision timestamp */
216 private function setParserTS( $ts ) {
217 $this->testParser
->Options()->setTimestamp( $ts );
218 $this->testParser
->mRevisionTimestamp
= $ts;
222 * Assertion helper to test a magic variable output
224 private function assertMagic( $expected, $magic ) {
225 if ( in_array( $magic, $this->expectedAsInteger
) ) {
226 $expected = (int)$expected;
229 # Generate a message for the assertion
230 $msg = sprintf( "Magic %s should be <%s:%s>",
238 $this->testParser
->getVariableValue( $magic ),