1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\Tests\Plugins\Import; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\Current; |
8
|
|
|
use PhpMyAdmin\DatabaseInterface; |
9
|
|
|
use PhpMyAdmin\File; |
10
|
|
|
use PhpMyAdmin\Http\Factory\ServerRequestFactory; |
11
|
|
|
use PhpMyAdmin\Import\ImportSettings; |
12
|
|
|
use PhpMyAdmin\Plugins\Import\ImportCsv; |
13
|
|
|
use PhpMyAdmin\Tests\AbstractTestCase; |
14
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
15
|
|
|
use PHPUnit\Framework\Attributes\Medium; |
16
|
|
|
|
17
|
|
|
use function __; |
18
|
|
|
|
19
|
|
|
#[CoversClass(ImportCsv::class)] |
20
|
|
|
#[Medium] |
21
|
|
|
class ImportCsvTest extends AbstractTestCase |
22
|
|
|
{ |
23
|
|
|
protected ImportCsv $object; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets up the fixture, for example, opens a network connection. |
27
|
|
|
* This method is called before a test is executed. |
28
|
|
|
*/ |
29
|
|
|
protected function setUp(): void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
$GLOBALS['errorUrl'] = 'index.php?route=/'; |
34
|
|
|
$GLOBALS['error'] = false; |
35
|
|
|
Current::$database = ''; |
36
|
|
|
Current::$table = ''; |
37
|
|
|
$GLOBALS['sql_query'] = ''; |
38
|
|
|
$GLOBALS['message'] = null; |
39
|
|
|
ImportSettings::$timeoutPassed = false; |
40
|
|
|
ImportSettings::$maximumTime = 0; |
41
|
|
|
ImportSettings::$charsetConversion = false; |
42
|
|
|
ImportSettings::$skipQueries = 0; |
43
|
|
|
ImportSettings::$maxSqlLength = 0; |
44
|
|
|
ImportSettings::$executedQueries = 0; |
45
|
|
|
ImportSettings::$runQuery = false; |
46
|
|
|
ImportSettings::$goSql = false; |
47
|
|
|
ImportSettings::$importType = 'database'; |
48
|
|
|
ImportSettings::$finished = false; |
49
|
|
|
ImportSettings::$readLimit = 100000000; |
50
|
|
|
ImportSettings::$offset = 0; |
51
|
|
|
ImportSettings::$importFile = 'tests/test_data/db_test.csv'; |
52
|
|
|
ImportSettings::$importFileName = 'db_test'; |
53
|
|
|
ImportSettings::$readMultiply = 10; |
54
|
|
|
ImportSettings::$sqlQueryDisabled = false; |
55
|
|
|
|
56
|
|
|
$this->object = new ImportCsv(); |
57
|
|
|
|
58
|
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/') |
59
|
|
|
->withParsedBody([ |
60
|
|
|
'csv_terminated' => "\015", |
61
|
|
|
'csv_enclosed' => '"', |
62
|
|
|
'csv_escaped' => '"', |
63
|
|
|
'csv_new_line' => 'auto', |
64
|
|
|
'csv_columns' => null, |
65
|
|
|
]); |
66
|
|
|
$this->object->setImportOptions($request); |
67
|
|
|
|
68
|
|
|
$GLOBALS['import_text'] = 'ImportCsv_Test'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Tears down the fixture, for example, closes a network connection. |
73
|
|
|
* This method is called after a test is executed. |
74
|
|
|
*/ |
75
|
|
|
protected function tearDown(): void |
76
|
|
|
{ |
77
|
|
|
parent::tearDown(); |
78
|
|
|
|
79
|
|
|
unset($this->object); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test for getProperties |
84
|
|
|
*/ |
85
|
|
|
public function testGetProperties(): void |
86
|
|
|
{ |
87
|
|
|
$properties = $this->object->getProperties(); |
88
|
|
|
self::assertSame( |
89
|
|
|
__('CSV'), |
90
|
|
|
$properties->getText(), |
91
|
|
|
); |
92
|
|
|
self::assertSame( |
93
|
|
|
'csv', |
94
|
|
|
$properties->getExtension(), |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test for doImport |
100
|
|
|
*/ |
101
|
|
|
public function testDoImport(): void |
102
|
|
|
{ |
103
|
|
|
$importHandle = new File(ImportSettings::$importFile); |
104
|
|
|
$importHandle->open(); |
105
|
|
|
|
106
|
|
|
DatabaseInterface::$instance = $this->getMockBuilder(DatabaseInterface::class) |
107
|
|
|
->disableOriginalConstructor() |
108
|
|
|
->getMock(); |
109
|
|
|
|
110
|
|
|
//Test function called |
111
|
|
|
$this->object->doImport($importHandle); |
112
|
|
|
|
113
|
|
|
//asset that all sql are executed |
114
|
|
|
self::assertStringContainsString( |
115
|
|
|
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER', |
116
|
|
|
$GLOBALS['sql_query'], |
117
|
|
|
); |
118
|
|
|
self::assertStringContainsString( |
119
|
|
|
'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . ImportSettings::$importFileName . '`', |
120
|
|
|
$GLOBALS['sql_query'], |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
self::assertTrue(ImportSettings::$finished); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test for partial import/setting table and database names in doImport |
128
|
|
|
*/ |
129
|
|
|
public function testDoPartialImport(): void |
130
|
|
|
{ |
131
|
|
|
$importHandle = new File(ImportSettings::$importFile); |
132
|
|
|
$importHandle->open(); |
133
|
|
|
|
134
|
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/') |
135
|
|
|
->withParsedBody([ |
136
|
|
|
'csv_terminated' => ',', |
137
|
|
|
'csv_enclosed' => '"', |
138
|
|
|
'csv_escaped' => '"', |
139
|
|
|
'csv_new_line' => 'auto', |
140
|
|
|
'csv_columns' => null, |
141
|
|
|
'csv_new_tbl_name' => 'ImportTestTable', |
142
|
|
|
'csv_new_db_name' => 'ImportTestDb', |
143
|
|
|
]); |
144
|
|
|
$this->object->setImportOptions($request); |
145
|
|
|
|
146
|
|
|
DatabaseInterface::$instance = $this->getMockBuilder(DatabaseInterface::class) |
147
|
|
|
->disableOriginalConstructor() |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
//Test function called |
151
|
|
|
$this->object->doImport($importHandle); |
152
|
|
|
|
153
|
|
|
//asset that all sql are executed |
154
|
|
|
self::assertStringContainsString( |
155
|
|
|
'CREATE DATABASE IF NOT EXISTS `ImportTestDb` DEFAULT CHARACTER', |
156
|
|
|
$GLOBALS['sql_query'], |
157
|
|
|
); |
158
|
|
|
self::assertStringContainsString( |
159
|
|
|
'CREATE TABLE IF NOT EXISTS `ImportTestDb`.`ImportTestTable`', |
160
|
|
|
$GLOBALS['sql_query'], |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
self::assertTrue(ImportSettings::$finished); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Test for getProperties for Table param |
168
|
|
|
*/ |
169
|
|
|
public function testGetPropertiesForTable(): void |
170
|
|
|
{ |
171
|
|
|
$properties = $this->object->getProperties(); |
172
|
|
|
self::assertSame( |
173
|
|
|
__('CSV'), |
174
|
|
|
$properties->getText(), |
175
|
|
|
); |
176
|
|
|
self::assertSame( |
177
|
|
|
'csv', |
178
|
|
|
$properties->getExtension(), |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Test for doImport for _getAnalyze = false, should be OK as well |
184
|
|
|
*/ |
185
|
|
|
public function testDoImportNotAnalysis(): void |
186
|
|
|
{ |
187
|
|
|
$importHandle = new File(ImportSettings::$importFile); |
188
|
|
|
$importHandle->open(); |
189
|
|
|
|
190
|
|
|
DatabaseInterface::$instance = $this->getMockBuilder(DatabaseInterface::class) |
191
|
|
|
->disableOriginalConstructor() |
192
|
|
|
->getMock(); |
193
|
|
|
|
194
|
|
|
//Test function called |
195
|
|
|
$this->object->doImport($importHandle); |
196
|
|
|
|
197
|
|
|
//asset that all sql are executed |
198
|
|
|
self::assertStringContainsString( |
199
|
|
|
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER', |
200
|
|
|
$GLOBALS['sql_query'], |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
self::assertStringContainsString( |
204
|
|
|
'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . ImportSettings::$importFileName . '`', |
205
|
|
|
$GLOBALS['sql_query'], |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
self::assertTrue(ImportSettings::$finished); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Test for doImport in the most basic and normal way |
213
|
|
|
*/ |
214
|
|
|
public function testDoImportNormal(): void |
215
|
|
|
{ |
216
|
|
|
ImportSettings::$importFile = 'none'; |
217
|
|
|
$GLOBALS['import_text'] = '"Row 1","Row 2"' . "\n" . '"123","456"'; |
218
|
|
|
|
219
|
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/') |
220
|
|
|
->withParsedBody([ |
221
|
|
|
'csv_terminated' => ',', |
222
|
|
|
'csv_enclosed' => '"', |
223
|
|
|
'csv_escaped' => '"', |
224
|
|
|
'csv_new_line' => 'auto', |
225
|
|
|
'csv_columns' => null, |
226
|
|
|
]); |
227
|
|
|
$this->object->setImportOptions($request); |
228
|
|
|
|
229
|
|
|
$dummyDbi = $this->createDbiDummy(); |
230
|
|
|
$dbi = $this->createDatabaseInterface($dummyDbi); |
231
|
|
|
DatabaseInterface::$instance = $dbi; |
232
|
|
|
|
233
|
|
|
$dummyDbi->addResult( |
234
|
|
|
'SHOW DATABASES', |
235
|
|
|
[], |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
$dummyDbi->addResult( |
239
|
|
|
'SELECT 1 FROM information_schema.VIEWS' |
240
|
|
|
. ' WHERE TABLE_SCHEMA = \'CSV_DB 1\' AND TABLE_NAME = \'db_test\'', |
241
|
|
|
[], |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
$this->object->doImport(); |
245
|
|
|
|
246
|
|
|
self::assertSame( |
247
|
|
|
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;' |
248
|
|
|
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`COL 1` varchar(5), `COL 2` varchar(5));' |
249
|
|
|
. 'INSERT INTO `CSV_DB 1`.`db_test`' |
250
|
|
|
. ' (`COL 1`, `COL 2`) VALUES (\'Row 1\', \'Row 2\'),' . "\n" . ' (\'123\', \'456\');', |
251
|
|
|
$GLOBALS['sql_query'], |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
self::assertTrue(ImportSettings::$finished); |
255
|
|
|
$dummyDbi->assertAllQueriesConsumed(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Test for doImport skipping headers |
260
|
|
|
*/ |
261
|
|
|
public function testDoImportSkipHeaders(): void |
262
|
|
|
{ |
263
|
|
|
ImportSettings::$importFile = 'none'; |
264
|
|
|
$GLOBALS['import_text'] = '"Row 1","Row 2"' . "\n" . '"123","456"'; |
265
|
|
|
|
266
|
|
|
$request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/') |
267
|
|
|
->withParsedBody([ |
268
|
|
|
'csv_terminated' => ',', |
269
|
|
|
'csv_enclosed' => '"', |
270
|
|
|
'csv_escaped' => '"', |
271
|
|
|
'csv_new_line' => 'auto', |
272
|
|
|
'csv_columns' => null, |
273
|
|
|
'csv_col_names' => 'yes', |
274
|
|
|
]); |
275
|
|
|
$this->object->setImportOptions($request); |
276
|
|
|
|
277
|
|
|
$dummyDbi = $this->createDbiDummy(); |
278
|
|
|
$dbi = $this->createDatabaseInterface($dummyDbi); |
279
|
|
|
DatabaseInterface::$instance = $dbi; |
280
|
|
|
|
281
|
|
|
$dummyDbi->addResult( |
282
|
|
|
'SHOW DATABASES', |
283
|
|
|
[], |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
$dummyDbi->addResult( |
287
|
|
|
'SELECT 1 FROM information_schema.VIEWS' |
288
|
|
|
. ' WHERE TABLE_SCHEMA = \'CSV_DB 1\' AND TABLE_NAME = \'db_test\'', |
289
|
|
|
[], |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
$this->object->doImport(); |
293
|
|
|
|
294
|
|
|
self::assertSame( |
295
|
|
|
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;' |
296
|
|
|
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`Row 1` int(3), `Row 2` int(3));' |
297
|
|
|
. 'INSERT INTO `CSV_DB 1`.`db_test`' |
298
|
|
|
. ' (`Row 1`, `Row 2`) VALUES (123, 456);', |
299
|
|
|
$GLOBALS['sql_query'], |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
self::assertTrue(ImportSettings::$finished); |
303
|
|
|
$dummyDbi->assertAllQueriesConsumed(); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|