Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
24.83% covered (danger)
24.83%
112 / 451
62.07% covered (warning)
62.07%
18 / 29
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_path_helper_test
24.83% covered (danger)
24.83%
112 / 451
62.07% covered (warning)
62.07%
18 / 29
386.16
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 set_phpbb_root_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_get_web_root_path
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 test_get_adm_relative_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_get_php_ext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 basic_update_web_root_path_data
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 test_basic_update_web_root_path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 test_update_web_root_path_app
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 update_web_root_path_data
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 1
2
 test_update_web_root_path
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 remove_web_root_path_data
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 test_remove_web_root_path
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 clean_url_data
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 test_clean_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 glue_url_params_data
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 test_glue_url_params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_url_parts_data
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_url_parts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 strip_url_params_data
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 test_strip_url_params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 append_url_params_data
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_web_root_path_ajax
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 test_append_url_params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_web_root_path_from_ajax_referer_data
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_web_root_path_from_ajax_referer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 data_get_valid_page
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_valid_page
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_router_used_data
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 test_is_router_used
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14use phpbb\filesystem\helper as filesystem_helper;
15
16class phpbb_path_helper_test extends phpbb_test_case
17{
18    /** @var \phpbb\path_helper */
19    protected $path_helper;
20    protected static $phpbb_root_path = '';
21
22    protected function setUp(): void
23    {
24        parent::setUp();
25
26        self::set_phpbb_root_path();
27
28        $this->path_helper = new \phpbb\path_helper(
29            new \phpbb\symfony_request(
30                new phpbb_mock_request()
31            ),
32            $this->createMock('\phpbb\request\request'),
33            self::$phpbb_root_path,
34            'php',
35            'adm/'
36        );
37    }
38
39    /**
40    * Set the phpbb_root_path
41    *
42    * This is necessary because dataProvider functions are called
43    *    before setUp or setUpBeforeClass; so we must set the path
44    *    any time we wish to use it in one of these functions (and
45    *    also in general for everything else)
46    */
47    public static function set_phpbb_root_path()
48    {
49        self::$phpbb_root_path = filesystem_helper::clean_path(__DIR__ . '/../../phpBB/');
50    }
51
52    public function test_get_web_root_path()
53    {
54        $this->assertEquals(self::$phpbb_root_path, $this->path_helper->get_web_root_path());
55
56        // Second call will use class property
57        $this->assertEquals(self::$phpbb_root_path, $this->path_helper->get_web_root_path());
58    }
59
60    public function test_get_adm_relative_path()
61    {
62        $this->assertEquals( 'adm/', $this->path_helper->get_adm_relative_path());
63    }
64
65    public function test_get_php_ext()
66    {
67        $this->assertSame('php', $this->path_helper->get_php_ext());
68    }
69
70    public static function basic_update_web_root_path_data()
71    {
72        self::set_phpbb_root_path();
73
74        return [
75            [
76                'http://www.test.com/test.php',
77                'http://www.test.com/test.php',
78                '/',
79            ],
80            [
81                self::$phpbb_root_path . 'test.php',
82                self::$phpbb_root_path . 'test.php',
83            ],
84            [
85                'test.php',
86                'test.php',
87            ],
88            [
89                self::$phpbb_root_path . self::$phpbb_root_path . 'test.php',
90                filesystem_helper::clean_path(self::$phpbb_root_path . self::$phpbb_root_path . 'test.php'),
91            ],
92        ];
93    }
94
95    /**
96    * @dataProvider basic_update_web_root_path_data
97    */
98    public function test_basic_update_web_root_path($input, $expected)
99    {
100        $this->assertEquals($expected, $this->path_helper->update_web_root_path($input));
101    }
102
103    public function test_update_web_root_path_app()
104    {
105        $path_helper = $this->getMockBuilder('\phpbb\path_helper')
106            ->setConstructorArgs([
107                new \phpbb\symfony_request(
108                    new phpbb_mock_request()
109                ),
110                $this->createMock('\phpbb\request\request'),
111                self::$phpbb_root_path,
112                'php',
113                'adm/'
114            ])
115            ->onlyMethods(['get_web_root_path'])
116            ->getMock();
117        $path_helper->method('get_web_root_path')
118            ->willReturn('/var/www/phpbb/app.php/');
119        $this->assertEquals('/var/www/phpbb/app.php/foo', $path_helper->update_web_root_path(self::$phpbb_root_path . 'app.php/foo'));
120    }
121
122    public static function update_web_root_path_data()
123    {
124        self::set_phpbb_root_path();
125
126        return array(
127            array(
128                self::$phpbb_root_path . 'test.php',
129                '/',
130                '',
131                '',
132                '',
133            ),
134            array(
135                self::$phpbb_root_path . 'test.php',
136                '//',
137                'foo/bar.php',
138                'bar.php',
139                './../',
140            ),
141            array(
142                self::$phpbb_root_path . 'test.php',
143                '/foo/template',
144                '/phpbb-fork/phpBB/app.php/foo/template',
145                '/phpbb-fork/phpBB/app.php',
146                './../../',
147            ),
148            array(
149                self::$phpbb_root_path . 'test.php',
150                '/foo/template',
151                '/phpbb-fork/phpBB/foo/template',
152                '/phpbb-fork/phpBB/app.php',
153                './../',
154            ),
155            array(
156                self::$phpbb_root_path . 'test.php',
157                '/',
158                '/phpbb-fork/phpBB/app.php/',
159                '/phpbb-fork/phpBB/app.php',
160                './../',
161            ),
162
163            // No correction if the path is already prepend by the web root path
164            array(
165                './../' . self::$phpbb_root_path . 'test.php',
166                '//',
167                'foo/bar.php',
168                'bar.php',
169                '',
170            ),
171            array(
172                './../../' . self::$phpbb_root_path . 'test.php',
173                '/foo/template',
174                '/phpbb-fork/phpBB/app.php/foo/template',
175                '/phpbb-fork/phpBB/app.php',
176                '',
177            ),
178            array(
179                './../' . self::$phpbb_root_path . 'test.php',
180                '/foo/template',
181                '/phpbb-fork/phpBB/foo/template',
182                '/phpbb-fork/phpBB/app.php',
183                '',
184            ),
185            array(
186                './../'.self::$phpbb_root_path . 'test.php',
187                '/',
188                '/phpbb-fork/phpBB/app.php/',
189                '/phpbb-fork/phpBB/app.php',
190                '',
191            ),
192            array(
193                './../'.self::$phpbb_root_path . 'test.php',
194                '',
195                '/phpbb-fork/phpBB/foo',
196                '/phpbb-fork/phpBB/app.php',
197                '',
198            ),
199        );
200    }
201
202    /**
203    * @dataProvider update_web_root_path_data
204    */
205    public function test_update_web_root_path($input, $getPathInfo, $getRequestUri, $getScriptName, $correction)
206    {
207        $symfony_request = $this->createMock('\phpbb\symfony_request');
208        $symfony_request->expects($this->any())
209            ->method('getPathInfo')
210            ->will($this->returnValue($getPathInfo));
211        $symfony_request->expects($this->any())
212            ->method('getRequestUri')
213            ->will($this->returnValue($getRequestUri));
214        $symfony_request->expects($this->any())
215            ->method('getScriptName')
216            ->will($this->returnValue($getScriptName));
217
218        $path_helper = new \phpbb\path_helper(
219            $symfony_request,
220            $this->createMock('\phpbb\request\request'),
221            self::$phpbb_root_path,
222            'php'
223        );
224
225        $this->assertEquals($correction . $input, $path_helper->update_web_root_path($input));
226    }
227
228    public static function remove_web_root_path_data()
229    {
230        self::set_phpbb_root_path();
231
232        return [
233            [
234                'web/root/path/some_url',
235                'web/root/path/some_url'
236            ],
237            [
238                '/var/www/phpbb/test.php',
239                self::$phpbb_root_path . 'test.php'
240            ]
241        ];
242    }
243
244    /**
245     * @dataProvider remove_web_root_path_data
246     */
247    public function test_remove_web_root_path($input, $expected)
248    {
249        $path_helper = $this->getMockBuilder('\phpbb\path_helper')
250            ->setConstructorArgs([
251                new \phpbb\symfony_request(
252                    new phpbb_mock_request()
253                ),
254                $this->createMock('\phpbb\request\request'),
255                self::$phpbb_root_path,
256                'php',
257                'adm/'
258            ])
259            ->onlyMethods(['get_web_root_path'])
260            ->getMock();
261        $path_helper->method('get_web_root_path')
262            ->willReturn('/var/www/phpbb/');
263
264        $this->assertEquals($expected, $path_helper->remove_web_root_path($input));
265    }
266
267    public static function clean_url_data()
268    {
269        return array(
270            array('', ''),
271            array('://', '://'),
272            array('http://', 'http://'),
273            array('http://one/two/three', 'http://one/two/three'),
274            array('http://../one/two', 'http://../one/two'),
275            array('http://one/../two/three', 'http://two/three'),
276            array('http://one/two/../three', 'http://one/three'),
277            array('http://one/two/../../three', 'http://three'),
278            array('http://one/two/../../../three', 'http://../three'),
279        );
280    }
281
282    /**
283    * @dataProvider clean_url_data
284    */
285    public function test_clean_url($input, $expected)
286    {
287        $this->assertEquals($expected, $this->path_helper->clean_url($input));
288    }
289
290    public static function glue_url_params_data()
291    {
292        return array(
293            array(
294                array(),
295                '',
296            ),
297            array(
298                array('test' => 'xyz'),
299                'test=xyz',
300            ),
301            array(
302                array('test' => 'xyz', 'var' => 'value'),
303                'test=xyz&amp;var=value',
304            ),
305            array(
306                array('test' => null),
307                'test',
308            ),
309            array(
310                array('test' => null, 'var' => null),
311                'test&amp;var',
312            ),
313            array(
314                array('test' => 'xyz', 'var' => null, 'bar' => 'value'),
315                'test=xyz&amp;var&amp;bar=value',
316            ),
317        );
318    }
319
320    /**
321    * @dataProvider glue_url_params_data
322    */
323    public function test_glue_url_params($params, $expected)
324    {
325        $this->assertEquals($expected, $this->path_helper->glue_url_params($params));
326    }
327
328    public static function get_url_parts_data()
329    {
330        return array(
331            array(
332                'viewtopic.php',
333                true,
334                array('base' => 'viewtopic.php', 'params' => array()),
335            ),
336            array(
337                './viewtopic.php?t=5&amp;f=6',
338                true,
339                array('base' => './viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
340            ),
341            array(
342                'viewtopic.php?t=5&f=6',
343                false,
344                array('base' => 'viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
345            ),
346            array(
347                'https://phpbb.com/community/viewtopic.php?t=5&amp;f=6',
348                true,
349                array('base' => 'https://phpbb.com/community/viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
350            ),
351            array(
352                'test.php?topic=post=5&amp;f=3',
353                true,
354                array('base' => 'test.php', 'params' => array('topic' => 'post=5', 'f' => '3')),
355            ),
356            array(
357                'mcp.php?&amp;t=4&amp;f=3',
358                true,
359                array('base' => 'mcp.php', 'params' => array('t' => '4', 'f' => '3')),
360            ),
361            array(
362                'mcp.php?=4&amp;f=3',
363                true,
364                array('base' => 'mcp.php', 'params' => array('f' => '3')),
365            ),
366            array(
367                'index.php?ready',
368                false,
369                array('base' => 'index.php', 'params' => array('ready' => null)),
370            ),
371            array(
372                'index.php?i=1&amp;ready',
373                true,
374                array('base' => 'index.php', 'params' => array('i' => '1', 'ready' => null)),
375            ),
376            array(
377                'index.php?ready&i=1',
378                false,
379                array('base' => 'index.php', 'params' => array('ready' => null, 'i' => '1')),
380            ),
381        );
382    }
383
384    /**
385    * @dataProvider get_url_parts_data
386    */
387    public function test_get_url_parts($url, $is_amp, $expected)
388    {
389        $this->assertEquals($expected, $this->path_helper->get_url_parts($url, $is_amp));
390    }
391
392    public static function strip_url_params_data()
393    {
394        return array(
395            array(
396                'viewtopic.php',
397                'sid',
398                false,
399                'viewtopic.php',
400            ),
401            array(
402                './viewtopic.php?t=5&amp;f=6',
403                'f',
404                true,
405                './viewtopic.php?t=5',
406            ),
407            array(
408                'viewtopic.php?t=5&f=6&sid=19adc288814103cbb4625e74e77455aa',
409                array('t'),
410                false,
411                'viewtopic.php?f=6&amp;sid=19adc288814103cbb4625e74e77455aa',
412            ),
413            array(
414                'https://phpbb.com/community/viewtopic.php?t=5&amp;f=6',
415                array('t', 'f'),
416                true,
417                'https://phpbb.com/community/viewtopic.php',
418            ),
419        );
420    }
421
422    /**
423    * @dataProvider strip_url_params_data
424    */
425    public function test_strip_url_params($url, $strip, $is_amp, $expected)
426    {
427        $this->assertEquals($expected, $this->path_helper->strip_url_params($url, $strip, $is_amp));
428    }
429
430    public static function append_url_params_data()
431    {
432        return array(
433            array(
434                'viewtopic.php',
435                array(),
436                false,
437                'viewtopic.php',
438            ),
439            array(
440                './viewtopic.php?t=5&amp;f=6',
441                array('t' => '7'),
442                true,
443                './viewtopic.php?t=7&amp;f=6',
444            ),
445            array(
446                'viewtopic.php?t=5&f=6&sid=19adc288814103cbb4625e74e77455aa',
447                array('p' => '5'),
448                false,
449                'viewtopic.php?t=5&amp;f=6&amp;p=5&amp;sid=19adc288814103cbb4625e74e77455aa',
450            ),
451            array(
452                'https://phpbb.com/community/viewtopic.php',
453                array('t' => '7', 'f' => '8'),
454                true,
455                'https://phpbb.com/community/viewtopic.php?t=7&amp;f=8',
456            ),
457        );
458    }
459
460    public function test_get_web_root_path_ajax()
461    {
462        $symfony_request = $this->getMockBuilder('\phpbb\symfony_request')
463            ->setConstructorArgs([new phpbb_mock_request()])
464            ->onlyMethods(['get', 'getSchemeAndHttpHost', 'getBasePath', 'getPathInfo'])
465            ->getMock();
466        $symfony_request->method('getSchemeAndHttpHost')
467            ->willReturn('http://www.phpbb.com');
468        $symfony_request->method('getBasePath')
469            ->willReturn('/community');
470        $symfony_request->expects($this->any())
471            ->method('getPathInfo')
472            ->will($this->returnValue('foo/bar'));
473
474        $request = $this->createMock('phpbb\request\request');
475        $request->method('is_ajax')
476            ->willReturn(true);
477        $request->method('escape')
478            ->willReturnArgument(0);
479        $request->method('header')
480            ->with('Referer')
481            ->willReturn('http://www.phpbb.com/community/route1/route2/');
482
483        $path_helper = new \phpbb\path_helper(
484            $symfony_request,
485            $request,
486            self::$phpbb_root_path,
487            'php',
488            'adm/'
489        );
490
491        $this->assertEquals(self::$phpbb_root_path . '../../', $path_helper->get_web_root_path());
492    }
493
494    /**
495    * @dataProvider append_url_params_data
496    */
497    public function test_append_url_params($url, $params, $is_amp, $expected)
498    {
499        $this->assertEquals($expected, $this->path_helper->append_url_params($url, $params, $is_amp));
500    }
501
502    public static function get_web_root_path_from_ajax_referer_data()
503    {
504        return [
505            [
506                'http://www.phpbb.com/community/route1/route2/',
507                'http://www.phpbb.com/community',
508                '../../',
509            ],
510            [
511                'http://www.phpbb.com/community/route1/route2/?f=9',
512                'http://www.phpbb.com/community',
513                '../../',
514            ],
515            [
516                'http://www.phpbb.com/community/route1/route2',
517                'http://www.phpbb.com/community',
518                '../',
519            ],
520            [
521                'http://www.phpbb.com/community/route1',
522                'http://www.phpbb.com/community',
523                '',
524            ],
525            [
526                'http://www.phpbb.com/community/',
527                'http://www.phpbb.com/community',
528                '',
529            ],
530            [
531                'http://www.phpbb.com/notcommunity/route1/route2/',
532                'http://www.phpbb.com/community',
533                '../../../community/',
534            ],
535            [
536                'http://www.phpbb.com/notcommunity/route1/route2/?f=9',
537                'http://www.phpbb.com/community',
538                '../../../community/',
539            ],
540            [
541                'http://www.phpbb.com/notcommunity/route1/route2',
542                'http://www.phpbb.com/community',
543                '../../community/',
544            ],
545            [
546                'http://www.phpbb.com/notcommunity/route1',
547                'http://www.phpbb.com/community',
548                '../community/',
549            ],
550            [
551                'http://www.phpbb.com/notcommunity/',
552                'http://www.phpbb.com/community',
553                '../community/',
554            ],
555            [
556                'http://www.phpbb.com/foobar',
557                'http://www.phpbb.com',
558                '',
559            ],
560            [
561                'http://www.foobar.com',
562                'http://www.phpbb.com',
563                '/www.phpbb.com/',
564            ],
565            [
566                'foobar',
567                'http://www.phpbb.com/community',
568                '',
569            ],
570            [
571                'https://www.phpbb.com',
572                'https://www.phpbb.com',
573                ''
574            ]
575        ];
576    }
577
578    /**
579    * @dataProvider get_web_root_path_from_ajax_referer_data
580    */
581    public function test_get_web_root_path_from_ajax_referer($referer_url, $board_url, $expected)
582    {
583        $this->assertEquals(self::$phpbb_root_path . $expected, $this->path_helper->get_web_root_path_from_ajax_referer($referer_url, $board_url));
584    }
585
586    public static function data_get_valid_page()
587    {
588        return array(
589            // array( current page , mod_rewrite setting , expected output )
590            array('index', true, 'index'),
591            array('index', false, 'index'),
592            array('foo/index', true, 'foo/index'),
593            array('foo/index', false, 'foo/index'),
594            array('app.php/foo', true, 'foo'),
595            array('app.php/foo', false, 'app.php/foo'),
596            array('/../app.php/foo', true, '../foo'),
597            array('/../app.php/foo', false, '../app.php/foo'),
598            array('/../example/app.php/foo/bar', true, '../example/foo/bar'),
599            array('/../example/app.php/foo/bar', false, '../example/app.php/foo/bar'),
600        );
601    }
602
603    /**
604     * @dataProvider data_get_valid_page
605     */
606    public function test_get_valid_page($page, $mod_rewrite, $expected)
607    {
608        $this->assertEquals(self::$phpbb_root_path . $expected, $this->path_helper->get_valid_page($page, $mod_rewrite));
609    }
610
611    public static function is_router_used_data()
612    {
613        return [
614            [
615                'index.php',
616                false,
617            ],
618            [
619                'app.php',
620                true,
621            ],
622        ];
623    }
624
625    /**
626     * @dataProvider is_router_used_data
627     */
628    public function test_is_router_used($script_name, $expected)
629    {
630        $symfony_request = $this->getMockBuilder('\phpbb\symfony_request')
631            ->setConstructorArgs([new phpbb_mock_request()])
632            ->onlyMethods(['getScriptName'])
633            ->getMock();
634        $symfony_request->method('getScriptName')
635            ->willReturn($script_name);
636
637        $path_helper = new \phpbb\path_helper(
638            $symfony_request,
639            $this->createMock('\phpbb\request\request'),
640            self::$phpbb_root_path,
641            'php',
642            'adm/'
643        );
644
645        $this->assertSame($expected, $path_helper->is_router_used());
646    }
647}