Remove `@access` annotations
[phpmyadmin.git] / test / classes / Plugins / Import / ImportCsvTest.php
blob4798f278aa93d9d81eb971415617e9a31b8fb551
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Plugins\Import;
7 use PhpMyAdmin\DatabaseInterface;
8 use PhpMyAdmin\File;
9 use PhpMyAdmin\Plugins\Import\ImportCsv;
10 use PhpMyAdmin\Tests\AbstractTestCase;
12 use function __;
13 use function basename;
15 /**
16 * @covers \PhpMyAdmin\Plugins\Import\ImportCsv
18 class ImportCsvTest extends AbstractTestCase
20 /** @var ImportCsv */
21 protected $object;
23 /**
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
29 parent::setUp();
30 $GLOBALS['server'] = 0;
31 $GLOBALS['plugin_param'] = 'csv';
32 $this->object = new ImportCsv();
34 unset($GLOBALS['db']);
36 //setting
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';
48 //separator for csv
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');
55 //$_SESSION
57 //Mock DBI
58 $dbi = $this->getMockBuilder(DatabaseInterface::class)
59 ->disableOriginalConstructor()
60 ->getMock();
61 $GLOBALS['dbi'] = $dbi;
64 /**
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
70 parent::tearDown();
71 unset($this->object);
74 /**
75 * Test for getProperties
77 * @group medium
79 public function testGetProperties(): void
81 $properties = $this->object->getProperties();
82 $this->assertEquals(
83 __('CSV'),
84 $properties->getText()
86 $this->assertEquals(
87 'csv',
88 $properties->getExtension()
92 /**
93 * Test for doImport
95 * @group medium
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'] . '`',
113 $sql_query
116 $this->assertTrue($GLOBALS['finished']);
120 * Test for partial import/setting table and database names in doImport
122 * @group medium
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
155 * @group medium
157 public function testGetPropertiesForTable(): void
159 $GLOBALS['plugin_param'] = 'table';
160 $this->object = new ImportCsv();
161 $properties = $this->object->getProperties();
162 $this->assertEquals(
163 __('CSV'),
164 $properties->getText()
166 $this->assertEquals(
167 'csv',
168 $properties->getExtension()
173 * Test for doImport for _getAnalyze = false, should be OK as well
175 * @group medium
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'] . '`',
194 $sql_query
197 $this->assertTrue($GLOBALS['finished']);
201 * Test for doImport in the most basic and normal way
203 * @group medium
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(
218 'SHOW DATABASES',
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();
230 $this->assertSame(
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\');;',
235 $sql_query
238 $this->assertEquals(true, $GLOBALS['finished']);
239 $this->assertAllQueriesConsumed();
243 * Test for doImport skipping headers
245 * @group medium
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(
262 'SHOW DATABASES',
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();
274 $this->assertSame(
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);;',
279 $sql_query
282 $this->assertEquals(true, $GLOBALS['finished']);
283 $this->assertAllQueriesConsumed();