2011-05-31 20:30:12 +00:00
|
|
|
<?php
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/NewParserTest.php';
|
2011-05-31 20:30:12 +00:00
|
|
|
|
|
|
|
|
/**
|
2011-11-12 21:32:39 +00:00
|
|
|
* The UnitTest must be either a class that inherits from MediaWikiTestCase
|
2013-02-15 10:24:31 +00:00
|
|
|
* or a class that provides a public static suite() method which returns
|
2011-05-31 20:30:12 +00:00
|
|
|
* an PHPUnit_Framework_Test object
|
2013-02-15 10:24:31 +00:00
|
|
|
*
|
2011-05-31 20:30:12 +00:00
|
|
|
* @group Parser
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class MediaWikiParserTest {
|
|
|
|
|
|
2013-05-10 19:20:12 +00:00
|
|
|
/**
|
|
|
|
|
* @defgroup filtering_constants Filtering constants
|
|
|
|
|
*
|
|
|
|
|
* Limit inclusion of parser tests files coming from MediaWiki core
|
|
|
|
|
* @{
|
|
|
|
|
*/
|
2011-05-31 20:30:12 +00:00
|
|
|
|
2013-05-10 19:20:12 +00:00
|
|
|
/** Include files shipped with MediaWiki core */
|
|
|
|
|
const CORE_ONLY = 1;
|
|
|
|
|
/** Include non core files as set in $wgParserTestFiles */
|
|
|
|
|
const NO_CORE = 2;
|
|
|
|
|
/** Include anything set via $wgParserTestFiles */
|
2013-08-24 15:06:25 +00:00
|
|
|
const WITH_ALL = 3; # CORE_ONLY | NO_CORE
|
2013-05-10 19:20:12 +00:00
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a PHPUnit test suite of parser tests. Optionally filtered with
|
|
|
|
|
* $flags.
|
|
|
|
|
*
|
|
|
|
|
* @par Examples:
|
|
|
|
|
* Get a suite of parser tests shipped by MediaWiki core:
|
|
|
|
|
* @code
|
|
|
|
|
* MediaWikiParserTest::suite( MediaWikiParserTest::CORE_ONLY );
|
|
|
|
|
* @endcode
|
|
|
|
|
* Get a suite of various parser tests, like extensions:
|
|
|
|
|
* @code
|
|
|
|
|
* MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
|
|
|
|
|
* @endcode
|
|
|
|
|
* Get any test defined via $wgParserTestFiles:
|
|
|
|
|
* @code
|
|
|
|
|
* MediaWikiParserTest::suite( MediaWikiParserTest::WITH_ALL );
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
|
2013-05-10 19:20:12 +00:00
|
|
|
* will be included. Default: MediaWikiParserTest::CORE_ONLY
|
|
|
|
|
*
|
|
|
|
|
* @return PHPUnit_Framework_TestSuite
|
|
|
|
|
*/
|
|
|
|
|
public static function suite( $flags = self::CORE_ONLY ) {
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( is_string( $flags ) ) {
|
2013-05-10 19:20:12 +00:00
|
|
|
$flags = self::CORE_ONLY;
|
|
|
|
|
}
|
|
|
|
|
global $wgParserTestFiles, $IP;
|
|
|
|
|
|
2013-08-24 15:06:25 +00:00
|
|
|
$mwTestDir = $IP . '/tests/';
|
2013-05-10 19:20:12 +00:00
|
|
|
|
|
|
|
|
# Human friendly helpers
|
2013-08-24 15:06:25 +00:00
|
|
|
$wantsCore = ( $flags & self::CORE_ONLY );
|
|
|
|
|
$wantsRest = ( $flags & self::NO_CORE );
|
2013-05-10 19:20:12 +00:00
|
|
|
|
|
|
|
|
# Will hold the .txt parser test files we will include
|
|
|
|
|
$filesToTest = array();
|
|
|
|
|
|
|
|
|
|
# Filter out .txt files
|
2013-08-24 15:06:25 +00:00
|
|
|
foreach ( $wgParserTestFiles as $parserTestFile ) {
|
2013-05-10 19:20:12 +00:00
|
|
|
$isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
|
2011-05-31 20:30:12 +00:00
|
|
|
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( $isCore && $wantsCore ) {
|
2013-05-10 19:20:12 +00:00
|
|
|
self::debug( "included core parser tests: $parserTestFile" );
|
|
|
|
|
$filesToTest[] = $parserTestFile;
|
2013-08-24 15:06:25 +00:00
|
|
|
} elseif ( !$isCore && $wantsRest ) {
|
2013-05-10 19:20:12 +00:00
|
|
|
self::debug( "included non core parser tests: $parserTestFile" );
|
|
|
|
|
$filesToTest[] = $parserTestFile;
|
|
|
|
|
} else {
|
|
|
|
|
self::debug( "skipped parser tests: $parserTestFile" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self::debug( 'parser tests files: '
|
2013-08-24 15:06:25 +00:00
|
|
|
. implode( ' ', $filesToTest ) );
|
2013-05-10 19:20:12 +00:00
|
|
|
|
|
|
|
|
$suite = new PHPUnit_Framework_TestSuite;
|
2013-06-05 16:14:27 +00:00
|
|
|
$testList = array();
|
|
|
|
|
$counter = 0;
|
2013-05-10 19:20:12 +00:00
|
|
|
foreach ( $filesToTest as $fileName ) {
|
2013-06-05 16:14:27 +00:00
|
|
|
// Call the highest level directory the extension name.
|
|
|
|
|
// It may or may not actually be, but it should be close
|
|
|
|
|
// enough to cause there to be separate names for different
|
|
|
|
|
// things, which is good enough for our purposes.
|
|
|
|
|
$extensionName = basename( dirname( $fileName ) );
|
2015-09-07 05:30:13 +00:00
|
|
|
$testsName = $extensionName . '__' . basename( $fileName, '.txt' );
|
2013-04-20 20:22:26 +00:00
|
|
|
$escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
|
2013-06-05 16:14:27 +00:00
|
|
|
$parserTestClassName = ucfirst( $testsName );
|
|
|
|
|
// Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
|
|
|
|
|
// Prepend 'ParserTest_' to be paranoid about it not starting with a number
|
|
|
|
|
$parserTestClassName = 'ParserTest_' . preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
|
|
|
|
|
if ( isset( $testList[$parserTestClassName] ) ) {
|
|
|
|
|
// If a conflict happens, gives a very unclear fatal.
|
|
|
|
|
// So as a last ditch effort to prevent that eventuality, if there
|
|
|
|
|
// is a conflict, append a number.
|
|
|
|
|
$counter++;
|
|
|
|
|
$parserTestClassName .= $counter;
|
|
|
|
|
}
|
|
|
|
|
$testList[$parserTestClassName] = true;
|
2013-04-20 20:22:26 +00:00
|
|
|
$parserTestClassDefinition = <<<EOT
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
* @group Parser
|
2013-04-20 20:28:38 +00:00
|
|
|
* @group ParserTests
|
|
|
|
|
* @group ParserTests_$parserTestClassName
|
2013-04-20 20:22:26 +00:00
|
|
|
*/
|
|
|
|
|
class $parserTestClassName extends NewParserTest {
|
|
|
|
|
protected \$file = '$escapedFileName';
|
|
|
|
|
}
|
|
|
|
|
EOT;
|
2012-12-07 01:11:38 +00:00
|
|
|
|
2013-04-20 20:22:26 +00:00
|
|
|
eval( $parserTestClassDefinition );
|
2013-05-10 19:20:12 +00:00
|
|
|
self::debug( "Adding test class $parserTestClassName" );
|
|
|
|
|
$suite->addTestSuite( $parserTestClassName );
|
2011-05-31 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
return $suite;
|
|
|
|
|
}
|
2013-05-10 19:20:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write $msg under log group 'tests-parser'
|
|
|
|
|
* @param string $msg Message to log
|
|
|
|
|
*/
|
|
|
|
|
protected static function debug( $msg ) {
|
|
|
|
|
return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
|
|
|
|
|
}
|
2011-05-31 20:30:12 +00:00
|
|
|
}
|