Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
98 / 98 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| phpbb_session_garbage_collection_test | |
100.00% |
98 / 98 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| getDataSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| test_session_gc | |
100.00% |
51 / 51 |
|
100.00% |
1 / 1 |
1 | |||
| test_cleanup_all | |
100.00% |
33 / 33 |
|
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 | |
| 14 | require_once __DIR__ . '/../test_framework/phpbb_session_test_case.php'; |
| 15 | |
| 16 | class phpbb_session_garbage_collection_test extends phpbb_session_test_case |
| 17 | { |
| 18 | public $session; |
| 19 | |
| 20 | public function getDataSet() |
| 21 | { |
| 22 | return $this->createXMLDataSet(__DIR__ . '/fixtures/sessions_garbage.xml'); |
| 23 | } |
| 24 | |
| 25 | protected function setUp(): void |
| 26 | { |
| 27 | parent::setUp(); |
| 28 | $this->session = $this->session_factory->get_session($this->db); |
| 29 | |
| 30 | global $phpbb_container; |
| 31 | |
| 32 | $plugins = new \phpbb\di\service_collection($phpbb_container); |
| 33 | $plugins->add('core.captcha.plugins.recaptcha'); |
| 34 | $phpbb_container->set( |
| 35 | 'captcha.factory', |
| 36 | new \phpbb\captcha\factory($phpbb_container, $plugins) |
| 37 | ); |
| 38 | $phpbb_container->set( |
| 39 | 'core.captcha.plugins.recaptcha', |
| 40 | new \phpbb\captcha\plugins\recaptcha() |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function test_session_gc() |
| 45 | { |
| 46 | global $config; |
| 47 | $config['session_length'] = 3600; |
| 48 | |
| 49 | $this->check_expired_sessions_recent( |
| 50 | [ |
| 51 | [ |
| 52 | 'session_user_id' => 4, |
| 53 | 'recent_time' => 1500000000, |
| 54 | ], |
| 55 | [ |
| 56 | 'session_user_id' => 5, |
| 57 | 'recent_time' => 1500000000, |
| 58 | ], |
| 59 | ], |
| 60 | 'Before test, should get recent expired sessions only.' |
| 61 | ); |
| 62 | |
| 63 | $this->check_user_session_data( |
| 64 | [ |
| 65 | [ |
| 66 | 'username_clean' => 'bar', |
| 67 | 'user_lastvisit' => 1400000000, |
| 68 | 'user_lastpage' => 'oldpage_user_bar.php', |
| 69 | ], |
| 70 | [ |
| 71 | 'username_clean' => 'foo', |
| 72 | 'user_lastvisit' => 1400000000, |
| 73 | 'user_lastpage' => 'oldpage_user_foo.php', |
| 74 | ], |
| 75 | ], |
| 76 | 'Before test, users session data is not updated yet.' |
| 77 | ); |
| 78 | |
| 79 | // There is an error unless the captcha plugin is set |
| 80 | $config['captcha_plugin'] = 'core.captcha.plugins.recaptcha'; |
| 81 | $this->session->session_gc(); |
| 82 | $this->check_expired_sessions_recent( |
| 83 | [], |
| 84 | 'After garbage collection, all expired sessions should be removed.' |
| 85 | ); |
| 86 | |
| 87 | $this->check_user_session_data( |
| 88 | [ |
| 89 | [ |
| 90 | 'username_clean' => 'bar', |
| 91 | 'user_lastvisit' => '1500000000', |
| 92 | 'user_lastpage' => 'newpage_user_bar.php', |
| 93 | ], |
| 94 | [ |
| 95 | 'username_clean' => 'foo', |
| 96 | 'user_lastvisit' => '1500000000', |
| 97 | 'user_lastpage' => 'newpage_user_foo.php', |
| 98 | ], |
| 99 | ], |
| 100 | 'After garbage collection, users session data should be updated to the recent expired sessions data.' |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | public function test_cleanup_all() |
| 105 | { |
| 106 | $this->check_sessions_equals( |
| 107 | [ |
| 108 | [ |
| 109 | 'session_id' => 'anon_session00000000000000000000', |
| 110 | 'session_user_id' => 1, |
| 111 | ], |
| 112 | [ |
| 113 | 'session_id' => 'bar_session000000000000000000000', |
| 114 | 'session_user_id' => 4, |
| 115 | ], |
| 116 | [ |
| 117 | 'session_id' => 'bar_session000000000000000000002', |
| 118 | 'session_user_id' => 4, |
| 119 | ], |
| 120 | [ |
| 121 | 'session_id' => 'foo_session000000000000000000000', |
| 122 | 'session_user_id' => 5, |
| 123 | ], |
| 124 | [ |
| 125 | 'session_id' => 'foo_session000000000000000000002', |
| 126 | 'session_user_id' => 5, |
| 127 | ], |
| 128 | ], |
| 129 | 'Before test, should have some sessions.' |
| 130 | ); |
| 131 | // Set session length so it clears all |
| 132 | global $config; |
| 133 | $config['session_length'] = 0; |
| 134 | // There is an error unless the captcha plugin is set |
| 135 | $config['captcha_plugin'] = 'core.captcha.plugins.recaptcha'; |
| 136 | $this->session->session_gc(); |
| 137 | $this->check_sessions_equals( |
| 138 | [], |
| 139 | 'After setting session time to 0, should remove all.' |
| 140 | ); |
| 141 | } |
| 142 | } |