Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
30.98% |
184 / 594 |
|
71.43% |
15 / 21 |
CRAP | |
0.00% |
0 / 1 |
| ban_manager_test | |
30.98% |
184 / 594 |
|
71.43% |
15 / 21 |
325.96 | |
0.00% |
0 / 1 |
| getDataSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
| data_check_ban | |
0.00% |
0 / 103 |
|
0.00% |
0 / 1 |
2 | |||
| test_check_ban | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| data_get_bans | |
0.00% |
0 / 96 |
|
0.00% |
0 / 1 |
2 | |||
| test_get_bans | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| data_get_ban_end | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
2 | |||
| test_get_ban_end | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| test_get_banned_users | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| test_get_banned_users_own_method | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
1 | |||
| test_ban_empty_ban_items | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
1 | |||
| data_test_ban | |
0.00% |
0 / 126 |
|
0.00% |
0 / 1 |
2 | |||
| test_ban | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| test_ban_actual | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| data_test_unban | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
2 | |||
| test_unban | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| test_unban_invalid_type | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| test_base_type_methods | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| data_get_ban_message | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
2 | |||
| test_get_ban_message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_get_ban_options_user | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace phpbb\tests\unit\ban; |
| 5 | |
| 6 | use phpbb\ban\exception\invalid_length_exception; |
| 7 | use phpbb\ban\exception\no_valid_emails_exception; |
| 8 | use phpbb\ban\exception\no_valid_ips_exception; |
| 9 | use phpbb\ban\exception\no_valid_users_exception; |
| 10 | use phpbb\ban\exception\type_not_found_exception; |
| 11 | |
| 12 | require_once __DIR__ . '/../test_framework/phpbb_session_test_case.php'; |
| 13 | |
| 14 | class ban_manager_test extends \phpbb_session_test_case |
| 15 | { |
| 16 | protected $ban_manager; |
| 17 | |
| 18 | protected $phpbb_container; |
| 19 | |
| 20 | |
| 21 | protected function getDataSet() |
| 22 | { |
| 23 | return $this->createXMLDataSet(__DIR__ . '/fixtures/sessions_banlist.xml'); |
| 24 | } |
| 25 | |
| 26 | public function setUp(): void |
| 27 | { |
| 28 | parent::setUp(); |
| 29 | |
| 30 | global $config, $phpbb_dispatcher, $phpbb_root_path, $phpEx; |
| 31 | |
| 32 | $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 33 | $user = new \phpbb\user($language, '\phpbb\datetime'); |
| 34 | $user->data['user_id'] = 2; |
| 35 | $user->data['user_email'] = 'foo@bar.com'; |
| 36 | $user->data['user_timezone'] = 0; |
| 37 | $config = new \phpbb\config\config([]); |
| 38 | $phpbb_dispatcher = new \phpbb_mock_event_dispatcher(); |
| 39 | |
| 40 | $phpbb_container = new \phpbb_mock_container_builder(); |
| 41 | $ban_type_email = new \phpbb\ban\type\email($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 42 | $ban_type_user = new \phpbb\ban\type\user($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 43 | $ban_type_ip = new \phpbb\ban\type\ip($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 44 | $phpbb_container->set('ban.type.email', $ban_type_email); |
| 45 | $phpbb_container->set('ban.type.user', $ban_type_user); |
| 46 | $phpbb_container->set('ban.type.ip', $ban_type_ip); |
| 47 | $collection = new \phpbb\di\service_collection($phpbb_container); |
| 48 | $collection->add('ban.type.email'); |
| 49 | $collection->add('ban.type.user'); |
| 50 | $collection->add('ban.type.ip'); |
| 51 | $phpbb_log = new \phpbb\log\dummy(); |
| 52 | |
| 53 | $this->ban_manager = new \phpbb\ban\manager($collection, new \phpbb\cache\driver\dummy(), $this->db, $language, $phpbb_log, $user, 'phpbb_bans', 'phpbb_users'); |
| 54 | $phpbb_container->set('ban.manager', $this->ban_manager); |
| 55 | $this->phpbb_container = $phpbb_container; |
| 56 | } |
| 57 | |
| 58 | public static function data_check_ban(): array |
| 59 | { |
| 60 | return [ |
| 61 | [ |
| 62 | [], |
| 63 | false |
| 64 | ], |
| 65 | [ |
| 66 | ['user_ip' => '127.0.0.1'], |
| 67 | [ |
| 68 | 'item' => '127.0.0.1', |
| 69 | 'end' => '0', |
| 70 | 'reason' => '1', |
| 71 | 'mode' => 'ip', |
| 72 | ], |
| 73 | ], |
| 74 | [ |
| 75 | ['user_ip' => '10.0.0.1'], // first IP for 10.0.0.1/28 range |
| 76 | [ |
| 77 | 'item' => '10.0.0.1/28', |
| 78 | 'end' => '0', |
| 79 | 'reason' => '1', |
| 80 | 'mode' => 'ip', |
| 81 | ], |
| 82 | ], |
| 83 | [ |
| 84 | ['user_ip' => '10.0.0.14'], // last IP for 10.0.0.1/28 range |
| 85 | [ |
| 86 | 'item' => '10.0.0.1/28', |
| 87 | 'end' => '0', |
| 88 | 'reason' => '1', |
| 89 | 'mode' => 'ip', |
| 90 | ], |
| 91 | ], |
| 92 | [ |
| 93 | ['user_ip' => '10.0.0.15'], // first IP outside 10.0.0.1/28 range |
| 94 | [ |
| 95 | 'item' => '10.0.0.1/28', |
| 96 | 'end' => '0', |
| 97 | 'reason' => '1', |
| 98 | 'mode' => 'ip', |
| 99 | ], |
| 100 | ], |
| 101 | [ |
| 102 | ['user_ip' => '2001:4860:4860::8888'], // first IP in 2001:4860:4860::8888/12 range |
| 103 | [ |
| 104 | 'item' => '2001:4860:4860::8888/12', |
| 105 | 'end' => '0', |
| 106 | 'reason' => '1', |
| 107 | 'mode' => 'ip', |
| 108 | ], |
| 109 | ], |
| 110 | [ |
| 111 | ['user_ip' => '200F:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF'], // last IP in 2001:4860:4860::8888/12 range |
| 112 | [ |
| 113 | 'item' => '2001:4860:4860::8888/12', |
| 114 | 'end' => '0', |
| 115 | 'reason' => '1', |
| 116 | 'mode' => 'ip', |
| 117 | ], |
| 118 | ], |
| 119 | [ |
| 120 | ['user_ip' => '2010:4860:4860::1'], // IP outside the 2001:4860:4860::8888/12 range |
| 121 | false, |
| 122 | ], |
| 123 | [ |
| 124 | ['user_id' => 2], |
| 125 | false, |
| 126 | ], |
| 127 | [ |
| 128 | ['user_id' => 5], // there is only an expired ban |
| 129 | false, |
| 130 | ], |
| 131 | [ |
| 132 | ['user_id' => 4], |
| 133 | [ |
| 134 | 'item' => '4', |
| 135 | 'end' => '0', |
| 136 | 'reason' => '1', |
| 137 | 'mode' => 'user', |
| 138 | ], |
| 139 | ], |
| 140 | [ |
| 141 | ['user_email' => 'test@phpbb.com'], |
| 142 | false, |
| 143 | ], |
| 144 | [ |
| 145 | ['user_email' => 'bar@example.org'], |
| 146 | [ |
| 147 | 'item' => 'bar@example.org', |
| 148 | 'end' => '0', |
| 149 | 'reason' => '1', |
| 150 | 'mode' => 'email', |
| 151 | ], |
| 152 | ], |
| 153 | [ |
| 154 | ['user_email' => 'test@foo.bar'], |
| 155 | [ |
| 156 | 'item' => '*@foo.bar', |
| 157 | 'end' => '0', |
| 158 | 'reason' => '1', |
| 159 | 'mode' => 'email', |
| 160 | ], |
| 161 | ], |
| 162 | ]; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @dataProvider data_check_ban |
| 167 | */ |
| 168 | public function test_check_ban($user_data, $expected) |
| 169 | { |
| 170 | $this->assertEquals($expected, $this->ban_manager->check($user_data)); |
| 171 | } |
| 172 | |
| 173 | public static function data_get_bans(): array |
| 174 | { |
| 175 | return [ |
| 176 | [ |
| 177 | 'foo', |
| 178 | '', |
| 179 | type_not_found_exception::class |
| 180 | ], |
| 181 | [ |
| 182 | 'ip', |
| 183 | [ |
| 184 | [ |
| 185 | 'ban_id' => '6', |
| 186 | 'ban_userid' => 0, |
| 187 | 'ban_item' => '10.0.0.1/28', |
| 188 | 'ban_start' => '1111', |
| 189 | 'ban_end' => '0', |
| 190 | 'ban_reason' => 'HAHAHA', |
| 191 | 'ban_reason_display' => '1', |
| 192 | 'ban_mode' => 'ip', |
| 193 | ], |
| 194 | [ |
| 195 | 'ban_id' => '2', |
| 196 | 'ban_userid' => 0, |
| 197 | 'ban_item' => '127.0.0.1', |
| 198 | 'ban_start' => '1111', |
| 199 | 'ban_end' => '0', |
| 200 | 'ban_reason' => 'HAHAHA', |
| 201 | 'ban_reason_display' => '1', |
| 202 | 'ban_mode' => 'ip', |
| 203 | ], |
| 204 | [ |
| 205 | 'ban_id' => '3', |
| 206 | 'ban_userid' => 0, |
| 207 | 'ban_item' => '127.1.1.1', |
| 208 | 'ban_start' => '1111', |
| 209 | 'ban_end' => '0', |
| 210 | 'ban_reason' => 'HAHAHA', |
| 211 | 'ban_reason_display' => '1', |
| 212 | 'ban_mode' => 'ip', |
| 213 | ], |
| 214 | [ |
| 215 | 'ban_id' => '7', |
| 216 | 'ban_userid' => 0, |
| 217 | 'ban_item' => '2001:4860:4860::8888/12', |
| 218 | 'ban_start' => '1111', |
| 219 | 'ban_end' => '0', |
| 220 | 'ban_reason' => 'HAHAHA', |
| 221 | 'ban_reason_display' => '1', |
| 222 | 'ban_mode' => 'ip', |
| 223 | ], |
| 224 | ], |
| 225 | ], |
| 226 | [ |
| 227 | 'email', |
| 228 | [ |
| 229 | [ |
| 230 | 'ban_id' => '9', |
| 231 | 'ban_userid' => 0, |
| 232 | 'ban_item' => '*@foo.bar', |
| 233 | 'ban_start' => '1111', |
| 234 | 'ban_end' => '0', |
| 235 | 'ban_reason' => 'HAHAHA', |
| 236 | 'ban_reason_display' => '1', |
| 237 | 'ban_mode' => 'email', |
| 238 | ], |
| 239 | [ |
| 240 | 'ban_id' => '5', |
| 241 | 'ban_userid' => 0, |
| 242 | 'ban_item' => 'bar@example.org', |
| 243 | 'ban_start' => '1111', |
| 244 | 'ban_end' => '0', |
| 245 | 'ban_reason' => 'HAHAHA', |
| 246 | 'ban_reason_display' => '1', |
| 247 | 'ban_mode' => 'email', |
| 248 | ], |
| 249 | ], |
| 250 | ], |
| 251 | [ |
| 252 | 'user', |
| 253 | [ |
| 254 | [ |
| 255 | 'ban_id' => '4', |
| 256 | 'ban_item' => '4', |
| 257 | 'ban_start' => '1111', |
| 258 | 'ban_end' => '0', |
| 259 | 'ban_reason' => 'HAHAHA', |
| 260 | 'ban_reason_display' => '1', |
| 261 | 'ban_mode' => 'user', |
| 262 | 'ban_userid' => 4, |
| 263 | 'user_id' => '4', |
| 264 | 'username' => 'ipv6_user', |
| 265 | 'username_clean' => 'ipv6_user', |
| 266 | 'label' => 'ipv6_user', |
| 267 | ], |
| 268 | ], |
| 269 | ], |
| 270 | ]; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @dataProvider data_get_bans |
| 275 | */ |
| 276 | public function test_get_bans($ban_type, $expected, $expected_exception = false) |
| 277 | { |
| 278 | if ($expected_exception !== false) |
| 279 | { |
| 280 | $this->expectException($expected_exception); |
| 281 | } |
| 282 | |
| 283 | $actual = $this->ban_manager->get_bans($ban_type); |
| 284 | // Sort both arrays by ban_item to be synced |
| 285 | if (is_array($expected) && !empty($actual)) |
| 286 | { |
| 287 | usort($expected, function($a, $b) |
| 288 | { |
| 289 | return strcmp($a['ban_item'], $b['ban_item']) <=> 0; |
| 290 | } |
| 291 | ); |
| 292 | usort($actual, function($a, $b) |
| 293 | { |
| 294 | return strcmp($a['ban_item'], $b['ban_item']) <=> 0; |
| 295 | } |
| 296 | ); |
| 297 | } |
| 298 | $this->assertEquals($expected, $actual); |
| 299 | } |
| 300 | |
| 301 | public static function data_get_ban_end(): array |
| 302 | { |
| 303 | return [ |
| 304 | [ |
| 305 | 0, |
| 306 | 20, |
| 307 | 0, |
| 308 | ], |
| 309 | [ |
| 310 | 80, // 1 minute plus 20 seconds |
| 311 | 20, |
| 312 | 1, |
| 313 | ], |
| 314 | [ |
| 315 | 20, |
| 316 | 20, |
| 317 | -1, |
| 318 | ], |
| 319 | [ |
| 320 | 2 * 86400, // Ban end should be before this time |
| 321 | 20, |
| 322 | -1, |
| 323 | '1970-01-02', |
| 324 | ], |
| 325 | [ |
| 326 | 0, |
| 327 | 20, |
| 328 | -1, |
| 329 | '1970-01-02-15:30', // wrong format |
| 330 | invalid_length_exception::class, |
| 331 | ], |
| 332 | ]; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @dataProvider data_get_ban_end |
| 337 | */ |
| 338 | public function test_get_ban_end($expected, $ban_start, $length, $end_date = '', $expected_exception = false) |
| 339 | { |
| 340 | if ($expected_exception) |
| 341 | { |
| 342 | $this->expectException($expected_exception); |
| 343 | } |
| 344 | |
| 345 | $start_time = new \DateTime(); |
| 346 | $start_time->setTimestamp($ban_start); |
| 347 | |
| 348 | $expected_end = new \DateTime(); |
| 349 | $expected_end->setTimestamp($expected); |
| 350 | |
| 351 | $ban_end = $this->ban_manager->get_ban_end($start_time, $length, $end_date); |
| 352 | |
| 353 | if ($length >= 0 || !$end_date) |
| 354 | { |
| 355 | $this->assertEquals($expected_end, $ban_end); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | $this->assertLessThan($expected_end, $ban_end); |
| 360 | $this->assertGreaterThan($start_time, $ban_end); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | public function test_get_banned_users() |
| 365 | { |
| 366 | $banned_users = $this->ban_manager->get_banned_users(); |
| 367 | $this->assertEquals( |
| 368 | [ |
| 369 | 4 => 0, |
| 370 | 5 => 0 |
| 371 | ], |
| 372 | $banned_users |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | public function test_get_banned_users_own_method() |
| 377 | { |
| 378 | global $phpbb_root_path, $phpEx; |
| 379 | |
| 380 | $phpbb_container = new \phpbb_mock_container_builder(); |
| 381 | $ban_type_email = new \phpbb\ban\type\email($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 382 | $ban_type_user = new \phpbb\ban\type\user($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 383 | $ban_type_ip = $this->getMockBuilder(\phpbb\ban\type\ip::class) |
| 384 | ->setConstructorArgs([$this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys']) |
| 385 | ->getMock(); |
| 386 | $ban_type_ip->method('get_banned_users') |
| 387 | ->willReturn([19 => 1234, 20 => 0]); |
| 388 | $phpbb_container->set('ban.type.email', $ban_type_email); |
| 389 | $phpbb_container->set('ban.type.user', $ban_type_user); |
| 390 | $phpbb_container->set('ban.type.ip', $ban_type_ip); |
| 391 | $collection = new \phpbb\di\service_collection($phpbb_container); |
| 392 | $collection->add('ban.type.email'); |
| 393 | $collection->add('ban.type.user'); |
| 394 | $collection->add('ban.type.ip'); |
| 395 | |
| 396 | $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 397 | $user = new \phpbb\user($language, '\phpbb\datetime'); |
| 398 | $phpbb_log = new \phpbb\log\dummy(); |
| 399 | |
| 400 | $ban_manager = new \phpbb\ban\manager($collection, new \phpbb\cache\driver\dummy(), $this->db, $language, $phpbb_log, $user, 'phpbb_bans', 'phpbb_users'); |
| 401 | |
| 402 | $this->assertEquals( |
| 403 | [ |
| 404 | 4 => 0, |
| 405 | 5 => 0, |
| 406 | 19 => 1234, |
| 407 | 20 => 0, |
| 408 | ], |
| 409 | $ban_manager->get_banned_users() |
| 410 | ); |
| 411 | |
| 412 | $ban_type_ip_reflection = new \ReflectionClass($ban_type_ip); |
| 413 | $get_excluded_reflection = $ban_type_ip_reflection->getMethod('get_excluded'); |
| 414 | $this->assertFalse($get_excluded_reflection->invoke($ban_type_ip)); |
| 415 | } |
| 416 | |
| 417 | public function test_ban_empty_ban_items() |
| 418 | { |
| 419 | global $phpbb_root_path, $phpEx; |
| 420 | |
| 421 | $phpbb_container = new \phpbb_mock_container_builder(); |
| 422 | $ban_type_email = new \phpbb\ban\type\email($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 423 | $ban_type_user = new \phpbb\ban\type\user($this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys'); |
| 424 | $ban_type_ip = $this->getMockBuilder(\phpbb\ban\type\ip::class) |
| 425 | ->setConstructorArgs([$this->db, 'phpbb_bans', 'phpbb_users', 'phpbb_sessions', 'phpbb_sessions_keys']) |
| 426 | ->getMock(); |
| 427 | $ban_type_ip->method('prepare_for_storage') |
| 428 | ->willReturn([]); |
| 429 | $ban_type_ip->method('get_type') |
| 430 | ->willReturn('ip'); |
| 431 | $phpbb_container->set('ban.type.email', $ban_type_email); |
| 432 | $phpbb_container->set('ban.type.user', $ban_type_user); |
| 433 | $phpbb_container->set('ban.type.ip', $ban_type_ip); |
| 434 | $collection = new \phpbb\di\service_collection($phpbb_container); |
| 435 | $collection->add('ban.type.email'); |
| 436 | $collection->add('ban.type.user'); |
| 437 | $collection->add('ban.type.ip'); |
| 438 | |
| 439 | $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); |
| 440 | $user = new \phpbb\user($language, '\phpbb\datetime'); |
| 441 | $phpbb_log = new \phpbb\log\dummy(); |
| 442 | |
| 443 | $ban_manager = new \phpbb\ban\manager($collection, new \phpbb\cache\driver\dummy(), $this->db, $language, $phpbb_log, $user, 'phpbb_bans', 'phpbb_users'); |
| 444 | |
| 445 | $start_time = new \DateTime(); |
| 446 | $start_time->setTimestamp(1000); |
| 447 | $end_time = new \DateTime(); |
| 448 | $end_time->setTimestamp(0); |
| 449 | |
| 450 | $this->assertFalse($ban_manager->ban( |
| 451 | 'ip', |
| 452 | ['192.168.1.1'], |
| 453 | $start_time, |
| 454 | $end_time, |
| 455 | '' |
| 456 | )); |
| 457 | } |
| 458 | |
| 459 | public static function data_test_ban(): array |
| 460 | { |
| 461 | return [ |
| 462 | [ |
| 463 | 'user', |
| 464 | ['normal_user'], |
| 465 | 1000, |
| 466 | 500, // end before start |
| 467 | '', |
| 468 | '', |
| 469 | false, |
| 470 | invalid_length_exception::class, |
| 471 | ], |
| 472 | [ |
| 473 | 'foo', // invalid ban type |
| 474 | ['normal_user'], |
| 475 | 1000, |
| 476 | 0, // end before start |
| 477 | '', |
| 478 | '', |
| 479 | false, |
| 480 | type_not_found_exception::class, |
| 481 | ], |
| 482 | [ |
| 483 | 'user', |
| 484 | [], // empty user list |
| 485 | 1000, |
| 486 | 0, |
| 487 | '', |
| 488 | '', |
| 489 | false, |
| 490 | no_valid_users_exception::class, |
| 491 | ], |
| 492 | [ |
| 493 | 'user', |
| 494 | ['founder'], // user same as current user |
| 495 | 1000, |
| 496 | 0, |
| 497 | '', |
| 498 | '', |
| 499 | false, |
| 500 | no_valid_users_exception::class, |
| 501 | ], |
| 502 | [ |
| 503 | 'user', |
| 504 | ['normal_user'], |
| 505 | 1000, |
| 506 | 0, |
| 507 | '', |
| 508 | '', |
| 509 | true, |
| 510 | ], |
| 511 | [ |
| 512 | 'user', |
| 513 | ['normal_u*'], |
| 514 | 1000, |
| 515 | 0, |
| 516 | '', |
| 517 | '', |
| 518 | true, |
| 519 | ], |
| 520 | [ |
| 521 | 'ip', |
| 522 | [], |
| 523 | 1000, |
| 524 | 0, |
| 525 | '', |
| 526 | '', |
| 527 | false, |
| 528 | no_valid_ips_exception::class, |
| 529 | ], |
| 530 | [ |
| 531 | 'ip', |
| 532 | ['192.168.I.1'], // invalid IP |
| 533 | 1000, |
| 534 | 0, |
| 535 | '', |
| 536 | '', |
| 537 | false, |
| 538 | no_valid_ips_exception::class, |
| 539 | ], |
| 540 | [ |
| 541 | 'ip', |
| 542 | ['192.168.1.1'], |
| 543 | 1000, |
| 544 | 0, |
| 545 | '', |
| 546 | '', |
| 547 | true, |
| 548 | ], |
| 549 | [ |
| 550 | 'email', |
| 551 | ['this_is_not_an_email'], |
| 552 | 1000, |
| 553 | 0, |
| 554 | '', |
| 555 | '', |
| 556 | false, |
| 557 | no_valid_emails_exception::class |
| 558 | ], |
| 559 | [ |
| 560 | 'email', |
| 561 | ['test@example.com'], |
| 562 | 1000, |
| 563 | 0, |
| 564 | '', |
| 565 | '', |
| 566 | true, |
| 567 | ], |
| 568 | [ |
| 569 | 'email', |
| 570 | ['*@foo.bar'], |
| 571 | 1000, |
| 572 | 0, |
| 573 | '', |
| 574 | '', |
| 575 | true, |
| 576 | ], |
| 577 | [ |
| 578 | 'email', |
| 579 | ['test@example.com', str_repeat('a', 100) . '@example.com'], // one email too long, shouldn't cause any issues though |
| 580 | 1000, |
| 581 | 0, |
| 582 | '', |
| 583 | '', |
| 584 | true, |
| 585 | ], |
| 586 | ]; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * @dataProvider data_test_ban |
| 591 | */ |
| 592 | public function test_ban($mode, $items, $start, $end, $reason, $display_reason, $expected, $expected_exception = '') |
| 593 | { |
| 594 | if ($expected_exception) |
| 595 | { |
| 596 | $this->expectException($expected_exception); |
| 597 | } |
| 598 | |
| 599 | $start_time = new \DateTime(); |
| 600 | $start_time->setTimestamp($start); |
| 601 | $end_time = new \DateTime(); |
| 602 | $end_time->setTimestamp($end); |
| 603 | |
| 604 | $ban_return = $this->ban_manager->ban($mode, $items, $start_time, $end_time, $reason, $display_reason); |
| 605 | |
| 606 | $this->assertEquals($expected, $ban_return); |
| 607 | } |
| 608 | |
| 609 | public function test_ban_actual() |
| 610 | { |
| 611 | $start_time = new \DateTime(); |
| 612 | $start_time->setTimestamp(1000); |
| 613 | $end_time = new \DateTime(); |
| 614 | $end_time->setTimestamp(0); |
| 615 | |
| 616 | $ban_return = $this->ban_manager->ban('ip', ['121.122.123.124'], $start_time, $end_time, '', 'because'); |
| 617 | |
| 618 | $this->assertTrue($ban_return); |
| 619 | |
| 620 | $this->assertEquals( |
| 621 | [ |
| 622 | 'item' => '121.122.123.124', |
| 623 | 'end' => 0, |
| 624 | 'reason' => 'because', |
| 625 | 'mode' => 'ip' |
| 626 | ], |
| 627 | $this->ban_manager->check(['user_ip' => '121.122.123.124']) |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | public static function data_test_unban(): array |
| 632 | { |
| 633 | return [ |
| 634 | [ |
| 635 | 'does_not_exist', |
| 636 | [10], |
| 637 | [], |
| 638 | type_not_found_exception::class |
| 639 | ], |
| 640 | [ |
| 641 | 'user', |
| 642 | [4], |
| 643 | [ |
| 644 | [ |
| 645 | 'ban_id' => '4', |
| 646 | 'ban_userid' => '4', |
| 647 | 'ban_item' => '4', |
| 648 | 'ban_start' => '1111', |
| 649 | 'ban_end' => '0', |
| 650 | 'ban_reason' => 'HAHAHA', |
| 651 | 'ban_reason_display' => '1', |
| 652 | 'ban_mode' => 'user', |
| 653 | 'user_id' => '4', |
| 654 | 'username' => 'ipv6_user', |
| 655 | 'username_clean' => 'ipv6_user', |
| 656 | 'label' => 'ipv6_user', |
| 657 | ], |
| 658 | ], |
| 659 | ], |
| 660 | ]; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * @dataProvider data_test_unban |
| 665 | */ |
| 666 | public function test_unban($mode, $items, $expected, $expected_exception = '') |
| 667 | { |
| 668 | if ($expected_exception) |
| 669 | { |
| 670 | $this->expectException($expected_exception); |
| 671 | } |
| 672 | |
| 673 | $before_bans = $this->ban_manager->get_bans($mode); |
| 674 | |
| 675 | $this->ban_manager->unban($mode, $items); |
| 676 | |
| 677 | $after_bans = $this->ban_manager->get_bans($mode); |
| 678 | |
| 679 | $ban_diff = array_diff_assoc($before_bans, count($after_bans) ? $after_bans : []); |
| 680 | |
| 681 | $this->assertEquals($expected, $ban_diff); |
| 682 | } |
| 683 | |
| 684 | public function test_unban_invalid_type() |
| 685 | { |
| 686 | $this->expectException(type_not_found_exception::class); |
| 687 | |
| 688 | $this->ban_manager->unban('does_not_exist', []); |
| 689 | } |
| 690 | |
| 691 | public function test_base_type_methods() |
| 692 | { |
| 693 | $ban_type_ip = $this->phpbb_container->get('ban.type.ip'); |
| 694 | $base_type_reflection = new \ReflectionClass(\phpbb\ban\type\base::class); |
| 695 | $after_unban = $base_type_reflection->getMethod('after_unban'); |
| 696 | $this->assertEquals([], $after_unban->invoke($ban_type_ip, ['items' => ['foo']])); |
| 697 | |
| 698 | $check = $base_type_reflection->getMethod('check'); |
| 699 | $this->assertFalse($check->invoke($ban_type_ip, [], [])); |
| 700 | } |
| 701 | |
| 702 | public static function data_get_ban_message(): array |
| 703 | { |
| 704 | return [ |
| 705 | [ |
| 706 | [ |
| 707 | 'end' => 0, |
| 708 | ], |
| 709 | 'foobar', |
| 710 | 'http://foo.bar', |
| 711 | 'You have been <strong>permanently</strong> banned from this board.<br><br>Please contact the <a href="http://foo.bar">Board Administrator</a> for more information.<br><br><em>BAN_TRIGGERED_BY_FOOBAR</em>', |
| 712 | ], |
| 713 | [ |
| 714 | [ |
| 715 | 'end' => 1, |
| 716 | ], |
| 717 | 'foobar', |
| 718 | 'http://foo.bar', |
| 719 | 'You have been banned from this board until <strong></strong>.<br><br>Please contact the <a href="http://foo.bar">Board Administrator</a> for more information.<br><br><em>BAN_TRIGGERED_BY_FOOBAR</em>', |
| 720 | ], |
| 721 | [ |
| 722 | [ |
| 723 | 'end' => 1, |
| 724 | 'reason' => 'just because', |
| 725 | ], |
| 726 | 'foobar', |
| 727 | 'http://foo.bar', |
| 728 | 'You have been banned from this board until <strong></strong>.<br><br>Please contact the <a href="http://foo.bar">Board Administrator</a> for more information.<br><br>Reason given for ban: <strong>just because</strong><br><br><em>BAN_TRIGGERED_BY_FOOBAR</em>', |
| 729 | ], |
| 730 | ]; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * @dataProvider data_get_ban_message |
| 735 | */ |
| 736 | public function test_get_ban_message($ban_row, $ban_triggered_by, $contact_link, $expected) |
| 737 | { |
| 738 | $this->assertEquals($expected, $this->ban_manager->get_ban_message($ban_row, $ban_triggered_by, $contact_link)); |
| 739 | } |
| 740 | |
| 741 | public function test_get_ban_options_user() |
| 742 | { |
| 743 | $foo = $this->ban_manager->get_bans('user'); |
| 744 | |
| 745 | $this->assertEquals( |
| 746 | [ |
| 747 | [ |
| 748 | 'ban_id' => 4, |
| 749 | 'ban_userid' => '4', |
| 750 | 'ban_mode' => 'user', |
| 751 | 'ban_item' => '4', |
| 752 | 'ban_start' => '1111', |
| 753 | 'ban_end' => '0', |
| 754 | 'ban_reason' => 'HAHAHA', |
| 755 | 'ban_reason_display' => '1', |
| 756 | 'user_id' => '4', |
| 757 | 'username' => 'ipv6_user', |
| 758 | 'username_clean' => 'ipv6_user', |
| 759 | 'label' => 'ipv6_user', |
| 760 | ], |
| 761 | ], |
| 762 | $foo |
| 763 | ); |
| 764 | } |
| 765 | } |