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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\Import;
use function time;
use const PHP_INT_MAX;
/**
* @covers \PhpMyAdmin\Import
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Import::class)]
class ImportTest extends AbstractTestCase
{
/** @var Import $import */
private $import;
/**
* Prepares environment for the test.
*/
protected function setUp(): void
{
parent::setUp();
$GLOBALS['server'] = 0;
$GLOBALS['cfg']['ServerDefault'] = '';
$this->import = new Import();
}
/**
* Test for checkTimeout
*/
public function testCheckTimeout(): void
{
global $timestamp, $maximum_time, $timeout_passed;
//Reinit values.
$timestamp = time();
$maximum_time = 0;
$timeout_passed = false;
self::assertFalse($this->import->checkTimeout());
//Reinit values.
$timestamp = time();
$maximum_time = 0;
$timeout_passed = true;
self::assertFalse($this->import->checkTimeout());
//Reinit values.
$timestamp = time();
$maximum_time = 30;
$timeout_passed = true;
self::assertTrue($this->import->checkTimeout());
//Reinit values.
$timestamp = time() - 15;
$maximum_time = 30;
$timeout_passed = false;
self::assertFalse($this->import->checkTimeout());
//Reinit values.
$timestamp = time() - 60;
$maximum_time = 30;
$timeout_passed = false;
self::assertTrue($this->import->checkTimeout());
}
/**
* Test for lookForUse
*/
public function testLookForUse(): void
{
self::assertSame([
null,
null,
], $this->import->lookForUse(null, null, null));
self::assertSame([
'myDb',
null,
], $this->import->lookForUse(null, 'myDb', null));
self::assertSame([
'myDb',
true,
], $this->import->lookForUse(null, 'myDb', true));
self::assertSame([
'myDb',
true,
], $this->import->lookForUse('select 1 from myTable', 'myDb', true));
self::assertSame([
'anotherDb',
true,
], $this->import->lookForUse('use anotherDb', 'myDb', false));
self::assertSame([
'anotherDb',
true,
], $this->import->lookForUse('use anotherDb', 'myDb', true));
self::assertSame([
'anotherDb',
true,
], $this->import->lookForUse('use `anotherDb`;', 'myDb', true));
}
/**
* Test for getColumnAlphaName
*
* @param string $expected Expected result of the function
* @param int $num The column number
*
* @dataProvider provGetColumnAlphaName
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provGetColumnAlphaName')]
public function testGetColumnAlphaName(string $expected, int $num): void
{
self::assertSame($expected, $this->import->getColumnAlphaName($num));
}
/**
* Data provider for testGetColumnAlphaName
*
* @return array
*/
public static function provGetColumnAlphaName(): array
{
return [
[
'A',
1,
],
[
'Z',
0,
],
[
'AA',
27,
],
[
'AZ',
52,
],
[
'BA',
53,
],
[
'BB',
54,
],
];
}
/**
* Test for getColumnNumberFromName
*
* @param int $expected Expected result of the function
* @param string $name column name(i.e. "A", or "BC", etc.)
*
* @dataProvider provGetColumnNumberFromName
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provGetColumnNumberFromName')]
public function testGetColumnNumberFromName(int $expected, string $name): void
{
self::assertSame($expected, $this->import->getColumnNumberFromName($name));
}
/**
* Data provider for testGetColumnNumberFromName
*
* @return array
*/
public static function provGetColumnNumberFromName(): array
{
return [
[
1,
'A',
],
[
26,
'Z',
],
[
27,
'AA',
],
[
52,
'AZ',
],
[
53,
'BA',
],
[
54,
'BB',
],
];
}
/**
* Test for getDecimalPrecision
*
* @param int $expected Expected result of the function
* @param string $size Size of field
*
* @dataProvider provGetDecimalPrecision
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provGetDecimalPrecision')]
public function testGetDecimalPrecision(int $expected, string $size): void
{
self::assertSame($expected, $this->import->getDecimalPrecision($size));
}
/**
* Data provider for testGetDecimalPrecision
*
* @return array
*/
public static function provGetDecimalPrecision(): array
{
return [
[
2,
'2,1',
],
[
6,
'6,2',
],
[
6,
'6,0',
],
[
16,
'16,2',
],
];
}
/**
* Test for getDecimalScale
*
* @param int $expected Expected result of the function
* @param string $size Size of field
*
* @dataProvider provGetDecimalScale
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provGetDecimalScale')]
public function testGetDecimalScale(int $expected, string $size): void
{
self::assertSame($expected, $this->import->getDecimalScale($size));
}
/**
* Data provider for testGetDecimalScale
*
* @return array
*/
public static function provGetDecimalScale(): array
{
return [
[
1,
'2,1',
],
[
2,
'6,2',
],
[
0,
'6,0',
],
[
20,
'30,20',
],
];
}
/**
* Test for getDecimalSize
*
* @param array $expected Expected result of the function
* @param string $cell Cell content
*
* @dataProvider provGetDecimalSize
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provGetDecimalSize')]
public function testGetDecimalSize(array $expected, string $cell): void
{
self::assertSame($expected, $this->import->getDecimalSize($cell));
}
/**
* Data provider for testGetDecimalSize
*
* @return array
*/
public static function provGetDecimalSize(): array
{
return [
[
[
2,
1,
'2,1',
],
'2.1',
],
[
[
2,
1,
'2,1',
],
'6.2',
],
[
[
3,
1,
'3,1',
],
'10.0',
],
[
[
4,
2,
'4,2',
],
'30.20',
],
];
}
/**
* Test for detectType
*
* @param int $expected Expected result of the function
* @param int|null $type Last cumulative column type (VARCHAR or INT or
* BIGINT or DECIMAL or NONE)
* @param string|null $cell String representation of the cell for which a
* best-fit type is to be determined
*
* @dataProvider provDetectType
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provDetectType')]
public function testDetectType(int $expected, ?int $type, ?string $cell): void
{
self::assertSame($expected, $this->import->detectType($type, $cell));
}
/**
* Data provider for testDetectType
*
* @return array
*/
public static function provDetectType(): array
{
$data = [
[
Import::NONE,
null,
'NULL',
],
[
Import::NONE,
Import::NONE,
'NULL',
],
[
Import::INT,
Import::INT,
'NULL',
],
[
Import::VARCHAR,
Import::VARCHAR,
'NULL',
],
[
Import::VARCHAR,
null,
null,
],
[
Import::VARCHAR,
Import::INT,
null,
],
[
Import::INT,
Import::INT,
'10',
],
[
Import::DECIMAL,
Import::DECIMAL,
'10.2',
],
[
Import::DECIMAL,
Import::INT,
'10.2',
],
[
Import::VARCHAR,
Import::VARCHAR,
'test',
],
[
Import::VARCHAR,
Import::INT,
'test',
],
];
if (PHP_INT_MAX > 2147483647) {
$data[] = [
Import::BIGINT,
Import::BIGINT,
'2147483648',
];
$data[] = [
Import::BIGINT,
Import::INT,
'2147483648',
];
} else {
// To be fixed ?
// Can not detect a BIGINT since the value is over PHP_INT_MAX
$data[] = [
Import::VARCHAR,
Import::BIGINT,
'2147483648',
];
$data[] = [
Import::VARCHAR,
Import::INT,
'2147483648',
];
}
return $data;
}
/**
* Test for checkIfRollbackPossible
*/
public function testPMACheckIfRollbackPossible(): void
{
$GLOBALS['db'] = 'PMA';
$sqlQuery = 'UPDATE `table_1` AS t1, `table_2` t2 SET `table_1`.`id` = `table_2`.`id` WHERE 1';
self::assertTrue($this->import->checkIfRollbackPossible($sqlQuery));
}
/**
* Data provider for testSkipByteOrderMarksFromContents
*
* @return array[]
*/
public static function providerContentWithByteOrderMarks(): array
{
return [
[
"\xEF\xBB\xBF blabla上海",
' blabla上海',
],
[
"\xEF\xBB\xBF blabla",
' blabla',
],
[
"\xEF\xBB\xBF blabla\xEF\xBB\xBF",
" blabla\xEF\xBB\xBF",
],
[
"\xFE\xFF blabla",
' blabla',
],
[
"\xFE\xFF blabla\xFE\xFF",
" blabla\xFE\xFF",
],
[
"\xFF\xFE blabla",
' blabla',
],
[
"\xFF\xFE blabla\xFF\xFE",
" blabla\xFF\xFE",
],
[
"\xEF\xBB\xBF\x44\x52\x4F\x50\x20\x54\x41\x42\x4C\x45\x20\x49\x46\x20\x45\x58\x49\x53\x54\x53",
'DROP TABLE IF EXISTS',
],
];
}
/**
* Test for skipByteOrderMarksFromContents
*
* @param string $input The contents to strip BOM
* @param string $cleanContents The contents cleaned
*
* @dataProvider providerContentWithByteOrderMarks
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerContentWithByteOrderMarks')]
public function testSkipByteOrderMarksFromContents(string $input, string $cleanContents): void
{
self::assertSame($cleanContents, $this->import->skipByteOrderMarksFromContents($input));
}
/**
* Test for runQuery
*/
public function testRunQuery(): void
{
$GLOBALS['run_query'] = true;
$sqlData = [];
$query = 'SELECT 1';
$full = 'SELECT 1';
$this->import->runQuery($query, $full, $sqlData);
self::assertSame([], $sqlData);
self::assertSame([
'sql' => 'SELECT 1;',
'full' => 'SELECT 1;',
], $GLOBALS['import_run_buffer']);
self::assertNull($GLOBALS['sql_query']);
self::assertNull($GLOBALS['complete_query']);
self::assertNull($GLOBALS['display_query']);
$query = 'SELECT 2';
$full = 'SELECT 2';
$this->import->runQuery($query, $full, $sqlData);
self::assertSame([
'valid_sql' => ['SELECT 1;'],
'valid_full' => ['SELECT 1;'],
'valid_queries' => 1,
], $sqlData);
self::assertSame([
'sql' => 'SELECT 2;',
'full' => 'SELECT 2;',
], $GLOBALS['import_run_buffer']);
self::assertSame('SELECT 1;', $GLOBALS['sql_query']);
self::assertSame('SELECT 1;', $GLOBALS['complete_query']);
self::assertSame('SELECT 1;', $GLOBALS['display_query']);
$query = '';
$full = '';
$this->import->runQuery($query, $full, $sqlData);
self::assertSame([
'valid_sql' => [
'SELECT 1;',
'SELECT 2;',
],
'valid_full' => [
'SELECT 1;',
'SELECT 2;',
],
'valid_queries' => 2,
], $sqlData);
self::assertArrayNotHasKey('import_run_buffer', $GLOBALS);
self::assertSame('SELECT 2;', $GLOBALS['sql_query']);
self::assertSame('SELECT 1;SELECT 2;', $GLOBALS['complete_query']);
self::assertSame('SELECT 1;SELECT 2;', $GLOBALS['display_query']);
}
}
|