Remove functions deprecated since 1.21 from EditPage.php
[mediawiki.git] / tests / testHelpers.inc
blob717c5f34d79ad3a94af290dbf0e74c924a571784
1 <?php
2 /**
3  * Recording for passing/failing tests.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup Testing
22  */
24 /**
25  * Interface to record parser test results.
26  *
27  * The ITestRecorder is a very simple interface to record the result of
28  * MediaWiki parser tests. One should call start() before running the
29  * full parser tests and end() once all the tests have been finished.
30  * After each test, you should use record() to keep track of your tests
31  * results. Finally, report() is used to generate a summary of your
32  * test run, one could dump it to the console for human consumption or
33  * register the result in a database for tracking purposes.
34  *
35  * @since 1.22
36  */
37 interface ITestRecorder {
39         /** Called at beginning of the parser test run */
40         public function start();
42         /** Called after each test */
43         public function record( $test, $result );
45         /** Called before finishing the test run */
46         public function report();
48         /** Called at the end of the parser test run */
49         public function end();
53 class TestRecorder implements ITestRecorder {
54         var $parent;
55         var $term;
57         function __construct( $parent ) {
58                 $this->parent = $parent;
59                 $this->term = $parent->term;
60         }
62         function start() {
63                 $this->total = 0;
64                 $this->success = 0;
65         }
67         function record( $test, $result ) {
68                 $this->total++;
69                 $this->success += ( $result ? 1 : 0 );
70         }
72         function end() {
73                 // dummy
74         }
76         function report() {
77                 if ( $this->total > 0 ) {
78                         $this->reportPercentage( $this->success, $this->total );
79                 } else {
80                         throw new MWException( "No tests found.\n" );
81                 }
82         }
84         function reportPercentage( $success, $total ) {
85                 $ratio = wfPercent( 100 * $success / $total );
86                 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
88                 if ( $success == $total ) {
89                         print $this->term->color( 32 ) . "ALL TESTS PASSED!";
90                 } else {
91                         $failed = $total - $success;
92                         print $this->term->color( 31 ) . "$failed tests failed!";
93                 }
95                 print $this->term->reset() . "\n";
97                 return ( $success == $total );
98         }
101 class DbTestPreviewer extends TestRecorder {
102         protected $lb; // /< Database load balancer
103         protected $db; // /< Database connection to the main DB
104         protected $curRun; // /< run ID number for the current run
105         protected $prevRun; // /< run ID number for the previous run, if any
106         protected $results; // /< Result array
108         /**
109          * This should be called before the table prefix is changed
110          */
111         function __construct( $parent ) {
112                 parent::__construct( $parent );
114                 $this->lb = wfGetLBFactory()->newMainLB();
115                 // This connection will have the wiki's table prefix, not parsertest_
116                 $this->db = $this->lb->getConnection( DB_MASTER );
117         }
119         /**
120          * Set up result recording; insert a record for the run with the date
121          * and all that fun stuff
122          */
123         function start() {
124                 parent::start();
126                 if ( !$this->db->tableExists( 'testrun', __METHOD__ )
127                         || !$this->db->tableExists( 'testitem', __METHOD__ )
128                 ) {
129                         print "WARNING> `testrun` table not found in database.\n";
130                         $this->prevRun = false;
131                 } else {
132                         // We'll make comparisons against the previous run later...
133                         $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
134                 }
136                 $this->results = array();
137         }
139         function record( $test, $result ) {
140                 parent::record( $test, $result );
141                 $this->results[$test] = $result;
142         }
144         function report() {
145                 if ( $this->prevRun ) {
146                         // f = fail, p = pass, n = nonexistent
147                         // codes show before then after
148                         $table = array(
149                                 'fp' => 'previously failing test(s) now PASSING! :)',
150                                 'pn' => 'previously PASSING test(s) removed o_O',
151                                 'np' => 'new PASSING test(s) :)',
153                                 'pf' => 'previously passing test(s) now FAILING! :(',
154                                 'fn' => 'previously FAILING test(s) removed O_o',
155                                 'nf' => 'new FAILING test(s) :(',
156                                 'ff' => 'still FAILING test(s) :(',
157                         );
159                         $prevResults = array();
161                         $res = $this->db->select( 'testitem', array( 'ti_name', 'ti_success' ),
162                                 array( 'ti_run' => $this->prevRun ), __METHOD__ );
164                         foreach ( $res as $row ) {
165                                 if ( !$this->parent->regex
166                                         || preg_match( "/{$this->parent->regex}/i", $row->ti_name )
167                                 ) {
168                                         $prevResults[$row->ti_name] = $row->ti_success;
169                                 }
170                         }
172                         $combined = array_keys( $this->results + $prevResults );
174                         # Determine breakdown by change type
175                         $breakdown = array();
176                         foreach ( $combined as $test ) {
177                                 if ( !isset( $prevResults[$test] ) ) {
178                                         $before = 'n';
179                                 } elseif ( $prevResults[$test] == 1 ) {
180                                         $before = 'p';
181                                 } else /* if ( $prevResults[$test] == 0 )*/ {
182                                         $before = 'f';
183                                 }
185                                 if ( !isset( $this->results[$test] ) ) {
186                                         $after = 'n';
187                                 } elseif ( $this->results[$test] == 1 ) {
188                                         $after = 'p';
189                                 } else /*if ( $this->results[$test] == 0 ) */ {
190                                         $after = 'f';
191                                 }
193                                 $code = $before . $after;
195                                 if ( isset( $table[$code] ) ) {
196                                         $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
197                                 }
198                         }
200                         # Write out results
201                         foreach ( $table as $code => $label ) {
202                                 if ( !empty( $breakdown[$code] ) ) {
203                                         $count = count( $breakdown[$code] );
204                                         printf( "\n%4d %s\n", $count, $label );
206                                         foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
207                                                 print "      * $differing_test_name  [$statusInfo]\n";
208                                         }
209                                 }
210                         }
211                 } else {
212                         print "No previous test runs to compare against.\n";
213                 }
215                 print "\n";
216                 parent::report();
217         }
219         /**
220          * Returns a string giving information about when a test last had a status change.
221          * Could help to track down when regressions were introduced, as distinct from tests
222          * which have never passed (which are more change requests than regressions).
223          */
224         private function getTestStatusInfo( $testname, $after ) {
225                 // If we're looking at a test that has just been removed, then say when it first appeared.
226                 if ( $after == 'n' ) {
227                         $changedRun = $this->db->selectField( 'testitem',
228                                 'MIN(ti_run)',
229                                 array( 'ti_name' => $testname ),
230                                 __METHOD__ );
231                         $appear = $this->db->selectRow( 'testrun',
232                                 array( 'tr_date', 'tr_mw_version' ),
233                                 array( 'tr_id' => $changedRun ),
234                                 __METHOD__ );
236                         return "First recorded appearance: "
237                                 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
238                                 . ", " . $appear->tr_mw_version;
239                 }
241                 // Otherwise, this test has previous recorded results.
242                 // See when this test last had a different result to what we're seeing now.
243                 $conds = array(
244                         'ti_name' => $testname,
245                         'ti_success' => ( $after == 'f' ? "1" : "0" ) );
247                 if ( $this->curRun ) {
248                         $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
249                 }
251                 $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
253                 // If no record of ever having had a different result.
254                 if ( is_null( $changedRun ) ) {
255                         if ( $after == "f" ) {
256                                 return "Has never passed";
257                         } else {
258                                 return "Has never failed";
259                         }
260                 }
262                 // Otherwise, we're looking at a test whose status has changed.
263                 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
264                 // In this situation, give as much info as we can as to when it changed status.
265                 $pre = $this->db->selectRow( 'testrun',
266                         array( 'tr_date', 'tr_mw_version' ),
267                         array( 'tr_id' => $changedRun ),
268                         __METHOD__ );
269                 $post = $this->db->selectRow( 'testrun',
270                         array( 'tr_date', 'tr_mw_version' ),
271                         array( "tr_id > " . $this->db->addQuotes( $changedRun ) ),
272                         __METHOD__,
273                         array( "LIMIT" => 1, "ORDER BY" => 'tr_id' )
274                 );
276                 if ( $post ) {
277                         $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
278                 } else {
279                         $postDate = 'now';
280                 }
282                 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
283                         . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
284                         . " and $postDate";
285         }
287         /**
288          * Commit transaction and clean up for result recording
289          */
290         function end() {
291                 $this->lb->commitMasterChanges();
292                 $this->lb->closeAll();
293                 parent::end();
294         }
297 class DbTestRecorder extends DbTestPreviewer {
298         var $version;
300         /**
301          * Set up result recording; insert a record for the run with the date
302          * and all that fun stuff
303          */
304         function start() {
305                 $this->db->begin( __METHOD__ );
307                 if ( !$this->db->tableExists( 'testrun' )
308                         || !$this->db->tableExists( 'testitem' )
309                 ) {
310                         print "WARNING> `testrun` table not found in database. Trying to create table.\n";
311                         $this->db->sourceFile( $this->db->patchPath( 'patch-testrun.sql' ) );
312                         echo "OK, resuming.\n";
313                 }
315                 parent::start();
317                 $this->db->insert( 'testrun',
318                         array(
319                                 'tr_date' => $this->db->timestamp(),
320                                 'tr_mw_version' => $this->version,
321                                 'tr_php_version' => PHP_VERSION,
322                                 'tr_db_version' => $this->db->getServerVersion(),
323                                 'tr_uname' => php_uname()
324                         ),
325                         __METHOD__ );
326                 if ( $this->db->getType() === 'postgres' ) {
327                         $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' );
328                 } else {
329                         $this->curRun = $this->db->insertId();
330                 }
331         }
333         /**
334          * Record an individual test item's success or failure to the db
335          *
336          * @param string $test
337          * @param bool $result
338          */
339         function record( $test, $result ) {
340                 parent::record( $test, $result );
342                 $this->db->insert( 'testitem',
343                         array(
344                                 'ti_run' => $this->curRun,
345                                 'ti_name' => $test,
346                                 'ti_success' => $result ? 1 : 0,
347                         ),
348                         __METHOD__ );
349         }
352 class TestFileIterator implements Iterator {
353         private $file;
354         private $fh;
355         /**
356          * @var ParserTest|MediaWikiParserTest An instance of ParserTest (parserTests.php)
357          *  or MediaWikiParserTest (phpunit)
358          */
359         private $parserTest;
360         private $index = 0;
361         private $test;
362         private $section = null;
363         /** String|null: current test section being analyzed */
364         private $sectionData = array();
365         private $lineNum;
366         private $eof;
368         function __construct( $file, $parserTest ) {
369                 $this->file = $file;
370                 $this->fh = fopen( $this->file, "rt" );
372                 if ( !$this->fh ) {
373                         throw new MWException( "Couldn't open file '$file'\n" );
374                 }
376                 $this->parserTest = $parserTest;
378                 $this->lineNum = $this->index = 0;
379         }
381         function rewind() {
382                 if ( fseek( $this->fh, 0 ) ) {
383                         throw new MWException( "Couldn't fseek to the start of '$this->file'\n" );
384                 }
386                 $this->index = -1;
387                 $this->lineNum = 0;
388                 $this->eof = false;
389                 $this->next();
391                 return true;
392         }
394         function current() {
395                 return $this->test;
396         }
398         function key() {
399                 return $this->index;
400         }
402         function next() {
403                 if ( $this->readNextTest() ) {
404                         $this->index++;
405                         return true;
406                 } else {
407                         $this->eof = true;
408                 }
409         }
411         function valid() {
412                 return $this->eof != true;
413         }
415         function readNextTest() {
416                 $this->clearSection();
418                 # Create a fake parser tests which never run anything unless
419                 # asked to do so. This will avoid running hooks for a disabled test
420                 $delayedParserTest = new DelayedParserTest();
422                 while ( false !== ( $line = fgets( $this->fh ) ) ) {
423                         $this->lineNum++;
424                         $matches = array();
426                         if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
427                                 $this->section = strtolower( $matches[1] );
429                                 if ( $this->section == 'endarticle' ) {
430                                         $this->checkSection( 'text' );
431                                         $this->checkSection( 'article' );
433                                         $this->parserTest->addArticle(
434                                                 ParserTest::chomp( $this->sectionData['article'] ),
435                                                 $this->sectionData['text'], $this->lineNum );
437                                         $this->clearSection();
439                                         continue;
440                                 }
442                                 if ( $this->section == 'endhooks' ) {
443                                         $this->checkSection( 'hooks' );
445                                         foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
446                                                 $line = trim( $line );
448                                                 if ( $line ) {
449                                                         $delayedParserTest->requireHook( $line );
450                                                 }
451                                         }
453                                         $this->clearSection();
455                                         continue;
456                                 }
458                                 if ( $this->section == 'endfunctionhooks' ) {
459                                         $this->checkSection( 'functionhooks' );
461                                         foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
462                                                 $line = trim( $line );
464                                                 if ( $line ) {
465                                                         $delayedParserTest->requireFunctionHook( $line );
466                                                 }
467                                         }
469                                         $this->clearSection();
471                                         continue;
472                                 }
474                                 if ( $this->section == 'endtransparenthooks' ) {
475                                         $this->checkSection( 'transparenthooks' );
477                                         foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
478                                                 $line = trim( $line );
480                                                 if ( $line ) {
481                                                         $delayedParserTest->requireTransparentHook( $line );
482                                                 }
483                                         }
485                                         $this->clearSection();
487                                         continue;
488                                 }
490                                 if ( $this->section == 'end' ) {
491                                         $this->checkSection( 'test' );
492                                         // "input" and "result" are old section names allowed
493                                         // for backwards-compatibility.
494                                         $input = $this->checkSection( array( 'wikitext', 'input' ), false );
495                                         $result = $this->checkSection( array( 'html/php', 'html/*', 'html', 'result' ), false );
497                                         if ( !isset( $this->sectionData['options'] ) ) {
498                                                 $this->sectionData['options'] = '';
499                                         }
501                                         if ( !isset( $this->sectionData['config'] ) ) {
502                                                 $this->sectionData['config'] = '';
503                                         }
505                                         if ( $input == false || $result == false ||
506                                                 ( ( preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] )
507                                                         && !$this->parserTest->runDisabled )
508                                                 || ( preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] )
509                                                         && $result != 'html/php' && !$this->parserTest->runParsoid )
510                                                 || !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] ) )
511                                         ) {
512                                                 # disabled test
513                                                 $this->clearSection();
515                                                 # Forget any pending hooks call since test is disabled
516                                                 $delayedParserTest->reset();
518                                                 continue;
519                                         }
521                                         # We are really going to run the test, run pending hooks and hooks function
522                                         wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
523                                         $hooksResult = $delayedParserTest->unleash( $this->parserTest );
524                                         if ( !$hooksResult ) {
525                                                 # Some hook reported an issue. Abort.
526                                                 return false;
527                                         }
529                                         $this->test = array(
530                                                 'test' => ParserTest::chomp( $this->sectionData['test'] ),
531                                                 'input' => ParserTest::chomp( $this->sectionData[$input] ),
532                                                 'result' => ParserTest::chomp( $this->sectionData[$result] ),
533                                                 'options' => ParserTest::chomp( $this->sectionData['options'] ),
534                                                 'config' => ParserTest::chomp( $this->sectionData['config'] ),
535                                         );
537                                         return true;
538                                 }
540                                 if ( isset( $this->sectionData[$this->section] ) ) {
541                                         throw new MWException( "duplicate section '$this->section' "
542                                                 . "at line {$this->lineNum} of $this->file\n" );
543                                 }
545                                 $this->sectionData[$this->section] = '';
547                                 continue;
548                         }
550                         if ( $this->section ) {
551                                 $this->sectionData[$this->section] .= $line;
552                         }
553                 }
555                 return false;
556         }
558         /**
559          * Clear section name and its data
560          */
561         private function clearSection() {
562                 $this->sectionData = array();
563                 $this->section = null;
565         }
567         /**
568          * Verify the current section data has some value for the given token
569          * name(s) (first parameter).
570          * Throw an exception if it is not set, referencing current section
571          * and adding the current file name and line number
572          *
573          * @param string|array $token Expected token(s) that should have been
574          * mentioned before closing this section
575          * @param bool $fatal True iff an exception should be thrown if
576          * the section is not found.
577          */
578         private function checkSection( $tokens, $fatal = true ) {
579                 if ( is_null( $this->section ) ) {
580                         throw new MWException( __METHOD__ . " can not verify a null section!\n" );
581                 }
582                 if ( !is_array( $tokens ) ) {
583                         $tokens = array( $tokens );
584                 }
585                 if ( count( $tokens ) == 0 ) {
586                         throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
587                 }
589                 $data = $this->sectionData;
590                 $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
591                         return isset( $data[$token] );
592                 } );
594                 if ( count( $tokens ) == 0 ) {
595                         if ( !$fatal ) {
596                                 return false;
597                         }
598                         throw new MWException( sprintf(
599                                 "'%s' without '%s' at line %s of %s\n",
600                                 $this->section,
601                                 implode( ',', $tokens ),
602                                 $this->lineNum,
603                                 $this->file
604                         ) );
605                 }
606                 if ( count( $tokens ) > 1 ) {
607                         throw new MWException( sprintf(
608                                 "'%s' with unexpected tokens '%s' at line %s of %s\n",
609                                 $this->section,
610                                 implode( ',', $tokens ),
611                                 $this->lineNum,
612                                 $this->file
613                         ) );
614                 }
616                 $tokens = array_values( $tokens );
617                 return $tokens[0];
618         }
622  * A class to delay execution of a parser test hooks.
623  */
624 class DelayedParserTest {
626         /** Initialized on construction */
627         private $hooks;
628         private $fnHooks;
629         private $transparentHooks;
631         public function __construct() {
632                 $this->reset();
633         }
635         /**
636          * Init/reset or forgot about the current delayed test.
637          * Call to this will erase any hooks function that were pending.
638          */
639         public function reset() {
640                 $this->hooks = array();
641                 $this->fnHooks = array();
642                 $this->transparentHooks = array();
643         }
645         /**
646          * Called whenever we actually want to run the hook.
647          * Should be the case if we found the parserTest is not disabled
648          * @param ParserTest|NewParserTest $parserTest
649          */
650         public function unleash( &$parserTest ) {
651                 if ( !( $parserTest instanceof ParserTest || $parserTest instanceof NewParserTest )     ) {
652                         throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or "
653                                 . "NewParserTest classes\n" );
654                 }
656                 # Trigger delayed hooks. Any failure will make us abort
657                 foreach ( $this->hooks as $hook ) {
658                         $ret = $parserTest->requireHook( $hook );
659                         if ( !$ret ) {
660                                 return false;
661                         }
662                 }
664                 # Trigger delayed function hooks. Any failure will make us abort
665                 foreach ( $this->fnHooks as $fnHook ) {
666                         $ret = $parserTest->requireFunctionHook( $fnHook );
667                         if ( !$ret ) {
668                                 return false;
669                         }
670                 }
672                 # Trigger delayed transparent hooks. Any failure will make us abort
673                 foreach ( $this->transparentHooks as $hook ) {
674                         $ret = $parserTest->requireTransparentHook( $hook );
675                         if ( !$ret ) {
676                                 return false;
677                         }
678                 }
680                 # Delayed execution was successful.
681                 return true;
682         }
684         /**
685          * Similar to ParserTest object but does not run anything
686          * Use unleash() to really execute the hook
687          * @param string $hook
688          */
689         public function requireHook( $hook ) {
690                 $this->hooks[] = $hook;
691         }
693         /**
694          * Similar to ParserTest object but does not run anything
695          * Use unleash() to really execute the hook function
696          * @param string $fnHook
697          */
698         public function requireFunctionHook( $fnHook ) {
699                 $this->fnHooks[] = $fnHook;
700         }
702         /**
703          * Similar to ParserTest object but does not run anything
704          * Use unleash() to really execute the hook function
705          * @param string $fnHook
706          */
707         public function requireTransparentHook( $hook ) {
708                 $this->transparentHooks[] = $hook;
709         }
714  * Initialize and detect the DjVu files support
715  */
716 class DjVuSupport {
718         /**
719          * Initialises DjVu tools global with default values
720          */
721         public function __construct() {
722                 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgFileExtensions, $wgDjvuTxt;
724                 $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu';
725                 $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump';
726                 $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml';
727                 $wgDjvuTxt = $wgDjvuTxt ? $wgDjvuTxt : '/usr/bin/djvutxt';
729                 if ( !in_array( 'djvu', $wgFileExtensions ) ) {
730                         $wgFileExtensions[] = 'djvu';
731                 }
732         }
734         /**
735          * Returns if the DjVu tools are usable
736          *
737          * @return bool
738          */
739         public function isEnabled() {
740                 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgDjvuTxt;
742                 return is_executable( $wgDjvuRenderer )
743                         && is_executable( $wgDjvuDump )
744                         && is_executable( $wgDjvuToXML )
745                         && is_executable( $wgDjvuTxt );
746         }