Merge "Paranoia, escape image alignment parameters before outputting."
[lhc/web/wiklou.git] / tests / phpunit / suites / ParserTestTopLevelSuite.php
1 <?php
2 use Wikimedia\ScopedCallback;
3
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
8 *
9 * @group Parser
10 * @group ParserTests
11 * @group Database
12 */
13 class ParserTestTopLevelSuite extends PHPUnit_Framework_TestSuite {
14 /** @var ParserTestRunner */
15 private $ptRunner;
16
17 /** @var ScopedCallback */
18 private $ptTeardownScope;
19
20 private $oldTablePrefix = '';
21
22 /**
23 * @defgroup filtering_constants Filtering constants
24 *
25 * Limit inclusion of parser tests files coming from MediaWiki core
26 * @{
27 */
28
29 /** Include files shipped with MediaWiki core */
30 const CORE_ONLY = 1;
31 /** Include non core files as set in $wgParserTestFiles */
32 const NO_CORE = 2;
33 /** Include anything set via $wgParserTestFiles */
34 const WITH_ALL = self::CORE_ONLY | self::NO_CORE;
35
36 /** @} */
37
38 /**
39 * Get a PHPUnit test suite of parser tests. Optionally filtered with
40 * $flags.
41 *
42 * @par Examples:
43 * Get a suite of parser tests shipped by MediaWiki core:
44 * @code
45 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::CORE_ONLY );
46 * @endcode
47 * Get a suite of various parser tests, like extensions:
48 * @code
49 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::NO_CORE );
50 * @endcode
51 * Get any test defined via $wgParserTestFiles:
52 * @code
53 * ParserTestTopLevelSuite::suite( ParserTestTopLevelSuite::WITH_ALL );
54 * @endcode
55 *
56 * @param int $flags Bitwise flag to filter out the $wgParserTestFiles that
57 * will be included. Default: ParserTestTopLevelSuite::CORE_ONLY
58 *
59 * @return PHPUnit_Framework_TestSuite
60 */
61 public static function suite( $flags = self::CORE_ONLY ) {
62 return new self( $flags );
63 }
64
65 function __construct( $flags ) {
66 parent::__construct();
67
68 $this->ptRecorder = new PhpunitTestRecorder;
69 $this->ptRunner = new ParserTestRunner( $this->ptRecorder );
70
71 if ( is_string( $flags ) ) {
72 $flags = self::CORE_ONLY;
73 }
74 global $IP;
75
76 $mwTestDir = $IP . '/tests/';
77
78 # Human friendly helpers
79 $wantsCore = ( $flags & self::CORE_ONLY );
80 $wantsRest = ( $flags & self::NO_CORE );
81
82 # Will hold the .txt parser test files we will include
83 $filesToTest = [];
84
85 # Filter out .txt files
86 $files = ParserTestRunner::getParserTestFiles();
87 foreach ( $files as $extName => $parserTestFile ) {
88 $isCore = ( 0 === strpos( $parserTestFile, $mwTestDir ) );
89
90 if ( $isCore && $wantsCore ) {
91 self::debug( "included core parser tests: $parserTestFile" );
92 $filesToTest[$extName] = $parserTestFile;
93 } elseif ( !$isCore && $wantsRest ) {
94 self::debug( "included non core parser tests: $parserTestFile" );
95 $filesToTest[$extName] = $parserTestFile;
96 } else {
97 self::debug( "skipped parser tests: $parserTestFile" );
98 }
99 }
100 self::debug( 'parser tests files: '
101 . implode( ' ', $filesToTest ) );
102
103 $testList = [];
104 $counter = 0;
105 foreach ( $filesToTest as $extensionName => $fileName ) {
106 if ( is_int( $extensionName ) ) {
107 // If there's no extension name because this is coming
108 // from the legacy global, then assume the next level directory
109 // is the extension name (e.g. extensions/FooBar/parserTests.txt).
110 $extensionName = basename( dirname( $fileName ) );
111 }
112 $testsName = $extensionName . '__' . basename( $fileName, '.txt' );
113 $parserTestClassName = ucfirst( $testsName );
114
115 // Official spec for class names: https://secure.php.net/manual/en/language.oop5.basic.php
116 // Prepend 'ParserTest_' to be paranoid about it not starting with a number
117 $parserTestClassName = 'ParserTest_' .
118 preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
119
120 if ( isset( $testList[$parserTestClassName] ) ) {
121 // If there is a conflict, append a number.
122 $counter++;
123 $parserTestClassName .= $counter;
124 }
125 $testList[$parserTestClassName] = true;
126
127 // Previously we actually created a class here, with eval(). We now
128 // just override the name.
129
130 self::debug( "Adding test class $parserTestClassName" );
131 $this->addTest( new ParserTestFileSuite(
132 $this->ptRunner, $parserTestClassName, $fileName ) );
133 }
134 }
135
136 public function setUp() {
137 wfDebug( __METHOD__ );
138 $db = wfGetDB( DB_MASTER );
139 $type = $db->getType();
140 $prefix = $type === 'oracle' ?
141 MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX;
142 $this->oldTablePrefix = $db->tablePrefix();
143 MediaWikiTestCase::setupTestDB( $db, $prefix );
144 CloneDatabase::changePrefix( $prefix );
145 $teardown = $this->ptRunner->setDatabase( $db );
146 $teardown = $this->ptRunner->setupUploads( $teardown );
147 $this->ptTeardownScope = $teardown;
148 }
149
150 public function tearDown() {
151 wfDebug( __METHOD__ );
152 if ( $this->ptTeardownScope ) {
153 ScopedCallback::consume( $this->ptTeardownScope );
154 }
155 CloneDatabase::changePrefix( $this->oldTablePrefix );
156 }
157
158 /**
159 * Write $msg under log group 'tests-parser'
160 * @param string $msg Message to log
161 */
162 protected static function debug( $msg ) {
163 wfDebugLog( 'tests-parser', wfGetCaller() . ' ' . $msg );
164 }
165 }