Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.81% |
83 / 84 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_session_testable_factory | |
98.81% |
83 / 84 |
|
90.91% |
10 / 11 |
11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
| get_session | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
1 | |||
| set_cookies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| merge_config_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_config_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| merge_cache_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_cache_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| merge_server_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| merge_test_data | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| get_server_data | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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 | |
| 14 | require_once __DIR__ . '/../mock/container_builder.php'; |
| 15 | require_once __DIR__ . '/../mock/auth_provider.php'; |
| 16 | |
| 17 | /** |
| 18 | * This class exists to setup an instance of phpbb's session class for testing. |
| 19 | * |
| 20 | * The session class has rather complex dependencies, so in order to make its |
| 21 | * tests more * understandable and to make its dependencies more visible this |
| 22 | * factory class sets up all the necessary global state & variable contents. |
| 23 | */ |
| 24 | class phpbb_session_testable_factory |
| 25 | { |
| 26 | protected $container; |
| 27 | protected $config_data; |
| 28 | protected $cache_data; |
| 29 | protected $cookies; |
| 30 | |
| 31 | protected $config; |
| 32 | protected $cache; |
| 33 | protected $request; |
| 34 | protected $server_data; |
| 35 | |
| 36 | /** |
| 37 | * Initialises the factory with a set of default config and cache values. |
| 38 | */ |
| 39 | public function __construct() |
| 40 | { |
| 41 | // default configuration values |
| 42 | $this->config_data = [ |
| 43 | 'allow_autologin' => false, |
| 44 | 'auth_method' => 'db', |
| 45 | 'forwarded_for_check' => true, |
| 46 | 'active_sessions' => 0, // disable |
| 47 | 'rand_seed' => 'foo', |
| 48 | 'rand_seed_last_update' => 0, |
| 49 | 'max_autologin_time' => 0, |
| 50 | 'session_length' => 100, |
| 51 | 'form_token_lifetime' => 100, |
| 52 | 'cookie_name' => '', |
| 53 | 'limit_load' => 0, |
| 54 | 'limit_search_load' => 0, |
| 55 | 'ip_check' => 3, |
| 56 | 'browser_check' => 1, |
| 57 | ]; |
| 58 | |
| 59 | $this->cache_data = [ |
| 60 | '_bots' => [], |
| 61 | '_ban_info' => [], |
| 62 | ]; |
| 63 | |
| 64 | $this->cookies = []; |
| 65 | |
| 66 | $this->server_data = $_SERVER; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Retrieve the configured session class instance |
| 71 | * |
| 72 | * @param \phpbb\db\driver\driver_interface $dbal The database connection to use for session data |
| 73 | * @return phpbb_mock_session_testable A session instance |
| 74 | */ |
| 75 | public function get_session(\phpbb\db\driver\driver_interface $dbal) |
| 76 | { |
| 77 | // set up all the global variables used by session |
| 78 | global $SID, $_SID, $db, $config, $cache, $request, $phpbb_container, $phpbb_dispatcher; |
| 79 | global $user, $phpbb_root_path, $phpEx; |
| 80 | |
| 81 | $request = $this->request = new phpbb_mock_request( |
| 82 | array(), |
| 83 | array(), |
| 84 | $this->cookies, |
| 85 | $this->server_data |
| 86 | ); |
| 87 | |
| 88 | $config = $this->config = new \phpbb\config\config($this->get_config_data()); |
| 89 | |
| 90 | $db = $dbal; |
| 91 | |
| 92 | $cache = $this->cache = new phpbb_mock_cache($this->get_cache_data()); |
| 93 | $SID = $_SID = null; |
| 94 | $language = new phpbb\language\language(new phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 95 | $user = new \phpbb\user($language, '\phpbb\datetime'); |
| 96 | |
| 97 | $phpbb_container = $this->container = new phpbb_mock_container_builder(); |
| 98 | $phpbb_container->set( |
| 99 | 'auth.provider.db', |
| 100 | new phpbb_mock_auth_provider() |
| 101 | ); |
| 102 | $phpbb_container->setParameter('core.environment', PHPBB_ENVIRONMENT); |
| 103 | $phpbb_container->setParameter('core.cache_dir', $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/'); |
| 104 | $provider_collection = new \phpbb\auth\provider_collection($phpbb_container, $config); |
| 105 | $provider_collection->add('auth.provider.db'); |
| 106 | $phpbb_container->set( |
| 107 | 'auth.provider_collection', |
| 108 | $provider_collection |
| 109 | ); |
| 110 | |
| 111 | $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); |
| 112 | |
| 113 | $ban_type_email = new \phpbb\ban\type\email($db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 114 | $ban_type_user = new \phpbb\ban\type\user($db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 115 | $ban_type_ip = new \phpbb\ban\type\ip($db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 116 | $phpbb_container->set('ban.type.email', $ban_type_email); |
| 117 | $phpbb_container->set('ban.type.user', $ban_type_user); |
| 118 | $phpbb_container->set('ban.type.ip', $ban_type_ip); |
| 119 | |
| 120 | $collection = new \phpbb\di\service_collection($phpbb_container); |
| 121 | $collection->add('ban.type.email'); |
| 122 | $collection->add('ban.type.user'); |
| 123 | $collection->add('ban.type.ip'); |
| 124 | $phpbb_log = new \phpbb\log\dummy(); |
| 125 | |
| 126 | $ban_manager = new \phpbb\ban\manager($collection, $cache, $db, $language, $phpbb_log, $user,'phpbb_bans', 'phpbb_users'); |
| 127 | $phpbb_container->set('ban.manager', $ban_manager); |
| 128 | |
| 129 | $session = new phpbb_mock_session_testable; |
| 130 | return $session; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Set the cookies which should be present in the request data. |
| 135 | * |
| 136 | * @param array $cookies The cookie data, structured like $_COOKIE contents. |
| 137 | */ |
| 138 | public function set_cookies(array $cookies) |
| 139 | { |
| 140 | $this->cookies = $cookies; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check if the cache used for the generated session contains correct data. |
| 145 | * |
| 146 | * @param PHPUnit\Framework\Assert $test The test case to call assert methods |
| 147 | * on |
| 148 | */ |
| 149 | public function check(PHPUnit\Framework\Assert $test) |
| 150 | { |
| 151 | $this->cache->check($test, $this->get_cache_data()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Merge config data with the current config data to be supplied to session. |
| 156 | * |
| 157 | * New values overwrite new ones. |
| 158 | * |
| 159 | * @param array $config_data The config data to merge with previous data |
| 160 | */ |
| 161 | public function merge_config_data(array $config_data) |
| 162 | { |
| 163 | $this->config_data = array_merge($this->config_data, $config_data); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Retrieve the entire config data to be passed to the session. |
| 168 | * |
| 169 | * @return array Configuration |
| 170 | */ |
| 171 | public function get_config_data() |
| 172 | { |
| 173 | return $this->config_data; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Merge the cache contents with more data. |
| 178 | * |
| 179 | * New values overwrite old ones. |
| 180 | * |
| 181 | * @param array $cache_data The additional cache data |
| 182 | */ |
| 183 | public function merge_cache_data(array $cache_data) |
| 184 | { |
| 185 | $this->cache_data = array_merge($this->cache_data, $cache_data); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Retrieve the entire cache data to be passed to the session. |
| 190 | * |
| 191 | * @return array Cache contents |
| 192 | */ |
| 193 | public function get_cache_data() |
| 194 | { |
| 195 | return $this->cache_data; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Merge the current server info ($_SERVER) with more data. |
| 200 | * |
| 201 | * New values overwrite old ones. |
| 202 | * |
| 203 | * @param array $server_data The additional server variables |
| 204 | */ |
| 205 | public function merge_server_data($server_data) |
| 206 | { |
| 207 | return $this->server_data = array_merge($this->server_data, $server_data); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Set cookies, merge config and server data in one step. |
| 212 | * |
| 213 | * New values overwrite old ones. |
| 214 | * |
| 215 | * @param $session_id |
| 216 | * @param $user_id |
| 217 | * @param $user_agent |
| 218 | * @param $ip |
| 219 | * @param int $time |
| 220 | */ |
| 221 | public function merge_test_data($session_id, $user_id, $user_agent, $ip, $time = 0) |
| 222 | { |
| 223 | $this->set_cookies(array( |
| 224 | '_sid' => $session_id, |
| 225 | '_u' => $user_id, |
| 226 | )); |
| 227 | $this->merge_config_data(array( |
| 228 | 'session_length' => time() + $time, // need to do this to allow sessions started at time 0 |
| 229 | )); |
| 230 | $this->merge_server_data(array( |
| 231 | 'HTTP_USER_AGENT' => $user_agent, |
| 232 | 'REMOTE_ADDR' => $ip, |
| 233 | )); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Retrieve all server variables to be passed to the session. |
| 238 | * |
| 239 | * @return array Server variables |
| 240 | */ |
| 241 | public function get_server_data() |
| 242 | { |
| 243 | return $this->server_data; |
| 244 | } |
| 245 | } |
| 246 |