* upgrade patches for oracle 1.17->1.19
[mediawiki.git] / tests / phpunit / includes / parser / MagicVariableTest.php
blob3c8430952ef7d8d5cb1fd25644ff12920686ad5c
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 Ashar Voultoiz
10 * @copyright Copyright © 2011, Ashar Voultoiz
11 * @file
14 /** */
15 class MagicVariableTest extends MediaWikiTestCase {
16 /** Will contains a parser object*/
17 private $testParser = null;
19 /**
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
23 * them as integer.
24 * @see MagicVariableTest::assertMagic()
26 private $expectedAsInteger = array(
27 'revisionday',
28 'revisionmonth1',
31 /** setup a basic parser object */
32 function setUp() {
33 global $wgContLang;
34 $wgContLang = Language::factory( 'en' );
36 $this->testParser = new Parser();
37 $this->testParser->Options( new ParserOptions() );
39 # initialize parser output
40 $this->testParser->clearState();
43 /** destroy parser (TODO: is it really neded?)*/
44 function tearDown() {
45 unset( $this->testParser );
48 ############### TESTS #############################################
49 # @todo FIXME:
50 # - those got copy pasted, we can probably make them cleaner
51 # - tests are lacking useful messages
53 # day
55 /** @dataProvider MediaWikiProvide::Days */
56 function testCurrentdayIsUnPadded( $day ) {
57 $this->assertUnPadded( 'currentday', $day );
59 /** @dataProvider MediaWikiProvide::Days */
60 function testCurrentdaytwoIsZeroPadded( $day ) {
61 $this->assertZeroPadded( 'currentday2', $day );
63 /** @dataProvider MediaWikiProvide::Days */
64 function testLocaldayIsUnPadded( $day ) {
65 $this->assertUnPadded( 'localday', $day );
67 /** @dataProvider MediaWikiProvide::Days */
68 function testLocaldaytwoIsZeroPadded( $day ) {
69 $this->assertZeroPadded( 'localday2', $day );
72 # month
74 /** @dataProvider MediaWikiProvide::Months */
75 function testCurrentmonthIsZeroPadded( $month ) {
76 $this->assertZeroPadded( 'currentmonth', $month );
78 /** @dataProvider MediaWikiProvide::Months */
79 function testCurrentmonthoneIsUnPadded( $month ) {
80 $this->assertUnPadded( 'currentmonth1', $month );
82 /** @dataProvider MediaWikiProvide::Months */
83 function testLocalmonthIsZeroPadded( $month ) {
84 $this->assertZeroPadded( 'localmonth', $month );
86 /** @dataProvider MediaWikiProvide::Months */
87 function testLocalmonthoneIsUnPadded( $month ) {
88 $this->assertUnPadded( 'localmonth1', $month );
92 # revision day
94 /** @dataProvider MediaWikiProvide::Days */
95 function testRevisiondayIsUnPadded( $day ) {
96 $this->assertUnPadded( 'revisionday', $day );
98 /** @dataProvider MediaWikiProvide::Days */
99 function testRevisiondaytwoIsZeroPadded( $day ) {
100 $this->assertZeroPadded( 'revisionday2', $day );
103 # revision month
105 /** @dataProvider MediaWikiProvide::Months */
106 function testRevisionmonthIsZeroPadded( $month ) {
107 $this->assertZeroPadded( 'revisionmonth', $month );
109 /** @dataProvider MediaWikiProvide::Months */
110 function testRevisionmonthoneIsUnPadded( $month ) {
111 $this->assertUnPadded( 'revisionmonth1', $month );
114 ############### HELPERS ############################################
116 /** assertion helper expecting a magic output which is zero padded */
117 PUBLIC function assertZeroPadded( $magic, $value ) {
118 $this->assertMagicPadding( $magic, $value, '%02d' );
121 /** assertion helper expecting a magic output which is unpadded */
122 PUBLIC function assertUnPadded( $magic, $value ) {
123 $this->assertMagicPadding( $magic, $value, '%d' );
127 * Main assertion helper for magic variables padding
128 * @param $magic string Magic variable name
129 * @param $value mixed Month or day
130 * @param $format string sprintf format for $value
132 private function assertMagicPadding( $magic, $value, $format ) {
133 # Initialize parser timestamp as year 2010 at 12h34 56s.
134 # month and day are given by the caller ($value). Month < 12!
135 if( $value > 12 ) { $month = $value % 12; }
136 else { $month = $value; }
138 $this->setParserTS(
139 sprintf( '2010%02d%02d123456', $month, $value )
142 # please keep the following commented line of code. It helps debugging.
143 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
145 # format expectation and test it
146 $expected = sprintf( $format, $value );
147 $this->assertMagic( $expected, $magic );
150 /** helper to set the parser timestamp and revision timestamp */
151 private function setParserTS( $ts ) {
152 $this->testParser->Options()->setTimestamp( $ts );
153 $this->testParser->mRevisionTimestamp = $ts;
157 * Assertion helper to test a magic variable output
159 private function assertMagic( $expected, $magic ) {
160 if( in_array( $magic, $this->expectedAsInteger ) ) {
161 $expected = (int) $expected;
164 # Generate a message for the assertion
165 $msg = sprintf( "Magic %s should be <%s:%s>",
166 $magic,
167 $expected,
168 gettype( $expected )
171 $this->assertSame(
172 $expected,
173 $this->testParser->getVariableValue( $magic ),
174 $msg