wiki.techinc.nl/tests/phpunit/includes/parser/MagicVariableTest.php

220 lines
5.7 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MediaWikiServices;
/**
* 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.
*
* @author Antoine Musso
* @copyright Copyright © 2011, Antoine Musso
* @file
*/
Deprecate Parser implementation methods (will be private in next release) The following public methods were renamed and made private; the old name is hard-deprecated and calls the new renamed private method: Parser::doMagicLinks() => handleMagicLinks() Parser::doDoubleUnderscore() => handleMagicLinks() Parser::doHeadings() => handleHeadings() Parser::doAllQuotes() => handleAllQuotes() Parser::replaceExternalLinks() => handleExternalLinks() Parser::replaceInternalLinks() => handleInternalLinks() Parser::replaceInternalLinks2() => handleInternalLinks2() Parser::getVariableValue() => expandMagicVariable() Parser::initialiseVariables() => initializeVariables() Parser::formatHeadings() => finalizeHeadings() Parser::test{Pst,Preprocess,Srvus}() => fuzzTest{Pst,Preprocess,Srvus}() Additionally, the following methods are not used externally, but are used outside the Parser class by core code. They have been marked @internal: Parser::doQuotes() (used by {{#displaytitle}}), Parser::getExternalLink{Rel,Attribs}() (used by Linker), Parser::normalizeLinkUrl() (used by Special:LinkSearch and elsewhere). Parser::{brace,arg,extension}Substitution() (used by PPFrame) Code search query: https://codesearch.wmflabs.org/deployed/?q=do%28MagicLinks%7CDoubleUnderscore%7CHeadings%7CAllQuotes%29%7Creplace%28ExternalLinks%7CInternalLinks%28%7C2%29%29%7CgetVariableValue%7CinitialiseVariables%7CformatHeadings%7Ctest%28Pst%7CPreprocess%7CSrvus%29%7CdoQuotes%7CgetExternalLink%28Rel%7CAttribs%29%7CnormalizeLinkUrl%7C%28brace%2Carg%2Cextension%29Substitution&i=nope&files=&repos= Bug: T236810 Change-Id: I19a43ffc5dcfdd2981b51079c33422c964acb076
2019-10-28 19:52:50 +00:00
use Wikimedia\TestingAccessWrapper;
/**
* @group Database
Deprecate Parser implementation methods (will be private in next release) The following public methods were renamed and made private; the old name is hard-deprecated and calls the new renamed private method: Parser::doMagicLinks() => handleMagicLinks() Parser::doDoubleUnderscore() => handleMagicLinks() Parser::doHeadings() => handleHeadings() Parser::doAllQuotes() => handleAllQuotes() Parser::replaceExternalLinks() => handleExternalLinks() Parser::replaceInternalLinks() => handleInternalLinks() Parser::replaceInternalLinks2() => handleInternalLinks2() Parser::getVariableValue() => expandMagicVariable() Parser::initialiseVariables() => initializeVariables() Parser::formatHeadings() => finalizeHeadings() Parser::test{Pst,Preprocess,Srvus}() => fuzzTest{Pst,Preprocess,Srvus}() Additionally, the following methods are not used externally, but are used outside the Parser class by core code. They have been marked @internal: Parser::doQuotes() (used by {{#displaytitle}}), Parser::getExternalLink{Rel,Attribs}() (used by Linker), Parser::normalizeLinkUrl() (used by Special:LinkSearch and elsewhere). Parser::{brace,arg,extension}Substitution() (used by PPFrame) Code search query: https://codesearch.wmflabs.org/deployed/?q=do%28MagicLinks%7CDoubleUnderscore%7CHeadings%7CAllQuotes%29%7Creplace%28ExternalLinks%7CInternalLinks%28%7C2%29%29%7CgetVariableValue%7CinitialiseVariables%7CformatHeadings%7Ctest%28Pst%7CPreprocess%7CSrvus%29%7CdoQuotes%7CgetExternalLink%28Rel%7CAttribs%29%7CnormalizeLinkUrl%7C%28brace%2Carg%2Cextension%29Substitution&i=nope&files=&repos= Bug: T236810 Change-Id: I19a43ffc5dcfdd2981b51079c33422c964acb076
2019-10-28 19:52:50 +00:00
* @covers Parser::expandMagicVariable
*/
class MagicVariableTest extends MediaWikiTestCase {
/**
* @var Parser
*/
private $testParser = null;
/** setup a basic parser object */
protected function setUp() : void {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
parent::setUp();
$services = MediaWikiServices::getInstance();
$contLang = $services->getLanguageFactory()->getLanguage( 'en' );
$this->setContentLang( $contLang );
$this->testParser = $services->getParserFactory()->create();
$this->testParser->setOptions( ParserOptions::newFromUserAndLang( new User, $contLang ) );
# initialize parser output
$this->testParser->clearState();
# Needs a title to do magic word stuff
$title = Title::newFromText( 'Tests' );
# Else it needs a db connection just to check if it's a redirect
# (when deciding the page language).
$title->mRedirect = false;
$this->testParser->setTitle( $title );
}
/**
* @param int $num Upper limit for numbers
* @return array Array of strings naming numbers from 1 up to $num
*/
private static function createProviderUpTo( $num ) {
$ret = [];
for ( $i = 1; $i <= $num; $i++ ) {
$ret[] = [ strval( $i ) ];
}
return $ret;
}
/**
* @return array Array of months numbers (as an integer)
*/
public static function provideMonths() {
return self::createProviderUpTo( 12 );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
/**
* @return array Array of days numbers (as an integer)
*/
public static function provideDays() {
return self::createProviderUpTo( 31 );
}
# ############## TESTS #############################################
# @todo FIXME:
# - those got copy pasted, we can probably make them cleaner
# - tests are lacking useful messages
# day
/** @dataProvider provideDays */
public function testCurrentdayIsUnPadded( $day ) {
$this->assertUnPadded( 'currentday', $day );
}
/** @dataProvider provideDays */
public function testCurrentdaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'currentday2', $day );
}
/** @dataProvider provideDays */
public function testLocaldayIsUnPadded( $day ) {
$this->assertUnPadded( 'localday', $day );
}
/** @dataProvider provideDays */
public function testLocaldaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'localday2', $day );
}
# month
/** @dataProvider provideMonths */
public function testCurrentmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'currentmonth', $month );
}
/** @dataProvider provideMonths */
public function testCurrentmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'currentmonth1', $month );
}
/** @dataProvider provideMonths */
public function testLocalmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'localmonth', $month );
}
/** @dataProvider provideMonths */
public function testLocalmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'localmonth1', $month );
}
# revision day
/** @dataProvider provideDays */
public function testRevisiondayIsUnPadded( $day ) {
$this->assertUnPadded( 'revisionday', $day );
}
/** @dataProvider provideDays */
public function testRevisiondaytwoIsZeroPadded( $day ) {
$this->assertZeroPadded( 'revisionday2', $day );
}
# revision month
/** @dataProvider provideMonths */
public function testRevisionmonthIsZeroPadded( $month ) {
$this->assertZeroPadded( 'revisionmonth', $month );
}
/** @dataProvider provideMonths */
public function testRevisionmonthoneIsUnPadded( $month ) {
$this->assertUnPadded( 'revisionmonth1', $month );
}
# ############## HELPERS ############################################
/** assertion helper expecting a magic output which is zero padded */
public function assertZeroPadded( $magic, $value ) {
$this->assertMagicPadding( $magic, $value, '%02d' );
}
/** assertion helper expecting a magic output which is unpadded */
public function assertUnPadded( $magic, $value ) {
$this->assertMagicPadding( $magic, $value, '%d' );
}
/**
* Main assertion helper for magic variables padding
* @param string $magic Magic variable name
* @param mixed $value Month or day
* @param string $format Sprintf format for $value
*/
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!
if ( $value > 12 ) {
$month = $value % 12;
} else {
$month = $value;
}
$this->setParserTS(
sprintf( '2010%02d%02d123456', $month, $value )
);
# please keep the following commented line of code. It helps debugging.
// print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
# format expectation and test it
$expected = sprintf( $format, $value );
$this->assertMagic( $expected, $magic );
}
/**
* helper to set the parser timestamp and revision timestamp
* @param string $ts
*/
private function setParserTS( $ts ) {
$this->testParser->getOptions()->setTimestamp( $ts );
$this->testParser->mRevisionTimestamp = $ts;
}
/**
* Assertion helper to test a magic variable output
* @param string|int $expected
* @param string $magic
*/
private function assertMagic( $expected, $magic ) {
# Generate a message for the assertion
$msg = sprintf( "Magic %s should be <%s:%s>",
$magic,
$expected,
gettype( $expected )
);
$this->assertSame(
$expected,
Deprecate Parser implementation methods (will be private in next release) The following public methods were renamed and made private; the old name is hard-deprecated and calls the new renamed private method: Parser::doMagicLinks() => handleMagicLinks() Parser::doDoubleUnderscore() => handleMagicLinks() Parser::doHeadings() => handleHeadings() Parser::doAllQuotes() => handleAllQuotes() Parser::replaceExternalLinks() => handleExternalLinks() Parser::replaceInternalLinks() => handleInternalLinks() Parser::replaceInternalLinks2() => handleInternalLinks2() Parser::getVariableValue() => expandMagicVariable() Parser::initialiseVariables() => initializeVariables() Parser::formatHeadings() => finalizeHeadings() Parser::test{Pst,Preprocess,Srvus}() => fuzzTest{Pst,Preprocess,Srvus}() Additionally, the following methods are not used externally, but are used outside the Parser class by core code. They have been marked @internal: Parser::doQuotes() (used by {{#displaytitle}}), Parser::getExternalLink{Rel,Attribs}() (used by Linker), Parser::normalizeLinkUrl() (used by Special:LinkSearch and elsewhere). Parser::{brace,arg,extension}Substitution() (used by PPFrame) Code search query: https://codesearch.wmflabs.org/deployed/?q=do%28MagicLinks%7CDoubleUnderscore%7CHeadings%7CAllQuotes%29%7Creplace%28ExternalLinks%7CInternalLinks%28%7C2%29%29%7CgetVariableValue%7CinitialiseVariables%7CformatHeadings%7Ctest%28Pst%7CPreprocess%7CSrvus%29%7CdoQuotes%7CgetExternalLink%28Rel%7CAttribs%29%7CnormalizeLinkUrl%7C%28brace%2Carg%2Cextension%29Substitution&i=nope&files=&repos= Bug: T236810 Change-Id: I19a43ffc5dcfdd2981b51079c33422c964acb076
2019-10-28 19:52:50 +00:00
TestingAccessWrapper::newFromObject( $this->testParser )->expandMagicVariable( $magic ),
$msg
);
}
}