Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.74% covered (warning)
54.74%
52 / 95
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_viewonline_helper_test
54.74% covered (warning)
54.74%
52 / 95
60.00% covered (warning)
60.00%
3 / 5
7.32
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
1 / 1
1
 session_pages_data
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_user_page
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_location_data
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
2
 test_get_location
100.00% covered (success)
100.00%
6 / 6
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
14class phpbb_viewonline_helper_test extends phpbb_test_case
15{
16    protected $viewonline_helper;
17    private $auth;
18
19    protected function setUp(): void
20    {
21        global $phpbb_dispatcher;
22
23        parent::setUp();
24
25        $phpbb_root_path = __DIR__ . '/../../phpBB/';
26        $phpEx = 'php';
27
28        $db = $this->getMockBuilder('\phpbb\db\driver\driver_interface')
29            ->disableOriginalConstructor()
30            ->getMock();
31
32        // Mock database to return forum data
33        $forum_data = [
34            'forum_id' => 1,
35            'forum_name' => 'Test Forum',
36            'parent_id' => 0,
37            'forum_type' => FORUM_POST,
38            'left_id' => 1,
39            'right_id' => 2,
40        ];
41
42        $db->method('sql_fetchrow')->willReturnOnConsecutiveCalls($forum_data, false);
43
44        $config = new \phpbb\config\config([]);
45
46        $dispatcher = new phpbb_mock_event_dispatcher();
47        $phpbb_dispatcher = $dispatcher;
48
49
50        $container = new phpbb_mock_container_builder();
51        $container->setParameter('core.environment', PHPBB_ENVIRONMENT);
52
53        $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader(
54            new \phpbb\routing\file_locator($phpbb_root_path)
55        );
56
57        $extension_manager = new phpbb_mock_extension_manager($phpbb_root_path);
58        $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(
59            $phpbb_root_path,
60            PHPBB_ENVIRONMENT,
61            $extension_manager
62        );
63
64        // Create router
65        $router = new phpbb_mock_router($container, $resources_locator, $loader, $phpEx, './', '', '');
66
67        // Mock controller helper to use the real router for generating route URLs
68        $controller_helper = $this->getMockBuilder('\phpbb\controller\helper')
69            ->disableOriginalConstructor()
70            ->getMock();
71        $controller_helper->method('route')
72            ->willReturnCallback(function($route) use ($router) {
73                // Use the real router to generate the actual URL for the route
74                return '.' . $router->generate($route);
75            });
76
77        $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
78
79        $this->auth = $this->getMockBuilder('\phpbb\auth\auth')
80            ->disableOriginalConstructor()
81            ->getMock();
82
83        $this->viewonline_helper = new \phpbb\members\viewonline_helper($db, $config, $dispatcher, $router, $controller_helper, $language, $this->auth, './', $phpEx, 'adm/', '%tables.users%', '%tables.sessions%', '%tables.topics%', '%tables.forums%');
84    }
85
86    public static function session_pages_data()
87    {
88        return array(
89            array('index.php', 'index'),
90            array('foobar/test.php', 'foobar/test'),
91            array('', ''),
92            array('./../../index.php', '../../index'),
93            array('../subdir/index.php', '../subdir/index'),
94            array('../index.php', '../index'),
95            array('././index.php', 'index'),
96            array('./index.php', 'index'),
97        );
98    }
99
100    /**
101    * @dataProvider session_pages_data
102    */
103    public function test_get_user_page($session_page, $expected)
104    {
105        $on_page = $this->viewonline_helper->get_user_page($session_page);
106        $this->assertArrayHasKey(1, $on_page);
107        $this->assertSame($expected, $on_page[1]);
108    }
109
110    public static function get_location_data()
111    {
112        return array(
113            // Route-based pages - routes have prefixes as defined in routing.yml
114            // help.yml routes have prefix /help
115            array('/help/bbcode', 0, true, 'Viewing FAQ', './help/faq'),
116            array('/help/faq', 0, true, 'Viewing FAQ', './help/faq'),
117            // members.yml routes have prefix /members
118            array('/members/online', 0, true, 'Viewing who is online', './members/online'),
119            array('/members/online/whois/abc123', 0, true, 'Viewing who is online', './members/online'),
120            array('/members/team', 0, true, 'Viewing member details', './memberlist.php'),
121            // report.yml routes have no prefix
122            array('/pm/5/report', 0, true, 'Reporting post', './index.php'),
123            array('/post/10/report', 0, true, 'Reporting post', './index.php'),
124
125            // Legacy pages - index
126            array('index.php', 0, true, 'Index page', './index.php'),
127            array('./index.php', 0, true, 'Index page', './index.php'),
128
129            // Legacy pages - admin
130            array('adm/index.php', 0, true, 'Administration Control Panel', './index.php'),
131
132            // Legacy pages - search
133            array('search.php', 0, true, 'Searching forums', './search.php'),
134
135            // Legacy pages - memberlist
136            array('memberlist.php', 0, true, 'Viewing member details', './memberlist.php'),
137            array('memberlist.php?mode=viewprofile&u=2', 0, true, 'Viewing member profile', './memberlist.php'),
138            array('memberlist.php?mode=contactadmin', 0, true, 'Viewing contact page', './memberlist.php?mode=contactadmin'),
139
140            // Legacy pages - MCP
141            array('mcp.php', 0, true, 'Viewing moderator control panel', './index.php'),
142
143            // Legacy pages - UCP
144            array('ucp.php', 0, true, 'Viewing user control panel', './index.php'),
145            array('ucp.php?mode=register', 0, true, 'Registering account', './index.php'),
146            array('ucp.php?i=pm&mode=compose', 0, true, 'Composing private message', './index.php'),
147            array('ucp.php?i=pm&mode=view', 0, true, 'Viewing private messages', './index.php'),
148            array('ucp.php?i=prefs&mode=view', 0, true, 'Changing board preferences', './index.php'),
149            array('ucp.php?i=profile&mode=edit', 0, true, 'Changing profile settings', './index.php'),
150
151            // Forum-based pages with forum access
152            array('posting.php?f=1&mode=reply', 1, true, 'Replying to message in Test Forum', './viewforum.php?f=1'),
153            array('posting.php?f=1&mode=quote', 1, true, 'Replying to message in Test Forum', './viewforum.php?f=1'),
154            array('posting.php?f=1&mode=post', 1, true, 'Posting message in Test Forum', './viewforum.php?f=1'),
155            array('viewtopic.php?f=1&t=1', 1, true, 'Reading topic in Test Forum', './viewforum.php?f=1'),
156            array('viewforum.php?f=1', 1, true, 'Viewing topics in Test Forum', './viewforum.php?f=1'),
157
158            // Forum-based pages without forum access - should redirect to index
159            array('posting.php?f=1&mode=reply', 1, false, 'Index page', './index.php'),
160            array('viewtopic.php?f=1&t=1', 1, false, 'Index page', './index.php'),
161            array('viewforum.php?f=1', 1, false, 'Index page', './index.php'),
162
163            // Unknown route
164            array('/unknown/route', 0, true, 'Index page', './index.php'),
165
166            // Empty page
167            array('', 0, true, 'Index page', './index.php'),
168        );
169    }
170
171    /**
172     * @dataProvider get_location_data
173     */
174    public function test_get_location($session_page, $forum_id, $has_access, $expected_location, $expected_url)
175    {
176        $this->auth->method('acl_get')->willReturn($has_access);
177
178        list($location, $location_url) = $this->viewonline_helper->get_location($session_page, $forum_id);
179
180        $this->assertIsString($location);
181        $this->assertIsString($location_url);
182
183        $this->assertEquals($expected_location, $location, "Location mismatch for page: {$session_page}");
184        $this->assertEquals($expected_url, $location_url, "URL mismatch for page: {$session_page}");
185    }
186}