| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
00 |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
o |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
namespace MediaWiki\Tests\Parser; |
| 16 |
|
| 17 |
use MediaWiki\MainConfigNames; |
| 18 |
use MediaWiki\Parser\Parser; |
| 19 |
use MediaWiki\Parser\ParserOptions; |
| 20 |
use MediaWiki\Title\Title; |
| 21 |
use MediaWiki\User\User; |
| 22 |
use MediaWikiIntegrationTestCase; |
| 23 |
use Wikimedia\TestingAccessWrapper; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class MagicVariableTest extends MediaWikiIntegrationTestCase { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private $testParser = null; |
| 34 |
|
| 35 |
|
| 36 |
protected function setUp(): void { |
| 37 |
parent::setUp(); |
| 38 |
|
| 39 |
$services = $this->getServiceContainer(); |
| 40 |
$contLang = $services->getLanguageFactory()->getLanguage( 'en' ); |
| 41 |
$this->setService( 'ContentLanguage', $contLang ); |
| 42 |
$this->overrideConfigValues( [ |
| 43 |
MainConfigNames::LanguageCode => $contLang->getCode(), |
| 44 |
|
| 45 |
MainConfigNames::Localtimezone => 'Europe/Stockholm', |
| 46 |
MainConfigNames::MiserMode => false, |
| 47 |
MainConfigNames::ParserCacheExpireTime => 86400 * 7, |
| 48 |
] ); |
| 49 |
|
| 50 |
$this->testParser = $services->getParserFactory()->create(); |
| 51 |
$this->testParser->setOptions( ParserOptions::newFromUserAndLang( new User, $contLang ) ); |
| 52 |
|
| 53 |
|
| 54 |
$this->testParser->clearState(); |
| 55 |
|
| 56 |
|
| 57 |
$title = Title::makeTitle( NS_MAIN, 'Tests' ); |
| 58 |
|
| 59 |
|
| 60 |
$title->mRedirect = false; |
| 61 |
|
| 62 |
$this->testParser->setTitle( $title ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private static function createProviderUpTo( $num ) { |
| 70 |
$ret = []; |
| 71 |
for ( $i = 1; $i <= $num; $i++ ) { |
| 72 |
$ret[] = [ strval( $i ) ]; |
| 73 |
} |
| 74 |
|
| 75 |
return $ret; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static function provideMonths() { |
| 82 |
return self::createProviderUpTo( 12 ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static function provideDays() { |
| 89 |
return self::createProviderUpTo( 31 ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function testCurrentdayIsUnPadded( $day ) { |
| 101 |
$this->assertUnPadded( 'currentday', $day ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
public function testCurrentdaytwoIsZeroPadded( $day ) { |
| 106 |
$this->assertZeroPadded( 'currentday2', $day ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public function testLocaldayIsUnPadded( $day ) { |
| 111 |
$this->assertUnPadded( 'localday', $day ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public function testLocaldaytwoIsZeroPadded( $day ) { |
| 116 |
$this->assertZeroPadded( 'localday2', $day ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function testCurrentmonthIsZeroPadded( $month ) { |
| 123 |
$this->assertZeroPadded( 'currentmonth', $month ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public function testCurrentmonthoneIsUnPadded( $month ) { |
| 128 |
$this->assertUnPadded( 'currentmonth1', $month ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public function testLocalmonthIsZeroPadded( $month ) { |
| 133 |
$this->assertZeroPadded( 'localmonth', $month ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
public function testLocalmonthoneIsUnPadded( $month ) { |
| 138 |
$this->assertUnPadded( 'localmonth1', $month ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testRevisiondayIsUnPadded( $day ) { |
| 145 |
$this->assertUnPadded( 'revisionday', $day ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
public function testRevisiondaytwoIsZeroPadded( $day ) { |
| 150 |
$this->assertZeroPadded( 'revisionday2', $day ); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function testRevisionmonthIsZeroPadded( $month ) { |
| 157 |
$this->assertZeroPadded( 'revisionmonth', $month ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
public function testRevisionmonthoneIsUnPadded( $month ) { |
| 162 |
$this->assertUnPadded( 'revisionmonth1', $month ); |
| 163 |
} |
| 164 |
|
| 165 |
public static function provideCurrentUnitTimestampWords() { |
| 166 |
return [ |
| 167 |
|
| 168 |
[ 'currentmonth', '20200208153011', '02', 604800 ], |
| 169 |
[ 'currentmonth1', '20200208153011', '2', 604800 ], |
| 170 |
[ 'currentmonthname', '20200208153011', 'February', 604800 ], |
| 171 |
[ 'currentmonthnamegen', '20200208153011', 'February', 604800 ], |
| 172 |
[ 'currentmonthabbrev', '20200208153011', 'Feb', 604800 ], |
| 173 |
[ 'currentday', '20200208153011', '8', 30601 ], |
| 174 |
[ 'currentday2', '20200208153011', '08', 30601 ], |
| 175 |
[ 'currentdayname', '20200208153011', 'Saturday', 30601 ], |
| 176 |
[ 'currentyear', '20200208153011', '2020', 604800 ], |
| 177 |
[ 'currenthour', '20200208153011', '15', 1801 ], |
| 178 |
[ 'currentweek', '20200208153011', '6', 30601 ], |
| 179 |
[ 'currentdow', '20200208153011', '6', 30601 ], |
| 180 |
[ 'currenttime', '20200208153011', '15:30', 3600 ], |
| 181 |
|
| 182 |
[ 'currentmonth', '20200208223011', '02', 604800 ], |
| 183 |
[ 'currentmonth1', '20200208223011', '2', 604800 ], |
| 184 |
[ 'currentmonthname', '20200208223011', 'February', 604800 ], |
| 185 |
[ 'currentmonthnamegen', '20200208223011', 'February', 604800 ], |
| 186 |
[ 'currentmonthabbrev', '20200208223011', 'Feb', 604800 ], |
| 187 |
[ 'currentday', '20200208223011', '8', 5401 ], |
| 188 |
[ 'currentday2', '20200208223011', '08', 5401 ], |
| 189 |
[ 'currentdayname', '20200208223011', 'Saturday', 5401 ], |
| 190 |
[ 'currentyear', '20200208223011', '2020', 604800 ], |
| 191 |
[ 'currenthour', '20200208223011', '22', 1801 ], |
| 192 |
[ 'currentweek', '20200208223011', '6', 5401 ], |
| 193 |
[ 'currentdow', '20200208223011', '6', 5401 ], |
| 194 |
[ 'currenttime', '20200208223011', '22:30', 3600 ] |
| 195 |
]; |
| 196 |
} |
| 197 |
|
| 198 |
public static function provideLocalUnitTimestampWords() { |
| 199 |
|
| 200 |
return [ |
| 201 |
|
| 202 |
[ 'localmonth', '20200208153011', '02', 604800 ], |
| 203 |
[ 'localmonth1', '20200208153011', '2', 604800 ], |
| 204 |
[ 'localmonthname', '20200208153011', 'February', 604800 ], |
| 205 |
[ 'localmonthnamegen', '20200208153011', 'February', 604800 ], |
| 206 |
[ 'localmonthabbrev', '20200208153011', 'Feb', 604800 ], |
| 207 |
[ 'localday', '20200208153011', '8', 27001 ], |
| 208 |
[ 'localday2', '20200208153011', '08', 27001 ], |
| 209 |
[ 'localdayname', '20200208153011', 'Saturday', 27001 ], |
| 210 |
[ 'localyear', '20200208153011', '2020', 604800 ], |
| 211 |
[ 'localhour', '20200208153011', '16', 1801 ], |
| 212 |
[ 'localweek', '20200208153011', '6', 27001 ], |
| 213 |
[ 'localdow', '20200208153011', '6', 27001 ], |
| 214 |
[ 'localtime', '20200208153011', '16:30', 3600 ], |
| 215 |
|
| 216 |
[ 'localmonth', '20200208223011', '02', 604800 ], |
| 217 |
[ 'localmonth1', '20200208223011', '2', 604800 ], |
| 218 |
[ 'localmonthname', '20200208223011', 'February', 604800 ], |
| 219 |
[ 'localmonthnamegen', '20200208223011', 'February', 604800 ], |
| 220 |
[ 'localmonthabbrev', '20200208223011', 'Feb', 604800 ], |
| 221 |
[ 'localday', '20200208223011', '8', 1801 ], |
| 222 |
[ 'localday2', '20200208223011', '08', 1801 ], |
| 223 |
[ 'localdayname', '20200208223011', 'Saturday', 1801 ], |
| 224 |
[ 'localyear', '20200208223011', '2020', 604800 ], |
| 225 |
[ 'localhour', '20200208223011', '23', 1801 ], |
| 226 |
[ 'localweek', '20200208223011', '6', 1801 ], |
| 227 |
[ 'localdow', '20200208223011', '6', 1801 ], |
| 228 |
[ 'localtime', '20200208223011', '23:30', 3600 ], |
| 229 |
|
| 230 |
[ 'localmonth', '20200208233011', '02', 604800 ], |
| 231 |
[ 'localmonth1', '20200208233011', '2', 604800 ], |
| 232 |
[ 'localmonthname', '20200208233011', 'February', 604800 ], |
| 233 |
[ 'localmonthnamegen', '20200208233011', 'February', 604800 ], |
| 234 |
[ 'localmonthabbrev', '20200208233011', 'Feb', 604800 ], |
| 235 |
[ 'localday', '20200208233011', '9', 84601 ], |
| 236 |
[ 'localday2', '20200208233011', '09', 84601 ], |
| 237 |
[ 'localdayname', '20200208233011', 'Sunday', 84601 ], |
| 238 |
[ 'localyear', '20200208233011', '2020', 604800 ], |
| 239 |
[ 'localhour', '20200208233011', '00', 1801 ], |
| 240 |
[ 'localweek', '20200208233011', '6', 84601 ], |
| 241 |
[ 'localdow', '20200208233011', '0', 84601 ], |
| 242 |
[ 'localtime', '20200208233011', '00:30', 3600 ] |
| 243 |
]; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function testCurrentUnitTimestampExpiry( $word, $ts, $expOutput, $expTTL ) { |
| 255 |
$this->setParserTimestamp( $ts ); |
| 256 |
|
| 257 |
$this->assertMagic( $expOutput, $word ); |
| 258 |
$this->assertSame( $expTTL, $this->testParser->getOutput()->getCacheExpiry() ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function assertZeroPadded( $magic, $value ) { |
| 269 |
$this->assertMagicPadding( $magic, $value, '%02d' ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function assertUnPadded( $magic, $value ) { |
| 278 |
$this->assertMagicPadding( $magic, $value, '%d' ); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
private function assertMagicPadding( $magic, $value, $format ) { |
| 288 |
|
| 289 |
|
| 290 |
if ( $value > 12 ) { |
| 291 |
$month = $value % 12; |
| 292 |
} else { |
| 293 |
$month = $value; |
| 294 |
} |
| 295 |
|
| 296 |
$this->setParserTimestamp( |
| 297 |
sprintf( '2010%02d%02d123456', $month, $value ) |
| 298 |
); |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
$expected = sprintf( $format, $value ); |
| 305 |
$this->assertMagic( $expected, $magic ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
private function setParserTimestamp( $ts ) { |
| 313 |
$this->testParser->getOptions()->setTimestamp( $ts ); |
| 314 |
TestingAccessWrapper::newFromObject( $this->testParser )->mRevisionTimestamp = $ts; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private function assertMagic( $expected, $magic ) { |
| 323 |
|
| 324 |
$msg = sprintf( "Magic %s should be <%s:%s>", |
| 325 |
$magic, |
| 326 |
$expected, |
| 327 |
get_debug_type( $expected ) |
| 328 |
); |
| 329 |
|
| 330 |
$this->assertSame( |
| 331 |
$expected, |
| 332 |
TestingAccessWrapper::newFromObject( $this->testParser )->expandMagicVariable( $magic ), |
| 333 |
$msg |
| 334 |
); |
| 335 |
} |
| 336 |
} |