Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / DatabaseIntegrationTest.php
blob1de730bc747379e5c6282945ac2257b61c0e7c79
1 <?php
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Rdbms\Database;
6 /**
7 * @group Database
8 * @coversNothing
9 */
10 class DatabaseIntegrationTest extends MediaWikiIntegrationTestCase {
11 /**
12 * @var Database
14 protected $db;
16 protected function setUp(): void {
17 parent::setUp();
18 $this->db = MediaWikiServices::getInstance()
19 ->getConnectionProvider()
20 ->getPrimaryDatabase();
23 public function testUnknownTableCorruptsResults() {
24 $res = $this->db->newSelectQueryBuilder()
25 ->select( '*' )
26 ->from( 'page' )
27 ->where( [ 'page_id' => 1 ] )
28 ->fetchResultSet();
29 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
30 $this->assertIsInt( $res->numRows() );
33 public function testUniformTablePrefix() {
34 global $IP;
35 $path = "$IP/sql/tables.json";
36 $tables = json_decode( file_get_contents( $path ), true );
38 // @todo Remove exception once these tables are fixed
39 $excludeList = [
40 'user_newtalk',
41 'objectcache',
44 $prefixes = [];
45 foreach ( $tables as $table ) {
46 $tableName = $table['name'];
48 if ( in_array( $tableName, $excludeList ) ) {
49 continue;
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
61 continue;
64 $list = implode( '_, ', $prefixes ) . '_';
66 $this->fail(
67 "Columns and indexes of '$tableName' table should"
68 . " have uniform prefix. Non-uniform found: [ $list ]"
72 $this->assertSame( [], $prefixes );
75 /**
76 * T352229
78 public function testBooleanValues() {
79 $res = $this->db->newSelectQueryBuilder()
80 ->select( [ 'false' => '1=0', 'true' => '1=1' ] )
81 ->fetchResultSet();
82 $obj = $res->fetchObject();
83 $this->assertCount( 2, (array)$obj );
84 $this->assertSame( '0', $obj->false );
85 $this->assertSame( '1', $obj->true );
87 $res->seek( 0 );
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 ) );
105 try {
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 ) );
111 } finally {
112 $this->db->query( "DROP VIEW IF EXISTS $view" );
113 $this->db->query( "DROP TABLE IF EXISTS $table" );