Visitar URL original
Add basic test for `Table\ColumnsDefinition` class · phpmyadmin/phpmyadmin@01ae382 · GitHub
Skip to content

Commit 01ae382

Browse files
committed
Add basic test for Table\ColumnsDefinition class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 14ac084 commit 01ae382

3 files changed

Lines changed: 208 additions & 2 deletions

File tree

‎infection.json.dist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"mutators": {
1414
"@default": true
1515
},
16-
"minMsi": 30,
16+
"minMsi": 31,
1717
"minCoveredMsi": 58
1818
}

‎libraries/classes/Table/ColumnsDefinition.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ public function displayForm(
483483
'privs_available' => ($GLOBALS['col_priv'] ?? false) && ($GLOBALS['is_reload_priv'] ?? false),
484484
'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255,
485485
'have_partitioning' => Partition::havePartitioning(),
486-
'dbi' => $this->dbi,
487486
'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'],
488487
];
489488
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Tests\Table;
6+
7+
use PhpMyAdmin\ConfigStorage\Relation;
8+
use PhpMyAdmin\FieldMetadata;
9+
use PhpMyAdmin\Table\ColumnsDefinition;
10+
use PhpMyAdmin\Tests\AbstractTestCase;
11+
use PhpMyAdmin\Transformations;
12+
13+
use function array_merge;
14+
15+
use const MYSQLI_TYPE_STRING;
16+
17+
/**
18+
* @covers \PhpMyAdmin\Table\ColumnsDefinition
19+
*/
20+
class ColumnsDefinitionTest extends AbstractTestCase
21+
{
22+
public function testDisplayForm(): void
23+
{
24+
$GLOBALS['cfg']['Server']['DisableIS'] = false;
25+
$dummyDbi = $this->createDbiDummy();
26+
$dbi = $this->createDatabaseInterface($dummyDbi);
27+
$GLOBALS['dbi'] = $dbi;
28+
29+
// phpcs:disable Generic.Files.LineLength.TooLong
30+
$columnMeta = ['Field' => 'actor_id', 'Type' => 'smallint(5) unsigned', 'Collation' => null, 'Null' => 'NO', 'Key' => 'PRI', 'Default' => null, 'Extra' => 'auto_increment', 'Privileges' => 'select,insert,update,references', 'Comment' => ''];
31+
$dummyDbi->addResult(
32+
'SHOW FULL COLUMNS FROM `sakila`.`actor`',
33+
[
34+
['actor_id', 'smallint(5) unsigned', null, 'NO', 'PRI', null, 'auto_increment', 'select,insert,update,references', ''],
35+
['first_name', 'varchar(45)', 'utf8mb4_general_ci', 'NO', '', null, '', 'select,insert,update,references', ''],
36+
['last_name', 'varchar(45)', 'utf8mb4_general_ci', 'NO', 'MUL', null, '', 'select,insert,update,references', ''],
37+
['last_update', 'timestamp', null, 'NO', '', 'current_timestamp()', 'on update current_timestamp()', 'select,insert,update,references', ''],
38+
],
39+
['Field', 'Type', 'Collation', 'Null', 'Key', 'Default', 'Extra', 'Privileges', 'Comment']
40+
);
41+
$dummyDbi->addResult(
42+
'SHOW INDEXES FROM `sakila`.`actor`',
43+
[
44+
['actor', '0', 'PRIMARY', '1', 'actor_id', 'A', '2', null, null, '', 'BTREE', '', '', 'NO'],
45+
['actor', '1', 'idx_actor_last_name', '1', 'last_name', 'A', '2', null, null, '', 'BTREE', '', '', 'NO'],
46+
],
47+
['Table', 'Non_unique', 'Key_name', 'Seq_in_index', 'Column_name', 'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null', 'Index_type', 'Comment', 'Index_comment', 'Ignored']
48+
);
49+
// phpcs:enable
50+
$dummyDbi->addResult(
51+
'SELECT * FROM `sakila`.`actor` LIMIT 1',
52+
[['1', 'PENELOPE', 'GUINESS', '2006-02-15 04:34:33']],
53+
['actor_id', 'first_name', 'last_name', 'last_update']
54+
);
55+
$createTable = <<<'SQL'
56+
CREATE TABLE `actor` (
57+
`actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
58+
`first_name` varchar(45) NOT NULL,
59+
`last_name` varchar(45) NOT NULL,
60+
`last_update` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
61+
PRIMARY KEY (`actor_id`),
62+
KEY `idx_actor_last_name` (`last_name`)
63+
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8mb4
64+
SQL;
65+
$dummyDbi->addResult(
66+
'SHOW CREATE TABLE `sakila`.`actor`',
67+
[['actor', $createTable]],
68+
['actor_id', 'first_name', 'last_name', 'last_update']
69+
);
70+
71+
$relation = new Relation($dbi);
72+
$columnsDefinition = new ColumnsDefinition($dbi, $relation, new Transformations());
73+
74+
$GLOBALS['server'] = 1;
75+
$GLOBALS['db'] = 'sakila';
76+
$GLOBALS['table'] = 'actor';
77+
$GLOBALS['col_priv'] = true;
78+
$GLOBALS['is_reload_priv'] = true;
79+
$GLOBALS['mime_map'] = null;
80+
$_SESSION['relation'] = [];
81+
82+
$actual = $columnsDefinition->displayForm('/table/structure/save', 1, null, ['actor_id'], [$columnMeta]);
83+
84+
$contentCell = [
85+
'column_number' => 0,
86+
'column_meta' => array_merge($columnMeta, ['DefaultType' => 'NONE', 'DefaultValue' => '']),
87+
'type_upper' => 'SMALLINT',
88+
'default_value' => '',
89+
'length_values_input_size' => 8,
90+
'length' => '5',
91+
'extracted_columnspec' => [
92+
'type' => 'smallint',
93+
'spec_in_brackets' => '5',
94+
'enum_set_values' => [],
95+
'print_type' => 'smallint(5)',
96+
'binary' => false,
97+
'unsigned' => true,
98+
'zerofill' => false,
99+
'attribute' => 'UNSIGNED',
100+
'can_contain_collation' => false,
101+
'displayed_type' => 'smallint(5)',
102+
],
103+
'submit_attribute' => null,
104+
'comments_map' => [],
105+
'fields_meta' => [$columnMeta],
106+
'is_backup' => true,
107+
'move_columns' => [
108+
new FieldMetadata(MYSQLI_TYPE_STRING, 0, (object) ['name' => 'actor_id']),
109+
new FieldMetadata(MYSQLI_TYPE_STRING, 0, (object) ['name' => 'first_name']),
110+
new FieldMetadata(MYSQLI_TYPE_STRING, 0, (object) ['name' => 'last_name']),
111+
new FieldMetadata(MYSQLI_TYPE_STRING, 0, (object) ['name' => 'last_update']),
112+
],
113+
'available_mime' => [],
114+
'mime_map' => [],
115+
];
116+
$expected = [
117+
'is_backup' => true,
118+
'fields_meta' => [$columnMeta],
119+
'relation_parameters' => $relation->getRelationParameters(),
120+
'action' => '/table/structure/save',
121+
'form_params' => [
122+
'db' => 'sakila',
123+
'table' => 'actor',
124+
'orig_num_fields' => 1,
125+
'orig_field_where' => null,
126+
'orig_after_field' => null,
127+
'selected[0]' => 'actor_id',
128+
'field_orig[0]' => 'actor_id',
129+
'field_type_orig[0]' => 'SMALLINT',
130+
'field_length_orig[0]' => '5',
131+
'field_default_value_orig[0]' => '',
132+
'field_default_type_orig[0]' => 'NONE',
133+
'field_collation_orig[0]' => '',
134+
'field_attribute_orig[0]' => 'UNSIGNED',
135+
'field_null_orig[0]' => 'NO',
136+
'field_extra_orig[0]' => 'auto_increment',
137+
'field_comments_orig[0]' => '',
138+
'field_virtuality_orig[0]' => '',
139+
'field_expression_orig[0]' => '',
140+
],
141+
'content_cells' => [$contentCell],
142+
'partition_details' => [
143+
'partition_by' => null,
144+
'partition_expr' => null,
145+
'subpartition_by' => null,
146+
'subpartition_expr' => null,
147+
'partition_count' => 0,
148+
'subpartition_count' => 0,
149+
'can_have_subpartitions' => false,
150+
'value_enabled' => false,
151+
],
152+
'primary_indexes' => null,
153+
'unique_indexes' => null,
154+
'indexes' => null,
155+
'fulltext_indexes' => null,
156+
'spatial_indexes' => null,
157+
'table' => null,
158+
'comment' => null,
159+
'tbl_collation' => null,
160+
'charsets' => [
161+
[
162+
'name' => 'armscii8',
163+
'description' => 'armscii8_general_ci',
164+
'collations' => [['name' => 'armscii8_general_ci', 'description' => 'Armenian, case-insensitive']],
165+
],
166+
[
167+
'name' => 'latin1',
168+
'description' => 'cp1252 West European',
169+
'collations' => [['name' => 'latin1_swedish_ci', 'description' => 'Swedish, case-insensitive']],
170+
],
171+
[
172+
'name' => 'utf8',
173+
'description' => 'UTF-8 Unicode',
174+
'collations' => [
175+
['name' => 'utf8_bin', 'description' => 'Unicode, binary'],
176+
['name' => 'utf8_general_ci', 'description' => 'Unicode, case-insensitive'],
177+
],
178+
],
179+
[
180+
'name' => 'utf8mb4',
181+
'description' => 'utf8mb4_0900_ai_ci',
182+
'collations' => [
183+
['name' => 'utf8mb4_general_ci', 'description' => 'Unicode (UCA 4.0.0), case-insensitive'],
184+
],
185+
],
186+
],
187+
'tbl_storage_engine' => null,
188+
'storage_engines' => ['dummy' => ['name' => 'dummy', 'comment' => 'dummy comment', 'is_default' => false]],
189+
'connection' => null,
190+
'change_column' => null,
191+
'is_virtual_columns_supported' => true,
192+
'is_integers_length_restricted' => false,
193+
'browse_mime' => true,
194+
'supports_stored_keyword' => false,
195+
'server_version' => $dbi->getVersion(),
196+
'max_rows' => 25,
197+
'char_editing' => 'input',
198+
'attribute_types' => ['', 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL', 'on update CURRENT_TIMESTAMP'],
199+
'privs_available' => true,
200+
'max_length' => 1024,
201+
'have_partitioning' => true,
202+
'disable_is' => false,
203+
];
204+
205+
$this->assertEquals($expected, $actual);
206+
}
207+
}

0 commit comments

Comments
 (0)