3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use PhpMyAdmin\DatabaseInterface
;
8 use PhpMyAdmin\Message
;
9 use PhpMyAdmin\Normalization
;
10 use PhpMyAdmin\Relation
;
11 use PhpMyAdmin\Template
;
12 use PhpMyAdmin\Transformations
;
16 use function json_encode
;
18 class NormalizationTest
extends AbstractTestCase
20 /** @var Normalization */
21 private $normalization;
24 * prepares environment for tests
26 protected function setUp(): void
29 parent
::defineVersionConstants();
30 $GLOBALS['cfg']['LimitChars'] = 50;
31 $GLOBALS['cfg']['ServerDefault'] = 'PMA_server';
32 $GLOBALS['cfg']['ShowHint'] = true;
33 $GLOBALS['cfg']['CharEditing'] = '';
34 $GLOBALS['cfg']['ActionLinksMode'] = 'icons';
35 $GLOBALS['db'] = 'PMA_db';
36 $GLOBALS['table'] = 'PMA_table';
37 $GLOBALS['server'] = 1;
38 $GLOBALS['cfg']['Server']['DisableIS'] = false;
39 $_POST['change_column'] = null;
44 $dbi = $this->getMockBuilder(DatabaseInterface
::class)
45 ->disableOriginalConstructor()
47 $dbi->types
= new Types($dbi);
48 $GLOBALS['dbi'] = $dbi;
50 $dbi->expects($this->any())
52 ->will($this->returnValue(true));
53 $dbi->expects($this->any())
54 ->method('getColumns')
58 'id' => ['Type' => 'integer'],
59 'col1' => ['Type' => 'varchar(100)'],
60 'col2' => ['Type' => 'DATETIME'],
64 $dbi->expects($this->any())
65 ->method('getColumnNames')
66 ->will($this->returnValue(['id', 'col1', 'col2']));
71 DatabaseInterface
::CONNECT_USER
,
77 DatabaseInterface
::CONNECT_USER
,
80 'Key_name' => 'PRIMARY',
81 'Column_name' => 'id',
88 DatabaseInterface
::CONNECT_USER
,
91 'Key_name' => 'PRIMARY',
92 'Column_name' => 'id',
95 'Key_name' => 'PRIMARY',
96 'Column_name' => 'col1',
101 $dbi->expects($this->any())
102 ->method('getTableIndexes')
103 ->will($this->returnValueMap($map));
104 $dbi->expects($this->any())
106 ->will($this->returnValue(true));
107 $dbi->expects($this->any())
108 ->method('fetchResult')
109 ->will($this->returnValue([0]));
111 $template = new Template();
112 $this->normalization
= new Normalization($dbi, new Relation($dbi, $template), new Transformations(), $template);
116 * Test for getHtmlForColumnsList
118 public function testGetHtmlForColumnsList(): void
121 $table = 'PMA_table';
122 $this->assertStringContainsString(
123 '<option value="id">id [ integer ]</option>',
124 $this->normalization
->getHtmlForColumnsList($table, $db)
127 '<input type="checkbox" value="col1">col1 [ varchar(100) ]<br>',
128 $this->normalization
->getHtmlForColumnsList($table, $db, 'String', 'checkbox')
133 * Test for getHtmlForCreateNewColumn
135 public function testGetHtmlForCreateNewColumn(): void
137 $GLOBALS['cfg']['BrowseMIME'] = true;
138 $GLOBALS['cfg']['MaxRows'] = 25;
139 $GLOBALS['col_priv'] = false;
141 $table = 'PMA_table';
143 $result = $this->normalization
->getHtmlForCreateNewColumn($numFields, $db, $table);
144 $this->assertStringContainsString(
145 '<table id="table_columns"',
151 * Test for getHtmlFor1NFStep1
153 public function testGetHtmlFor1NFStep1(): void
156 $table = 'PMA_table';
157 $normalizedTo = '1nf';
158 $result = $this->normalization
->getHtmlFor1NFStep1($db, $table, $normalizedTo);
159 $this->assertStringContainsString(
160 "<h3 class='text-center'>"
161 . __('First step of normalization (1NF)') . '</h3>',
164 $this->assertStringContainsString(
165 "<div id='mainContent'",
168 $this->assertStringContainsString('<legend>' . __('Step 1.'), $result);
170 $this->assertStringContainsString(
175 $this->assertStringContainsString(
180 $this->assertStringContainsString(
181 "<select id='selectNonAtomicCol'",
185 $this->assertStringContainsString(
186 $this->normalization
->getHtmlForColumnsList(
189 _pgettext('string types', 'String')
196 * Test for getHtmlContentsFor1NFStep2
198 public function testGetHtmlContentsFor1NFStep2(): void
201 $table = 'PMA_table1';
202 $result = $this->normalization
->getHtmlContentsFor1NFStep2($db, $table);
203 $this->assertIsArray($result);
204 $this->assertArrayHasKey('legendText', $result);
205 $this->assertArrayHasKey('headText', $result);
206 $this->assertArrayHasKey('subText', $result);
207 $this->assertArrayHasKey('hasPrimaryKey', $result);
208 $this->assertArrayHasKey('extra', $result);
209 $this->assertStringContainsString(
210 '<a href="#" id="createPrimaryKey">',
213 $this->assertStringContainsString(
214 '<a href="#" id="addNewPrimary">',
217 $this->assertEquals('0', $result['hasPrimaryKey']);
218 $this->assertStringContainsString(__('Step 1.') . 2, $result['legendText']);
219 $result1 = $this->normalization
->getHtmlContentsFor1NFStep2($db, 'PMA_table');
220 $this->assertEquals('1', $result1['hasPrimaryKey']);
224 * Test for getHtmlContentsFor1NFStep4
226 public function testGetHtmlContentsFor1NFStep4(): void
229 $table = 'PMA_table';
230 $result = $this->normalization
->getHtmlContentsFor1NFStep4($db, $table);
231 $this->assertIsArray($result);
232 $this->assertArrayHasKey('legendText', $result);
233 $this->assertArrayHasKey('headText', $result);
234 $this->assertArrayHasKey('subText', $result);
235 $this->assertArrayHasKey('extra', $result);
236 $this->assertStringContainsString(__('Step 1.') . 4, $result['legendText']);
237 $this->assertStringContainsString(
238 $this->normalization
->getHtmlForColumnsList($db, $table, 'all', 'checkbox'),
241 $this->assertStringContainsString(
242 '<input class="btn btn-secondary" type="submit" id="removeRedundant"',
248 * Test for getHtmlContentsFor1NFStep3
250 public function testGetHtmlContentsFor1NFStep3(): void
253 $table = 'PMA_table';
254 $result = $this->normalization
->getHtmlContentsFor1NFStep3($db, $table);
255 $this->assertIsArray($result);
256 $this->assertArrayHasKey('legendText', $result);
257 $this->assertArrayHasKey('headText', $result);
258 $this->assertArrayHasKey('subText', $result);
259 $this->assertArrayHasKey('extra', $result);
260 $this->assertArrayHasKey('primary_key', $result);
261 $this->assertStringContainsString(__('Step 1.') . 3, $result['legendText']);
262 $this->assertStringContainsString(
263 $this->normalization
->getHtmlForColumnsList($db, $table, 'all', 'checkbox'),
266 $this->assertStringContainsString(
267 '<input class="btn btn-secondary" type="submit" id="moveRepeatingGroup"',
270 $this->assertEquals(json_encode(['id']), $result['primary_key']);
274 * Test for getHtmlFor2NFstep1
276 public function testGetHtmlFor2NFstep1(): void
279 $table = 'PMA_table';
280 $result = $this->normalization
->getHtmlFor2NFstep1($db, $table);
281 $this->assertIsArray($result);
282 $this->assertArrayHasKey('legendText', $result);
283 $this->assertArrayHasKey('headText', $result);
284 $this->assertArrayHasKey('subText', $result);
285 $this->assertArrayHasKey('extra', $result);
286 $this->assertArrayHasKey('primary_key', $result);
287 $this->assertStringContainsString(__('Step 2.') . 1, $result['legendText']);
288 $this->assertEquals('id', $result['primary_key']);
289 $result1 = $this->normalization
->getHtmlFor2NFstep1($db, 'PMA_table2');
290 $this->assertEquals('id, col1', $result1['primary_key']);
291 $this->assertStringContainsString(
292 '<a href="#" id="showPossiblePd"',
295 $this->assertStringContainsString(
296 '<input type="checkbox" name="pd" value="id"',
302 * Test for getHtmlForNewTables2NF
304 public function testGetHtmlForNewTables2NF(): void
306 $table = 'PMA_table';
307 $partialDependencies = ['col1' => ['col2']];
308 $result = $this->normalization
->getHtmlForNewTables2NF($partialDependencies, $table);
309 $this->assertStringContainsString(
310 '<input type="text" name="col1"',
316 * Test for createNewTablesFor2NF
318 public function testCreateNewTablesFor2NF(): void
320 $table = 'PMA_table';
322 $tablesName = new stdClass();
323 $tablesName->id
= 'PMA_table';
324 $tablesName->col1
= 'PMA_table1';
325 $partialDependencies = ['id' => ['col2']];
326 $result = $this->normalization
->createNewTablesFor2NF(
327 $partialDependencies,
332 $this->assertIsArray($result);
333 $this->assertArrayHasKey('legendText', $result);
334 $this->assertArrayHasKey('headText', $result);
335 $this->assertArrayHasKey('queryError', $result);
336 $partialDependencies = [
340 $result1 = $this->normalization
->createNewTablesFor2NF(
341 $partialDependencies,
346 $this->assertArrayHasKey('extra', $result1);
347 $this->assertEquals(__('End of step'), $result1['legendText']);
348 $this->assertEquals('', $result1['extra']);
352 * Test for getHtmlForNewTables3NF
354 public function testGetHtmlForNewTables3NF(): void
356 $tables = ['PMA_table' => ['col1']];
358 $dependencies = new stdClass();
359 $dependencies->col1
= ['col2'];
360 $result = $this->normalization
->getHtmlForNewTables3NF($dependencies, $tables, $db);
375 $dependencies->PMA_table
= [
379 $result1 = $this->normalization
->getHtmlForNewTables3NF($dependencies, $tables, $db);
380 $this->assertIsArray($result1);
381 $this->assertStringContainsString(
382 '<input type="text" name="PMA_table"',
394 'nonpk' => 'col4, col5',
398 $result1['newTables']
403 * Test for createNewTablesFor3NF
405 public function testCreateNewTablesFor3NF(): void
408 $cols = new stdClass();
410 $cols->nonpk
= 'col1, col2';
411 $cols1 = new stdClass();
413 $cols1->nonpk
= 'col3, col4';
416 'PMA_table' => $cols,
420 $result = $this->normalization
->createNewTablesFor3NF(
424 $this->assertIsArray($result);
425 $this->assertArrayHasKey('legendText', $result);
426 $this->assertArrayHasKey('headText', $result);
427 $this->assertArrayHasKey('queryError', $result);
429 $result1 = $this->normalization
->createNewTablesFor3NF(
433 $this->assertArrayHasKey('queryError', $result1);
434 $this->assertEquals(__('End of step'), $result1['legendText']);
435 $this->assertFalse($result1['queryError']);
439 * Test for moveRepeatingGroup
441 public function testMoveRepeatingGroup(): void
443 $repeatingColumns = 'col1, col2';
444 $primaryColumns = 'id,col1';
445 $newTable = 'PMA_newTable';
446 $newColumn = 'PMA_newCol';
447 $table = 'PMA_table';
449 $result = $this->normalization
->moveRepeatingGroup(
457 $this->assertIsArray($result);
458 $this->assertArrayHasKey('queryError', $result);
459 $this->assertArrayHasKey('message', $result);
460 $this->assertInstanceOf(
467 * Test for getHtmlFor3NFstep1
469 public function testGetHtmlFor3NFstep1(): void
472 $tables = ['PMA_table'];
473 $result = $this->normalization
->getHtmlFor3NFstep1($db, $tables);
474 $this->assertIsArray($result);
475 $this->assertArrayHasKey('legendText', $result);
476 $this->assertArrayHasKey('headText', $result);
477 $this->assertArrayHasKey('subText', $result);
478 $this->assertArrayHasKey('extra', $result);
479 $this->assertStringContainsString(__('Step 3.') . 1, $result['legendText']);
480 $this->assertStringContainsString(
484 $this->assertStringContainsString(
485 '<input type="checkbox" name="pd" value="col1"',
488 $result1 = $this->normalization
->getHtmlFor3NFstep1($db, ['PMA_table2']);
496 * Test for getHtmlForNormalizeTable
498 public function testgetHtmlForNormalizeTable(): void
500 $result = $this->normalization
->getHtmlForNormalizeTable();
501 $this->assertStringContainsString(
502 '<form method="post" action="' . Url
::getFromRoute('/normalization')
503 . '" name="normalize" id="normalizeTable"',
506 $this->assertStringContainsString(
507 '<input type="hidden" name="step1" value="1">',
511 $this->assertStringContainsString('type="radio" name="normalizeTo"', $result);
512 $this->assertStringContainsString('id="normalizeToRadio1" value="1nf" checked>', $result);
513 $this->assertStringContainsString('id="normalizeToRadio2" value="2nf">', $result);
514 $this->assertStringContainsString('id="normalizeToRadio3" value="3nf">', $result);
518 * Test for findPartialDependencies
520 public function testFindPartialDependencies(): void
522 $table = 'PMA_table2';
524 $result = $this->normalization
->findPartialDependencies($table, $db);
525 $this->assertStringContainsString(
526 '<div class="dependencies_box"',
529 $this->assertStringContainsString(__('No partial dependencies found!'), $result);
533 * Test for getAllCombinationPartialKeys
535 public function testGetAllCombinationPartialKeys(): void
542 $result = $this->callFunction(
543 $this->normalization
,
544 Normalization
::class,
545 'getAllCombinationPartialKeys',