Manually set the title as not being a redirect. Otherwise,
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / MagicVariableTest.php
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
5 *
6 * As of february 2011, it only tests some revisions and date related
7 * magic variables.
8 *
9 * @author Ashar Voultoiz
10 * @copyright Copyright © 2011, Ashar Voultoiz
11 * @file
12 */
13
14 /** */
15 class MagicVariableTest extends MediaWikiTestCase {
16 /** Will contains a parser object*/
17 private $testParser = null;
18
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()
25 */
26 private $expectedAsInteger = array(
27 'revisionday',
28 'revisionmonth1',
29 );
30
31 /** setup a basic parser object */
32 function setUp() {
33 global $wgContLang;
34 $wgContLang = Language::factory( 'en' );
35
36 $this->testParser = new Parser();
37 $this->testParser->Options( new ParserOptions() );
38
39 # initialize parser output
40 $this->testParser->clearState();
41
42 # Needs a title to do magic word stuff
43 $title = Title::newFromText( 'Tests' );
44 $title->mRedirect = false; # Else it needs a db connection just to check if it's a redirect (when deciding the page language)
45
46 $this->testParser->setTitle( $title );
47 }
48
49 /** destroy parser (TODO: is it really neded?)*/
50 function tearDown() {
51 unset( $this->testParser );
52 }
53
54 ############### TESTS #############################################
55 # @todo FIXME:
56 # - those got copy pasted, we can probably make them cleaner
57 # - tests are lacking useful messages
58
59 # day
60
61 /** @dataProvider MediaWikiProvide::Days */
62 function testCurrentdayIsUnPadded( $day ) {
63 $this->assertUnPadded( 'currentday', $day );
64 }
65 /** @dataProvider MediaWikiProvide::Days */
66 function testCurrentdaytwoIsZeroPadded( $day ) {
67 $this->assertZeroPadded( 'currentday2', $day );
68 }
69 /** @dataProvider MediaWikiProvide::Days */
70 function testLocaldayIsUnPadded( $day ) {
71 $this->assertUnPadded( 'localday', $day );
72 }
73 /** @dataProvider MediaWikiProvide::Days */
74 function testLocaldaytwoIsZeroPadded( $day ) {
75 $this->assertZeroPadded( 'localday2', $day );
76 }
77
78 # month
79
80 /** @dataProvider MediaWikiProvide::Months */
81 function testCurrentmonthIsZeroPadded( $month ) {
82 $this->assertZeroPadded( 'currentmonth', $month );
83 }
84 /** @dataProvider MediaWikiProvide::Months */
85 function testCurrentmonthoneIsUnPadded( $month ) {
86 $this->assertUnPadded( 'currentmonth1', $month );
87 }
88 /** @dataProvider MediaWikiProvide::Months */
89 function testLocalmonthIsZeroPadded( $month ) {
90 $this->assertZeroPadded( 'localmonth', $month );
91 }
92 /** @dataProvider MediaWikiProvide::Months */
93 function testLocalmonthoneIsUnPadded( $month ) {
94 $this->assertUnPadded( 'localmonth1', $month );
95 }
96
97
98 # revision day
99
100 /** @dataProvider MediaWikiProvide::Days */
101 function testRevisiondayIsUnPadded( $day ) {
102 $this->assertUnPadded( 'revisionday', $day );
103 }
104 /** @dataProvider MediaWikiProvide::Days */
105 function testRevisiondaytwoIsZeroPadded( $day ) {
106 $this->assertZeroPadded( 'revisionday2', $day );
107 }
108
109 # revision month
110
111 /** @dataProvider MediaWikiProvide::Months */
112 function testRevisionmonthIsZeroPadded( $month ) {
113 $this->assertZeroPadded( 'revisionmonth', $month );
114 }
115 /** @dataProvider MediaWikiProvide::Months */
116 function testRevisionmonthoneIsUnPadded( $month ) {
117 $this->assertUnPadded( 'revisionmonth1', $month );
118 }
119
120 ############### HELPERS ############################################
121
122 /** assertion helper expecting a magic output which is zero padded */
123 PUBLIC function assertZeroPadded( $magic, $value ) {
124 $this->assertMagicPadding( $magic, $value, '%02d' );
125 }
126
127 /** assertion helper expecting a magic output which is unpadded */
128 PUBLIC function assertUnPadded( $magic, $value ) {
129 $this->assertMagicPadding( $magic, $value, '%d' );
130 }
131
132 /**
133 * Main assertion helper for magic variables padding
134 * @param $magic string Magic variable name
135 * @param $value mixed Month or day
136 * @param $format string sprintf format for $value
137 */
138 private function assertMagicPadding( $magic, $value, $format ) {
139 # Initialize parser timestamp as year 2010 at 12h34 56s.
140 # month and day are given by the caller ($value). Month < 12!
141 if( $value > 12 ) { $month = $value % 12; }
142 else { $month = $value; }
143
144 $this->setParserTS(
145 sprintf( '2010%02d%02d123456', $month, $value )
146 );
147
148 # please keep the following commented line of code. It helps debugging.
149 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
150
151 # format expectation and test it
152 $expected = sprintf( $format, $value );
153 $this->assertMagic( $expected, $magic );
154 }
155
156 /** helper to set the parser timestamp and revision timestamp */
157 private function setParserTS( $ts ) {
158 $this->testParser->Options()->setTimestamp( $ts );
159 $this->testParser->mRevisionTimestamp = $ts;
160 }
161
162 /**
163 * Assertion helper to test a magic variable output
164 */
165 private function assertMagic( $expected, $magic ) {
166 if( in_array( $magic, $this->expectedAsInteger ) ) {
167 $expected = (int) $expected;
168 }
169
170 # Generate a message for the assertion
171 $msg = sprintf( "Magic %s should be <%s:%s>",
172 $magic,
173 $expected,
174 gettype( $expected )
175 );
176
177 $this->assertSame(
178 $expected,
179 $this->testParser->getVariableValue( $magic ),
180 $msg
181 );
182 }
183 }