2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PhpMyAdmin\Import
6 * @package PhpMyAdmin-test
8 declare(strict_types
=1);
10 namespace PhpMyAdmin\Tests
;
12 use PhpMyAdmin\Import
;
13 use PhpMyAdmin\SqlParser\Parser
;
16 use PHPUnit\Framework\TestCase
;
19 * Tests for import functions
21 * @package PhpMyAdmin-test
23 class ImportTest
extends TestCase
31 * Prepares environment for the test.
35 protected function setUp(): void
37 $GLOBALS['server'] = 0;
38 $GLOBALS['cfg']['ServerDefault'] = '';
39 $this->import
= new Import();
43 * Test for checkTimeout
47 public function testCheckTimeout()
49 global $timestamp, $maximum_time, $timeout_passed;
54 $timeout_passed = false;
56 $this->assertFalse($this->import
->checkTimeout());
61 $timeout_passed = true;
63 $this->assertFalse($this->import
->checkTimeout());
68 $timeout_passed = true;
70 $this->assertTrue($this->import
->checkTimeout());
73 $timestamp = time() - 15;
75 $timeout_passed = false;
77 $this->assertFalse($this->import
->checkTimeout());
80 $timestamp = time() - 60;
82 $timeout_passed = false;
84 $this->assertTrue($this->import
->checkTimeout());
92 public function testLookForUse()
99 $this->import
->lookForUse(null, null, null)
107 $this->import
->lookForUse(null, 'myDb', null)
115 $this->import
->lookForUse(null, 'myDb', true)
123 $this->import
->lookForUse('select 1 from myTable', 'myDb', true)
131 $this->import
->lookForUse('use anotherDb', 'myDb', false)
139 $this->import
->lookForUse('use anotherDb', 'myDb', true)
147 $this->import
->lookForUse('use `anotherDb`;', 'myDb', true)
152 * Test for getColumnAlphaName
154 * @param string $expected Expected result of the function
155 * @param int $num The column number
159 * @dataProvider provGetColumnAlphaName
161 public function testGetColumnAlphaName($expected, $num): void
163 $this->assertEquals($expected, $this->import
->getColumnAlphaName($num));
167 * Data provider for testGetColumnAlphaName
171 public function provGetColumnAlphaName()
202 * Test for getColumnNumberFromName
204 * @param int $expected Expected result of the function
205 * @param string|null $name column name(i.e. "A", or "BC", etc.)
209 * @dataProvider provGetColumnNumberFromName
211 public function testGetColumnNumberFromName($expected, $name): void
213 $this->assertEquals($expected, $this->import
->getColumnNumberFromName($name));
217 * Data provider for testGetColumnNumberFromName
221 public function provGetColumnNumberFromName()
252 * Test for getDecimalPrecision
254 * @param int $expected Expected result of the function
255 * @param string|null $size Size of field
259 * @dataProvider provGetDecimalPrecision
261 public function testGetDecimalPrecision($expected, $size): void
263 $this->assertEquals($expected, $this->import
->getDecimalPrecision($size));
267 * Data provider for testGetDecimalPrecision
271 public function provGetDecimalPrecision()
294 * Test for getDecimalScale
296 * @param int $expected Expected result of the function
297 * @param string|null $size Size of field
301 * @dataProvider provGetDecimalScale
303 public function testGetDecimalScale($expected, $size): void
305 $this->assertEquals($expected, $this->import
->getDecimalScale($size));
309 * Data provider for testGetDecimalScale
313 public function provGetDecimalScale()
336 * Test for getDecimalSize
338 * @param array $expected Expected result of the function
339 * @param string|null $cell Cell content
343 * @dataProvider provGetDecimalSize
345 public function testGetDecimalSize($expected, $cell): void
347 $this->assertEquals($expected, $this->import
->getDecimalSize($cell));
351 * Data provider for testGetDecimalSize
355 public function provGetDecimalSize()
390 * Test for detectType
392 * @param int $expected Expected result of the function
393 * @param int|null $type Last cumulative column type (VARCHAR or INT or
394 * BIGINT or DECIMAL or NONE)
395 * @param string|null $cell String representation of the cell for which a
396 * best-fit type is to be determined
400 * @dataProvider provDetectType
402 public function testDetectType($expected, $type, $cell): void
404 $this->assertEquals($expected, $this->import
->detectType($type, $cell));
408 * Data provider for testDetectType
412 public function provDetectType()
472 if (PHP_INT_MAX
> 2147483647) {
485 // Can not detect a BIGINT since the value is over PHP_INT_MAX
502 * Test for getMatchedRows.
506 public function testPMAGetMatchedRows()
508 $GLOBALS['db'] = 'PMA';
510 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
511 ->disableOriginalConstructor()
514 $update_query = 'UPDATE `table_1` '
517 $simulated_update_query = 'SELECT `id` FROM `table_1` WHERE `id` > 10 AND (`id` <> 20)';
519 $delete_query = 'DELETE FROM `table_1` '
521 $simulated_delete_query = 'SELECT * FROM `table_1` WHERE `id` > 10';
523 $dbi->expects($this->any())
526 ->will($this->returnValue(2));
528 $dbi->expects($this->any())
531 ->will($this->returnValue(true));
533 $dbi->expects($this->at(1))
535 ->with($simulated_update_query)
536 ->will($this->returnValue([]));
538 $dbi->expects($this->at(4))
540 ->with($simulated_delete_query)
541 ->will($this->returnValue([]));
543 $GLOBALS['dbi'] = $dbi;
545 $this->simulatedQueryTest($update_query, $simulated_update_query);
546 $this->simulatedQueryTest($delete_query, $simulated_delete_query);
550 * Tests simulated UPDATE/DELETE query.
552 * @param string $sql_query SQL query
553 * @param string $simulated_query Simulated query
557 public function simulatedQueryTest($sql_query, $simulated_query)
559 $parser = new Parser($sql_query);
560 $analyzed_sql_results = [
561 'query' => $sql_query,
563 'statement' => $parser->statements
[0],
566 $simulated_data = $this->import
->getMatchedRows($analyzed_sql_results);
568 // URL to matched rows.
571 'sql_query' => $simulated_query,
573 $matched_rows_url = 'sql.php' . Url
::getCommon($_url_params);
577 'sql_query' => Util
::formatSql(
578 $analyzed_sql_results['query']
581 'matched_rows_url' => $matched_rows_url,
588 * Test for checkIfRollbackPossible
592 public function testPMACheckIfRollbackPossible()
594 $GLOBALS['db'] = 'PMA';
596 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
597 ->disableOriginalConstructor()
600 // List of Transactional Engines.
601 $transactional_engines = [
612 $check_query = 'SELECT `ENGINE` FROM `information_schema`.`tables` '
613 . 'WHERE `table_name` = "%s" '
614 . 'AND `table_schema` = "%s" '
615 . 'AND UPPER(`engine`) IN ("'
616 . implode('", "', $transactional_engines)
619 $check_table_query = 'SELECT * FROM `%s`.`%s` '
622 $dbi->expects($this->at(0))
624 ->with(sprintf($check_table_query, 'PMA', 'table_1'))
625 ->will($this->returnValue(['table']));
627 $dbi->expects($this->at(1))
629 ->with(sprintf($check_query, 'table_1', 'PMA'))
630 ->will($this->returnValue(true));
632 $dbi->expects($this->at(2))
634 ->will($this->returnValue(1));
636 $dbi->expects($this->at(3))
638 ->with(sprintf($check_table_query, 'PMA', 'table_2'))
639 ->will($this->returnValue(['table']));
641 $dbi->expects($this->at(4))
643 ->with(sprintf($check_query, 'table_2', 'PMA'))
644 ->will($this->returnValue(true));
646 $dbi->expects($this->at(5))
648 ->will($this->returnValue(1));
650 $GLOBALS['dbi'] = $dbi;
652 $sql_query = 'UPDATE `table_1` AS t1, `table_2` t2 '
653 . 'SET `table_1`.`id` = `table_2`.`id` '
656 $this->assertEquals(true, $this->import
->checkIfRollbackPossible($sql_query));