2011-02-02 21:04:21 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This file is intended to test magic variables in the parser
|
|
|
|
|
* It was inspired by Raymond & Matěj Grabovský commenting about r66200
|
|
|
|
|
*
|
|
|
|
|
* As of february 2011, it only tests some revisions and date related
|
|
|
|
|
* magic variables.
|
|
|
|
|
*
|
2011-10-24 09:08:13 +00:00
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2011, Antoine Musso
|
2011-02-02 21:04:21 +00:00
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2017-12-28 08:22:35 +00:00
|
|
|
/**
|
2018-03-09 21:56:41 +00:00
|
|
|
* @group Database
|
2017-12-28 08:22:35 +00:00
|
|
|
* @covers Parser::getVariableValue
|
|
|
|
|
*/
|
2011-05-25 00:30:06 +00:00
|
|
|
class MagicVariableTest extends MediaWikiTestCase {
|
2013-10-22 09:46:48 +00:00
|
|
|
/**
|
|
|
|
|
* @var Parser
|
|
|
|
|
*/
|
2011-02-02 21:04:21 +00:00
|
|
|
private $testParser = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An array of magicword returned as type integer by the parser
|
|
|
|
|
* They are usually returned as a string for i18n since we support
|
|
|
|
|
* persan numbers for example, but some magic explicitly return
|
|
|
|
|
* them as integer.
|
|
|
|
|
* @see MagicVariableTest::assertMagic()
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private $expectedAsInteger = [
|
2011-02-02 21:04:21 +00:00
|
|
|
'revisionday',
|
|
|
|
|
'revisionmonth1',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-02-02 21:04:21 +00:00
|
|
|
|
|
|
|
|
/** setup a basic parser object */
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2012-10-14 19:34:26 +00:00
|
|
|
$contLang = Language::factory( 'en' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2012-11-05 10:10:42 +00:00
|
|
|
'wgLanguageCode' => 'en',
|
|
|
|
|
'wgContLang' => $contLang,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-02-02 21:04:21 +00:00
|
|
|
|
|
|
|
|
$this->testParser = new Parser();
|
2012-10-14 19:34:26 +00:00
|
|
|
$this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
|
2011-02-02 21:04:21 +00:00
|
|
|
|
|
|
|
|
# initialize parser output
|
|
|
|
|
$this->testParser->clearState();
|
2011-08-27 21:10:41 +00:00
|
|
|
|
2011-08-17 00:46:58 +00:00
|
|
|
# Needs a title to do magic word stuff
|
2011-08-27 21:10:41 +00:00
|
|
|
$title = Title::newFromText( 'Tests' );
|
2014-04-24 17:52:09 +00:00
|
|
|
# Else it needs a db connection just to check if it's a redirect
|
|
|
|
|
# (when deciding the page language).
|
|
|
|
|
$title->mRedirect = false;
|
2011-08-27 21:10:41 +00:00
|
|
|
|
|
|
|
|
$this->testParser->setTitle( $title );
|
2011-02-02 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/**
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param int $num Upper limit for numbers
|
|
|
|
|
* @return array Array of numbers from 1 up to $num
|
2013-10-22 09:46:48 +00:00
|
|
|
*/
|
|
|
|
|
private static function createProviderUpTo( $num ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2013-10-22 09:46:48 +00:00
|
|
|
for ( $i = 1; $i <= $num; $i++ ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret[] = [ $i ];
|
2013-10-22 09:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-17 18:43:42 +00:00
|
|
|
* @return array Array of months numbers (as an integer)
|
2013-10-22 09:46:48 +00:00
|
|
|
*/
|
|
|
|
|
public static function provideMonths() {
|
|
|
|
|
return self::createProviderUpTo( 12 );
|
|
|
|
|
}
|
2012-10-08 10:56:20 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/**
|
2014-04-17 18:43:42 +00:00
|
|
|
* @return array Array of days numbers (as an integer)
|
2013-10-22 09:46:48 +00:00
|
|
|
*/
|
|
|
|
|
public static function provideDays() {
|
|
|
|
|
return self::createProviderUpTo( 31 );
|
2011-02-02 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-11 13:44:59 +00:00
|
|
|
# ############## TESTS #############################################
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME:
|
2011-02-02 21:04:21 +00:00
|
|
|
# - those got copy pasted, we can probably make them cleaner
|
|
|
|
|
# - tests are lacking useful messages
|
|
|
|
|
|
|
|
|
|
# day
|
|
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testCurrentdayIsUnPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'currentday', $day );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testCurrentdaytwoIsZeroPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'currentday2', $day );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testLocaldayIsUnPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'localday', $day );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testLocaldaytwoIsZeroPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'localday2', $day );
|
|
|
|
|
}
|
2012-11-05 10:10:42 +00:00
|
|
|
|
2011-02-02 21:04:21 +00:00
|
|
|
# month
|
|
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testCurrentmonthIsZeroPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'currentmonth', $month );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testCurrentmonthoneIsUnPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'currentmonth1', $month );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testLocalmonthIsZeroPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'localmonth', $month );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testLocalmonthoneIsUnPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'localmonth1', $month );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# revision day
|
|
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testRevisiondayIsUnPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'revisionday', $day );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideDays */
|
|
|
|
|
public function testRevisiondaytwoIsZeroPadded( $day ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'revisionday2', $day );
|
|
|
|
|
}
|
2012-11-05 10:10:42 +00:00
|
|
|
|
2011-02-02 21:04:21 +00:00
|
|
|
# revision month
|
|
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testRevisionmonthIsZeroPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertZeroPadded( 'revisionmonth', $month );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2013-10-22 09:46:48 +00:00
|
|
|
/** @dataProvider provideMonths */
|
|
|
|
|
public function testRevisionmonthoneIsUnPadded( $month ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertUnPadded( 'revisionmonth1', $month );
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 13:44:59 +00:00
|
|
|
# ############## HELPERS ############################################
|
2011-02-02 21:04:21 +00:00
|
|
|
|
|
|
|
|
/** assertion helper expecting a magic output which is zero padded */
|
2013-10-22 09:46:48 +00:00
|
|
|
public function assertZeroPadded( $magic, $value ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertMagicPadding( $magic, $value, '%02d' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** assertion helper expecting a magic output which is unpadded */
|
2013-10-22 09:46:48 +00:00
|
|
|
public function assertUnPadded( $magic, $value ) {
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->assertMagicPadding( $magic, $value, '%d' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Main assertion helper for magic variables padding
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $magic Magic variable name
|
|
|
|
|
* @param mixed $value Month or day
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $format Sprintf format for $value
|
2011-02-02 21:04:21 +00:00
|
|
|
*/
|
|
|
|
|
private function assertMagicPadding( $magic, $value, $format ) {
|
|
|
|
|
# Initialize parser timestamp as year 2010 at 12h34 56s.
|
|
|
|
|
# month and day are given by the caller ($value). Month < 12!
|
2013-02-15 10:24:31 +00:00
|
|
|
if ( $value > 12 ) {
|
|
|
|
|
$month = $value % 12;
|
|
|
|
|
} else {
|
|
|
|
|
$month = $value;
|
|
|
|
|
}
|
2012-11-05 10:10:42 +00:00
|
|
|
|
2011-02-02 21:04:21 +00:00
|
|
|
$this->setParserTS(
|
|
|
|
|
sprintf( '2010%02d%02d123456', $month, $value )
|
|
|
|
|
);
|
|
|
|
|
|
2012-11-05 10:10:42 +00:00
|
|
|
# please keep the following commented line of code. It helps debugging.
|
2015-09-11 13:44:59 +00:00
|
|
|
// print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
|
2011-02-02 21:04:21 +00:00
|
|
|
|
|
|
|
|
# format expectation and test it
|
|
|
|
|
$expected = sprintf( $format, $value );
|
2012-11-05 10:10:42 +00:00
|
|
|
$this->assertMagic( $expected, $magic );
|
2011-02-02 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-17 18:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* helper to set the parser timestamp and revision timestamp
|
|
|
|
|
* @param string $ts
|
|
|
|
|
*/
|
2011-02-02 21:04:21 +00:00
|
|
|
private function setParserTS( $ts ) {
|
|
|
|
|
$this->testParser->Options()->setTimestamp( $ts );
|
|
|
|
|
$this->testParser->mRevisionTimestamp = $ts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion helper to test a magic variable output
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string|int $expected
|
|
|
|
|
* @param string $magic
|
2011-02-02 21:04:21 +00:00
|
|
|
*/
|
|
|
|
|
private function assertMagic( $expected, $magic ) {
|
2013-02-15 10:24:31 +00:00
|
|
|
if ( in_array( $magic, $this->expectedAsInteger ) ) {
|
2013-01-28 10:27:15 +00:00
|
|
|
$expected = (int)$expected;
|
2011-02-02 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Generate a message for the assertion
|
|
|
|
|
$msg = sprintf( "Magic %s should be <%s:%s>",
|
|
|
|
|
$magic,
|
|
|
|
|
$expected,
|
|
|
|
|
gettype( $expected )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$expected,
|
|
|
|
|
$this->testParser->getVariableValue( $magic ),
|
|
|
|
|
$msg
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|