3 abstract class MediaWikiTestCase
extends PHPUnit_Framework_TestCase
{
6 public $runDisabled = false;
8 protected static $databaseSetupDone = false;
11 protected $oldTablePrefix;
12 protected $useTemporaryTables = true;
14 function __construct( $name = null, array $data = array(), $dataName = '' ) {
16 $this->setName($name);
20 $this->dataName
= $dataName;
23 function run( PHPUnit_Framework_TestResult
$result = NULL ) {
24 if( $this->needsDB() && !is_object( $this->dbClone
) ) {
26 $this->addCoreDBData();
29 parent
::run( $result );
32 function __destruct() {
33 if( is_object( $this->dbClone
) && $this->dbClone
instanceof CloneDatabase
) {
39 $rc = new ReflectionClass( $this );
40 return strpos( $rc->getDocComment(), '@group Database' ) !== false;
43 function addDBData() {}
45 private function addCoreDBData() {
48 $user = User
::newFromName( 'UTSysop' );
50 if ( $user->idForName() == 0 ) {
51 $user->addToDatabase();
52 $user->setPassword( 'UTSysopPassword' );
54 $user->addGroup( 'sysop' );
55 $user->addGroup( 'bureaucrat' );
56 $user->saveSettings();
60 //Make 1 page with 1 revision
61 $article = new Article( Title
::newFromText( 'UTPage' ) );
62 $article->doEdit( 'UTContent',
66 User
::newFromName( 'UTSysop' ) );
69 private function initDB() {
72 if ( self
::$databaseSetupDone ) {
76 $this->db
= wfGetDB( DB_MASTER
);
77 $dbType = $this->db
->getType();
79 if ( $wgDBprefix === 'unittest_' ||
( $dbType == 'oracle' && $wgDBprefix === 'ut_' ) ) {
80 throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
83 self
::$databaseSetupDone = true;
84 $this->oldTablePrefix
= $wgDBprefix;
86 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
87 # It seems to have been fixed since (r55079?).
88 # If it fails, $wgCaches[CACHE_DB] = new HashBagOStuff(); should work around it.
90 # CREATE TEMPORARY TABLE breaks if there is more than one server
91 if ( wfGetLB()->getServerCount() != 1 ) {
92 $this->useTemporaryTables
= false;
95 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
97 $tables = $this->listTables();
99 $prefix = $dbType != 'oracle' ?
'unittest_' : 'ut_';
101 $this->dbClone
= new CloneDatabase( $this->db
, $tables, $prefix );
102 $this->dbClone
->useTemporaryTables( $temporary );
103 $this->dbClone
->cloneTableStructure();
105 if ( $dbType == 'oracle' )
106 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
108 if ( $dbType == 'oracle' ) {
109 # Insert 0 user to prevent FK violations
112 $this->db
->insert( 'user', array(
114 'user_name' => 'Anonymous' ) );
119 protected function destroyDB() {
120 if ( !self
::$databaseSetupDone ) {
124 $this->dbClone
->destroy();
125 self
::$databaseSetupDone = false;
127 if ( $this->useTemporaryTables
) {
128 # Don't need to do anything
130 //Temporary tables seem to be broken ATM, delete anyway
133 if( $this->db
->getType() == 'oracle' ) {
134 $tables = $this->db
->listTables( 'ut_', __METHOD__
);
137 $tables = $this->db
->listTables( 'unittest_', __METHOD__
);
140 foreach ( $tables as $table ) {
141 $sql = $this->db
->getType() == 'oracle' ?
"DROP TABLE $table DROP CONSTRAINTS" : "DROP TABLE `$table`";
142 $this->db
->query( $sql );
145 if ( $this->db
->getType() == 'oracle' )
146 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
151 function __call( $func, $args ) {
152 static $compatibility = array(
153 'assertInternalType' => 'assertType',
154 'assertNotInternalType' => 'assertNotType',
155 'assertInstanceOf' => 'assertType',
158 if ( method_exists( $this->suite
, $func ) ) {
159 return call_user_func_array( array( $this->suite
, $func ), $args);
160 } elseif ( isset( $compatibility[$func] ) ) {
161 return call_user_func_array( array( $this, $compatibility[$func] ), $args);
163 throw new MWException( "Called non-existant $func method on "
164 . get_class( $this ) );
168 protected function listTables() {
171 $tables = $this->db
->listTables( $wgDBprefix, __METHOD__
);
172 $tables = array_map( create_function( '$table', 'global $wgDBprefix; return substr( $table, strlen( $wgDBprefix ) );' ), $tables );