Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / parser / MagicVariableTest.php
blob6a2afad6d7fc66a29511130f03021402d681bbdc
1 <?php
2 /**
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
7 * magic variables.
9 * @author Antoine Musso
10 * @copyright Copyright © 2011, Antoine Musso
11 * @file
12 * @todo covers tags
14 * @group Database
17 class MagicVariableTest extends MediaWikiTestCase {
18 /**
19 * @var Parser
21 private $testParser = null;
23 /**
24 * An array of magicword returned as type integer by the parser
25 * They are usually returned as a string for i18n since we support
26 * persan numbers for example, but some magic explicitly return
27 * them as integer.
28 * @see MagicVariableTest::assertMagic()
30 private $expectedAsInteger = [
31 'revisionday',
32 'revisionmonth1',
35 /** setup a basic parser object */
36 protected function setUp() {
37 parent::setUp();
39 $contLang = Language::factory( 'en' );
40 $this->setMwGlobals( [
41 'wgLanguageCode' => 'en',
42 'wgContLang' => $contLang,
43 ] );
45 $this->testParser = new Parser();
46 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
48 # initialize parser output
49 $this->testParser->clearState();
51 # Needs a title to do magic word stuff
52 $title = Title::newFromText( 'Tests' );
53 # Else it needs a db connection just to check if it's a redirect
54 # (when deciding the page language).
55 $title->mRedirect = false;
57 $this->testParser->setTitle( $title );
60 /**
61 * @param int $num Upper limit for numbers
62 * @return array Array of numbers from 1 up to $num
64 private static function createProviderUpTo( $num ) {
65 $ret = [];
66 for ( $i = 1; $i <= $num; $i++ ) {
67 $ret[] = [ $i ];
70 return $ret;
73 /**
74 * @return array Array of months numbers (as an integer)
76 public static function provideMonths() {
77 return self::createProviderUpTo( 12 );
80 /**
81 * @return array Array of days numbers (as an integer)
83 public static function provideDays() {
84 return self::createProviderUpTo( 31 );
87 # ############## TESTS #############################################
88 # @todo FIXME:
89 # - those got copy pasted, we can probably make them cleaner
90 # - tests are lacking useful messages
92 # day
94 /** @dataProvider provideDays */
95 public function testCurrentdayIsUnPadded( $day ) {
96 $this->assertUnPadded( 'currentday', $day );
99 /** @dataProvider provideDays */
100 public function testCurrentdaytwoIsZeroPadded( $day ) {
101 $this->assertZeroPadded( 'currentday2', $day );
104 /** @dataProvider provideDays */
105 public function testLocaldayIsUnPadded( $day ) {
106 $this->assertUnPadded( 'localday', $day );
109 /** @dataProvider provideDays */
110 public function testLocaldaytwoIsZeroPadded( $day ) {
111 $this->assertZeroPadded( 'localday2', $day );
114 # month
116 /** @dataProvider provideMonths */
117 public function testCurrentmonthIsZeroPadded( $month ) {
118 $this->assertZeroPadded( 'currentmonth', $month );
121 /** @dataProvider provideMonths */
122 public function testCurrentmonthoneIsUnPadded( $month ) {
123 $this->assertUnPadded( 'currentmonth1', $month );
126 /** @dataProvider provideMonths */
127 public function testLocalmonthIsZeroPadded( $month ) {
128 $this->assertZeroPadded( 'localmonth', $month );
131 /** @dataProvider provideMonths */
132 public function testLocalmonthoneIsUnPadded( $month ) {
133 $this->assertUnPadded( 'localmonth1', $month );
136 # revision day
138 /** @dataProvider provideDays */
139 public function testRevisiondayIsUnPadded( $day ) {
140 $this->assertUnPadded( 'revisionday', $day );
143 /** @dataProvider provideDays */
144 public function testRevisiondaytwoIsZeroPadded( $day ) {
145 $this->assertZeroPadded( 'revisionday2', $day );
148 # revision month
150 /** @dataProvider provideMonths */
151 public function testRevisionmonthIsZeroPadded( $month ) {
152 $this->assertZeroPadded( 'revisionmonth', $month );
155 /** @dataProvider provideMonths */
156 public function testRevisionmonthoneIsUnPadded( $month ) {
157 $this->assertUnPadded( 'revisionmonth1', $month );
160 # ############## HELPERS ############################################
162 /** assertion helper expecting a magic output which is zero padded */
163 public function assertZeroPadded( $magic, $value ) {
164 $this->assertMagicPadding( $magic, $value, '%02d' );
167 /** assertion helper expecting a magic output which is unpadded */
168 public function assertUnPadded( $magic, $value ) {
169 $this->assertMagicPadding( $magic, $value, '%d' );
173 * Main assertion helper for magic variables padding
174 * @param string $magic Magic variable name
175 * @param mixed $value Month or day
176 * @param string $format Sprintf format for $value
178 private function assertMagicPadding( $magic, $value, $format ) {
179 # Initialize parser timestamp as year 2010 at 12h34 56s.
180 # month and day are given by the caller ($value). Month < 12!
181 if ( $value > 12 ) {
182 $month = $value % 12;
183 } else {
184 $month = $value;
187 $this->setParserTS(
188 sprintf( '2010%02d%02d123456', $month, $value )
191 # please keep the following commented line of code. It helps debugging.
192 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
194 # format expectation and test it
195 $expected = sprintf( $format, $value );
196 $this->assertMagic( $expected, $magic );
200 * helper to set the parser timestamp and revision timestamp
201 * @param string $ts
203 private function setParserTS( $ts ) {
204 $this->testParser->Options()->setTimestamp( $ts );
205 $this->testParser->mRevisionTimestamp = $ts;
209 * Assertion helper to test a magic variable output
210 * @param string|int $expected
211 * @param string $magic
213 private function assertMagic( $expected, $magic ) {
214 if ( in_array( $magic, $this->expectedAsInteger ) ) {
215 $expected = (int)$expected;
218 # Generate a message for the assertion
219 $msg = sprintf( "Magic %s should be <%s:%s>",
220 $magic,
221 $expected,
222 gettype( $expected )
225 $this->assertSame(
226 $expected,
227 $this->testParser->getVariableValue( $magic ),
228 $msg