Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UnitTestsHookHandler
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onUnitTestsBeforeDatabaseTeardown
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 onUnitTestsAfterDatabaseSetup
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Hooks\Handlers;
22
23use MediaWiki\Hook\UnitTestsAfterDatabaseSetupHook;
24use MediaWiki\Hook\UnitTestsBeforeDatabaseTeardownHook;
25use Wikimedia\Rdbms\ILoadBalancer;
26use Wikimedia\Rdbms\IMaintainableDatabase;
27
28class UnitTestsHookHandler implements
29    UnitTestsAfterDatabaseSetupHook,
30    UnitTestsBeforeDatabaseTeardownHook
31{
32    public const CENTRALAUTH_TABLES = [
33        'global_edit_count',
34        'global_group_permissions',
35        'global_group_restrictions',
36        'global_user_autocreate_serial',
37        'global_user_groups',
38        'globalnames',
39        'globaluser',
40        'localnames',
41        'localuser',
42        'wikiset',
43        'renameuser_status',
44        'renameuser_queue',
45        'users_to_rename',
46    ];
47
48    /** @var ILoadBalancer */
49    private $loadBalancer;
50
51    /**
52     * @param ILoadBalancer $loadBalancer
53     */
54    public function __construct( ILoadBalancer $loadBalancer ) {
55        $this->loadBalancer = $loadBalancer;
56    }
57
58    /**
59     * UnitTestsBeforeDatabaseTeardown hook handler
60     * Cleans up tables created by onUnitTestsAfterDatabaseSetup() above
61     */
62    public function onUnitTestsBeforeDatabaseTeardown() {
63        $dbw = $this->loadBalancer->getMaintenanceConnectionRef( DB_PRIMARY );
64        foreach ( self::CENTRALAUTH_TABLES as $table ) {
65            $dbw->dropTable( $table );
66        }
67    }
68
69    /**
70     * UnitTestsAfterDatabaseSetup hook handler
71     *
72     * Setup the centralauth tables in the current DB, so we don't have
73     * to worry about rights on another database. The first time it's called
74     * we have to set the DB prefix ourselves, and reset it back to the original
75     * so that CloneDatabase will work. On subsequent runs, the prefix is already
76     * set up for us.
77     *
78     * @param IMaintainableDatabase $db
79     * @param string $prefix
80     */
81    public function onUnitTestsAfterDatabaseSetup( $db, $prefix ) {
82        $originalPrefix = $db->tablePrefix();
83        $db->tablePrefix( $prefix );
84        if ( !$db->tableExists( 'globaluser', __METHOD__ ) ) {
85            $engine = $db->getType();
86            $db->sourceFile( __DIR__ . "/../../../schema/$engine/tables-generated.sql" );
87        }
88        $db->tablePrefix( $originalPrefix );
89    }
90}