Remove some useless comments
[phpmyadmin.git] / test / classes / NormalizationTest.php
blob47d4f14119677df4b94ea7a370fa6ea9ae05bcea
1 <?php
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;
13 use PhpMyAdmin\Types;
14 use PhpMyAdmin\Url;
15 use stdClass;
16 use function json_encode;
18 class NormalizationTest extends AbstractTestCase
20 /** @var Normalization */
21 private $normalization;
23 /**
24 * prepares environment for tests
26 protected function setUp(): void
28 parent::setUp();
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;
41 //$_SESSION
43 //mock DBI
44 $dbi = $this->getMockBuilder(DatabaseInterface::class)
45 ->disableOriginalConstructor()
46 ->getMock();
47 $dbi->types = new Types($dbi);
48 $GLOBALS['dbi'] = $dbi;
49 // set expectations
50 $dbi->expects($this->any())
51 ->method('selectDb')
52 ->will($this->returnValue(true));
53 $dbi->expects($this->any())
54 ->method('getColumns')
55 ->will(
56 $this->returnValue(
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']));
67 $map = [
69 'PMA_db',
70 'PMA_table1',
71 DatabaseInterface::CONNECT_USER,
72 [],
75 'PMA_db',
76 'PMA_table',
77 DatabaseInterface::CONNECT_USER,
80 'Key_name' => 'PRIMARY',
81 'Column_name' => 'id',
86 'PMA_db',
87 'PMA_table2',
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())
105 ->method('tryQuery')
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
120 $db = 'PMA_db';
121 $table = 'PMA_table';
122 $this->assertStringContainsString(
123 '<option value="id">id [ integer ]</option>',
124 $this->normalization->getHtmlForColumnsList($table, $db)
126 $this->assertEquals(
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;
140 $db = 'PMA_db';
141 $table = 'PMA_table';
142 $numFields = 1;
143 $result = $this->normalization->getHtmlForCreateNewColumn($numFields, $db, $table);
144 $this->assertStringContainsString(
145 '<table id="table_columns"',
146 $result
151 * Test for getHtmlFor1NFStep1
153 public function testGetHtmlFor1NFStep1(): void
155 $db = 'PMA_db';
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>',
162 $result
164 $this->assertStringContainsString(
165 "<div id='mainContent'",
166 $result
168 $this->assertStringContainsString('<legend>' . __('Step 1.'), $result);
170 $this->assertStringContainsString(
171 '<h4',
172 $result
175 $this->assertStringContainsString(
176 '<p',
177 $result
180 $this->assertStringContainsString(
181 "<select id='selectNonAtomicCol'",
182 $result
185 $this->assertStringContainsString(
186 $this->normalization->getHtmlForColumnsList(
187 $db,
188 $table,
189 _pgettext('string types', 'String')
191 $result
196 * Test for getHtmlContentsFor1NFStep2
198 public function testGetHtmlContentsFor1NFStep2(): void
200 $db = 'PMA_db';
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">',
211 $result['subText']
213 $this->assertStringContainsString(
214 '<a href="#" id="addNewPrimary">',
215 $result['extra']
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
228 $db = 'PMA_db';
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'),
239 $result['extra']
241 $this->assertStringContainsString(
242 '<input class="btn btn-secondary" type="submit" id="removeRedundant"',
243 $result['extra']
248 * Test for getHtmlContentsFor1NFStep3
250 public function testGetHtmlContentsFor1NFStep3(): void
252 $db = 'PMA_db';
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'),
264 $result['extra']
266 $this->assertStringContainsString(
267 '<input class="btn btn-secondary" type="submit" id="moveRepeatingGroup"',
268 $result['extra']
270 $this->assertEquals(json_encode(['id']), $result['primary_key']);
274 * Test for getHtmlFor2NFstep1
276 public function testGetHtmlFor2NFstep1(): void
278 $db = 'PMA_db';
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"',
293 $result1['headText']
295 $this->assertStringContainsString(
296 '<input type="checkbox" name="pd" value="id"',
297 $result1['extra']
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"',
311 $result
316 * Test for createNewTablesFor2NF
318 public function testCreateNewTablesFor2NF(): void
320 $table = 'PMA_table';
321 $db = 'PMA_db';
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,
328 $tablesName,
329 $table,
332 $this->assertIsArray($result);
333 $this->assertArrayHasKey('legendText', $result);
334 $this->assertArrayHasKey('headText', $result);
335 $this->assertArrayHasKey('queryError', $result);
336 $partialDependencies = [
337 'id' => ['col2'],
338 'col1' => ['col2'],
340 $result1 = $this->normalization->createNewTablesFor2NF(
341 $partialDependencies,
342 $tablesName,
343 $table,
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']];
357 $db = 'PMA_db';
358 $dependencies = new stdClass();
359 $dependencies->col1 = ['col2'];
360 $result = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db);
361 $this->assertEquals(
363 'html' => '',
364 'success' => true,
365 'newTables' => [],
367 $result
369 $tables = [
370 'PMA_table' => [
371 'col1',
372 'PMA_table',
375 $dependencies->PMA_table = [
376 'col4',
377 'col5',
379 $result1 = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db);
380 $this->assertIsArray($result1);
381 $this->assertStringContainsString(
382 '<input type="text" name="PMA_table"',
383 $result1['html']
385 $this->assertEquals(
387 'PMA_table' => [
388 'PMA_table' => [
389 'pk' => 'col1',
390 'nonpk' => 'col2',
392 'table2' => [
393 'pk' => 'id',
394 'nonpk' => 'col4, col5',
398 $result1['newTables']
403 * Test for createNewTablesFor3NF
405 public function testCreateNewTablesFor3NF(): void
407 $db = 'PMA_db';
408 $cols = new stdClass();
409 $cols->pk = 'id';
410 $cols->nonpk = 'col1, col2';
411 $cols1 = new stdClass();
412 $cols1->pk = 'col2';
413 $cols1->nonpk = 'col3, col4';
414 $newTables = [
415 'PMA_table' => [
416 'PMA_table' => $cols,
417 'table1' => $cols1,
420 $result = $this->normalization->createNewTablesFor3NF(
421 $newTables,
424 $this->assertIsArray($result);
425 $this->assertArrayHasKey('legendText', $result);
426 $this->assertArrayHasKey('headText', $result);
427 $this->assertArrayHasKey('queryError', $result);
428 $newTables1 = [];
429 $result1 = $this->normalization->createNewTablesFor3NF(
430 $newTables1,
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';
448 $db = 'PMA_db';
449 $result = $this->normalization->moveRepeatingGroup(
450 $repeatingColumns,
451 $primaryColumns,
452 $newTable,
453 $newColumn,
454 $table,
457 $this->assertIsArray($result);
458 $this->assertArrayHasKey('queryError', $result);
459 $this->assertArrayHasKey('message', $result);
460 $this->assertInstanceOf(
461 Message::class,
462 $result['message']
467 * Test for getHtmlFor3NFstep1
469 public function testGetHtmlFor3NFstep1(): void
471 $db = 'PMA_db';
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(
481 '<form',
482 $result['extra']
484 $this->assertStringContainsString(
485 '<input type="checkbox" name="pd" value="col1"',
486 $result['extra']
488 $result1 = $this->normalization->getHtmlFor3NFstep1($db, ['PMA_table2']);
489 $this->assertEquals(
491 $result1['subText']
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"',
504 $result
506 $this->assertStringContainsString(
507 '<input type="hidden" name="step1" value="1">',
508 $result
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';
523 $db = 'PMA_db';
524 $result = $this->normalization->findPartialDependencies($table, $db);
525 $this->assertStringContainsString(
526 '<div class="dependencies_box"',
527 $result
529 $this->assertStringContainsString(__('No partial dependencies found!'), $result);
533 * Test for getAllCombinationPartialKeys
535 public function testGetAllCombinationPartialKeys(): void
537 $primaryKey = [
538 'id',
539 'col1',
540 'col2',
542 $result = $this->callFunction(
543 $this->normalization,
544 Normalization::class,
545 'getAllCombinationPartialKeys',
546 [$primaryKey]
549 $this->assertEquals(
552 'id',
553 'col1',
554 'col1,id',
555 'col2',
556 'col2,id',
557 'col2,col1',
559 $result