SECURITY: blacklist CSS var()
[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 Antoine Musso
10 * @copyright Copyright © 2011, Antoine Musso
11 * @file
12 */
13
14 /**
15 * @group Database
16 * @covers Parser::getVariableValue
17 */
18 class MagicVariableTest extends MediaWikiTestCase {
19 /**
20 * @var Parser
21 */
22 private $testParser = null;
23
24 /**
25 * An array of magicword returned as type integer by the parser
26 * They are usually returned as a string for i18n since we support
27 * persan numbers for example, but some magic explicitly return
28 * them as integer.
29 * @see MagicVariableTest::assertMagic()
30 */
31 private $expectedAsInteger = [
32 'revisionday',
33 'revisionmonth1',
34 ];
35
36 /** setup a basic parser object */
37 protected function setUp() {
38 parent::setUp();
39
40 $contLang = Language::factory( 'en' );
41 $this->setContentLang( $contLang );
42
43 $this->testParser = new Parser();
44 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
45
46 # initialize parser output
47 $this->testParser->clearState();
48
49 # Needs a title to do magic word stuff
50 $title = Title::newFromText( 'Tests' );
51 # Else it needs a db connection just to check if it's a redirect
52 # (when deciding the page language).
53 $title->mRedirect = false;
54
55 $this->testParser->setTitle( $title );
56 }
57
58 /**
59 * @param int $num Upper limit for numbers
60 * @return array Array of numbers from 1 up to $num
61 */
62 private static function createProviderUpTo( $num ) {
63 $ret = [];
64 for ( $i = 1; $i <= $num; $i++ ) {
65 $ret[] = [ $i ];
66 }
67
68 return $ret;
69 }
70
71 /**
72 * @return array Array of months numbers (as an integer)
73 */
74 public static function provideMonths() {
75 return self::createProviderUpTo( 12 );
76 }
77
78 /**
79 * @return array Array of days numbers (as an integer)
80 */
81 public static function provideDays() {
82 return self::createProviderUpTo( 31 );
83 }
84
85 # ############## TESTS #############################################
86 # @todo FIXME:
87 # - those got copy pasted, we can probably make them cleaner
88 # - tests are lacking useful messages
89
90 # day
91
92 /** @dataProvider provideDays */
93 public function testCurrentdayIsUnPadded( $day ) {
94 $this->assertUnPadded( 'currentday', $day );
95 }
96
97 /** @dataProvider provideDays */
98 public function testCurrentdaytwoIsZeroPadded( $day ) {
99 $this->assertZeroPadded( 'currentday2', $day );
100 }
101
102 /** @dataProvider provideDays */
103 public function testLocaldayIsUnPadded( $day ) {
104 $this->assertUnPadded( 'localday', $day );
105 }
106
107 /** @dataProvider provideDays */
108 public function testLocaldaytwoIsZeroPadded( $day ) {
109 $this->assertZeroPadded( 'localday2', $day );
110 }
111
112 # month
113
114 /** @dataProvider provideMonths */
115 public function testCurrentmonthIsZeroPadded( $month ) {
116 $this->assertZeroPadded( 'currentmonth', $month );
117 }
118
119 /** @dataProvider provideMonths */
120 public function testCurrentmonthoneIsUnPadded( $month ) {
121 $this->assertUnPadded( 'currentmonth1', $month );
122 }
123
124 /** @dataProvider provideMonths */
125 public function testLocalmonthIsZeroPadded( $month ) {
126 $this->assertZeroPadded( 'localmonth', $month );
127 }
128
129 /** @dataProvider provideMonths */
130 public function testLocalmonthoneIsUnPadded( $month ) {
131 $this->assertUnPadded( 'localmonth1', $month );
132 }
133
134 # revision day
135
136 /** @dataProvider provideDays */
137 public function testRevisiondayIsUnPadded( $day ) {
138 $this->assertUnPadded( 'revisionday', $day );
139 }
140
141 /** @dataProvider provideDays */
142 public function testRevisiondaytwoIsZeroPadded( $day ) {
143 $this->assertZeroPadded( 'revisionday2', $day );
144 }
145
146 # revision month
147
148 /** @dataProvider provideMonths */
149 public function testRevisionmonthIsZeroPadded( $month ) {
150 $this->assertZeroPadded( 'revisionmonth', $month );
151 }
152
153 /** @dataProvider provideMonths */
154 public function testRevisionmonthoneIsUnPadded( $month ) {
155 $this->assertUnPadded( 'revisionmonth1', $month );
156 }
157
158 # ############## HELPERS ############################################
159
160 /** assertion helper expecting a magic output which is zero padded */
161 public function assertZeroPadded( $magic, $value ) {
162 $this->assertMagicPadding( $magic, $value, '%02d' );
163 }
164
165 /** assertion helper expecting a magic output which is unpadded */
166 public function assertUnPadded( $magic, $value ) {
167 $this->assertMagicPadding( $magic, $value, '%d' );
168 }
169
170 /**
171 * Main assertion helper for magic variables padding
172 * @param string $magic Magic variable name
173 * @param mixed $value Month or day
174 * @param string $format Sprintf format for $value
175 */
176 private function assertMagicPadding( $magic, $value, $format ) {
177 # Initialize parser timestamp as year 2010 at 12h34 56s.
178 # month and day are given by the caller ($value). Month < 12!
179 if ( $value > 12 ) {
180 $month = $value % 12;
181 } else {
182 $month = $value;
183 }
184
185 $this->setParserTS(
186 sprintf( '2010%02d%02d123456', $month, $value )
187 );
188
189 # please keep the following commented line of code. It helps debugging.
190 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
191
192 # format expectation and test it
193 $expected = sprintf( $format, $value );
194 $this->assertMagic( $expected, $magic );
195 }
196
197 /**
198 * helper to set the parser timestamp and revision timestamp
199 * @param string $ts
200 */
201 private function setParserTS( $ts ) {
202 $this->testParser->Options()->setTimestamp( $ts );
203 $this->testParser->mRevisionTimestamp = $ts;
204 }
205
206 /**
207 * Assertion helper to test a magic variable output
208 * @param string|int $expected
209 * @param string $magic
210 */
211 private function assertMagic( $expected, $magic ) {
212 if ( in_array( $magic, $this->expectedAsInteger ) ) {
213 $expected = (int)$expected;
214 }
215
216 # Generate a message for the assertion
217 $msg = sprintf( "Magic %s should be <%s:%s>",
218 $magic,
219 $expected,
220 gettype( $expected )
221 );
222
223 $this->assertSame(
224 $expected,
225 $this->testParser->getVariableValue( $magic ),
226 $msg
227 );
228 }
229 }