Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/platform/MySQLPlatformTest.php

raw symbols SCIP
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
21 namespace Wikimedia\Tests\Rdbms;
22
23 use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
24 use MediaWikiCoversValidator;
25 use PHPUnit\Framework\TestCase;
26 use Wikimedia\Rdbms\DatabaseDomain;
27 use Wikimedia\Rdbms\DBLanguageError;
28 use Wikimedia\Rdbms\Platform\MySQLPlatform;
29
30 /**
31 * @covers \Wikimedia\Rdbms\Platform\MySQLPlatform
32 */
33 class MySQLPlatformTest extends TestCase {
34
35 use MediaWikiCoversValidator;
36
37 /** @var MySQLPlatform */
38 private $platform;
39
40 protected function setUp(): void {
41 parent::setUp();
42 $this->platform = new MySQLPlatform( new AddQuoterMock() );
43 }
44
45 /**
46 * @dataProvider provideDiapers
47 */
48 public function testAddIdentifierQuotes( $expected, $in ) {
49 if ( $expected === 'yagni' ) {
50 $this->expectException( DBLanguageError::class );
51 }
52 $quoted = $this->platform->addIdentifierQuotes( $in );
53 $this->assertEquals( $expected, $quoted );
54 }
55
56 public function testAddIdentifierQuotesNull() {
57 // Ignore PHP 8.1+ warning about null to str_replace()
58 $quoted = @$this->platform->addIdentifierQuotes( null );
59 $this->assertEquals( '``', $quoted );
60 }
61
62 /**
63 * Feeds testAddIdentifierQuotes
64 *
65 * Named per T22281 convention.
66 */
67 public static function provideDiapers() {
68 return [
69 // Format: expected, input
70 // If expected is "yagni" that means that you're probably not going
71 // to need the case to work correctly
72 [ '``', '' ],
73
74 // Dear codereviewer, guess what addIdentifierQuotes()
75 // will return with thoses:
76 [ '``', false ],
77 [ '`1`', true ],
78
79 // We never know what could happen
80 [ '`0`', 0 ],
81 [ '`1`', 1 ],
82
83 // Whatchout! Should probably use something more meaningful
84 'single quote' => [ 'yagni', "'" ],
85 'double quote' => [ 'yagni', '"' ],
86 'backtick' => [ 'yagni', '`' ],
87 'apostrophe' => [ '`’`', '’' ],
88
89 // sneaky NUL bytes are lurking everywhere
90 [ 'yagni', "\0" ],
91 [ 'yagni', "\0x\0y\0z\0z\0y\0" ],
92
93 // unicode chars
94 [
95 "`\u{0001}a\u{FFFF}b`",
96 "\u{0001}a\u{FFFF}b"
97 ],
98 [
99 "yagni",
100 "\u{0001}\u{0000}\u{FFFF}\u{0000}"
101 ],
102 [ '`☃`', '☃' ],
103 [ '`メインページ`', 'メインページ' ],
104 [ '`Басты_бет`', 'Басты_бет' ],
105
106 // Real world:
107 [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
108 [ 'yagni', 'Backtick: `' ],
109 [ '`This is a test`', 'This is a test' ],
110 ];
111 }
112
113 public static function provideTableIdentifiers() {
114 // No DB name set
115 yield [
116 'table',
117 new DatabaseDomain( null, null, '' ),
118 [ '', 'table' ],
119 null
120 ];
121 yield [
122 'database.table',
123 new DatabaseDomain( null, null, '' ),
124 [ 'database', 'table' ],
125 null
126 ];
127 yield [
128 'database.schema.table',
129 new DatabaseDomain( null, null, '' ),
130 [ 'database', 'table' ],
131 DBLanguageError::class
132 ];
133 yield [
134 '"database"."schema"."table"',
135 new DatabaseDomain( null, null, '' ),
136 [ 'database', 'table' ],
137 DBLanguageError::class
138 ];
139 yield [
140 '`database`.`table`',
141 new DatabaseDomain( null, null, '' ),
142 [ 'database', 'table' ],
143 null
144 ];
145 // DB name set
146 yield [
147 'table',
148 new DatabaseDomain( 'database', null, '' ),
149 [ 'database', 'table' ],
150 null
151 ];
152 yield [
153 'database.table',
154 new DatabaseDomain( 'database', null, '' ),
155 [ 'database', 'table' ],
156 null
157 ];
158 yield [
159 'database.schema.table',
160 new DatabaseDomain( 'database', null, '' ),
161 [ 'database', 'table' ],
162 DBLanguageError::class
163 ];
164 yield [
165 '`database`.`schema`.`table`',
166 new DatabaseDomain( 'database', null, '' ),
167 [ 'database', 'table' ],
168 DBLanguageError::class
169 ];
170 yield [
171 '`database`.`table`',
172 new DatabaseDomain( 'database', null, '' ),
173 [ 'database', 'table' ],
174 null
175 ];
176 }
177
178 /**
179 * @dataProvider provideTableIdentifiers
180 */
181 public function testGetDatabaseAndTableIdentifiers(
182 $tableName,
183 $domain,
184 $expectedIdentifiers,
185 $expectedException
186 ) {
187 $platform = new MySQLPlatform( new AddQuoterMock(), null, $domain );
188
189 if ( $expectedException !== null ) {
190 $this->expectException( DBLanguageError::class );
191 $platform->getDatabaseAndTableIdentifier( $tableName );
192 } else {
193 $this->assertSame(
194 $expectedIdentifiers,
195 $platform->getDatabaseAndTableIdentifier( $tableName )
196 );
197 }
198 }
199 }