Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.58% covered (warning)
81.58%
62 / 76
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_session_check_ban_test
81.58% covered (warning)
81.58%
62 / 76
80.00% covered (warning)
80.00%
4 / 5
6.23
0.00% covered (danger)
0.00%
0 / 1
 getDataSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 check_banned_data
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 setUp
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
1
 tearDown
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 test_check_is_banned
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
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
14require_once __DIR__ . '/../test_framework/phpbb_session_test_case.php';
15
16class phpbb_session_check_ban_test extends phpbb_session_test_case
17{
18    protected $user_id = 4;
19    protected $key_id = 4;
20    /** @var \phpbb\session */
21    protected $session;
22    protected $backup_cache;
23
24    public function getDataSet()
25    {
26        return $this->createXMLDataSet(__DIR__ . '/fixtures/sessions_banlist.xml');
27    }
28
29    static function check_banned_data(): array
30    {
31        return [
32            'not_banned' => [
33                'All false values, should not be banned',
34                false, false, false, false, /* should be banned? -> */ false
35            ],
36            'banned' => [
37                'Matching values in the database, should be banned',
38                4, '127.0.0.1', 'bar@example.org', true, /* should be banned? -> */ true
39            ],
40            'banned_with_trigger_error' => [
41                'Matching values in the database, should be banned',
42                4, '127.0.0.1', 'bar@example.org', false, /* should be banned? -> */ true
43            ],
44        ];
45    }
46
47    protected function setUp(): void
48    {
49        parent::setUp();
50        // Get session here so that config is mocked correctly
51        $this->session = $this->session_factory->get_session($this->db);
52        $this->session->data['user_id'] = ANONYMOUS; // Don't get into the session_kill() procedure
53        $this->session->lang = [
54            'BOARD_BAN_TIME'    => 'BOARD_BAN_TIME',
55            'BOARD_BAN_PERM'    => 'BOARD_BAN_PERM',
56            'BOARD_BAN_REASON'    => 'BOARD_BAN_REASON',
57            'BAN_TRIGGERED_BY_EMAIL'    => 'BAN_TRIGGERED_BY_EMAIL',
58            'BAN_TRIGGERED_BY_IP'        => 'BAN_TRIGGERED_BY_IP',
59            'BAN_TRIGGERED_BY_USER'        => 'BAN_TRIGGERED_BY_USER',
60        ];
61
62        global $cache, $config, $phpbb_root_path, $phpEx, $phpbb_filesystem, $phpbb_container, $user;
63
64        $language = new phpbb\language\language(new phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
65        $user = new \phpbb\user($language, '\phpbb\datetime');
66
67        $phpbb_filesystem = new \phpbb\filesystem\filesystem();
68
69        $this->backup_cache = $cache;
70
71        // Event dispatcher
72        $phpbb_dispatcher = new phpbb_mock_event_dispatcher();
73
74        // Change the global cache object for this test because
75        // the mock cache object does not hit the database as is needed
76        // for this test.
77        $cache = new \phpbb\cache\service(
78            new \phpbb\cache\driver\dummy(),
79            $config,
80            $this->db,
81            $phpbb_dispatcher,
82            $phpbb_root_path,
83            $phpEx
84        );
85
86        $phpbb_container = new phpbb_mock_container_builder();
87        $ban_type_email = new \phpbb\ban\type\email($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys');
88        $ban_type_user = new \phpbb\ban\type\user($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys');
89        $ban_type_ip = new \phpbb\ban\type\ip($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys');
90        $phpbb_container->set('ban.type.email', $ban_type_email);
91        $phpbb_container->set('ban.type.user', $ban_type_user);
92        $phpbb_container->set('ban.type.ip', $ban_type_ip);
93        $collection = new \phpbb\di\service_collection($phpbb_container);
94        $collection->add('ban.type.email');
95        $collection->add('ban.type.user');
96        $collection->add('ban.type.ip');
97        $phpbb_log = new \phpbb\log\dummy();
98
99        $ban_manager = new \phpbb\ban\manager($collection, $cache->get_driver(), $this->db, $language, $phpbb_log, $user, 'phpbb_bans', 'phpbb_users');
100        $phpbb_container->set('ban.manager', $ban_manager);
101
102        $phpbb_container->set(
103            'auth.provider.db',
104            new phpbb_mock_auth_provider()
105        );
106        $provider_collection = new \phpbb\auth\provider_collection($phpbb_container, $config);
107        $provider_collection->add('auth.provider.db');
108        $phpbb_container->set(
109            'auth.provider_collection',
110            $provider_collection
111        );
112    }
113
114    protected function tearDown(): void
115    {
116        parent::tearDown();
117        // Set cache back to what it was before the test changed it
118        global $cache;
119        $cache = $this->backup_cache;
120    }
121
122    /** @dataProvider check_banned_data */
123    public function test_check_is_banned($test_msg, $user_id, $user_ips, $user_email, $return, $should_be_banned)
124    {
125        $is_banned = false;
126
127        // Set a custom handler to "catch" the notice
128        set_error_handler(function ($errno, $errstr) use (&$is_banned) {
129            $is_banned = true;
130            return true; // Stop PHP from handling the error further
131        }, E_USER_NOTICE);
132
133        $ban = $this->session->check_ban($user_id, $user_ips, $user_email, $return);
134
135        // Restore the default handler immediately after the call
136        restore_error_handler();
137
138        $is_banned = !empty($ban) || $is_banned;
139
140        $this->assertEquals($should_be_banned, $is_banned, $test_msg);
141    }
142}