3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\Rdbms\Database
;
10 class DatabaseIntegrationTest
extends MediaWikiIntegrationTestCase
{
16 protected function setUp(): void
{
18 $this->db
= MediaWikiServices
::getInstance()
19 ->getConnectionProvider()
20 ->getPrimaryDatabase();
23 public function testUnknownTableCorruptsResults() {
24 $res = $this->db
->newSelectQueryBuilder()
27 ->where( [ 'page_id' => 1 ] )
29 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
30 $this->assertIsInt( $res->numRows() );
33 public function testUniformTablePrefix() {
35 $path = "$IP/sql/tables.json";
36 $tables = json_decode( file_get_contents( $path ), true );
38 // @todo Remove exception once these tables are fixed
45 foreach ( $tables as $table ) {
46 $tableName = $table['name'];
48 if ( in_array( $tableName, $excludeList ) ) {
52 foreach ( $table['columns'] as $column ) {
53 $prefixes[] = strtok( $column['name'], '_' );
55 foreach ( $table['indexes'] ??
[] as $index ) {
56 $prefixes[] = strtok( $index['name'], '_' );
59 if ( count( array_unique( $prefixes ) ) === 1 ) {
60 $prefixes = []; // reset
64 $list = implode( '_, ', $prefixes ) . '_';
67 "Columns and indexes of '$tableName' table should"
68 . " have uniform prefix. Non-uniform found: [ $list ]"
72 $this->assertSame( [], $prefixes );
78 public function testBooleanValues() {
79 $res = $this->db
->newSelectQueryBuilder()
80 ->select( [ 'false' => '1=0', 'true' => '1=1' ] )
82 $obj = $res->fetchObject();
83 $this->assertCount( 2, (array)$obj );
84 $this->assertSame( '0', $obj->false );
85 $this->assertSame( '1', $obj->true );
88 $row = $res->fetchRow();
89 $this->assertCount( 4, $row );
90 $this->assertSame( '0', $row[0] );
91 $this->assertSame( '1', $row[1] );
92 $this->assertSame( '0', $row['false'] );
93 $this->assertSame( '1', $row['true'] );
96 public function testListTables() {
97 $prefix = $this->db
->tablePrefix() . 'listtables_';
98 $table = $prefix . 'table';
99 $view = $prefix . 'view';
100 $allTables = $this->db
->listTables();
101 $this->assertIsArray( $allTables );
103 $this->assertSame( [], $this->db
->listTables( $prefix ) );
106 $this->db
->query( "CREATE TABLE $table (i INT)" );
107 $this->assertSame( [ $table ], $this->db
->listTables( $prefix ) );
108 // Confirm that listTables() does not include views (T45571)
109 $this->db
->query( "CREATE VIEW $view AS SELECT * FROM $table" );
110 $this->assertSame( [ $table ], $this->db
->listTables( $prefix ) );
112 $this->db
->query( "DROP VIEW IF EXISTS $view" );
113 $this->db
->query( "DROP TABLE IF EXISTS $table" );