~ubuntu-branches/ubuntu/trusty/mediawiki/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/phpunit/MediaWikiTestCase.php

  • Committer: Package Import Robot
  • Author(s): Thorsten Glaser
  • Date: 2014-03-28 09:56:29 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20140328095629-1526y9tchdd507id
Tags: 1:1.19.14+dfsg-1
* New upstream security fix release (Closes: #742857):
  - (bug 62497) SECURITY: Add CSRF token on Special:ChangePassword
  - (bug 62467) Set a title for the context during import on the cli
* Use upstream-provided signing key bundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
4
        public $suite;
 
5
        public $regex = '';
 
6
        public $runDisabled = false;
 
7
 
 
8
        /**
 
9
         * @var DatabaseBase
 
10
         */
 
11
        protected $db;
 
12
        protected $oldTablePrefix;
 
13
        protected $useTemporaryTables = true;
 
14
        protected $reuseDB = false;
 
15
        protected $tablesUsed = array(); // tables with data
 
16
 
 
17
        private static $dbSetup = false;
 
18
 
 
19
        /**
 
20
         * Table name prefixes. Oracle likes it shorter.
 
21
         */
 
22
        const DB_PREFIX = 'unittest_';
 
23
        const ORA_DB_PREFIX = 'ut_';
 
24
 
 
25
        protected $supportedDBs = array(
 
26
                'mysql',
 
27
                'sqlite',
 
28
                'postgres',
 
29
                'oracle'
 
30
        );
 
31
 
 
32
        function  __construct( $name = null, array $data = array(), $dataName = '' ) {
 
33
                parent::__construct( $name, $data, $dataName );
 
34
 
 
35
                $this->backupGlobals = false;
 
36
                $this->backupStaticAttributes = false;
 
37
        }
 
38
 
 
39
        function run( PHPUnit_Framework_TestResult $result = NULL ) {
 
40
                /* Some functions require some kind of caching, and will end up using the db,
 
41
                 * which we can't allow, as that would open a new connection for mysql.
 
42
                 * Replace with a HashBag. They would not be going to persist anyway.
 
43
                 */
 
44
                ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
 
45
 
 
46
                if( $this->needsDB() ) {
 
47
                        global $wgDBprefix;
 
48
                        
 
49
                        $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
 
50
                        $this->reuseDB = $this->getCliArg('reuse-db');
 
51
 
 
52
                        $this->db = wfGetDB( DB_MASTER );
 
53
 
 
54
                        $this->checkDbIsSupported();
 
55
 
 
56
                        $this->oldTablePrefix = $wgDBprefix;
 
57
 
 
58
                        if( !self::$dbSetup ) {
 
59
                                $this->initDB();
 
60
                                self::$dbSetup = true;
 
61
                        }
 
62
 
 
63
                        $this->addCoreDBData();
 
64
                        $this->addDBData();
 
65
 
 
66
                        parent::run( $result );
 
67
 
 
68
                        $this->resetDB();
 
69
                } else {
 
70
                        parent::run( $result );
 
71
                }
 
72
        }
 
73
 
 
74
        function dbPrefix() {
 
75
                return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
 
76
        }
 
77
 
 
78
        function needsDB() {
 
79
                $rc = new ReflectionClass( $this );
 
80
                return strpos( $rc->getDocComment(), '@group Database' ) !== false;
 
81
        }
 
82
 
 
83
        /**
 
84
         * Stub. If a test needs to add additional data to the database, it should
 
85
         * implement this method and do so
 
86
         */
 
87
        function addDBData() {}
 
88
 
 
89
        private function addCoreDBData() {
 
90
                # disabled for performance
 
91
                #$this->tablesUsed[] = 'page';
 
92
                #$this->tablesUsed[] = 'revision';
 
93
 
 
94
                if ( $this->db->getType() == 'oracle' ) {
 
95
 
 
96
                        # Insert 0 user to prevent FK violations
 
97
                        # Anonymous user
 
98
                        $this->db->insert( 'user', array(
 
99
                                'user_id'               => 0,
 
100
                                'user_name'     => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
 
101
 
 
102
                        # Insert 0 page to prevent FK violations
 
103
                        # Blank page
 
104
                        $this->db->insert( 'page', array(
 
105
                                'page_id' => 0,
 
106
                                'page_namespace' => 0,
 
107
                                'page_title' => ' ',
 
108
                                'page_restrictions' => NULL,
 
109
                                'page_counter' => 0,
 
110
                                'page_is_redirect' => 0,
 
111
                                'page_is_new' => 0,
 
112
                                'page_random' => 0,
 
113
                                'page_touched' => $this->db->timestamp(),
 
114
                                'page_latest' => 0,
 
115
                                'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
 
116
 
 
117
                }
 
118
 
 
119
                User::resetIdByNameCache();
 
120
 
 
121
                //Make sysop user
 
122
                $user = User::newFromName( 'UTSysop' );
 
123
 
 
124
                if ( $user->idForName() == 0 ) {
 
125
                        $user->addToDatabase();
 
126
                        $user->setPassword( 'UTSysopPassword' );
 
127
 
 
128
                        $user->addGroup( 'sysop' );
 
129
                        $user->addGroup( 'bureaucrat' );
 
130
                        $user->saveSettings();
 
131
                }
 
132
 
 
133
 
 
134
                //Make 1 page with 1 revision
 
135
                $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
 
136
                if ( !$page->getId() == 0 ) {
 
137
                        $page->doEdit( 'UTContent',
 
138
                                                        'UTPageSummary',
 
139
                                                        EDIT_NEW,
 
140
                                                        false,
 
141
                                                        User::newFromName( 'UTSysop' ) );
 
142
                }
 
143
        }
 
144
 
 
145
        private function initDB() {
 
146
                global $wgDBprefix;
 
147
                if ( $wgDBprefix === $this->dbPrefix() ) {
 
148
                        throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
 
149
                }
 
150
 
 
151
                $tablesCloned = $this->listTables();
 
152
                $dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
 
153
                $dbClone->useTemporaryTables( $this->useTemporaryTables );
 
154
 
 
155
                if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
 
156
                        CloneDatabase::changePrefix( $this->dbPrefix() );
 
157
                        $this->resetDB();
 
158
                        return;
 
159
                } else {
 
160
                        $dbClone->cloneTableStructure();
 
161
                }
 
162
 
 
163
                if ( $this->db->getType() == 'oracle' ) {
 
164
                        $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
 
165
                }
 
166
        }
 
167
 
 
168
        /**
 
169
         * Empty all tables so they can be repopulated for tests
 
170
         */
 
171
        private function resetDB() {
 
172
                if( $this->db ) {
 
173
                        if ( $this->db->getType() == 'oracle' )  {
 
174
                                if ( $this->useTemporaryTables ) {
 
175
                                        wfGetLB()->closeAll();
 
176
                                        $this->db = wfGetDB( DB_MASTER );
 
177
                                } else {
 
178
                                        foreach( $this->tablesUsed as $tbl ) {
 
179
                                                if( $tbl == 'interwiki') continue;
 
180
                                                $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
 
181
                                        }
 
182
                                }
 
183
                        } else {
 
184
                                foreach( $this->tablesUsed as $tbl ) {
 
185
                                        if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
 
186
                                        $this->db->delete( $tbl, '*', __METHOD__ );
 
187
                                }
 
188
                        }
 
189
                }
 
190
        }
 
191
 
 
192
        function __call( $func, $args ) {
 
193
                static $compatibility = array(
 
194
                        'assertInternalType' => 'assertType',
 
195
                        'assertNotInternalType' => 'assertNotType',
 
196
                        'assertInstanceOf' => 'assertType',
 
197
                        'assertEmpty' => 'assertEmpty2',
 
198
                );
 
199
 
 
200
                if ( method_exists( $this->suite, $func ) ) {
 
201
                        return call_user_func_array( array( $this->suite, $func ), $args);
 
202
                } elseif ( isset( $compatibility[$func] ) ) {
 
203
                        return call_user_func_array( array( $this, $compatibility[$func] ), $args);
 
204
                } else {
 
205
                        throw new MWException( "Called non-existant $func method on "
 
206
                                . get_class( $this ) );
 
207
                }
 
208
        }
 
209
 
 
210
        private function assertEmpty2( $value, $msg ) {
 
211
                return $this->assertTrue( $value == '', $msg );
 
212
        }
 
213
 
 
214
        static private function unprefixTable( $tableName ) {
 
215
                global $wgDBprefix;
 
216
                return substr( $tableName, strlen( $wgDBprefix ) );
 
217
        }
 
218
 
 
219
        static private function isNotUnittest( $table ) {
 
220
                return strpos( $table, 'unittest_' ) !== 0;
 
221
        }
 
222
 
 
223
        protected function listTables() {
 
224
                global $wgDBprefix;
 
225
 
 
226
                $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
 
227
                $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
 
228
 
 
229
                // Don't duplicate test tables from the previous fataled run
 
230
                $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
 
231
 
 
232
                if ( $this->db->getType() == 'sqlite' ) {
 
233
                        $tables = array_flip( $tables );
 
234
                        // these are subtables of searchindex and don't need to be duped/dropped separately
 
235
                        unset( $tables['searchindex_content'] );
 
236
                        unset( $tables['searchindex_segdir'] );
 
237
                        unset( $tables['searchindex_segments'] );
 
238
                        $tables = array_flip( $tables );
 
239
                }
 
240
                return $tables;
 
241
        }
 
242
 
 
243
        protected function checkDbIsSupported() {
 
244
                if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
 
245
                        throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
 
246
                }
 
247
        }
 
248
 
 
249
        public function getCliArg( $offset ) {
 
250
 
 
251
                if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
 
252
                        return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
 
253
                }
 
254
 
 
255
        }
 
256
 
 
257
        public function setCliArg( $offset, $value ) {
 
258
 
 
259
                MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
 
260
 
 
261
        }
 
262
 
 
263
        public static function disableInterwikis( $prefix, &$data ) {
 
264
                return false;
 
265
        }
 
266
 
 
267
        /**
 
268
         * Don't throw a warning if $function is deprecated and called later
 
269
         *
 
270
         * @param $function String
 
271
         * @return null
 
272
         */
 
273
        function hideDeprecated( $function ) {
 
274
                wfSuppressWarnings();
 
275
                wfDeprecated( $function );
 
276
                wfRestoreWarnings();
 
277
        }
 
278
}