File: CreateAddFieldTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.1%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 131,332 kB
  • sloc: javascript: 212,681; php: 168,094; xml: 18,098; sql: 504; sh: 274; makefile: 205; python: 199
file content (513 lines) | stat: -rw-r--r-- 20,215 bytes parent folder | download
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\CreateAddField;

use function json_encode;

/**
 * @covers \PhpMyAdmin\CreateAddField
 */
class CreateAddFieldTest extends AbstractTestCase
{
    /** @var CreateAddField */
    private $createAddField;

    /**
     * Set up for test cases
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->createAddField = new CreateAddField($GLOBALS['dbi']);
    }

    /**
     * Test for getPartitionsDefinition
     *
     * @param string $expected Expected result
     * @param array  $request  $_REQUEST array
     *
     * @dataProvider providerGetPartitionsDefinition
     */
    public function testGetPartitionsDefinition(string $expected, array $request): void
    {
        $_POST = $request;
        $actual = $this->createAddField->getPartitionsDefinition();
        $this->assertEquals($expected, $actual);
    }

    /**
     * Data provider for testGetPartitionsDefinition
     *
     * @return array
     */
    public function providerGetPartitionsDefinition(): array
    {
        return [
            [
                '',
                [],
            ],
            [
                ' PARTITION BY HASH (EXPR()) PARTITIONS 2',
                [
                    'partition_by' => 'HASH',
                    'partition_expr' => 'EXPR()',
                    'partition_count' => '2',
                ],
            ],
            [
                ' PARTITION BY LIST (EXPR2()) PARTITIONS 2 ( PARTITION p0,  PARTITION p1)',
                [
                    'partition_by' => 'LIST',
                    'partition_count' => '2',
                    'subpartition_by' => 'HASH',
                    'partition_expr' => 'EXPR2()',
                    'partitions' => [
                        [
                            'name' => 'p0',
                            'value_type' => '',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                        [
                            'name' => 'p1',
                            'value_type' => '',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                    ],
                ],
            ],
            [
                ' PARTITION BY LIST (EXPR2()) PARTITIONS 2 SUBPARTITION BY HASH (EXPR1()) '
                . 'SUBPARTITIONS 2 ( PARTITION p0,  PARTITION p1,  PARTITION p2 VALUES <value type> (<value>))',
                [
                    'partition_by' => 'LIST',
                    'partition_count' => '2',
                    'subpartition_by' => 'HASH',
                    'subpartition_expr' => 'EXPR1()',
                    'subpartition_count' => '2',
                    'partition_expr' => 'EXPR2()',
                    'partitions' => [
                        [
                            'name' => 'p0',
                            'value_type' => '',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                        [
                            'name' => 'p1',
                            'value_type' => '',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                        [
                            'name' => 'p2',
                            'value_type' => '<value type>',
                            'value' => '<value>',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                    ],
                ],
            ],
            [
                ' PARTITION BY LIST (EXPR2()) PARTITIONS 2 SUBPARTITION BY HASH (EXPR1()) '
                . 'SUBPARTITIONS 2 ( PARTITION p0 ENGINE = MRG_MyISAM COMMENT = \'Partition zero\' '
                . 'MAX_ROWS = 2048 MIN_ROWS = 25,  PARTITION p1 VALUES LESS THAN MAXVALUE, '
                . ' PARTITION p2 ( SUBPARTITION p2_s1 DATA DIRECTORY = \'datadir\' INDEX_DIRECTORY = \'indexdir\' '
                . 'TABLESPACE = space1 NODEGROUP = ngroup1))',
                [
                    'partition_by' => 'LIST',
                    'subpartition_by' => 'HASH',
                    'subpartition_expr' => 'EXPR1()',
                    'subpartition_count' => '2',
                    'partition_expr' => 'EXPR2()',
                    'partition_count' => '2',
                    'partitions' => [
                        [
                            'name' => 'p0',
                            'value_type' => '',
                            'value' => '',
                            'engine' => 'MRG_MyISAM',
                            'comment' => 'Partition zero',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '2048',
                            'min_rows' => '25',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                        [
                            'name' => 'p1',
                            'value_type' => 'LESS THAN MAXVALUE',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                        ],
                        [
                            'name' => 'p2',
                            'value_type' => '',
                            'value' => '',
                            'engine' => '',
                            'comment' => '',
                            'data_directory' => '',
                            'index_directory' => '',
                            'max_rows' => '',
                            'min_rows' => '',
                            'tablespace' => '',
                            'node_group' => '',
                            'subpartitions' => [
                                [
                                    'name' => 'p2_s1',
                                    'value_type' => '',
                                    'value' => '1',
                                    'engine' => '',
                                    'comment' => '',
                                    'data_directory' => 'datadir',
                                    'index_directory' => 'indexdir',
                                    'max_rows' => '',
                                    'min_rows' => '',
                                    'tablespace' => 'space1',
                                    'node_group' => 'ngroup1',
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    /**
     * Test for getTableCreationQuery
     *
     * @param string $expected Expected result
     * @param string $db       Database name
     * @param string $table    Table name
     * @param array  $request  $_REQUEST array
     *
     * @dataProvider providerGetTableCreationQuery
     */
    public function testGetTableCreationQuery(string $expected, string $db, string $table, array $request): void
    {
        $_POST = $request;
        $actual = $this->createAddField->getTableCreationQuery($db, $table);
        $this->assertEquals($expected, $actual);
    }

    /**
     * Data provider for testGetTableCreationQuery
     *
     * @return array
     */
    public function providerGetTableCreationQuery(): array
    {
        return [
            [
                'CREATE TABLE `db`.`table` ();',
                'db',
                'table',
                [
                    'field_name' => [],
                    'primary_indexes' => '{}',
                    'indexes' => '{}',
                    'unique_indexes' => '{}',
                    'fulltext_indexes' => '{}',
                    'spatial_indexes' => '{}',
                ],
            ],
            [
                'CREATE TABLE `db`.`table` () ENGINE = Inno\\\'DB CHARSET=armscii8 COMMENT = \'my \\\'table\';',
                'db',
                'table',
                [
                    'field_name' => [],
                    'primary_indexes' => '{}',
                    'indexes' => '{}',
                    'unique_indexes' => '{}',
                    'fulltext_indexes' => '{}',
                    'spatial_indexes' => '{}',
                    'tbl_storage_engine' => 'Inno\'DB',
                    'tbl_collation' => 'armscii8',
                    'connection' => 'aaaa',
                    'comment' => 'my \'table',
                ],
            ],
        ];
    }

    /**
     * Test for getNumberOfFieldsFromRequest
     *
     * @param int   $expected Expected result
     * @param array $request  $_REQUEST array
     *
     * @dataProvider providerGetNumberOfFieldsFromRequest
     */
    public function testGetNumberOfFieldsFromRequest(int $expected, array $request): void
    {
        $_POST = $request;
        $actual = $this->createAddField->getNumberOfFieldsFromRequest();
        $this->assertEquals($expected, $actual);
    }

    /**
     * Data provider for testGetNumberOfFieldsFromRequest
     *
     * @return array
     */
    public function providerGetNumberOfFieldsFromRequest(): array
    {
        return [
            [
                4,
                [],
            ],
        ];
    }

    /**
     * Data provider for testGetColumnCreationQuery
     *
     * @return array[]
     */
    public function providerGetColumnCreationQueryRequest(): array
    {
        return [
            [
                'ALTER TABLE `my_table` ADD `dd` INT NOT NULL AFTER `d`;',
                [
                    'db' => '2fa',
                    'field_where' => 'after',
                    'after_field' => 'd',
                    'table' => 'aes',
                    'orig_num_fields' => '1',
                    'orig_field_where' => 'after',
                    'orig_after_field' => 'd',
                    'primary_indexes' => '[]',
                    'unique_indexes' => '[]',
                    'indexes' => '[]',
                    'fulltext_indexes' => '[]',
                    'spatial_indexes' => '[]',
                    'field_name' => ['dd'],
                    'field_type' => ['INT'],
                    'field_length' => [''],
                    'field_default_type' => ['NONE'],
                    'field_default_value' => [''],
                    'field_collation' => [''],
                    'field_attribute' => [''],
                    'field_key' => ['none_0'],
                    'field_comments' => [''],
                    'field_virtuality' => [''],
                    'field_expression' => [''],
                    'field_move_to' => [''],
                    'field_mimetype' => [''],
                    'field_transformation' => [''],
                    'field_transformation_options' => [''],
                    'field_input_transformation' => [''],
                    'field_input_transformation_options' => [''],
                    'do_save_data' => '1',
                    'preview_sql' => '1',
                    'ajax_request' => '1',
                ],
            ],
            [
                'ALTER TABLE `my_table` ADD `dd` INT NOT NULL AFTER `d`, ALGORITHM=INPLACE, LOCK=NONE;',
                [
                    'db' => '2fa',
                    'field_where' => 'after',
                    'after_field' => 'd',
                    'table' => 'aes',
                    'orig_num_fields' => '1',
                    'orig_field_where' => 'after',
                    'orig_after_field' => 'd',
                    'primary_indexes' => '[]',
                    'unique_indexes' => '[]',
                    'indexes' => '[]',
                    'fulltext_indexes' => '[]',
                    'spatial_indexes' => '[]',
                    'field_name' => ['dd'],
                    'field_type' => ['INT'],
                    'field_length' => [''],
                    'field_default_type' => ['NONE'],
                    'field_default_value' => [''],
                    'field_collation' => [''],
                    'field_attribute' => [''],
                    'field_key' => ['none_0'],
                    'field_comments' => [''],
                    'field_virtuality' => [''],
                    'field_expression' => [''],
                    'field_move_to' => [''],
                    'field_mimetype' => [''],
                    'field_transformation' => [''],
                    'field_transformation_options' => [''],
                    'field_input_transformation' => [''],
                    'field_input_transformation_options' => [''],
                    'do_save_data' => '1',
                    'preview_sql' => '1',
                    'ajax_request' => '1',
                    'online_transaction' => 'ONLINE_TRANSACTION_ENABLED',
                ],
            ],
            [
                'ALTER TABLE `my_table` ADD `dd` INT NOT NULL AFTER `d`, '
                . 'ADD UNIQUE `un1` (`dd`(12), `dd`), ADD UNIQUE `un3` (`dd`(12)) USING '
                . 'BTREE COMMENT \'Unique 3\', ADD UNIQUE `un3.1` (`dd`(12)) WITH '
                . 'PARSER Parser 1 COMMENT \'Unique 3.1\', ADD UNIQUE `un2` (`dd`(12)) '
                . 'KEY_BLOCK_SIZE = 32 USING BTREE COMMENT \'Unique 2\', ALGORITHM=INPLACE, LOCK=NONE;',
                [
                    'db' => '2fa',
                    'field_where' => 'after',
                    'after_field' => 'd',
                    'table' => 'aes',
                    'orig_num_fields' => '1',
                    'orig_field_where' => 'after',
                    'orig_after_field' => 'd',
                    'primary_indexes' => json_encode([]),
                    'unique_indexes' => json_encode([
                        [
                            'Key_name' => 'un1',
                            'Index_comment' => '',
                            'Index_choice' => 'UNIQUE',
                            'Key_block_size' => '',
                            'Parser' => '',
                            'Index_type' => '',
                            'columns' => [
                                [
                                    'col_index' => '0',
                                    'size' => '12',
                                ],
                                [
                                    'col_index' => '0',
                                    'size' => '',
                                ],
                            ],
                        ],
                        [
                            'Key_name' => 'un3',
                            'Index_comment' => 'Unique 3',
                            'Index_choice' => 'UNIQUE',
                            'Key_block_size' => '',
                            'Parser' => 'Parser 1',
                            'Index_type' => 'BTREE',
                            'columns' => [
                                [
                                    'col_index' => '0',
                                    'size' => '12',
                                ],
                            ],
                        ],
                        [
                            'Key_name' => 'un3.1',
                            'Index_comment' => 'Unique 3.1',
                            'Index_choice' => 'FULLTEXT',
                            'Key_block_size' => '',
                            'Parser' => 'Parser 1',
                            'Index_type' => 'BTREE',
                            'columns' => [
                                [
                                    'col_index' => '0',
                                    'size' => '12',
                                ],
                            ],
                        ],
                        [
                            'Key_name' => 'un2',
                            'Index_comment' => 'Unique 2',
                            'Index_choice' => 'UNIQUE',
                            'Key_block_size' => '32',
                            'Parser' => '',
                            'Index_type' => 'BTREE',
                            'columns' => [
                                [
                                    'col_index' => '0',
                                    'size' => '12',
                                ],
                            ],
                        ],
                    ]),
                    'indexes' => json_encode([]),
                    'fulltext_indexes' => json_encode([]),
                    'spatial_indexes' => json_encode([]),
                    'field_name' => ['dd'],
                    'field_type' => ['INT'],
                    'field_length' => [''],
                    'field_default_type' => ['NONE'],
                    'field_default_value' => [''],
                    'field_collation' => [''],
                    'field_attribute' => [''],
                    'field_key' => ['none_0'],
                    'field_comments' => [''],
                    'field_virtuality' => [''],
                    'field_expression' => [''],
                    'field_move_to' => [''],
                    'field_mimetype' => [''],
                    'field_transformation' => [''],
                    'field_transformation_options' => [''],
                    'field_input_transformation' => [''],
                    'field_input_transformation_options' => [''],
                    'do_save_data' => '1',
                    'preview_sql' => '1',
                    'ajax_request' => '1',
                    'online_transaction' => 'ONLINE_TRANSACTION_ENABLED',
                ],
            ],
        ];
    }

    /**
     * @dataProvider providerGetColumnCreationQueryRequest
     */
    public function testGetColumnCreationQuery(string $expected, array $request): void
    {
        $_POST = $request;
        $sqlQuery = $this->createAddField->getColumnCreationQuery('my_table');
        $this->assertEquals($expected, $sqlQuery);
    }
}