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