File: MySQLPlatformTest.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 (199 lines) | stat: -rw-r--r-- 5,007 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace Wikimedia\Tests\Rdbms;

use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
use MediaWikiCoversValidator;
use PHPUnit\Framework\TestCase;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\DBLanguageError;
use Wikimedia\Rdbms\Platform\MySQLPlatform;

/**
 * @covers \Wikimedia\Rdbms\Platform\MySQLPlatform
 */
class MySQLPlatformTest extends TestCase {

	use MediaWikiCoversValidator;

	/** @var MySQLPlatform */
	private $platform;

	protected function setUp(): void {
		parent::setUp();
		$this->platform = new MySQLPlatform( new AddQuoterMock() );
	}

	/**
	 * @dataProvider provideDiapers
	 */
	public function testAddIdentifierQuotes( $expected, $in ) {
		if ( $expected === 'yagni' ) {
			$this->expectException( DBLanguageError::class );
		}
		$quoted = $this->platform->addIdentifierQuotes( $in );
		$this->assertEquals( $expected, $quoted );
	}

	public function testAddIdentifierQuotesNull() {
		// Ignore PHP 8.1+ warning about null to str_replace()
		$quoted = @$this->platform->addIdentifierQuotes( null );
		$this->assertEquals( '``', $quoted );
	}

	/**
	 * Feeds testAddIdentifierQuotes
	 *
	 * Named per T22281 convention.
	 */
	public static function provideDiapers() {
		return [
			// Format: expected, input
			// If expected is "yagni" that means that you're probably not going
			// to need the case to work correctly
			[ '``', '' ],

			// Dear codereviewer, guess what addIdentifierQuotes()
			// will return with thoses:
			[ '``', false ],
			[ '`1`', true ],

			// We never know what could happen
			[ '`0`', 0 ],
			[ '`1`', 1 ],

			// Whatchout! Should probably use something more meaningful
			'single quote' => [ 'yagni', "'" ],
			'double quote' => [ 'yagni', '"' ],
			'backtick' => [ 'yagni', '`' ],
			'apostrophe' => [ '`’`', '’' ],

			// sneaky NUL bytes are lurking everywhere
			[ 'yagni', "\0" ],
			[ 'yagni', "\0x\0y\0z\0z\0y\0" ],

			// unicode chars
			[
				"`\u{0001}a\u{FFFF}b`",
				"\u{0001}a\u{FFFF}b"
			],
			[
				"yagni",
				"\u{0001}\u{0000}\u{FFFF}\u{0000}"
			],
			[ '`☃`', '☃' ],
			[ '`メインページ`', 'メインページ' ],
			[ '`Басты_бет`', 'Басты_бет' ],

			// Real world:
			[ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
			[ 'yagni', 'Backtick: `' ],
			[ '`This is a test`', 'This is a test' ],
		];
	}

	public static function provideTableIdentifiers() {
		// No DB name set
		yield [
			'table',
			new DatabaseDomain( null, null, '' ),
			[ null, 'table' ],
			null
		];
		yield [
			'database.table',
			new DatabaseDomain( null, null, '' ),
			[ 'database', 'table' ],
			null
		];
		yield [
			'database.schema.table',
			new DatabaseDomain( null, null, '' ),
			[ 'database', 'table' ],
			DBLanguageError::class
		];
		yield [
			'"database"."schema"."table"',
			new DatabaseDomain( null, null, '' ),
			[ 'database', 'table' ],
			DBLanguageError::class
		];
		yield [
			'`database`.`table`',
			new DatabaseDomain( null, null, '' ),
			[ 'database', 'table' ],
			null
		];
		// DB name set
		yield [
			'table',
			new DatabaseDomain( 'database', null, '' ),
			[ 'database', 'table' ],
			null
		];
		yield [
			'database.table',
			new DatabaseDomain( 'database', null, '' ),
			[ 'database', 'table' ],
			null
		];
		yield [
			'database.schema.table',
			new DatabaseDomain( 'database', null, '' ),
			[ 'database', 'table' ],
			DBLanguageError::class
		];
		yield [
			'`database`.`schema`.`table`',
			new DatabaseDomain( 'database', null, '' ),
			[ 'database', 'table' ],
			DBLanguageError::class
		];
		yield [
			'`database`.`table`',
			new DatabaseDomain( 'database', null, '' ),
			[ 'database', 'table' ],
			null
		];
	}

	/**
	 * @dataProvider provideTableIdentifiers
	 */
	public function testGetDatabaseAndTableIdentifiers(
		$tableName,
		$domain,
		$expectedIdentifiers,
		$expectedException
	) {
		$platform = new MySQLPlatform( new AddQuoterMock(), null, $domain );

		if ( $expectedException !== null ) {
			$this->expectException( DBLanguageError::class );
			$platform->getDatabaseAndTableIdentifier( $tableName );
		} else {
			$this->assertSame(
				$expectedIdentifiers,
				$platform->getDatabaseAndTableIdentifier( $tableName )
			);
		}
	}
}