3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests\Plugins\Import
;
7 use PhpMyAdmin\DatabaseInterface
;
9 use PhpMyAdmin\Plugins\Import\ImportCsv
;
10 use PhpMyAdmin\Tests\AbstractTestCase
;
13 use function basename
;
16 * @covers \PhpMyAdmin\Plugins\Import\ImportCsv
18 class ImportCsvTest
extends AbstractTestCase
24 * Sets up the fixture, for example, opens a network connection.
25 * This method is called before a test is executed.
27 protected function setUp(): void
30 $GLOBALS['server'] = 0;
31 $GLOBALS['plugin_param'] = 'csv';
32 $this->object = new ImportCsv();
34 unset($GLOBALS['db']);
37 $GLOBALS['finished'] = false;
38 $GLOBALS['read_limit'] = 100000000;
39 $GLOBALS['offset'] = 0;
40 $GLOBALS['cfg']['Server']['DisableIS'] = false;
42 $GLOBALS['import_file'] = 'test/test_data/db_test.csv';
43 $GLOBALS['import_text'] = 'ImportCsv_Test';
44 $GLOBALS['compression'] = 'none';
45 $GLOBALS['read_multiply'] = 10;
46 $GLOBALS['import_type'] = 'Xml';
49 $GLOBALS['csv_terminated'] = "\015";
50 $GLOBALS['csv_enclosed'] = '"';
51 $GLOBALS['csv_escaped'] = '"';
52 $GLOBALS['csv_new_line'] = 'auto';
53 $GLOBALS['import_file_name'] = basename($GLOBALS['import_file'], '.csv');
58 $dbi = $this->getMockBuilder(DatabaseInterface
::class)
59 ->disableOriginalConstructor()
61 $GLOBALS['dbi'] = $dbi;
65 * Tears down the fixture, for example, closes a network connection.
66 * This method is called after a test is executed.
68 protected function tearDown(): void
75 * Test for getProperties
79 public function testGetProperties(): void
81 $properties = $this->object->getProperties();
84 $properties->getText()
88 $properties->getExtension()
97 public function testDoImport(): void
99 //$sql_query_disabled will show the import SQL detail
100 global $sql_query, $sql_query_disabled;
101 $sql_query_disabled = false;
103 $importHandle = new File($GLOBALS['import_file']);
104 $importHandle->open();
106 //Test function called
107 $this->object->doImport($importHandle);
109 //asset that all sql are executed
110 $this->assertStringContainsString('CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER', $sql_query);
111 $this->assertStringContainsString(
112 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . $GLOBALS['import_file_name'] . '`',
116 $this->assertTrue($GLOBALS['finished']);
120 * Test for partial import/setting table and database names in doImport
124 public function testDoPartialImport(): void
126 //$sql_query_disabled will show the import SQL detail
127 global $sql_query, $sql_query_disabled;
128 $sql_query_disabled = false;
130 $importHandle = new File($GLOBALS['import_file']);
131 $importHandle->open();
133 $GLOBALS['import_file'] = 'test/test_data/db_test_partial_import.csv';
134 $_REQUEST['csv_new_tbl_name'] = 'ImportTestTable';
135 $_REQUEST['csv_new_db_name'] = 'ImportTestDb';
136 $_REQUEST['csv_partial_import'] = 5;
138 //Test function called
139 $this->object->doImport($importHandle);
141 //asset that all sql are executed
142 $this->assertStringContainsString('CREATE DATABASE IF NOT EXISTS `ImportTestDb` DEFAULT CHARACTER', $sql_query);
143 $this->assertStringContainsString('CREATE TABLE IF NOT EXISTS `ImportTestDb`.`ImportTestTable`', $sql_query);
145 $this->assertTrue($GLOBALS['finished']);
147 unset($_REQUEST['csv_new_tbl_name']);
148 unset($_REQUEST['csv_new_db_name']);
149 unset($_REQUEST['csv_partial_import']);
153 * Test for getProperties for Table param
157 public function testGetPropertiesForTable(): void
159 $GLOBALS['plugin_param'] = 'table';
160 $this->object = new ImportCsv();
161 $properties = $this->object->getProperties();
164 $properties->getText()
168 $properties->getExtension()
173 * Test for doImport for _getAnalyze = false, should be OK as well
177 public function testDoImportNotAnalysis(): void
179 //$sql_query_disabled will show the import SQL detail
180 global $sql_query, $sql_query_disabled;
181 $sql_query_disabled = false;
183 $importHandle = new File($GLOBALS['import_file']);
184 $importHandle->open();
186 //Test function called
187 $this->object->doImport($importHandle);
189 //asset that all sql are executed
190 $this->assertStringContainsString('CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER', $sql_query);
192 $this->assertStringContainsString(
193 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . $GLOBALS['import_file_name'] . '`',
197 $this->assertTrue($GLOBALS['finished']);
201 * Test for doImport in the most basic and normal way
205 public function testDoImportNormal(): void
207 //$sql_query_disabled will show the import SQL detail
208 global $sql_query, $sql_query_disabled;
209 $sql_query_disabled = false;
210 $GLOBALS['import_type'] = 'query';
211 $GLOBALS['import_file'] = 'none';
212 $GLOBALS['csv_terminated'] = ',';
213 $GLOBALS['import_text'] = '"Row 1","Row 2"' . "\n" . '"123","456"';
215 parent
::setGlobalDbi();
217 $this->dummyDbi
->addResult(
222 $this->dummyDbi
->addResult(
223 'SELECT TABLE_NAME FROM information_schema.VIEWS'
224 . ' WHERE TABLE_SCHEMA = \'CSV_DB 1\' AND TABLE_NAME = \'db_test\'',
228 $this->object->doImport();
231 'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
232 . 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`COL 1` varchar(5), `COL 2` varchar(5))'
233 . ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;INSERT INTO `CSV_DB 1`.`db_test`'
234 . ' (`COL 1`, `COL 2`) VALUES (\'Row 1\', \'Row 2\'),' . "\n" . ' (\'123\', \'456\');;',
238 $this->assertEquals(true, $GLOBALS['finished']);
239 $this->assertAllQueriesConsumed();
243 * Test for doImport skipping headers
247 public function testDoImportSkipHeaders(): void
249 //$sql_query_disabled will show the import SQL detail
250 global $sql_query, $sql_query_disabled;
251 $sql_query_disabled = false;
252 $GLOBALS['import_type'] = 'query';
253 $GLOBALS['import_file'] = 'none';
254 $GLOBALS['csv_terminated'] = ',';
255 $GLOBALS['import_text'] = '"Row 1","Row 2"' . "\n" . '"123","456"';
257 $_REQUEST['csv_col_names'] = 'something';
259 parent
::setGlobalDbi();
261 $this->dummyDbi
->addResult(
266 $this->dummyDbi
->addResult(
267 'SELECT TABLE_NAME FROM information_schema.VIEWS'
268 . ' WHERE TABLE_SCHEMA = \'CSV_DB 1\' AND TABLE_NAME = \'db_test\'',
272 $this->object->doImport();
275 'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
276 . 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`Row 1` int(3), `Row 2` int(3))'
277 . ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;INSERT INTO `CSV_DB 1`.`db_test`'
278 . ' (`Row 1`, `Row 2`) VALUES (123, 456);;',
282 $this->assertEquals(true, $GLOBALS['finished']);
283 $this->assertAllQueriesConsumed();