MediaWiki REL1_31
MagicVariableTest.php
Go to the documentation of this file.
1<?php
22 private $testParser = null;
23
32 'revisionday',
33 'revisionmonth1',
34 ];
35
37 protected function setUp() {
38 parent::setUp();
39
40 $contLang = Language::factory( 'en' );
41 $this->setMwGlobals( [
42 'wgLanguageCode' => 'en',
43 'wgContLang' => $contLang,
44 ] );
45
46 $this->testParser = new Parser();
47 $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
48
49 # initialize parser output
50 $this->testParser->clearState();
51
52 # Needs a title to do magic word stuff
53 $title = Title::newFromText( 'Tests' );
54 # Else it needs a db connection just to check if it's a redirect
55 # (when deciding the page language).
56 $title->mRedirect = false;
57
58 $this->testParser->setTitle( $title );
59 }
60
65 private static function createProviderUpTo( $num ) {
66 $ret = [];
67 for ( $i = 1; $i <= $num; $i++ ) {
68 $ret[] = [ $i ];
69 }
70
71 return $ret;
72 }
73
77 public static function provideMonths() {
78 return self::createProviderUpTo( 12 );
79 }
80
84 public static function provideDays() {
85 return self::createProviderUpTo( 31 );
86 }
87
88 # ############## TESTS #############################################
89 # @todo FIXME:
90 # - those got copy pasted, we can probably make them cleaner
91 # - tests are lacking useful messages
92
93 # day
94
96 public function testCurrentdayIsUnPadded( $day ) {
97 $this->assertUnPadded( 'currentday', $day );
98 }
99
101 public function testCurrentdaytwoIsZeroPadded( $day ) {
102 $this->assertZeroPadded( 'currentday2', $day );
103 }
104
106 public function testLocaldayIsUnPadded( $day ) {
107 $this->assertUnPadded( 'localday', $day );
108 }
109
111 public function testLocaldaytwoIsZeroPadded( $day ) {
112 $this->assertZeroPadded( 'localday2', $day );
113 }
114
115 # month
116
118 public function testCurrentmonthIsZeroPadded( $month ) {
119 $this->assertZeroPadded( 'currentmonth', $month );
120 }
121
123 public function testCurrentmonthoneIsUnPadded( $month ) {
124 $this->assertUnPadded( 'currentmonth1', $month );
125 }
126
128 public function testLocalmonthIsZeroPadded( $month ) {
129 $this->assertZeroPadded( 'localmonth', $month );
130 }
131
133 public function testLocalmonthoneIsUnPadded( $month ) {
134 $this->assertUnPadded( 'localmonth1', $month );
135 }
136
137 # revision day
138
140 public function testRevisiondayIsUnPadded( $day ) {
141 $this->assertUnPadded( 'revisionday', $day );
142 }
143
145 public function testRevisiondaytwoIsZeroPadded( $day ) {
146 $this->assertZeroPadded( 'revisionday2', $day );
147 }
148
149 # revision month
150
152 public function testRevisionmonthIsZeroPadded( $month ) {
153 $this->assertZeroPadded( 'revisionmonth', $month );
154 }
155
157 public function testRevisionmonthoneIsUnPadded( $month ) {
158 $this->assertUnPadded( 'revisionmonth1', $month );
159 }
160
161 # ############## HELPERS ############################################
162
164 public function assertZeroPadded( $magic, $value ) {
165 $this->assertMagicPadding( $magic, $value, '%02d' );
166 }
167
169 public function assertUnPadded( $magic, $value ) {
170 $this->assertMagicPadding( $magic, $value, '%d' );
171 }
172
179 private function assertMagicPadding( $magic, $value, $format ) {
180 # Initialize parser timestamp as year 2010 at 12h34 56s.
181 # month and day are given by the caller ($value). Month < 12!
182 if ( $value > 12 ) {
183 $month = $value % 12;
184 } else {
185 $month = $value;
186 }
187
188 $this->setParserTS(
189 sprintf( '2010%02d%02d123456', $month, $value )
190 );
191
192 # please keep the following commented line of code. It helps debugging.
193 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
194
195 # format expectation and test it
196 $expected = sprintf( $format, $value );
197 $this->assertMagic( $expected, $magic );
198 }
199
204 private function setParserTS( $ts ) {
205 $this->testParser->Options()->setTimestamp( $ts );
206 $this->testParser->mRevisionTimestamp = $ts;
207 }
208
214 private function assertMagic( $expected, $magic ) {
215 if ( in_array( $magic, $this->expectedAsInteger ) ) {
216 $expected = (int)$expected;
217 }
218
219 # Generate a message for the assertion
220 $msg = sprintf( "Magic %s should be <%s:%s>",
221 $magic,
222 $expected,
223 gettype( $expected )
224 );
225
226 $this->assertSame(
227 $expected,
228 $this->testParser->getVariableValue( $magic ),
229 $msg
230 );
231 }
232}
Database Parser::getVariableValue.
assertZeroPadded( $magic, $value)
assertion helper expecting a magic output which is zero padded
setParserTS( $ts)
helper to set the parser timestamp and revision timestamp
static createProviderUpTo( $num)
testRevisiondayIsUnPadded( $day)
provideDays
setUp()
setup a basic parser object
testLocaldaytwoIsZeroPadded( $day)
provideDays
testCurrentdayIsUnPadded( $day)
provideDays
$expectedAsInteger
An array of magicword returned as type integer by the parser They are usually returned as a string fo...
testLocalmonthoneIsUnPadded( $month)
provideMonths
testLocaldayIsUnPadded( $day)
provideDays
testRevisiondaytwoIsZeroPadded( $day)
provideDays
testCurrentdaytwoIsZeroPadded( $day)
provideDays
testRevisionmonthIsZeroPadded( $month)
provideMonths
assertMagic( $expected, $magic)
Assertion helper to test a magic variable output.
testCurrentmonthIsZeroPadded( $month)
provideMonths
assertMagicPadding( $magic, $value, $format)
Main assertion helper for magic variables padding.
testRevisionmonthoneIsUnPadded( $month)
provideMonths
testLocalmonthIsZeroPadded( $month)
provideMonths
assertUnPadded( $magic, $value)
assertion helper expecting a magic output which is unpadded
testCurrentmonthoneIsUnPadded( $month)
provideMonths
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005