3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
9 * @var Array of TestUser
17 protected $tablesUsed = array(); // tables with data
19 private static $useTemporaryTables = true;
20 private static $reuseDB = false;
21 private static $dbSetup = false;
22 private static $oldTablePrefix = false;
25 * Holds the paths of temporary files/directories created through getNewTempFile,
26 * and getNewTempDirectory
30 private $tmpfiles = array();
33 * Holds original values of MediaWiki configuration settings
34 * to be restored in tearDown().
35 * See also setMwGlobal().
38 private $mwGlobals = array();
41 * Table name prefixes. Oracle likes it shorter.
43 const DB_PREFIX
= 'unittest_';
44 const ORA_DB_PREFIX
= 'ut_';
46 protected $supportedDBs = array(
53 function __construct( $name = null, array $data = array(), $dataName = '' ) {
54 parent
::__construct( $name, $data, $dataName );
56 $this->backupGlobals
= false;
57 $this->backupStaticAttributes
= false;
60 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
61 /* Some functions require some kind of caching, and will end up using the db,
62 * which we can't allow, as that would open a new connection for mysql.
63 * Replace with a HashBag. They would not be going to persist anyway.
65 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
67 $needsResetDB = false;
68 $logName = get_class( $this ) . '::' . $this->getName( false );
70 if( $this->needsDB() ) {
71 // set up a DB connection for this test to use
73 self
::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
74 self
::$reuseDB = $this->getCliArg('reuse-db');
76 $this->db
= wfGetDB( DB_MASTER
);
78 $this->checkDbIsSupported();
80 if( !self
::$dbSetup ) {
81 wfProfileIn( $logName . ' (clone-db)' );
83 // switch to a temporary clone of the database
84 self
::setupTestDB( $this->db
, $this->dbPrefix() );
86 if ( ( $this->db
->getType() == 'oracle' ||
!self
::$useTemporaryTables ) && self
::$reuseDB ) {
90 wfProfileOut( $logName . ' (clone-db)' );
93 wfProfileIn( $logName . ' (prepare-db)' );
94 $this->addCoreDBData();
96 wfProfileOut( $logName . ' (prepare-db)' );
101 wfProfileIn( $logName );
102 parent
::run( $result );
103 wfProfileOut( $logName );
105 if( $needsResetDB ) {
106 wfProfileIn( $logName . ' (reset-db)' );
108 wfProfileOut( $logName . ' (reset-db)' );
113 * obtains a new temporary file name
115 * The obtained filename is enlisted to be removed upon tearDown
117 * @returns string: absolute name of the temporary file
119 protected function getNewTempFile() {
120 $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' );
121 $this->tmpfiles
[] = $fname;
126 * obtains a new temporary directory
128 * The obtained directory is enlisted to be removed (recursively with all its contained
129 * files) upon tearDown.
131 * @returns string: absolute name of the temporary directory
133 protected function getNewTempDirectory() {
134 // Starting of with a temporary /file/.
135 $fname = $this->getNewTempFile();
137 // Converting the temporary /file/ to a /directory/
139 // The following is not atomic, but at least we now have a single place,
140 // where temporary directory creation is bundled and can be improved
142 $this->assertTrue( wfMkdirParents( $fname ) );
147 * setUp and tearDown should (where significant)
148 * happen in reverse order.
150 protected function setUp() {
151 wfProfileIn( __METHOD__
);
155 //@todo: global variables to restore for *every* test
165 // Cleaning up temporary files
166 foreach ( $this->tmpfiles
as $fname ) {
167 if ( is_file( $fname ) ||
( is_link( $fname ) ) ) {
169 } elseif ( is_dir( $fname ) ) {
170 wfRecursiveRemoveDir( $fname );
174 // Clean up open transactions
175 if ( $this->needsDB() && $this->db
) {
176 while( $this->db
->trxLevel() > 0 ) {
177 $this->db
->rollback();
181 wfProfileOut( __METHOD__
);
184 protected function tearDown() {
185 wfProfileIn( __METHOD__
);
187 // Cleaning up temporary files
188 foreach ( $this->tmpfiles
as $fname ) {
189 if ( is_file( $fname ) ||
( is_link( $fname ) ) ) {
191 } elseif ( is_dir( $fname ) ) {
192 wfRecursiveRemoveDir( $fname );
196 // Clean up open transactions
197 if ( $this->needsDB() && $this->db
) {
198 while( $this->db
->trxLevel() > 0 ) {
199 $this->db
->rollback();
203 // Restore mw globals
204 foreach ( $this->mwGlobals
as $key => $value ) {
205 $GLOBALS[$key] = $value;
207 $this->mwGlobals
= array();
210 wfProfileOut( __METHOD__
);
214 * Individual test functions may override globals (either directly or through this
215 * setMwGlobals() function), however one must call this method at least once for
216 * each key within the setUp().
217 * That way the key is added to the array of globals that will be reset afterwards
218 * in the tearDown(). And, equally important, that way all other tests are executed
219 * with the same settings (instead of using the unreliable local settings for most
220 * tests and fix it only for some tests).
224 * protected function setUp() {
225 * $this->setMwGlobals( 'wgRestrictStuff', true );
228 * function testFoo() {}
230 * function testBar() {}
231 * $this->assertTrue( self::getX()->doStuff() );
233 * $this->setMwGlobals( 'wgRestrictStuff', false );
234 * $this->assertTrue( self::getX()->doStuff() );
237 * function testQuux() {}
240 * @param array|string $pairs Key to the global variable, or an array
241 * of key/value pairs.
242 * @param mixed $value Value to set the global to (ignored
243 * if an array is given as first argument).
245 protected function setMwGlobals( $pairs, $value = null ) {
247 // Normalize (string, value) to an array
248 if( is_string( $pairs ) ) {
249 $pairs = array( $pairs => $value );
252 foreach ( $pairs as $key => $value ) {
253 // NOTE: make sure we only save the global once or a second call to
254 // setMwGlobals() on the same global would override the original
256 if ( !array_key_exists( $key, $this->mwGlobals
) ) {
257 $this->mwGlobals
[$key] = $GLOBALS[$key];
260 // Override the global
261 $GLOBALS[$key] = $value;
266 * Merges the given values into a MW global array variable.
267 * Useful for setting some entries in a configuration array, instead of
268 * setting the entire array.
270 * @param String $name The name of the global, as in wgFooBar
271 * @param Array $values The array containing the entries to set in that global
273 * @throws MWException if the designated global is not an array.
275 protected function mergeMwGlobalArrayValue( $name, $values ) {
276 if ( !isset( $GLOBALS[$name] ) ) {
279 if ( !is_array( $GLOBALS[$name] ) ) {
280 throw new MWException( "MW global $name is not an array." );
283 // NOTE: do not use array_merge, it screws up for numeric keys.
284 $merged = $GLOBALS[$name];
285 foreach ( $values as $k => $v ) {
290 $this->setMwGlobals( $name, $merged );
293 function dbPrefix() {
294 return $this->db
->getType() == 'oracle' ? self
::ORA_DB_PREFIX
: self
::DB_PREFIX
;
298 # if the test says it uses database tables, it needs the database
299 if ( $this->tablesUsed
) {
303 # if the test says it belongs to the Database group, it needs the database
304 $rc = new ReflectionClass( $this );
305 if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
313 * Stub. If a test needs to add additional data to the database, it should
314 * implement this method and do so
316 function addDBData() {}
318 private function addCoreDBData() {
319 # disabled for performance
320 #$this->tablesUsed[] = 'page';
321 #$this->tablesUsed[] = 'revision';
323 if ( $this->db
->getType() == 'oracle' ) {
325 # Insert 0 user to prevent FK violations
327 $this->db
->insert( 'user', array(
329 'user_name' => 'Anonymous' ), __METHOD__
, array( 'IGNORE' ) );
331 # Insert 0 page to prevent FK violations
333 $this->db
->insert( 'page', array(
335 'page_namespace' => 0,
337 'page_restrictions' => NULL,
339 'page_is_redirect' => 0,
342 'page_touched' => $this->db
->timestamp(),
344 'page_len' => 0 ), __METHOD__
, array( 'IGNORE' ) );
348 User
::resetIdByNameCache();
351 $user = User
::newFromName( 'UTSysop' );
353 if ( $user->idForName() == 0 ) {
354 $user->addToDatabase();
355 $user->setPassword( 'UTSysopPassword' );
357 $user->addGroup( 'sysop' );
358 $user->addGroup( 'bureaucrat' );
359 $user->saveSettings();
363 //Make 1 page with 1 revision
364 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
365 if ( !$page->getId() == 0 ) {
366 $page->doEditContent(
367 new WikitextContent( 'UTContent' ),
371 User
::newFromName( 'UTSysop' ) );
376 * Restores MediaWiki to using the table set (table prefix) it was using before
377 * setupTestDB() was called. Useful if we need to perform database operations
378 * after the test run has finished (such as saving logs or profiling info).
380 public static function teardownTestDB() {
381 if ( !self
::$dbSetup ) {
385 CloneDatabase
::changePrefix( self
::$oldTablePrefix );
387 self
::$oldTablePrefix = false;
388 self
::$dbSetup = false;
392 * Creates an empty skeleton of the wiki database by cloning its structure
393 * to equivalent tables using the given $prefix. Then sets MediaWiki to
394 * use the new set of tables (aka schema) instead of the original set.
396 * This is used to generate a dummy table set, typically consisting of temporary
397 * tables, that will be used by tests instead of the original wiki database tables.
399 * @note: the original table prefix is stored in self::$oldTablePrefix. This is used
400 * by teardownTestDB() to return the wiki to using the original table set.
402 * @note: this method only works when first called. Subsequent calls have no effect,
403 * even if using different parameters.
405 * @param DatabaseBase $db The database connection
406 * @param String $prefix The prefix to use for the new table set (aka schema).
408 * @throws MWException if the database table prefix is already $prefix
410 public static function setupTestDB( DatabaseBase
$db, $prefix ) {
412 if ( $wgDBprefix === $prefix ) {
413 throw new MWException( 'Cannot run unit tests, the database prefix is already "' . $prefix . '"' );
416 if ( self
::$dbSetup ) {
420 $tablesCloned = self
::listTables( $db );
421 $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix );
422 $dbClone->useTemporaryTables( self
::$useTemporaryTables );
424 self
::$dbSetup = true;
425 self
::$oldTablePrefix = $wgDBprefix;
427 if ( ( $db->getType() == 'oracle' ||
!self
::$useTemporaryTables ) && self
::$reuseDB ) {
428 CloneDatabase
::changePrefix( $prefix );
431 $dbClone->cloneTableStructure();
434 if ( $db->getType() == 'oracle' ) {
435 $db->query( 'BEGIN FILL_WIKI_INFO; END;' );
440 * Empty all tables so they can be repopulated for tests
442 private function resetDB() {
444 if ( $this->db
->getType() == 'oracle' ) {
445 if ( self
::$useTemporaryTables ) {
446 wfGetLB()->closeAll();
447 $this->db
= wfGetDB( DB_MASTER
);
449 foreach( $this->tablesUsed
as $tbl ) {
450 if( $tbl == 'interwiki') continue;
451 $this->db
->query( 'TRUNCATE TABLE '.$this->db
->tableName($tbl), __METHOD__
);
455 foreach( $this->tablesUsed
as $tbl ) {
456 if( $tbl == 'interwiki' ||
$tbl == 'user' ) continue;
457 $this->db
->delete( $tbl, '*', __METHOD__
);
463 function __call( $func, $args ) {
464 static $compatibility = array(
465 'assertInternalType' => 'assertType',
466 'assertNotInternalType' => 'assertNotType',
467 'assertInstanceOf' => 'assertType',
468 'assertEmpty' => 'assertEmpty2',
471 if ( method_exists( $this->suite
, $func ) ) {
472 return call_user_func_array( array( $this->suite
, $func ), $args);
473 } elseif ( isset( $compatibility[$func] ) ) {
474 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
476 throw new MWException( "Called non-existant $func method on "
477 . get_class( $this ) );
481 private function assertEmpty2( $value, $msg ) {
482 return $this->assertTrue( $value == '', $msg );
485 static private function unprefixTable( $tableName ) {
487 return substr( $tableName, strlen( $wgDBprefix ) );
490 static private function isNotUnittest( $table ) {
491 return strpos( $table, 'unittest_' ) !== 0;
494 public static function listTables( $db ) {
497 $tables = $db->listTables( $wgDBprefix, __METHOD__
);
498 $tables = array_map( array( __CLASS__
, 'unprefixTable' ), $tables );
500 // Don't duplicate test tables from the previous fataled run
501 $tables = array_filter( $tables, array( __CLASS__
, 'isNotUnittest' ) );
503 if ( $db->getType() == 'sqlite' ) {
504 $tables = array_flip( $tables );
505 // these are subtables of searchindex and don't need to be duped/dropped separately
506 unset( $tables['searchindex_content'] );
507 unset( $tables['searchindex_segdir'] );
508 unset( $tables['searchindex_segments'] );
509 $tables = array_flip( $tables );
514 protected function checkDbIsSupported() {
515 if( !in_array( $this->db
->getType(), $this->supportedDBs
) ) {
516 throw new MWException( $this->db
->getType() . " is not currently supported for unit testing." );
520 public function getCliArg( $offset ) {
522 if( isset( MediaWikiPHPUnitCommand
::$additionalOptions[$offset] ) ) {
523 return MediaWikiPHPUnitCommand
::$additionalOptions[$offset];
528 public function setCliArg( $offset, $value ) {
530 MediaWikiPHPUnitCommand
::$additionalOptions[$offset] = $value;
535 * Don't throw a warning if $function is deprecated and called later
537 * @param $function String
540 function hideDeprecated( $function ) {
541 wfSuppressWarnings();
542 wfDeprecated( $function );
547 * Asserts that the given database query yields the rows given by $expectedRows.
548 * The expected rows should be given as indexed (not associative) arrays, with
549 * the values given in the order of the columns in the $fields parameter.
550 * Note that the rows are sorted by the columns given in $fields.
554 * @param $table String|Array the table(s) to query
555 * @param $fields String|Array the columns to include in the result (and to sort by)
556 * @param $condition String|Array "where" condition(s)
557 * @param $expectedRows Array - an array of arrays giving the expected rows.
559 * @throws MWException if this test cases's needsDB() method doesn't return true.
560 * Test cases can use "@group Database" to enable database test support,
561 * or list the tables under testing in $this->tablesUsed, or override the
564 protected function assertSelect( $table, $fields, $condition, array $expectedRows ) {
565 if ( !$this->needsDB() ) {
566 throw new MWException( 'When testing database state, the test cases\'s needDB()' .
567 ' method should return true. Use @group Database or $this->tablesUsed.');
570 $db = wfGetDB( DB_SLAVE
);
572 $res = $db->select( $table, $fields, $condition, wfGetCaller(), array( 'ORDER BY' => $fields ) );
573 $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
577 foreach ( $expectedRows as $expected ) {
578 $r = $res->fetchRow();
579 self
::stripStringKeys( $r );
582 $this->assertNotEmpty( $r, "row #$i missing" );
584 $this->assertEquals( $expected, $r, "row #$i mismatches" );
587 $r = $res->fetchRow();
588 self
::stripStringKeys( $r );
590 $this->assertFalse( $r, "found extra row (after #$i)" );
594 * Utility method taking an array of elements and wrapping
595 * each element in it's own array. Useful for data providers
596 * that only return a single argument.
600 * @param array $elements
604 protected function arrayWrap( array $elements ) {
606 function( $element ) {
607 return array( $element );
614 * Assert that two arrays are equal. By default this means that both arrays need to hold
615 * the same set of values. Using additional arguments, order and associated key can also
616 * be set as relevant.
620 * @param array $expected
621 * @param array $actual
622 * @param boolean $ordered If the order of the values should match
623 * @param boolean $named If the keys should match
625 protected function assertArrayEquals( array $expected, array $actual, $ordered = false, $named = false ) {
627 $this->objectAssociativeSort( $expected );
628 $this->objectAssociativeSort( $actual );
632 $expected = array_values( $expected );
633 $actual = array_values( $actual );
636 call_user_func_array(
637 array( $this, 'assertEquals' ),
638 array_merge( array( $expected, $actual ), array_slice( func_get_args(), 4 ) )
643 * Put each HTML element on its own line and then equals() the results
645 * Use for nicely formatting of PHPUnit diff output when comparing very
650 * @param String $expected HTML on oneline
651 * @param String $actual HTML on oneline
652 * @param String $msg Optional message
654 protected function assertHTMLEquals( $expected, $actual, $msg='' ) {
655 $expected = str_replace( '>', ">\n", $expected );
656 $actual = str_replace( '>', ">\n", $actual );
658 $this->assertEquals( $expected, $actual, $msg );
662 * Does an associative sort that works for objects.
666 * @param array $array
668 protected function objectAssociativeSort( array &$array ) {
672 return serialize( $a ) > serialize( $b ) ?
1 : -1;
678 * Utility function for eliminating all string keys from an array.
679 * Useful to turn a database result row as returned by fetchRow() into
680 * a pure indexed array.
684 * @param $r mixed the array to remove string keys from.
686 protected static function stripStringKeys( &$r ) {
687 if ( !is_array( $r ) ) {
691 foreach ( $r as $k => $v ) {
692 if ( is_string( $k ) ) {
699 * Asserts that the provided variable is of the specified
700 * internal type or equals the $value argument. This is useful
701 * for testing return types of functions that return a certain
702 * type or *value* when not set or on error.
706 * @param string $type
707 * @param mixed $actual
708 * @param mixed $value
709 * @param string $message
711 protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) {
712 if ( $actual === $value ) {
713 $this->assertTrue( true, $message );
716 $this->assertType( $type, $actual, $message );
721 * Asserts the type of the provided value. This can be either
722 * in internal type such as boolean or integer, or a class or
723 * interface the value extends or implements.
727 * @param string $type
728 * @param mixed $actual
729 * @param string $message
731 protected function assertType( $type, $actual, $message = '' ) {
732 if ( class_exists( $type ) ||
interface_exists( $type ) ) {
733 $this->assertInstanceOf( $type, $actual, $message );
736 $this->assertInternalType( $type, $actual, $message );
741 * Returns true iff the given namespace defaults to Wikitext
742 * according to $wgNamespaceContentModels
744 * @param int $ns The namespace ID to check
749 protected function isWikitextNS( $ns ) {
750 global $wgNamespaceContentModels;
752 if ( isset( $wgNamespaceContentModels[$ns] ) ) {
753 return $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
;
760 * Returns the ID of a namespace that defaults to Wikitext.
761 * Throws an MWException if there is none.
763 * @return int the ID of the wikitext Namespace
766 protected function getDefaultWikitextNS() {
767 global $wgNamespaceContentModels;
769 static $wikitextNS = null; // this is not going to change
770 if ( $wikitextNS !== null ) {
774 // quickly short out on most common case:
775 if ( !isset( $wgNamespaceContentModels[NS_MAIN
] ) ) {
779 // NOTE: prefer content namespaces
780 $namespaces = array_unique( array_merge(
781 MWNamespace
::getContentNamespaces(),
782 array( NS_MAIN
, NS_HELP
, NS_PROJECT
), // prefer these
783 MWNamespace
::getValidNamespaces()
786 $namespaces = array_diff( $namespaces, array(
787 NS_FILE
, NS_CATEGORY
, NS_MEDIAWIKI
, NS_USER
// don't mess with magic namespaces
790 $talk = array_filter( $namespaces, function ( $ns ) {
791 return MWNamespace
::isTalk( $ns );
794 // prefer non-talk pages
795 $namespaces = array_diff( $namespaces, $talk );
796 $namespaces = array_merge( $namespaces, $talk );
798 // check default content model of each namespace
799 foreach ( $namespaces as $ns ) {
800 if ( !isset( $wgNamespaceContentModels[$ns] ) ||
801 $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
) {
809 // @todo: Inside a test, we could skip the test as incomplete.
810 // But frequently, this is used in fixture setup.
811 throw new MWException( "No namespace defaults to wikitext!" );
815 * Check, if $wgDiff3 is set and ready to merge
816 * Will mark the calling test as skipped, if not ready
820 protected function checkHasDiff3() {
823 # This check may also protect against code injection in
824 # case of broken installations.
825 wfSuppressWarnings();
826 $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
830 $this->markTestSkipped( "Skip test, since diff3 is not configured" );
835 * Asserts that an exception of the specified type occurs when running
840 * @param callable $code
841 * @param string $expected
842 * @param string $message
844 protected function assertException( $code, $expected = 'Exception', $message = '' ) {
848 call_user_func( $code );
850 catch ( Exception
$pokemons ) {
851 // Gotta Catch 'Em All!
854 if ( $message === '' ) {
855 $message = 'An exception of type "' . $expected . '" should have been thrown';
858 $this->assertInstanceOf( $expected, $pokemons, $message );