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
17 * @covers Parser::getVariableValue
19 class MagicVariableTest
extends MediaWikiTestCase
{
23 private $testParser = null;
26 * An array of magicword returned as type integer by the parser
27 * They are usually returned as a string for i18n since we support
28 * persan numbers for example, but some magic explicitly return
30 * @see MagicVariableTest::assertMagic()
32 private $expectedAsInteger = [
37 /** setup a basic parser object */
38 protected function setUp() {
41 $contLang = Language
::factory( 'en' );
42 $this->setMwGlobals( [
43 'wgLanguageCode' => 'en',
44 'wgContLang' => $contLang,
47 $this->testParser
= new Parser();
48 $this->testParser
->Options( ParserOptions
::newFromUserAndLang( new User
, $contLang ) );
50 # initialize parser output
51 $this->testParser
->clearState();
53 # Needs a title to do magic word stuff
54 $title = Title
::newFromText( 'Tests' );
55 # Else it needs a db connection just to check if it's a redirect
56 # (when deciding the page language).
57 $title->mRedirect
= false;
59 $this->testParser
->setTitle( $title );
63 * @param int $num Upper limit for numbers
64 * @return array Array of numbers from 1 up to $num
66 private static function createProviderUpTo( $num ) {
68 for ( $i = 1; $i <= $num; $i++
) {
76 * @return array Array of months numbers (as an integer)
78 public static function provideMonths() {
79 return self
::createProviderUpTo( 12 );
83 * @return array Array of days numbers (as an integer)
85 public static function provideDays() {
86 return self
::createProviderUpTo( 31 );
89 # ############## TESTS #############################################
91 # - those got copy pasted, we can probably make them cleaner
92 # - tests are lacking useful messages
96 /** @dataProvider provideDays */
97 public function testCurrentdayIsUnPadded( $day ) {
98 $this->assertUnPadded( 'currentday', $day );
101 /** @dataProvider provideDays */
102 public function testCurrentdaytwoIsZeroPadded( $day ) {
103 $this->assertZeroPadded( 'currentday2', $day );
106 /** @dataProvider provideDays */
107 public function testLocaldayIsUnPadded( $day ) {
108 $this->assertUnPadded( 'localday', $day );
111 /** @dataProvider provideDays */
112 public function testLocaldaytwoIsZeroPadded( $day ) {
113 $this->assertZeroPadded( 'localday2', $day );
118 /** @dataProvider provideMonths */
119 public function testCurrentmonthIsZeroPadded( $month ) {
120 $this->assertZeroPadded( 'currentmonth', $month );
123 /** @dataProvider provideMonths */
124 public function testCurrentmonthoneIsUnPadded( $month ) {
125 $this->assertUnPadded( 'currentmonth1', $month );
128 /** @dataProvider provideMonths */
129 public function testLocalmonthIsZeroPadded( $month ) {
130 $this->assertZeroPadded( 'localmonth', $month );
133 /** @dataProvider provideMonths */
134 public function testLocalmonthoneIsUnPadded( $month ) {
135 $this->assertUnPadded( 'localmonth1', $month );
140 /** @dataProvider provideDays */
141 public function testRevisiondayIsUnPadded( $day ) {
142 $this->assertUnPadded( 'revisionday', $day );
145 /** @dataProvider provideDays */
146 public function testRevisiondaytwoIsZeroPadded( $day ) {
147 $this->assertZeroPadded( 'revisionday2', $day );
152 /** @dataProvider provideMonths */
153 public function testRevisionmonthIsZeroPadded( $month ) {
154 $this->assertZeroPadded( 'revisionmonth', $month );
157 /** @dataProvider provideMonths */
158 public function testRevisionmonthoneIsUnPadded( $month ) {
159 $this->assertUnPadded( 'revisionmonth1', $month );
162 # ############## HELPERS ############################################
164 /** assertion helper expecting a magic output which is zero padded */
165 public function assertZeroPadded( $magic, $value ) {
166 $this->assertMagicPadding( $magic, $value, '%02d' );
169 /** assertion helper expecting a magic output which is unpadded */
170 public function assertUnPadded( $magic, $value ) {
171 $this->assertMagicPadding( $magic, $value, '%d' );
175 * Main assertion helper for magic variables padding
176 * @param string $magic Magic variable name
177 * @param mixed $value Month or day
178 * @param string $format Sprintf format for $value
180 private function assertMagicPadding( $magic, $value, $format ) {
181 # Initialize parser timestamp as year 2010 at 12h34 56s.
182 # month and day are given by the caller ($value). Month < 12!
184 $month = $value %
12;
190 sprintf( '2010%02d%02d123456', $month, $value )
193 # please keep the following commented line of code. It helps debugging.
194 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
196 # format expectation and test it
197 $expected = sprintf( $format, $value );
198 $this->assertMagic( $expected, $magic );
202 * helper to set the parser timestamp and revision timestamp
205 private function setParserTS( $ts ) {
206 $this->testParser
->Options()->setTimestamp( $ts );
207 $this->testParser
->mRevisionTimestamp
= $ts;
211 * Assertion helper to test a magic variable output
212 * @param string|int $expected
213 * @param string $magic
215 private function assertMagic( $expected, $magic ) {
216 if ( in_array( $magic, $this->expectedAsInteger
) ) {
217 $expected = (int)$expected;
220 # Generate a message for the assertion
221 $msg = sprintf( "Magic %s should be <%s:%s>",
229 $this->testParser
->getVariableValue( $magic ),