Passed
Pull Request — master (#14116)
by
unknown
09:32
created

testGetHtmlForTableNavigation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 49
rs 9.2258
c 0
b 0
f 0
1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
/**
4
 * tests for PhpMyAdmin\CentralColumns
5
 *
6
 * @package PhpMyAdmin-test
7
 */
8
namespace PhpMyAdmin\Tests;
9
10
use PhpMyAdmin\CentralColumns;
11
use PhpMyAdmin\Config;
12
use PhpMyAdmin\DatabaseInterface;
13
use PhpMyAdmin\Types;
14
use PhpMyAdmin\Url;
15
use PhpMyAdmin\Util;
16
use PHPUnit\Framework\TestCase;
17
use ReflectionClass;
18
19
$GLOBALS['server'] = 1;
20
21
/**
22
 * tests for PhpMyAdmin\CentralColumns
23
 *
24
 * @package PhpMyAdmin-test
25
 */
26
class CentralColumnsTest extends TestCase
27
{
28
    private $centralColumns;
29
30
    private $columnData = array(
31
        array(
32
            'col_name' => "id", "col_type" => 'integer',
33
            'col_length' => 0, 'col_isNull' => 0,
34
            'col_extra' => 'UNSIGNED,auto_increment',
35
            'col_default' => 1, 'col_collation' => ''
36
        ),
37
        array('col_name' => "col1", 'col_type' => 'varchar',
38
            'col_length' => 100, 'col_isNull' => 1, 'col_extra' => 'BINARY',
39
            'col_default' => 1, 'col_collation' => ''
40
        ),
41
        array(
42
            'col_name' => "col2", 'col_type' => 'DATETIME',
43
            'col_length' => 0, 'col_isNull' => 1,
44
            'col_extra' => 'on update CURRENT_TIMESTAMP',
45
            'col_default' => 'CURRENT_TIMESTAMP', 'col_collation' => ''
46
        )
47
    );
48
49
    private $modifiedColumnData = array(
50
        array(
51
            'col_name' => "id", "col_type" => 'integer',
52
            'col_length' => 0, 'col_isNull' => 0, 'col_extra' => 'auto_increment',
53
            'col_default' => 1, 'col_collation' => '', 'col_attribute' => 'UNSIGNED'
54
        ),
55
        array('col_name' => "col1", 'col_type' => 'varchar',
56
            'col_length' => 100, 'col_isNull' => 1, 'col_extra' => '',
57
            'col_default' => 1, 'col_collation' => '', 'col_attribute' => 'BINARY'
58
        ),
59
        array(
60
            'col_name' => "col2", 'col_type' => 'DATETIME',
61
            'col_length' => 0, 'col_isNull' => 1, 'col_extra' => '',
62
            'col_default' => 'CURRENT_TIMESTAMP', 'col_collation' => '',
63
            'col_attribute' => 'on update CURRENT_TIMESTAMP'
64
        )
65
    );
66
67
    /**
68
     * prepares environment for tests
69
     *
70
     * @return void
71
     */
72
    public function setUp()
73
    {
74
        $GLOBALS['PMA_Config'] = new Config();
75
        $GLOBALS['cfg']['Server']['user'] = 'pma_user';
76
        $GLOBALS['cfg']['Server']['DisableIS'] = true;
77
        $GLOBALS['cfg']['MaxRows'] = 10;
78
        $GLOBALS['cfg']['ServerDefault'] = "PMA_server";
79
        $GLOBALS['cfg']['ActionLinksMode'] = 'icons';
80
        $GLOBALS['cfg']['CharEditing'] = '';
81
        $GLOBALS['cfg']['LimitChars'] = 50;
82
        $GLOBALS['db'] = 'PMA_db';
83
        $GLOBALS['table'] = 'PMA_table';
84
85
        //$_SESSION
86
        $GLOBALS['server'] = 1;
87
        $_SESSION['relation'][1] = array(
88
            'PMA_VERSION' => PMA_VERSION,
89
            'centralcolumnswork' => true,
90
            'relwork' => 1,
91
            'db' => 'phpmyadmin',
92
            'relation' => 'relation',
93
            'central_columns' => 'pma_central_columns'
94
        );
95
96
        // mock DBI
97
        $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
98
            ->disableOriginalConstructor()
99
            ->getMock();
100
        $dbi->types = new Types($dbi);
101
        $GLOBALS['dbi'] = $dbi;
102
103
        // set some common expectations
104
        $dbi->expects($this->any())
105
            ->method('selectDb')
106
            ->will($this->returnValue(true));
107
        $dbi->expects($this->any())
108
            ->method('getColumns')
109
            ->will(
110
                $this->returnValue(
111
                    array(
112
                        "id"=>array("Type"=>"integer", "Null"=>"NO"),
113
                        "col1"=>array("Type"=>'varchar(100)', "Null"=>"YES"),
114
                        "col2"=>array("Type"=>'DATETIME', "Null"=>"NO")
115
                    )
116
                )
117
            );
118
        $dbi->expects($this->any())
119
            ->method('getColumnNames')
120
            ->will($this->returnValue(array("id", "col1", "col2")));
121
        $dbi->expects($this->any())
122
            ->method('tryQuery')
123
            ->will($this->returnValue(true));
124
        $dbi->expects($this->any())
125
            ->method('getTables')
126
            ->will(
127
                $this->returnValue(array("PMA_table", "PMA_table1", "PMA_table2"))
128
            );
129
        $dbi->expects($this->any())->method('escapeString')
130
            ->will($this->returnArgument(0));
131
132
        $this->centralColumns = new CentralColumns($dbi);
133
    }
134
135
    /**
136
     * Call protected functions by setting visibility to public.
137
     *
138
     * @param string         $name   method name
139
     * @param array          $params parameters for the invocation
140
     * @param CentralColumns $object CentralColumns instance object
141
     *
142
     * @return mixed the output from the protected method.
143
     */
144 View Code Duplication
    private function callProtectedMethod(
145
        $name,
146
        array $params = [],
147
        CentralColumns $object = null
148
    ) {
149
        $class = new ReflectionClass(CentralColumns::class);
150
        $method = $class->getMethod($name);
151
        $method->setAccessible(true);
152
        return $method->invokeArgs(
153
            $object !== null ? $object : $this->centralColumns,
154
            $params
155
        );
156
    }
157
158
    /**
159
     * Test for getParams
160
     *
161
     * @return void
162
     */
163
    public function testGetParams()
164
    {
165
        $this->assertSame(
166
            array(
167
                'user' => 'pma_user',
168
                'db' => 'phpmyadmin',
169
                'table' => 'pma_central_columns'
170
            ),
171
            $this->centralColumns->getParams()
172
        );
173
    }
174
175
    /**
176
     * Test for getColumnsList
177
     *
178
     * @return void
179
     */
180
    public function testGetColumnsList()
181
    {
182
        $GLOBALS['dbi']->expects($this->exactly(2))
183
            ->method('fetchResult')
184
            ->willReturnOnConsecutiveCalls(
185
                $this->columnData,
186
                array_slice($this->columnData, 1, 2)
187
            );
188
189
        $this->assertEquals(
190
            $this->modifiedColumnData,
191
            $this->centralColumns->getColumnsList('phpmyadmin')
192
        );
193
        $this->assertEquals(
194
            array_slice($this->modifiedColumnData, 1, 2),
195
            $this->centralColumns->getColumnsList('phpmyadmin', 1, 2)
196
        );
197
    }
198
199
    /**
200
     * Test for getCount
201
     *
202
     * @return void
203
     */
204
    function testGetCount()
205
    {
206
        $GLOBALS['dbi']->expects($this->once())
207
            ->method('fetchResult')
208
            ->with(
209
                "SELECT count(db_name) FROM `pma_central_columns` "
210
                . "WHERE db_name = 'phpmyadmin';",
211
                null, null, DatabaseInterface::CONNECT_CONTROL
212
            )
213
            ->will(
214
                $this->returnValue(array(3))
215
            );
216
217
        $this->assertEquals(
218
            3,
219
            $this->centralColumns->getCount('phpmyadmin')
220
        );
221
    }
222
223
    /**
224
     * Test for syncUniqueColumns
225
     *
226
     * @return void
227
     */
228
    public function testSyncUniqueColumns()
229
    {
230
        $_REQUEST['db'] = 'PMA_db';
231
        $_REQUEST['table'] = 'PMA_table';
232
233
        $this->assertTrue(
234
            $this->centralColumns->syncUniqueColumns(
235
                array('PMA_table')
236
            )
237
        );
238
    }
239
240
    /**
241
     * Test for deleteColumnsFromList
242
     *
243
     * @return void
244
     */
245
    public function testDeleteColumnsFromList()
246
    {
247
        $_REQUEST['db'] = 'PMA_db';
248
        $_REQUEST['table'] = 'PMA_table';
249
250
        // when column exists in the central column list
251
        $GLOBALS['dbi']->expects($this->at(4))
252
            ->method('fetchResult')
253
            ->with(
254
                "SELECT col_name FROM `pma_central_columns` "
255
                . "WHERE db_name = 'PMA_db' AND col_name IN ('col1');",
256
                null, null, DatabaseInterface::CONNECT_CONTROL
257
            )
258
            ->will(
259
                $this->returnValue(array('col1'))
260
            );
261
262
        $GLOBALS['dbi']->expects($this->at(7))
263
            ->method('tryQuery')
264
            ->with(
265
                "DELETE FROM `pma_central_columns` "
266
                . "WHERE db_name = 'PMA_db' AND col_name IN ('col1');",
267
                DatabaseInterface::CONNECT_CONTROL
268
            )
269
            ->will(
270
                $this->returnValue(array('col1'))
271
            );
272
273
        $this->assertTrue(
274
            $this->centralColumns->deleteColumnsFromList(
275
                array("col1"),
276
                false
277
            )
278
        );
279
280
        // when column does not exist in the central column list
281
        $this->assertInstanceOf(
282
            'PhpMyAdmin\Message', $this->centralColumns->deleteColumnsFromList(
283
                array('column1'),
284
                false
285
            )
286
        );
287
288
        $this->assertInstanceOf(
289
            'PhpMyAdmin\Message', $this->centralColumns->deleteColumnsFromList(
290
                array('PMA_table')
291
            )
292
        );
293
    }
294
295
    /**
296
     * Test for makeConsistentWithList
297
     *
298
     * @return void
299
     */
300
    public function testMakeConsistentWithList()
301
    {
302
        $GLOBALS['dbi']->expects($this->any())
303
            ->method('fetchResult')
304
            ->will(
305
                $this->returnValue($this->columnData)
306
            );
307
        $GLOBALS['dbi']->expects($this->any())
308
            ->method('fetchValue')
309
            ->will(
310
                $this->returnValue('PMA_table=CREATE table `PMA_table` (id integer)')
311
            );
312
        $this->assertTrue(
313
            $this->centralColumns->makeConsistentWithList(
314
                "phpmyadmin",
315
                array('PMA_table')
316
            )
317
        );
318
    }
319
320
    /**
321
     * Test for getFromTable
322
     *
323
     * @return void
324
     */
325
    public function testGetFromTable()
326
    {
327
        $db = 'PMA_db';
328
        $table = 'PMA_table';
329
330
        $GLOBALS['dbi']->expects($this->once())
331
            ->method('fetchResult')
332
            ->with(
333
                "SELECT col_name FROM `pma_central_columns` "
334
                . "WHERE db_name = 'PMA_db' AND col_name IN ('id','col1','col2');",
335
                null, null, DatabaseInterface::CONNECT_CONTROL
336
            )
337
            ->will(
338
                $this->returnValue(array('id','col1'))
339
            );
340
        $this->assertEquals(
341
            array("id", "col1"),
342
            $this->centralColumns->getFromTable(
343
                $db,
344
                $table
345
            )
346
        );
347
    }
348
349
    /**
350
     * Test for getFromTable with $allFields = true
351
     *
352
     * @return void
353
     */
354
    public function testGetFromTableWithAllFields()
355
    {
356
        $db = 'PMA_db';
357
        $table = 'PMA_table';
358
359
        $GLOBALS['dbi']->expects($this->once())
360
            ->method('fetchResult')
361
            ->with(
362
                "SELECT * FROM `pma_central_columns` "
363
                . "WHERE db_name = 'PMA_db' AND col_name IN ('id','col1','col2');",
364
                null, null, DatabaseInterface::CONNECT_CONTROL
365
            )
366
            ->will(
367
                $this->returnValue(array_slice($this->columnData, 0, 2))
368
            );
369
        $this->assertEquals(
370
            array_slice($this->modifiedColumnData, 0, 2),
371
            $this->centralColumns->getFromTable(
372
                $db,
373
                $table,
374
                true
375
            )
376
        );
377
    }
378
379
    /**
380
     * Test for updateOneColumn
381
     *
382
     * @return void
383
     */
384
    public function testUpdateOneColumn()
385
    {
386
        $this->assertTrue(
387
            $this->centralColumns->updateOneColumn(
388
                "phpmyadmin", "", "", "", "", "", "", "", "", ""
389
            )
390
        );
391
        $this->assertTrue(
392
            $this->centralColumns->updateOneColumn(
393
                "phpmyadmin", "col1", "", "", "", "", "", "", "", ""
394
            )
395
        );
396
    }
397
398
    /**
399
     * Test for updateMultipleColumn
400
     *
401
     * @return void
402
     */
403
    public function testUpdateMultipleColumn()
404
    {
405
        $_POST['db'] = 'phpmyadmin';
406
        $_POST['orig_col_name'] = array("col1","col2");
407
        $_POST['field_name'] = array("col1","col2");
408
        $_POST['field_default_type'] = array("","");
409
        $_POST['col_extra'] = array("","");
410
        $_POST['field_length'] = array("","");
411
        $_POST['field_attribute'] = array("","");
412
        $_POST['field_type'] = array("","");
413
        $_POST['field_collation'] = array("","");
414
        $this->assertTrue(
415
            $this->centralColumns->updateMultipleColumn()
416
        );
417
418
    }
419
420
    /**
421
     * Test for getHtmlForEditingPage
422
     *
423
     * @return void
424
     */
425
    public function testGetHtmlForEditingPage()
426
    {
427
        $GLOBALS['dbi']->expects($this->any())
428
            ->method('fetchResult')
429
            ->with(
430
                "SELECT * FROM `pma_central_columns` "
431
                . "WHERE db_name = 'phpmyadmin' AND col_name IN ('col1','col2');",
432
                null, null, DatabaseInterface::CONNECT_CONTROL
433
            )
434
            ->will(
435
                $this->returnValue($this->columnData)
436
            );
437
        $result = $this->centralColumns->getHtmlForEditingPage(
438
            array("col1", "col2"),
439
            'phpmyadmin'
440
        );
441
        $this->assertContains(
442
            '<form',
443
            $result
444
        );
445
        $header_cells = array(
446
        __('Name'), __('Type'), __('Length/Values'), __('Default'),
447
        __('Collation'), __('Attributes'), __('Null'), __('A_I')
448
        );
449
        $this->assertContains(
450
            $this->callProtectedMethod(
451
                'getEditTableHeader',
452
                [$header_cells]
453
            ),
454
            $result
455
        );
456
        $list_detail_cols = $this->callProtectedMethod(
457
            'findExistingColNames',
458
            [
459
                'phpmyadmin',
460
                "'col1','col2'",
461
                true,
462
            ]
463
        );
464
        $this->assertContains(
465
            $this->callProtectedMethod(
466
                'getHtmlForEditTableRow',
467
                [
468
                    $list_detail_cols[0],
469
                    0,
470
                ]
471
            ),
472
            $result
473
        );
474
        $this->assertContains(
475
            $this->callProtectedMethod('getEditTableFooter'),
476
            $result
477
        );
478
479
    }
480
481
    /**
482
     * Test for getHtmlForTableNavigation
483
     *
484
     * @return void
485
     */
486
    public function testGetHtmlForTableNavigation()
487
    {
488
        $result = $this->centralColumns->getHtmlForTableNavigation(
489
            0,
490
            0,
491
            'phpmyadmin'
492
        );
493
        $this->assertContains(
494
            '<table',
495
            $result
496
        );
497
        $this->assertContains(
498
            __('Search this table'),
499
            $result
500
        );
501
        $result_1 = $this->centralColumns->getHtmlForTableNavigation(
502
            25,
503
            10,
504
            'phpmyadmin'
505
        );
506
        $this->assertContains(
507
            '<form action="db_central_columns.php" method="post">',
508
            $result_1
509
        );
510
        $this->assertContains(
511
            Url::getHiddenInputs(
512
                'phpmyadmin'
513
            ),
514
            $result_1
515
        );
516
        $this->assertContains(
517
            '<input type="submit" name="navig"'
518
            . ' class="ajax" '
519
            . 'value="&lt" />',
520
            $result_1
521
        );
522
        $this->assertContains(
523
            Util::pageselector(
524
                'pos', 10, 2, 3
525
            ),
526
            $result_1
527
        );
528
        $this->assertContains(
529
            '<input type="submit" name="navig"'
530
            . ' class="ajax" '
531
            . 'value="&gt" />',
532
            $result_1
533
        );
534
    }
535
536
    /**
537
     * Test for getTableHeader
538
     *
539
     * @return void
540
     */
541
    public function testGetTableHeader()
542
    {
543
        $this->assertContains(
544
            '<thead',
545
            $this->centralColumns->getTableHeader(
546
                'column_heading', __('Click to sort'), 2
547
            )
548
        );
549
    }
550
551
    /**
552
     * Test for getListRaw
553
     *
554
     * @return void
555
     */
556 View Code Duplication
    public function testGetListRaw()
557
    {
558
        $GLOBALS['dbi']->expects($this->once())
559
            ->method('fetchResult')
560
            ->with(
561
                "SELECT * FROM `pma_central_columns` "
562
                . "WHERE db_name = 'phpmyadmin';",
563
                null, null, DatabaseInterface::CONNECT_CONTROL
564
            )
565
            ->will(
566
                $this->returnValue($this->columnData)
567
            );
568
        $this->assertEquals(
569
            json_encode($this->modifiedColumnData),
570
            $this->centralColumns->getListRaw(
571
                'phpmyadmin',
572
                ''
573
            )
574
        );
575
    }
576
577
    /**
578
     * Test for getListRaw with a table name
579
     *
580
     * @return void
581
     */
582 View Code Duplication
    public function testGetListRawWithTable()
583
    {
584
        $GLOBALS['dbi']->expects($this->once())
585
            ->method('fetchResult')
586
            ->with(
587
                "SELECT * FROM `pma_central_columns` "
588
                . "WHERE db_name = 'phpmyadmin' AND col_name "
589
                . "NOT IN ('id','col1','col2');",
590
                null, null, DatabaseInterface::CONNECT_CONTROL
591
            )
592
            ->will(
593
                $this->returnValue($this->columnData)
594
            );
595
        $this->assertEquals(
596
            json_encode($this->modifiedColumnData),
597
            $this->centralColumns->getListRaw(
598
                'phpmyadmin',
599
                'table1'
600
            )
601
        );
602
603
    }
604
605
    /**
606
     * Test for getHtmlForAddNewColumn
607
     *
608
     * @return void
609
     */
610
    public function testGetHtmlForAddNewColumn()
611
    {
612
        $result = $this->centralColumns->getHtmlForAddNewColumn(
613
            'phpmyadmin',
614
            0
615
        );
616
        $this->assertContains(
617
            '<form',
618
            $result
619
        );
620
        $this->assertContains(
621
            '<table',
622
            $result
623
        );
624
        $this->assertContains(
625
            __('Add new column'),
626
            $result
627
        );
628
        $this->assertContains(
629
            Url::getHiddenInputs('phpmyadmin'),
630
            $result
631
        );
632
    }
633
634
    /**
635
     * Test for configErrorMessage
636
     *
637
     * @return void
638
     */
639
    public function testConfigErrorMessage()
640
    {
641
        $this->assertInstanceOf(
642
            'PhpMyAdmin\Message',
643
            $this->callProtectedMethod('configErrorMessage')
644
        );
645
    }
646
647
    /**
648
     * Test for findExistingColNames
649
     *
650
     * @return void
651
     */
652
    public function testFindExistingColNames()
653
    {
654
        $GLOBALS['dbi']->expects($this->once())
655
            ->method('fetchResult')
656
            ->with(
657
                "SELECT * FROM `pma_central_columns` WHERE db_name = 'phpmyadmin'"
658
                . " AND col_name IN ('col1');",
659
                null, null, DatabaseInterface::CONNECT_CONTROL
660
            )
661
            ->will(
662
                $this->returnValue(array_slice($this->columnData, 1, 1))
663
            );
664
        $this->assertEquals(
665
            array_slice($this->modifiedColumnData, 1, 1),
666
            $this->callProtectedMethod(
667
                'findExistingColNames',
668
                [
669
                    'phpmyadmin',
670
                    "'col1'",
671
                    true,
672
                ]
673
            )
674
        );
675
    }
676
677
    /**
678
     * Test for getHtmlForTableDropdown
679
     *
680
     * @return void
681
     */
682
    public function testGetHtmlForTableDropdown()
683
    {
684
        $db = 'PMA_db';
685
        $result = $this->callProtectedMethod(
686
            'getHtmlForTableDropdown',
687
            [$db]
688
        );
689
        $this->assertContains(
690
            '<select name="table-select" id="table-select"',
691
            $result
692
        );
693
        $this->assertContains(
694
            '<option value="PMA_table"',
695
            $result
696
        );
697
    }
698
699
    /**
700
     * Test for getHtmlForColumnDropdown
701
     *
702
     * @return void
703
     */
704
    public function testGetHtmlForColumnDropdown()
705
    {
706
        $db = 'PMA_db';
707
        $selected_tbl = 'PMA_table';
708
        $result = $this->centralColumns->getHtmlForColumnDropdown(
709
            $db,
710
            $selected_tbl
711
        );
712
        $this->assertEquals(
713
            '<option value="id">id</option><option value="col1">col1</option>'
714
            . '<option value="col2">col2</option>',
715
            $result
716
        );
717
    }
718
719
    /**
720
     * Test for getHtmlForAddColumn
721
     *
722
     * @return void
723
     */
724
    public function testGetHtmlForAddColumn()
725
    {
726
        $result = $this->centralColumns->getHtmlForAddColumn(20, 0, 'phpmyadmin');
727
        $this->assertContains(
728
            '<table',
729
            $result
730
        );
731
        $this->assertContains(
732
            '<form',
733
            $result
734
        );
735
        $this->assertContains(
736
            Url::getHiddenInputs('phpmyadmin'),
737
            $result
738
        );
739
        $this->assertContains(
740
            '<input type="hidden" name="add_column" value="add">',
741
            $result
742
        );
743
        $this->assertContains(
744
            '<input type="hidden" name="pos" value="0" />',
745
            $result
746
        );
747
        $this->assertContains(
748
            '<input type="hidden" name="total_rows" value="20"/>',
749
            $result
750
        );
751
    }
752
753
    /**
754
     * Test for getTableFooter
755
     *
756
     * @return void
757
     */
758
    public function testGetTableFooter()
759
    {
760
        $pmaThemeImage = "pmaThemeImage";
761
        $text_dir = "text_dir";
762
        $result = $this->centralColumns->getTableFooter($pmaThemeImage, $text_dir);
763
        $this->assertContains(
764
            '<input type="checkbox" id="tableslistcontainer_checkall" class="checkall_box"',
765
            $result
766
        );
767
        $this->assertContains("With selected:", $result);
768
        $this->assertContains(
769
            '<button class="mult_submit change_central_columns"',
770
            $result
771
        );
772
    }
773
}
774