File: DatabaseIntegrationTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (149 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\Database;

/**
 * @group Database
 * @coversNothing
 */
class DatabaseIntegrationTest extends MediaWikiIntegrationTestCase {
	/**
	 * @var Database
	 */
	protected $db;

	protected function setUp(): void {
		parent::setUp();
		$this->db = MediaWikiServices::getInstance()
			->getConnectionProvider()
			->getPrimaryDatabase();
	}

	public function testUnknownTableCorruptsResults() {
		$res = $this->db->newSelectQueryBuilder()
			->select( '*' )
			->from( 'page' )
			->where( [ 'page_id' => 1 ] )
			->fetchResultSet();
		$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
		$this->assertIsInt( $res->numRows() );
	}

	public function testUniformTablePrefix() {
		global $IP;
		$path = "$IP/maintenance/tables.json";
		$tables = json_decode( file_get_contents( $path ), true );

		// @todo Remove exception once these tables are fixed
		$excludeList = [
			'user_newtalk',
			'objectcache',
		];

		$prefixes = [];
		foreach ( $tables as $table ) {
			$tableName = $table['name'];

			if ( in_array( $tableName, $excludeList ) ) {
				continue;
			}

			foreach ( $table['columns'] as $column ) {
				$prefixes[] = strtok( $column['name'], '_' );
			}
			foreach ( $table['indexes'] ?? [] as $index ) {
				$prefixes[] = strtok( $index['name'], '_' );
			}

			if ( count( array_unique( $prefixes ) ) === 1 ) {
				$prefixes = []; // reset
				continue;
			}

			$list = implode( '_, ', $prefixes ) . '_';

			$this->fail(
				"Columns and indexes of '$tableName' table should"
				. " have uniform prefix. Non-uniform found: [ $list ]"
			);
		}

		$this->assertSame( [], $prefixes );
	}

	public function automaticSqlGenerationParams() {
		return [
			[ 'mysql' ],
			[ 'sqlite' ],
			[ 'postgres' ],
		];
	}

	/**
	 * @dataProvider automaticSqlGenerationParams
	 */
	public function testAutomaticSqlGeneration( $type ) {
		global $IP;
		$abstractSchemaPath = "$IP/maintenance/tables.json";
		if ( $type === 'mysql' ) {
			$oldPath = "$IP/maintenance/tables-generated.sql";
		} else {
			$oldPath = "$IP/maintenance/$type/tables-generated.sql";
		}
		$oldContent = file_get_contents( $oldPath );
		$newPath = $this->getNewTempFile();
		$maintenanceScript = new GenerateSchemaSql();
		$maintenanceScript->loadWithArgv(
			[ '--json=' . $abstractSchemaPath, '--sql=' . $newPath, '--type=' . $type, '--quiet' ]
		);
		$maintenanceScript->execute();
		$this->assertEquals(
			$oldContent,
			file_get_contents( $newPath ),
			"The generated schema in '$type' type has to be the same"
		);
	}

	/**
	 * T352229
	 */
	public function testBooleanValues() {
		$res = $this->db->newSelectQueryBuilder()
			->select( [ 'false' => '1=0', 'true' => '1=1' ] )
			->fetchResultSet();
		$obj = $res->fetchObject();
		$this->assertCount( 2, (array)$obj );
		$this->assertSame( '0', $obj->false );
		$this->assertSame( '1', $obj->true );

		$res->seek( 0 );
		$row = $res->fetchRow();
		$this->assertCount( 4, $row );
		$this->assertSame( '0', $row[0] );
		$this->assertSame( '1', $row[1] );
		$this->assertSame( '0', $row['false'] );
		$this->assertSame( '1', $row['true'] );
	}

	public function testListTables() {
		$prefix = $this->db->tablePrefix() . 'listtables_';
		$table = $prefix . 'table';
		$view = $prefix . 'view';
		$allTables = $this->db->listTables();
		$this->assertIsArray( $allTables );

		$this->assertSame( [], $this->db->listTables( $prefix ) );

		try {
			$this->db->query( "CREATE TABLE $table (i INT)" );
			$this->assertSame( [ $table ], $this->db->listTables( $prefix ) );
			// Confirm that listTables() does not include views (T45571)
			$this->db->query( "CREATE VIEW $view AS SELECT * FROM $table" );
			$this->assertSame( [ $table ], $this->db->listTables( $prefix ) );
		} finally {
			$this->db->query( "DROP VIEW IF EXISTS $view" );
			$this->db->query( "DROP TABLE IF EXISTS $table" );
		}
	}
}