Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PMA_DBQbe_test.php « classes « test - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b19bcadc3ea787dcfbd51d87af58cebc7c12feb (plain)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
/**
 * Tests for DBQbe.class.php
 *
 * @package PhpMyAdmin-test
 */

/*
 * Include to test.
 */

require_once 'libraries/DBQbe.class.php';
require_once 'libraries/php-gettext/gettext.inc';
require_once 'libraries/Util.class.php';
require_once 'libraries/core.lib.php';
require_once 'libraries/database_interface.inc.php';
require_once 'libraries/Tracker.class.php';
require_once 'libraries/relation.lib.php';

/**
 * Tests for PMA_DBQbe class
 *
 *  @package PhpMyAdmin-test
 */
class PMA_DBQbe_Test extends PHPUnit_Framework_TestCase
{
    /**
     * @access protected
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     * @return void
     */
    protected function setUp()
    {
        $this->object = new PMA_DBQbe('pma_test');
        $GLOBALS['server'] = 0;
        $GLOBALS['db'] = 'pma_test';
        //mock DBI
        $dbi = $this->getMockBuilder('PMA_DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();

        $create_table = 'CREATE TABLE `table1` ('
            . '`id` int(11) NOT NULL,'
            . '`value` int(11) NOT NULL,'
            . 'PRIMARY KEY (`id`,`value`),'
            . 'KEY `value` (`value`)'
            . ') ENGINE=InnoDB DEFAULT CHARSET=latin1';

        $dbi->expects($this->any())
            ->method('fetchValue')
            ->with('SHOW CREATE TABLE `pma_test`.`table1`', 0, 1)
            ->will($this->returnValue($create_table));

        $dbi->expects($this->any())
            ->method('getTableIndexes')
            ->will($this->returnValue(array()));

        $GLOBALS['dbi'] = $dbi;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     * @return void
     */
    protected function tearDown()
    {
        unset($this->object);
    }

    /**
     * Call protected functions by setting visibility to public.
     *
     * @param string $name   method name
     * @param array  $params parameters for the invocation
     *
     * @return the output from the protected method.
     */
    private function _callProtectedFunction($name, $params)
    {
        $class = new ReflectionClass('PMA_DBQbe');
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method->invokeArgs($this->object, $params);
    }

    /**
     * Test for _getSortSelectCell
     *
     * @return void
     */
    public function testGetSortSelectCell()
    {
        $this->assertEquals(
            '<td class="center"><select style="width: 12ex" name="criteriaSort[1]" '
            . 'size="1"><option value="">&nbsp;</option><option value="ASC">'
            . 'Ascending</option><option value="DESC">Descending</option>'
            . '</select></td>',
            $this->_callProtectedFunction(
                '_getSortSelectCell',
                array(1)
            )
        );
    }

    /**
     * Test for _getSortRow
     *
     * @return void
     */
    public function testGetSortRow()
    {
        $this->assertEquals(
            '<tr class="even noclick"><th>Sort:</th><td class="center">'
            . '<select style="width: 12ex" name="criteriaSort[0]" size="1">'
            . '<option value="">&nbsp;</option><option value="ASC">Ascending'
            . '</option><option value="DESC">Descending</option></select></td>'
            . '<td class="center"><select style="width: 12ex" '
            . 'name="criteriaSort[1]" size="1"><option value="">&nbsp;</option>'
            . '<option value="ASC">Ascending</option><option value="DESC">'
            . 'Descending</option></select></td><td class="center">'
            . '<select style="width: 12ex" name="criteriaSort[2]" size="1">'
            . '<option value="">&nbsp;</option><option value="ASC">Ascending'
            . '</option><option value="DESC">Descending</option></select></td></tr>',
            $this->_callProtectedFunction(
                '_getSortRow',
                array()
            )
        );
    }

    /**
     * Test for _getShowRow
     *
     * @return void
     */
    public function testGetShowRow()
    {
        $this->assertEquals(
            '<tr class="odd noclick"><th>Show:</th><td class="center"><input type'
            . '="checkbox" name="criteriaShow[0]" /></td><td class="center">'
            . '<input type="checkbox" name="criteriaShow[1]" /></td><td '
            . 'class="center"><input type="checkbox" name="criteriaShow[2]" />'
            . '</td></tr>',
            $this->_callProtectedFunction(
                '_getShowRow',
                array()
            )
        );
    }

    /**
     * Test for _getCriteriaInputboxRow
     *
     * @return void
     */
    public function testGetCriteriaInputboxRow()
    {
        $this->assertEquals(
            '<tr class="even noclick"><th>Criteria:</th><td class="center">'
            . '<input type="hidden" name="prev_criteria[0]" value="" />'
            . '<input type="text" name="criteria[0]" value="" class="textfield" '
            . 'style="width: 12ex" size="20" /></td><td class="center">'
            . '<input type="hidden" name="prev_criteria[1]" value="" />'
            . '<input type="text" name="criteria[1]" value="" class="textfield" '
            . 'style="width: 12ex" size="20" /></td><td class="center">'
            . '<input type="hidden" name="prev_criteria[2]" value="" />'
            . '<input type="text" name="criteria[2]" value="" class="textfield" '
            . 'style="width: 12ex" size="20" /></td></tr>',
            $this->_callProtectedFunction(
                '_getCriteriaInputboxRow',
                array()
            )
        );
    }

    /**
     * Test for _getFootersOptions
     *
     * @return void
     */
    public function testGetFootersOptions()
    {
        $this->assertEquals(
            '<div class="floatleft">Add/Delete criteria rows:<select size="1" '
            . 'name="criteriaRowAdd"><option value="-3">-3</option><option '
            . 'value="-2">-2</option><option value="-1">-1</option><option '
            . 'value="0" selected="selected">0</option><option value="1">1'
            . '</option><option value="2">2</option><option value="3">3</option>'
            . '</select></div>',
            $this->_callProtectedFunction(
                '_getFootersOptions',
                array('row')
            )
        );
    }

    /**
     * Test for _getTableFooters
     *
     * @return void
     */
    public function testGetTableFooters()
    {
        $this->assertEquals(
            '<fieldset class="tblFooters"><div class="floatleft">Add/Delete criteria'
            . ' rows:<select size="1" name="criteriaRowAdd"><option value="-3">-3'
            . '</option><option value="-2">-2</option><option value="-1">-1</option>'
            . '<option value="0" selected="selected">0</option><option value="1">1'
            . '</option><option value="2">2</option><option value="3">3</option>'
            . '</select></div><div class="floatleft">Add/Delete columns:<select '
            . 'size="1" name="criteriaColumnAdd"><option value="-3">-3</option>'
            . '<option value="-2">-2</option><option value="-1">-1</option>'
            . '<option value="0" selected="selected">0</option><option value="1">1'
            . '</option><option value="2">2</option><option value="3">3</option>'
            . '</select></div><div class="floatleft"><input type="submit" '
            . 'name="modify"value="Update Query" /></div></fieldset>',
            $this->_callProtectedFunction(
                '_getTableFooters',
                array()
            )
        );
    }

    /**
     * Test for _getAndOrColCell
     *
     * @return void
     */
    public function testGetAndOrColCell()
    {
        $this->assertEquals(
            '<td class="center"><strong>Or:</strong><input type="radio" '
            . 'name="criteriaAndOrColumn[1]" value="or" />&nbsp;&nbsp;<strong>And:'
            . '</strong><input type="radio" name="criteriaAndOrColumn[1]" value='
            . '"and" /><br />Ins<input type="checkbox" name="criteriaColumnInsert'
            . '[1]" />&nbsp;&nbsp;Del<input type="checkbox" '
            . 'name="criteriaColumnDelete[1]" /></td>',
            $this->_callProtectedFunction(
                '_getAndOrColCell',
                array(1)
            )
        );
    }

    /**
     * Test for _getModifyColumnsRow
     *
     * @return void
     */
    public function testGetModifyColumnsRow()
    {
        $this->assertEquals(
            '<tr class="even noclick"><th>Modify:</th><td class="center"><strong>'
            . 'Or:</strong><input type="radio" name="criteriaAndOrColumn[0]" value'
            . '="or" />&nbsp;&nbsp;<strong>And:</strong><input type="radio" name='
            . '"criteriaAndOrColumn[0]" value="and" checked="checked" /><br />Ins'
            . '<input type="checkbox" name="criteriaColumnInsert[0]" />&nbsp;&nbsp;'
            . 'Del<input type="checkbox" name="criteriaColumnDelete[0]" /></td><td '
            . 'class="center"><strong>Or:</strong><input type="radio" name="'
            . 'criteriaAndOrColumn[1]" value="or" />&nbsp;&nbsp;<strong>And:'
            . '</strong><input type="radio" name="criteriaAndOrColumn[1]" value='
            . '"and" checked="checked" /><br />Ins<input type="checkbox" name='
            . '"criteriaColumnInsert[1]" />&nbsp;&nbsp;Del<input type="checkbox" '
            . 'name="criteriaColumnDelete[1]" /></td><td class="center"><br />Ins'
            . '<input type="checkbox" name="criteriaColumnInsert[2]" />&nbsp;&nbsp;'
            . 'Del<input type="checkbox" name="criteriaColumnDelete[2]" /></td>'
            . '</tr>',
            $this->_callProtectedFunction(
                '_getModifyColumnsRow',
                array()
            )
        );
    }

    /**
     * Test for _getInsDelAndOrCell
     *
     * @return void
     */
    public function testGetInsDelAndOrCell()
    {
        $GLOBALS['cell_align_right'] = 'cellAlign';
        $this->assertEquals(
            '<td class="cellAlign nowrap"><!-- Row controls --><table class="nospac'
            . 'ing nopadding"><tr><td class="cellAlign nowrap"><small>Ins:</small>'
            . '<input type="checkbox" name="criteriaRowInsert[3]" /></td><td '
            . 'class="cellAlign"><strong>And:</strong></td><td><input type="radio" '
            . 'name="criteriaAndOrRow[3]" value="and" /></td></tr><tr><td class="'
            . 'cellAlign nowrap"><small>Del:</small><input type="checkbox" '
            . 'name="criteriaRowDelete[3]" /></td><td class="cellAlign"><strong>'
            . 'Or:</strong></td><td><input type="radio" name="criteriaAndOrRow[3]" '
            . 'value="or" checked="checked" /></td></tr></table></td>',
            $this->_callProtectedFunction(
                '_getInsDelAndOrCell',
                array(3, array('and' => '', 'or' => ' checked="checked"'))
            )
        );
    }

    /**
     * Test for _getInputboxRow
     *
     * @return void
     */
    public function testGetInputboxRow()
    {
        $this->assertEquals(
            '<td class="center"><input type="text" name="Or2[0]" value="" class='
            . '"textfield" style="width: 12ex" size="20" /></td><td class="center">'
            . '<input type="text" name="Or2[1]" value="" class="textfield" '
            . 'style="width: 12ex" size="20" /></td><td class="center"><input '
            . 'type="text" name="Or2[2]" value="" class="textfield" style="width: '
            . '12ex" size="20" /></td>',
            $this->_callProtectedFunction(
                '_getInputboxRow',
                array(2)
            )
        );
    }

    /**
     * Test for _getInsDelAndOrCriteriaRows
     *
     * @return void
     */
    public function testGetInsDelAndOrCriteriaRows()
    {
        $GLOBALS['cell_align_right'] = 'cellAlign';
        $this->assertEquals(
            '<tr class="odd noclick"><td class="cellAlign nowrap"><!-- Row controls'
            . ' --><table class="nospacing nopadding"><tr><td class="cellAlign '
            . 'nowrap"><small>Ins:</small><input type="checkbox" name="'
            . 'criteriaRowInsert[0]" /></td><td class="cellAlign"><strong>And:'
            . '</strong></td><td><input type="radio" name="criteriaAndOrRow[0]" '
            . 'value="and" /></td></tr><tr><td class="cellAlign nowrap"><small>Del:'
            . '</small><input type="checkbox" name="criteriaRowDelete[0]" /></td>'
            . '<td class="cellAlign"><strong>Or:</strong></td><td><input type='
            . '"radio" name="criteriaAndOrRow[0]" value="or" checked="checked" />'
            . '</td></tr></table></td><td class="center"><input type="text" '
            . 'name="Or0[0]" value="" class="textfield" style="width: 12ex" '
            . 'size="20" /></td><td class="center"><input type="text" name="Or0[1]" '
            . 'value="" class="textfield" style="width: 12ex" size="20" /></td><td '
            . 'class="center"><input type="text" name="Or0[2]" value="" class='
            . '"textfield" style="width: 12ex" size="20" /></td></tr>',
            $this->_callProtectedFunction(
                '_getInsDelAndOrCriteriaRows',
                array(2,3)
            )
        );
    }

    /**
     * Test for _getSelectClause
     *
     * @return void
     */
    public function testGetSelectClause()
    {
        $this->assertEquals(
            '',
            $this->_callProtectedFunction(
                '_getSelectClause',
                array()
            )
        );
    }

    /**
     * Test for _getWhereClause
     *
     * @return void
     */
    public function testGetWhereClause()
    {
        $this->assertEquals(
            '',
            $this->_callProtectedFunction(
                '_getWhereClause',
                array()
            )
        );
    }

    /**
     * Test for _getOrderByClause
     *
     * @return void
     */
    public function testGetOrderByClause()
    {
        $this->assertEquals(
            '',
            $this->_callProtectedFunction(
                '_getOrderByClause',
                array()
            )
        );
    }

    /**
     * Test for _getIndexes
     *
     * @return void
     */
    public function testGetIndexes()
    {
        $this->assertEquals(
            array(
                'unique' => array(),
                'index' => array()
            ),
            $this->_callProtectedFunction(
                '_getIndexes',
                array(
                    array('`table1`','table2'),
                    array('column1', 'column2', 'column3'),
                    array('column2')
                )
            )
        );
    }

    /**
     * Test for _getLeftJoinColumnCandidates
     *
     * @return void
     */
    public function testGetLeftJoinColumnCandidates()
    {
        $this->assertEquals(
            array(
                0 => 'column2'
            ),
            $this->_callProtectedFunction(
                '_getLeftJoinColumnCandidates',
                array(
                    array('`table1`','table2'),
                    array('column1', 'column2', 'column3'),
                    array('column2')
                )
            )
        );
    }

    /**
     * Test for _getMasterTable
     *
     * @return void
     */
    public function testGetMasterTable()
    {
        $this->assertEquals(
            0,
            $this->_callProtectedFunction(
                '_getMasterTable',
                array(
                    array('table1','table2'),
                    array('column1', 'column2', 'column3'),
                    array('column2'),
                    array('qbe_test')
                )
            )
        );
    }

    /**
     * Test for _getWhereClauseTablesAndColumns
     *
     * @return void
     */
    public function testGetWhereClauseTablesAndColumns()
    {
        $_POST['criteriaColumn'] = array(
            'table1.id',
            'table1.value',
            'table1.name',
            'table1.deleted'
        );
        $this->assertEquals(
            array(
                'where_clause_tables' => array(),
                'where_clause_columns' => array()
            ),
            $this->_callProtectedFunction(
                '_getWhereClauseTablesAndColumns',
                array()
            )
        );
    }

    /**
     * Test for _getFromClause
     *
     * @return void
     */
    public function testGetFromClause()
    {
        $_POST['criteriaColumn'] = array(
            'table1.id',
            'table1.value',
            'table1.name',
            'table1.deleted'
        );
        $this->assertEquals(
            '`table1`',
            $this->_callProtectedFunction(
                '_getFromClause', array()
            )
        );
    }

    /**
     * Test for _getSQLQuery
     *
     * @return void
     */
    public function testGetSQLQuery()
    {
        $_POST['criteriaColumn'] = array(
            'table1.id',
            'table1.value',
            'table1.name',
            'table1.deleted'
        );
        $this->assertEquals(
            'FROM `table1`
',
            $this->_callProtectedFunction(
                '_getSQLQuery',
                array()
            )
        );
    }
}