1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */ |
3
|
|
|
/** |
4
|
|
|
* Test for faked database access |
5
|
|
|
* |
6
|
|
|
* @package PhpMyAdmin-test |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace PhpMyAdmin\Tests; |
11
|
|
|
|
12
|
|
|
use PhpMyAdmin\DatabaseInterface; |
13
|
|
|
use PhpMyAdmin\Dbi\DbiDummy; |
14
|
|
|
use PhpMyAdmin\Tests\PmaTestCase; |
15
|
|
|
use PhpMyAdmin\Util; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests basic functionality of dummy dbi driver |
19
|
|
|
* |
20
|
|
|
* @package PhpMyAdmin-test |
21
|
|
|
*/ |
22
|
|
|
class DatabaseInterfaceTest extends PmaTestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DatabaseInterface |
26
|
|
|
*/ |
27
|
|
|
private $_dbi; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Configures test parameters. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$GLOBALS['server'] = 0; |
37
|
|
|
$extension = new DbiDummy(); |
38
|
|
|
$this->_dbi = new DatabaseInterface($extension); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Tests for DBI::getCurrentUser() method. |
43
|
|
|
* |
44
|
|
|
* @param array $value value |
45
|
|
|
* @param string $string string |
46
|
|
|
* @param array $expected expected result |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
* @test |
50
|
|
|
* @dataProvider currentUserData |
51
|
|
|
*/ |
52
|
|
|
public function testGetCurrentUser($value, $string, $expected) |
53
|
|
|
{ |
54
|
|
|
Util::cacheUnset('mysql_cur_user'); |
55
|
|
|
|
56
|
|
|
$extension = new DbiDummy(); |
57
|
|
|
$extension->setResult('SELECT CURRENT_USER();', $value); |
58
|
|
|
|
59
|
|
|
$dbi = new DatabaseInterface($extension); |
60
|
|
|
|
61
|
|
|
$this->assertEquals( |
62
|
|
|
$expected, |
63
|
|
|
$dbi->getCurrentUserAndHost() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertEquals( |
67
|
|
|
$string, |
68
|
|
|
$dbi->getCurrentUser() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Data provider for getCurrentUser() tests. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function currentUserData() |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
[[['pma@localhost']], 'pma@localhost', ['pma', 'localhost']], |
81
|
|
|
[[['@localhost']], '@localhost', ['', 'localhost']], |
82
|
|
|
[false, '@', ['', '']], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Tests for DBI::getColumnMapFromSql() method. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function testPMAGetColumnMap() |
93
|
|
|
{ |
94
|
|
|
$extension = $this->getMockBuilder('PhpMyAdmin\Dbi\DbiDummy') |
95
|
|
|
->disableOriginalConstructor() |
96
|
|
|
->getMock(); |
97
|
|
|
|
98
|
|
|
$extension->expects($this->any()) |
99
|
|
|
->method('realQuery') |
100
|
|
|
->will($this->returnValue(true)); |
101
|
|
|
|
102
|
|
|
$meta1 = new \stdClass(); |
103
|
|
|
$meta1->table = "meta1_table"; |
104
|
|
|
$meta1->name = "meta1_name"; |
105
|
|
|
|
106
|
|
|
$meta2 = new \stdClass(); |
107
|
|
|
$meta2->table = "meta2_table"; |
108
|
|
|
$meta2->name = "meta2_name"; |
109
|
|
|
|
110
|
|
|
$extension->expects($this->any()) |
111
|
|
|
->method('getFieldsMeta') |
112
|
|
|
->will( |
113
|
|
|
$this->returnValue( |
114
|
|
|
[ |
115
|
|
|
$meta1, $meta2 |
116
|
|
|
] |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$dbi = new DatabaseInterface($extension); |
121
|
|
|
|
122
|
|
|
$sql_query = "PMA_sql_query"; |
123
|
|
|
$view_columns = [ |
124
|
|
|
"view_columns1", "view_columns2" |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
$column_map = $dbi->getColumnMapFromSql( |
128
|
|
|
$sql_query, |
129
|
|
|
$view_columns |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->assertEquals( |
133
|
|
|
[ |
134
|
|
|
'table_name' => 'meta1_table', |
135
|
|
|
'refering_column' => 'meta1_name', |
136
|
|
|
'real_column' => 'view_columns1' |
137
|
|
|
], |
138
|
|
|
$column_map[0] |
139
|
|
|
); |
140
|
|
|
$this->assertEquals( |
141
|
|
|
[ |
142
|
|
|
'table_name' => 'meta2_table', |
143
|
|
|
'refering_column' => 'meta2_name', |
144
|
|
|
'real_column' => 'view_columns2' |
145
|
|
|
], |
146
|
|
|
$column_map[1] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Tests for DBI::getSystemDatabase() method. |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
* @test |
155
|
|
|
*/ |
156
|
|
|
public function testGetSystemDatabase() |
157
|
|
|
{ |
158
|
|
|
$sd = $this->_dbi->getSystemDatabase(); |
159
|
|
|
$this->assertInstanceOf('PhpMyAdmin\SystemDatabase', $sd); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Tests for DBI::postConnectControl() method. |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
* @test |
167
|
|
|
*/ |
168
|
|
|
public function testPostConnectControl() |
169
|
|
|
{ |
170
|
|
|
$GLOBALS['db'] = ''; |
171
|
|
|
$GLOBALS['cfg']['Server']['only_db'] = []; |
172
|
|
|
$this->_dbi->postConnectControl(); |
173
|
|
|
$this->assertInstanceOf('PhpMyAdmin\Database\DatabaseList', $GLOBALS['dblist']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Test for getDbCollation |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
* @test |
181
|
|
|
*/ |
182
|
|
|
public function testGetDbCollation() |
183
|
|
|
{ |
184
|
|
|
$GLOBALS['server'] = 1; |
185
|
|
|
// test case for system schema |
186
|
|
|
$this->assertEquals( |
187
|
|
|
'utf8_general_ci', |
188
|
|
|
$this->_dbi->getDbCollation("information_schema") |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$GLOBALS['cfg']['Server']['DisableIS'] = false; |
192
|
|
|
$GLOBALS['cfg']['DBG']['sql'] = false; |
193
|
|
|
|
194
|
|
|
$this->assertEquals( |
195
|
|
|
'utf8_general_ci', |
196
|
|
|
$this->_dbi->getDbCollation('pma_test') |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test for getServerCollation |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
* @test |
205
|
|
|
*/ |
206
|
|
|
public function testGetServerCollation() |
207
|
|
|
{ |
208
|
|
|
$GLOBALS['server'] = 1; |
209
|
|
|
$GLOBALS['cfg']['DBG']['sql'] = true; |
210
|
|
|
$this->assertEquals('utf8_general_ci', $this->_dbi->getServerCollation()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Test for getConnectionParams |
215
|
|
|
* |
216
|
|
|
* @param array $server_cfg Server configuration |
217
|
|
|
* @param integer $mode Mode to test |
218
|
|
|
* @param array|null $server Server array to test |
219
|
|
|
* @param array $expected Expected result |
220
|
|
|
* |
221
|
|
|
* @return void |
222
|
|
|
* |
223
|
|
|
* @dataProvider connectionParams |
224
|
|
|
*/ |
225
|
|
|
public function testGetConnectionParams($server_cfg, $mode, $server, $expected) |
226
|
|
|
{ |
227
|
|
|
$GLOBALS['cfg']['Server'] = $server_cfg; |
228
|
|
|
$result = $this->_dbi->getConnectionParams($mode, $server); |
229
|
|
|
$this->assertEquals($expected, $result); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Data provider for getConnectionParams test |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function connectionParams() |
238
|
|
|
{ |
239
|
|
|
$cfg_basic = [ |
240
|
|
|
'user' => 'u', |
241
|
|
|
'password' => 'pass', |
242
|
|
|
'host' => '', |
243
|
|
|
'controluser' => 'u2', |
244
|
|
|
'controlpass' => 'p2', |
245
|
|
|
]; |
246
|
|
|
$cfg_ssl = [ |
247
|
|
|
'user' => 'u', |
248
|
|
|
'password' => 'pass', |
249
|
|
|
'host' => '', |
250
|
|
|
'ssl' => true, |
251
|
|
|
'controluser' => 'u2', |
252
|
|
|
'controlpass' => 'p2', |
253
|
|
|
]; |
254
|
|
|
$cfg_control_ssl = [ |
255
|
|
|
'user' => 'u', |
256
|
|
|
'password' => 'pass', |
257
|
|
|
'host' => '', |
258
|
|
|
'control_ssl' => true, |
259
|
|
|
'controluser' => 'u2', |
260
|
|
|
'controlpass' => 'p2', |
261
|
|
|
]; |
262
|
|
|
return [ |
263
|
|
|
[ |
264
|
|
|
$cfg_basic, |
265
|
|
|
DatabaseInterface::CONNECT_USER, |
266
|
|
|
null, |
267
|
|
|
[ |
268
|
|
|
'u', |
269
|
|
|
'pass', |
270
|
|
|
[ |
271
|
|
|
'user' => 'u', |
272
|
|
|
'password' => 'pass', |
273
|
|
|
'host' => 'localhost', |
274
|
|
|
'socket' => null, |
275
|
|
|
'port' => 0, |
276
|
|
|
'ssl' => false, |
277
|
|
|
'compress' => false, |
278
|
|
|
'controluser' => 'u2', |
279
|
|
|
'controlpass' => 'p2', |
280
|
|
|
] |
281
|
|
|
], |
282
|
|
|
], |
283
|
|
|
[ |
284
|
|
|
$cfg_basic, |
285
|
|
|
DatabaseInterface::CONNECT_CONTROL, |
286
|
|
|
null, |
287
|
|
|
[ |
288
|
|
|
'u2', |
289
|
|
|
'p2', |
290
|
|
|
[ |
291
|
|
|
'host' => 'localhost', |
292
|
|
|
'socket' => null, |
293
|
|
|
'port' => 0, |
294
|
|
|
'ssl' => false, |
295
|
|
|
'compress' => false, |
296
|
|
|
] |
297
|
|
|
], |
298
|
|
|
], |
299
|
|
|
[ |
300
|
|
|
$cfg_ssl, |
301
|
|
|
DatabaseInterface::CONNECT_USER, |
302
|
|
|
null, |
303
|
|
|
[ |
304
|
|
|
'u', |
305
|
|
|
'pass', |
306
|
|
|
[ |
307
|
|
|
'user' => 'u', |
308
|
|
|
'password' => 'pass', |
309
|
|
|
'host' => 'localhost', |
310
|
|
|
'socket' => null, |
311
|
|
|
'port' => 0, |
312
|
|
|
'ssl' => true, |
313
|
|
|
'compress' => false, |
314
|
|
|
'controluser' => 'u2', |
315
|
|
|
'controlpass' => 'p2', |
316
|
|
|
] |
317
|
|
|
], |
318
|
|
|
], |
319
|
|
|
[ |
320
|
|
|
$cfg_ssl, |
321
|
|
|
DatabaseInterface::CONNECT_CONTROL, |
322
|
|
|
null, |
323
|
|
|
[ |
324
|
|
|
'u2', |
325
|
|
|
'p2', |
326
|
|
|
[ |
327
|
|
|
'host' => 'localhost', |
328
|
|
|
'socket' => null, |
329
|
|
|
'port' => 0, |
330
|
|
|
'ssl' => true, |
331
|
|
|
'compress' => false, |
332
|
|
|
] |
333
|
|
|
], |
334
|
|
|
], |
335
|
|
|
[ |
336
|
|
|
$cfg_control_ssl, |
337
|
|
|
DatabaseInterface::CONNECT_USER, |
338
|
|
|
null, |
339
|
|
|
[ |
340
|
|
|
'u', |
341
|
|
|
'pass', |
342
|
|
|
[ |
343
|
|
|
'user' => 'u', |
344
|
|
|
'password' => 'pass', |
345
|
|
|
'host' => 'localhost', |
346
|
|
|
'socket' => null, |
347
|
|
|
'port' => 0, |
348
|
|
|
'ssl' => false, |
349
|
|
|
'compress' => false, |
350
|
|
|
'controluser' => 'u2', |
351
|
|
|
'controlpass' => 'p2', |
352
|
|
|
'control_ssl' => true, |
353
|
|
|
] |
354
|
|
|
], |
355
|
|
|
], |
356
|
|
|
[ |
357
|
|
|
$cfg_control_ssl, |
358
|
|
|
DatabaseInterface::CONNECT_CONTROL, |
359
|
|
|
null, |
360
|
|
|
[ |
361
|
|
|
'u2', |
362
|
|
|
'p2', |
363
|
|
|
[ |
364
|
|
|
'host' => 'localhost', |
365
|
|
|
'socket' => null, |
366
|
|
|
'port' => 0, |
367
|
|
|
'ssl' => true, |
368
|
|
|
'compress' => false, |
369
|
|
|
] |
370
|
|
|
], |
371
|
|
|
], |
372
|
|
|
]; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Test error formatting |
377
|
|
|
* |
378
|
|
|
* @param int $error_number Error code |
379
|
|
|
* @param string $error_message Error message as returned by server |
380
|
|
|
* @param string $match Expected text |
381
|
|
|
* |
382
|
|
|
* @return void |
383
|
|
|
* |
384
|
|
|
* @dataProvider errorData |
385
|
|
|
*/ |
386
|
|
|
public function testFormatError($error_number, $error_message, $match) |
387
|
|
|
{ |
388
|
|
|
$this->assertContains( |
389
|
|
|
$match, |
390
|
|
|
DatabaseInterface::formatError($error_number, $error_message) |
391
|
|
|
); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
|
|
public function errorData() |
398
|
|
|
{ |
399
|
|
|
return [ |
400
|
|
|
[2002, 'msg', 'The server is not responding'], |
401
|
|
|
[2003, 'msg', 'The server is not responding'], |
402
|
|
|
[1698, 'msg', 'logout.php'], |
403
|
|
|
[1005, 'msg', 'server_engines.php'], |
404
|
|
|
[1005, 'errno: 13', 'Please check privileges'], |
405
|
|
|
[-1, 'error message', 'error message'], |
406
|
|
|
]; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Tests for DBI::isAmazonRds() method. |
411
|
|
|
* |
412
|
|
|
* @param mixed $value value |
413
|
|
|
* @param mixed $expected expected result |
414
|
|
|
* |
415
|
|
|
* @return void |
416
|
|
|
* @test |
417
|
|
|
* @dataProvider isAmazonRdsData |
418
|
|
|
*/ |
419
|
|
|
public function atestIsAmazonRdsData($value, $expected) |
420
|
|
|
{ |
421
|
|
|
Util::cacheUnset('is_amazon_rds'); |
422
|
|
|
|
423
|
|
|
$extension = new DbiDummy(); |
424
|
|
|
$extension->setResult('SELECT @@basedir', $value); |
425
|
|
|
|
426
|
|
|
$dbi = new DatabaseInterface($extension); |
427
|
|
|
|
428
|
|
|
$this->assertEquals( |
429
|
|
|
$expected, |
430
|
|
|
$dbi->isAmazonRds() |
431
|
|
|
); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Data provider for isAmazonRds() tests. |
436
|
|
|
* |
437
|
|
|
* @return array |
438
|
|
|
*/ |
439
|
|
|
public function isAmazonRdsData() |
440
|
|
|
{ |
441
|
|
|
return [ |
442
|
|
|
[[['/usr']], false], |
443
|
|
|
[[['E:/mysql']], false], |
444
|
|
|
[[['/rdsdbbin/mysql/']], true], |
445
|
|
|
[[['/rdsdbbin/mysql-5.7.18/']], true], |
446
|
|
|
]; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Test for version parsing |
451
|
|
|
* |
452
|
|
|
* @param string $version version to parse |
453
|
|
|
* @param int $expected expected numeric version |
454
|
|
|
* @param int $major expected major version |
455
|
|
|
* @param bool $upgrade whether upgrade should ne needed |
456
|
|
|
* |
457
|
|
|
* @return void |
458
|
|
|
* |
459
|
|
|
* @dataProvider versionData |
460
|
|
|
*/ |
461
|
|
|
public function testVersion($version, $expected, $major, $upgrade) |
462
|
|
|
{ |
463
|
|
|
$ver_int = DatabaseInterface::versionToInt($version); |
464
|
|
|
$this->assertEquals($expected, $ver_int); |
465
|
|
|
$this->assertEquals($major, (int)($ver_int / 10000)); |
466
|
|
|
$this->assertEquals($upgrade, $ver_int < $GLOBALS['cfg']['MysqlMinVersion']['internal']); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @return array |
471
|
|
|
*/ |
472
|
|
|
public function versionData() |
473
|
|
|
{ |
474
|
|
|
return [ |
475
|
|
|
['5.0.5', 50005, 5, true], |
476
|
|
|
['5.05.01', 50501, 5, false], |
477
|
|
|
['5.6.35', 50635, 5, false], |
478
|
|
|
['10.1.22-MariaDB-', 100122, 10, false], |
479
|
|
|
]; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Tests for DBI::setCollationl() method. |
484
|
|
|
* |
485
|
|
|
* @return void |
486
|
|
|
* @test |
487
|
|
|
*/ |
488
|
|
|
public function testSetCollation() |
489
|
|
|
{ |
490
|
|
|
$extension = $this->getMockBuilder('PhpMyAdmin\Dbi\DbiDummy') |
491
|
|
|
->disableOriginalConstructor() |
492
|
|
|
->getMock(); |
493
|
|
|
$extension->expects($this->any())->method('escapeString') |
494
|
|
|
->will($this->returnArgument(1)); |
495
|
|
|
|
496
|
|
|
$extension->expects($this->exactly(4)) |
497
|
|
|
->method('realQuery') |
498
|
|
|
->withConsecutive( |
499
|
|
|
["SET collation_connection = 'utf8_czech_ci';"], |
500
|
|
|
["SET collation_connection = 'utf8mb4_bin_ci';"], |
501
|
|
|
["SET collation_connection = 'utf8_czech_ci';"], |
502
|
|
|
["SET collation_connection = 'utf8_bin_ci';"] |
503
|
|
|
) |
504
|
|
|
->willReturnOnConsecutiveCalls( |
505
|
|
|
true, |
506
|
|
|
true, |
507
|
|
|
true, |
508
|
|
|
true |
509
|
|
|
); |
510
|
|
|
|
511
|
|
|
$dbi = new DatabaseInterface($extension); |
512
|
|
|
|
513
|
|
|
$GLOBALS['charset_connection'] = 'utf8mb4'; |
514
|
|
|
$dbi->setCollation('utf8_czech_ci'); |
515
|
|
|
$dbi->setCollation('utf8mb4_bin_ci'); |
516
|
|
|
$GLOBALS['charset_connection'] = 'utf8'; |
517
|
|
|
$dbi->setCollation('utf8_czech_ci'); |
518
|
|
|
$dbi->setCollation('utf8mb4_bin_ci'); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Tests for DBI::getForeignKeyConstrains() method. |
523
|
|
|
* |
524
|
|
|
* @return void |
525
|
|
|
* @test |
526
|
|
|
*/ |
527
|
|
|
public function testGetForeignKeyConstrains() |
528
|
|
|
{ |
529
|
|
|
$this->assertEquals([ |
530
|
|
|
[ |
531
|
|
|
'TABLE_NAME' => 'table2', |
532
|
|
|
'COLUMN_NAME' => 'idtable2', |
533
|
|
|
'REFERENCED_TABLE_NAME' => 'table1', |
534
|
|
|
'REFERENCED_COLUMN_NAME' => 'idtable1', |
535
|
|
|
] |
536
|
|
|
], $this->_dbi->getForeignKeyConstrains('test',['table1', 'table2'])); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
|