Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / parser / parserTests.php
1 <?php
2 /**
3 * MediaWiki parser test suite
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Testing
25 */
26
27 // Some methods which are discouraged for normal code throw exceptions unless
28 // we declare this is just a test.
29 define( 'MW_PARSER_TEST', true );
30
31 require __DIR__ . '/../../maintenance/Maintenance.php';
32
33 use MediaWiki\MediaWikiServices;
34
35 class ParserTestsMaintenance extends Maintenance {
36 function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Run parser tests' );
39
40 $this->addOption( 'quick', 'Suppress diff output of failed tests' );
41 $this->addOption( 'quiet', 'Suppress notification of passed tests (shows only failed tests)' );
42 $this->addOption( 'show-output', 'Show expected and actual output' );
43 $this->addOption( 'color', '[=yes|no] Override terminal detection and force ' .
44 'color output on or off. Use wgCommandLineDarkBg = true; if your term is dark',
45 false, true );
46 $this->addOption( 'regex', 'Only run tests whose descriptions which match given regex',
47 false, true );
48 $this->addOption( 'filter', 'Alias for --regex', false, true );
49 $this->addOption( 'file', 'Run test cases from a custom file instead of parserTests.txt',
50 false, true, false, true );
51 $this->addOption( 'record', 'Record tests in database' );
52 $this->addOption( 'compare', 'Compare with recorded results, without updating the database.' );
53 $this->addOption( 'setversion', 'When using --record, set the version string to use (useful' .
54 'with "git rev-parse HEAD" to get the exact revision)',
55 false, true );
56 $this->addOption( 'keep-uploads', 'Re-use the same upload directory for each ' .
57 'test, don\'t delete it' );
58 $this->addOption( 'file-backend', 'Use the file backend with the given name,' .
59 'and upload files to it, instead of creating a mock file backend.', false, true );
60 $this->addOption( 'upload-dir', 'Specify the upload directory to use. Useful in ' .
61 'conjunction with --keep-uploads. Causes a real (non-mock) file backend to ' .
62 'be used.', false, true );
63 $this->addOption( 'run-disabled', 'run disabled tests' );
64 $this->addOption( 'disable-save-parse', 'Don\'t run the parser when ' .
65 'inserting articles into the database' );
66 $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
67 $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
68 $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
69 'both the expected and actual output in order to resolve ' .
70 'irrelevant differences. The accepted normalization functions ' .
71 'are: removeTbody to remove <tbody> tags; and trimWhitespace ' .
72 'to trim whitespace from the start and end of text nodes.',
73 false, true );
74 $this->addOption( 'use-tidy-config',
75 'Use the wiki\'s Tidy configuration instead of known-good' .
76 'defaults.' );
77 }
78
79 public function finalSetup() {
80 parent::finalSetup();
81 self::requireTestsAutoloader();
82 TestSetup::applyInitialConfig();
83 }
84
85 public function execute() {
86 global $wgDBtype;
87
88 // Cases of weird db corruption were encountered when running tests on earlyish
89 // versions of SQLite
90 if ( $wgDBtype == 'sqlite' ) {
91 $db = wfGetDB( DB_MASTER );
92 $version = $db->getServerVersion();
93 if ( version_compare( $version, '3.6' ) < 0 ) {
94 die( "Parser tests require SQLite version 3.6 or later, you have $version\n" );
95 }
96 }
97
98 // Print out software version to assist with locating regressions
99 $version = SpecialVersion::getVersion( 'nodb' );
100 echo "This is MediaWiki version {$version}.\n\n";
101
102 // Only colorize output if stdout is a terminal.
103 $color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
104
105 if ( $this->hasOption( 'color' ) ) {
106 switch ( $this->getOption( 'color' ) ) {
107 case 'no':
108 $color = false;
109 break;
110 case 'yes':
111 default:
112 $color = true;
113 break;
114 }
115 }
116
117 $record = $this->hasOption( 'record' );
118 $compare = $this->hasOption( 'compare' );
119
120 $regex = $this->getOption( 'filter', $this->getOption( 'regex', false ) );
121 if ( $regex !== false ) {
122 $regex = "/$regex/i";
123
124 if ( $record ) {
125 echo "Warning: --record cannot be used with --regex, disabling --record\n";
126 $record = false;
127 }
128 }
129
130 $term = $color
131 ? new AnsiTermColorer()
132 : new DummyTermColorer();
133
134 $recorder = new MultiTestRecorder;
135
136 $recorder->addRecorder( new ParserTestPrinter(
137 $term,
138 [
139 'showDiffs' => !$this->hasOption( 'quick' ),
140 'showProgress' => !$this->hasOption( 'quiet' ),
141 'showFailure' => !$this->hasOption( 'quiet' )
142 || ( !$record && !$compare ), // redundant output
143 'showOutput' => $this->hasOption( 'show-output' ),
144 'useDwdiff' => $this->hasOption( 'dwdiff' ),
145 'markWhitespace' => $this->hasOption( 'mark-ws' ),
146 ]
147 ) );
148
149 $recorderLB = false;
150 if ( $record || $compare ) {
151 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
152 $recorderLB = $lbFactory->newMainLB();
153 // This connection will have the wiki's table prefix, not parsertest_
154 $recorderDB = $recorderLB->getConnection( DB_MASTER );
155
156 // Add recorder before previewer because recorder will create the
157 // DB table if it doesn't exist
158 if ( $record ) {
159 $recorder->addRecorder( new DbTestRecorder( $recorderDB ) );
160 }
161 $recorder->addRecorder( new DbTestPreviewer(
162 $recorderDB,
163 function ( $name ) use ( $regex ) {
164 // Filter reports of old tests by the filter regex
165 if ( $regex === false ) {
166 return true;
167 } else {
168 return (bool)preg_match( $regex, $name );
169 }
170 } ) );
171 }
172
173 // Default parser tests and any set from extensions or local config
174 $files = $this->getOption( 'file', ParserTestRunner::getParserTestFiles() );
175
176 $norm = $this->hasOption( 'norm' ) ? explode( ',', $this->getOption( 'norm' ) ) : [];
177
178 $tester = new ParserTestRunner( $recorder, [
179 'norm' => $norm,
180 'regex' => $regex,
181 'keep-uploads' => $this->hasOption( 'keep-uploads' ),
182 'run-disabled' => $this->hasOption( 'run-disabled' ),
183 'disable-save-parse' => $this->hasOption( 'disable-save-parse' ),
184 'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
185 'file-backend' => $this->getOption( 'file-backend' ),
186 'upload-dir' => $this->getOption( 'upload-dir' ),
187 ] );
188
189 $ok = $tester->runTestsFromFiles( $files );
190 if ( $recorderLB ) {
191 $recorderLB->closeAll();
192 }
193 if ( !$ok ) {
194 exit( 1 );
195 }
196 }
197 }
198
199 $maintClass = 'ParserTestsMaintenance';
200 require_once RUN_MAINTENANCE_IF_MAIN;