Passed
Push — master ( c3c58e...8ab7e2 )
by Maurício
08:33
created

FooterTest::testGetSelfLinkWithRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Tests for Footer class
4
 *
5
 * @package PhpMyAdmin-test
6
 */
7
declare(strict_types=1);
8
9
namespace PhpMyAdmin\Tests;
10
11
use PhpMyAdmin\Config;
12
use PhpMyAdmin\ErrorHandler;
13
use PhpMyAdmin\Footer;
14
use PhpMyAdmin\Tests\PmaTestCase;
15
use PhpMyAdmin\Theme;
16
use ReflectionClass;
17
18
/**
19
 * Tests for Footer class
20
 *
21
 * @package PhpMyAdmin-test
22
 */
23
class FooterTest extends PmaTestCase
24
{
25
26
    /**
27
     * @var array store private attributes of PhpMyAdmin\Footer
28
     */
29
    public $privates = [];
30
31
    /**
32
     * @access protected
33
     */
34
    protected $object;
35
36
    /**
37
     * Sets up the fixture, for example, opens a network connection.
38
     * This method is called before a test is executed.
39
     *
40
     * @access protected
41
     * @return void
42
     */
43
    protected function setUp(): void
44
    {
45
        $_SERVER['SCRIPT_NAME'] = 'index.php';
46
        $GLOBALS['PMA_PHP_SELF'] = 'index.php';
47
        $GLOBALS['db'] = '';
48
        $GLOBALS['table'] = '';
49
        $GLOBALS['text_dir'] = 'ltr';
50
        $GLOBALS['PMA_Config'] = new Config();
51
        $GLOBALS['PMA_Config']->enableBc();
52
        $GLOBALS['cfg']['Server']['DisableIS'] = false;
53
        $GLOBALS['cfg']['Server']['verbose'] = 'verbose host';
54
        $GLOBALS['server'] = '1';
55
        $_GET['reload_left_frame'] = '1';
56
        $GLOBALS['focus_querywindow'] = 'main_pane_left';
57
        $this->object = new Footer();
58
        unset($GLOBALS['error_message']);
59
        unset($GLOBALS['sql_query']);
60
        $GLOBALS['error_handler'] = new ErrorHandler();
61
        unset($_POST);
62
    }
63
64
    /**
65
     * Tears down the fixture, for example, closes a network connection.
66
     * This method is called after a test is executed.
67
     *
68
     * @access protected
69
     * @return void
70
     */
71
    protected function tearDown(): void
72
    {
73
        unset($this->object);
74
    }
75
76
    /**
77
     * Call private functions by setting visibility to public.
78
     *
79
     * @param string $name   method name
80
     * @param array  $params parameters for the invocation
81
     *
82
     * @return mixed the output from the private method.
83
     */
84
    private function _callPrivateFunction($name, $params)
85
    {
86
        $class = new ReflectionClass(Footer::class);
87
        $method = $class->getMethod($name);
88
        $method->setAccessible(true);
89
        return $method->invokeArgs($this->object, $params);
90
    }
91
92
    /**
93
     * Test for getDebugMessage
94
     *
95
     * @return void
96
     *
97
     * @group medium
98
     */
99
    public function testGetDebugMessage()
100
    {
101
        $GLOBALS['cfg']['DBG']['sql'] = true;
102
        $_SESSION['debug']['queries'] = [
103
            [
104
                'count' => 1,
105
                'time' => 0.2,
106
                'query' => 'SELECT * FROM `pma_bookmark` WHERE 1',
107
            ],
108
            [
109
                'count' => 1,
110
                'time' => 2.5,
111
                'query' => 'SELECT * FROM `db` WHERE 1',
112
            ],
113
        ];
114
115
        $this->assertEquals(
116
            '{"queries":[{"count":1,"time":0.2,"query":"SELECT * FROM `pma_bookmark` WHERE 1"},'
117
            . '{"count":1,"time":2.5,"query":"SELECT * FROM `db` WHERE 1"}]}',
118
            $this->object->getDebugMessage()
119
        );
120
    }
121
122
    /**
123
     * Test for _removeRecursion
124
     *
125
     * @return void
126
     */
127
    public function testRemoveRecursion()
128
    {
129
        $object = (object) [];
130
        $object->child = (object) [];
131
        $object->child->parent = $object;
132
133
        $this->_callPrivateFunction(
134
            '_removeRecursion',
135
            [
136
                &$object,
137
            ]
138
        );
139
140
        $this->assertEquals(
141
            '{"child":{"parent":"***RECURSION***"}}',
142
            json_encode($object)
143
        );
144
    }
145
146
    /**
147
     * Test for _getSelfLink
148
     *
149
     * @return void
150
     */
151
    public function testGetSelfLink()
152
    {
153
154
        $GLOBALS['cfg']['TabsMode'] = 'text';
155
        $GLOBALS['cfg']['ServerDefault'] = 1;
156
        $GLOBALS['db'] = 'db';
157
        $GLOBALS['table'] = 'table';
158
        $_REQUEST['target'] = 'target';
159
160
        $this->assertEquals(
161
            '<div id="selflink" class="print_ignore"><a href="index.php?db=db&amp;'
162
            . 'table=table&amp;server=1&amp;target=target&amp;lang=en'
163
            . '" title="Open new phpMyAdmin window" '
164
            . 'target="_blank" rel="noopener noreferrer">Open new phpMyAdmin window</a></div>',
165
            $this->_callPrivateFunction(
166
                '_getSelfLink',
167
                [
168
                    $this->object->getSelfUrl(),
169
                ]
170
            )
171
        );
172
    }
173
174
    /**
175
     * Test for _getSelfLink
176
     *
177
     * @return void
178
     */
179
    public function testGetSelfLinkWithImage()
180
    {
181
182
        $GLOBALS['cfg']['TabsMode'] = 'icons';
183
        $GLOBALS['cfg']['ServerDefault'] = 1;
184
185
        $this->assertEquals(
186
            '<div id="selflink" class="print_ignore"><a href="'
187
            . 'index.php?server=1&amp;lang=en" title="Open new phpMyAdmin window" '
188
            . 'target="_blank" rel="noopener noreferrer"><img src="themes/dot.gif" title="Open new '
189
            . 'phpMyAdmin window" alt="Open new phpMyAdmin window" '
190
            . 'class="icon ic_window-new"></a></div>',
191
            $this->_callPrivateFunction(
192
                '_getSelfLink',
193
                [
194
                    $this->object->getSelfUrl(),
195
                ]
196
            )
197
        );
198
    }
199
200
    /**
201
     * Test for _getSelfLink
202
     *
203
     * @return void
204
     */
205
    public function testGetSelfLinkWithRoute()
206
    {
207
        $_GET['route'] = '/test';
208
209
        $GLOBALS['cfg']['TabsMode'] = 'text';
210
        $GLOBALS['cfg']['ServerDefault'] = 1;
211
212
        $this->assertEquals(
213
            '<div id="selflink" class="print_ignore"><a href="index.php?route=%2Ftest'
214
            . '&amp;server=1&amp;lang=en'
215
            . '" title="Open new phpMyAdmin window" '
216
            . 'target="_blank" rel="noopener noreferrer">Open new phpMyAdmin window</a></div>',
217
            $this->_callPrivateFunction(
218
                '_getSelfLink',
219
                [
220
                    $this->object->getSelfUrl(),
221
                ]
222
            )
223
        );
224
    }
225
226
    /**
227
     * Test for disable
228
     *
229
     * @return void
230
     */
231
    public function testDisable()
232
    {
233
        $footer = new Footer();
234
        $footer->disable();
235
        $this->assertEquals(
236
            '',
237
            $footer->getDisplay()
238
        );
239
    }
240
241
    /**
242
     * Test for footer when ajax enabled
243
     *
244
     * @return void
245
     */
246
    public function testAjax()
247
    {
248
        $footer = new Footer();
249
        $footer->setAjax(true);
250
        $this->assertEquals(
251
            '',
252
            $footer->getDisplay()
253
        );
254
    }
255
256
    /**
257
     * Test for footer get Scripts
258
     *
259
     * @return void
260
     */
261
    public function testGetScripts()
262
    {
263
        $footer = new Footer();
264
        $this->assertStringContainsString(
265
            '<script data-cfasync="false" type="text/javascript">',
266
            $footer->getScripts()->getDisplay()
267
        );
268
    }
269
270
    /**
271
     * Test for displaying footer
272
     *
273
     * @return void
274
     * @group medium
275
     */
276
    public function testDisplay()
277
    {
278
        $footer = new Footer();
279
        $this->assertStringContainsString(
280
            'Open new phpMyAdmin window',
281
            $footer->getDisplay()
282
        );
283
    }
284
285
    /**
286
     * Test for minimal footer
287
     *
288
     * @return void
289
     */
290
    public function testMinimal()
291
    {
292
        $footer = new Footer();
293
        $footer->setMinimal();
294
        $this->assertEquals(
295
            "  </div>\n  </body>\n</html>\n",
296
            $footer->getDisplay()
297
        );
298
    }
299
}
300