Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / parser / MediaWikiParserTest.php
blobcd0f139f96c2e822fc5ffb7ea900a3972127c431
1 <?php
2 require_once __DIR__ . '/NewParserTest.php';
4 /**
5 * The UnitTest must be either a class that inherits from MediaWikiTestCase
6 * or a class that provides a public static suite() method which returns
7 * an PHPUnit_Framework_Test object
9 * @group Parser
10 * @group ParserTests
11 * @group Database
13 class MediaWikiParserTest {
15 /**
16 * @defgroup filtering_constants Filtering constants
18 * Limit inclusion of parser tests files coming from MediaWiki core
19 * @{
22 /** Include files shipped with MediaWiki core */
23 const CORE_ONLY = 1;
24 /** Include non core files as set in $wgParserTestFiles */
25 const NO_CORE = 2;
26 /** Include anything set via $wgParserTestFiles */
27 const WITH_ALL = 3; # CORE_ONLY | NO_CORE
29 /** @} */
31 /**
32 * Get a PHPUnit test suite of parser tests. Optionally filtered with
33 * $flags.
35 * @par Examples:
36 * Get a suite of parser tests shipped by MediaWiki core:
37 * @code
38 * MediaWikiParserTest::suite( MediaWikiParserTest::CORE_ONLY );
39 * @endcode
40 * Get a suite of various parser tests, like extensions:
41 * @code
42 * MediaWikiParserTest::suite( MediaWikiParserTest::NO_CORE );
43 * @endcode
44 * Get any test defined via $wgParserTestFiles:
45 * @code
46 * MediaWikiParserTest::suite( MediaWikiParserTest::WITH_ALL );
47 * @endcode
49 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
50 * will be included. Default: MediaWikiParserTest::CORE_ONLY
52 * @return PHPUnit_Framework_TestSuite
54 public static function suite( $flags = self::CORE_ONLY ) {
55 if ( is_string( $flags ) ) {
56 $flags = self::CORE_ONLY;
58 global $wgParserTestFiles, $IP;
60 $mwTestDir = $IP . '/tests/';
62 # Human friendly helpers
63 $wantsCore = ( $flags & self::CORE_ONLY );
64 $wantsRest = ( $flags & self::NO_CORE );
66 # Will hold the .txt parser test files we will include
67 $filesToTest = array();
69 # Filter out .txt files
70 foreach ( $wgParserTestFiles as $parserTestFile ) {
71 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
73 if ( $isCore && $wantsCore ) {
74 self::debug( "included core parser tests: $parserTestFile" );
75 $filesToTest[] = $parserTestFile;
76 } elseif ( !$isCore && $wantsRest ) {
77 self::debug( "included non core parser tests: $parserTestFile" );
78 $filesToTest[] = $parserTestFile;
79 } else {
80 self::debug( "skipped parser tests: $parserTestFile" );
83 self::debug( 'parser tests files: '
84 . implode( ' ', $filesToTest ) );
86 $suite = new PHPUnit_Framework_TestSuite;
87 $testList = array();
88 $counter = 0;
89 foreach ( $filesToTest as $fileName ) {
90 // Call the highest level directory the extension name.
91 // It may or may not actually be, but it should be close
92 // enough to cause there to be separate names for different
93 // things, which is good enough for our purposes.
94 $extensionName = basename( dirname( $fileName ) );
95 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
96 $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
97 $parserTestClassName = ucfirst( $testsName );
99 // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
100 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
101 $parserTestClassName = 'ParserTest_' .
102 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
104 if ( isset( $testList[$parserTestClassName] ) ) {
105 // If a conflict happens, gives a very unclear fatal.
106 // So as a last ditch effort to prevent that eventuality, if there
107 // is a conflict, append a number.
108 $counter++;
109 $parserTestClassName .= $counter;
111 $testList[$parserTestClassName] = true;
112 $parserTestClassDefinition = <<<EOT
114 * @group Database
115 * @group Parser
116 * @group ParserTests
117 * @group ParserTests_$parserTestClassName
119 class $parserTestClassName extends NewParserTest {
120 protected \$file = '$escapedFileName';
122 EOT;
124 eval( $parserTestClassDefinition );
125 self::debug( "Adding test class $parserTestClassName" );
126 $suite->addTestSuite( $parserTestClassName );
128 return $suite;
132 * Write $msg under log group 'tests-parser'
133 * @param string $msg Message to log
135 protected static function debug( $msg ) {
136 return wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );