Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
45.12% |
74 / 164 |
|
93.75% |
15 / 16 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_request_test | |
45.12% |
74 / 164 |
|
93.75% |
15 / 16 |
58.31 | |
0.00% |
0 / 1 |
| setUp | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| test_toggle_super_globals | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| test_server | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_server_escaping | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| test_header | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_header_escaping | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| test_file | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| test_file_not_exists | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| test_disable_post_super_global | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| test_is_set_post | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| test_is_ajax_without_ajax | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_is_ajax_with_ajax | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| data_is_secure | |
0.00% |
0 / 90 |
|
0.00% |
0 / 1 |
2 | |||
| test_is_secure | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| test_variable_names | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| tearDown | |
100.00% |
1 / 1 |
|
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 | class phpbb_request_test extends phpbb_test_case |
| 15 | { |
| 16 | /** @var \phpbb\request\type_cast_helper_interface */ |
| 17 | private $type_cast_helper; |
| 18 | |
| 19 | /** @var \phpbb\request\request */ |
| 20 | private $request; |
| 21 | |
| 22 | protected function setUp(): void |
| 23 | { |
| 24 | // populate super globals |
| 25 | $_POST['test'] = 1; |
| 26 | $_GET['test'] = 2; |
| 27 | $_COOKIE['test'] = 3; |
| 28 | $_REQUEST['test'] = 3; |
| 29 | $_GET['unset'] = ''; |
| 30 | $_FILES['test'] = array( |
| 31 | 'name' => 'file', |
| 32 | 'tmp_name' => 'tmp', |
| 33 | 'size' => 256, |
| 34 | 'type' => 'application/octet-stream', |
| 35 | 'error' => UPLOAD_ERR_OK, |
| 36 | ); |
| 37 | |
| 38 | $_SERVER['HTTP_HOST'] = 'example.com'; |
| 39 | $_SERVER['HTTP_ACCEPT'] = 'application/json'; |
| 40 | $_SERVER['HTTP_SOMEVAR'] = '<value>'; |
| 41 | |
| 42 | $this->type_cast_helper = $this->createMock('\phpbb\request\type_cast_helper_interface'); |
| 43 | $this->request = new \phpbb\request\request($this->type_cast_helper); |
| 44 | } |
| 45 | |
| 46 | public function test_toggle_super_globals() |
| 47 | { |
| 48 | $this->assertTrue($this->request->super_globals_disabled(), 'Superglobals were not disabled'); |
| 49 | |
| 50 | $this->request->enable_super_globals(); |
| 51 | |
| 52 | $this->assertFalse($this->request->super_globals_disabled(), 'Superglobals were not enabled'); |
| 53 | |
| 54 | $this->assertEquals(1, $_POST['test'], 'Checking $_POST after enable_super_globals'); |
| 55 | $this->assertEquals(2, $_GET['test'], 'Checking $_GET after enable_super_globals'); |
| 56 | $this->assertEquals(3, $_COOKIE['test'], 'Checking $_COOKIE after enable_super_globals'); |
| 57 | $this->assertEquals(3, $_REQUEST['test'], 'Checking $_REQUEST after enable_super_globals'); |
| 58 | $this->assertEquals(256, $_FILES['test']['size']); |
| 59 | |
| 60 | $_POST['x'] = 2; |
| 61 | $this->assertEquals($_POST, $GLOBALS['_POST'], 'Checking whether $_POST can still be accessed via $GLOBALS[\'_POST\']'); |
| 62 | } |
| 63 | |
| 64 | public function test_server() |
| 65 | { |
| 66 | $this->assertEquals('example.com', $this->request->server('HTTP_HOST')); |
| 67 | } |
| 68 | |
| 69 | public function test_server_escaping() |
| 70 | { |
| 71 | $this->type_cast_helper |
| 72 | ->expects($this->once()) |
| 73 | ->method('recursive_set_var') |
| 74 | ->with( |
| 75 | $this->anything(), |
| 76 | '', |
| 77 | true |
| 78 | ); |
| 79 | |
| 80 | $this->request->server('HTTP_SOMEVAR'); |
| 81 | } |
| 82 | |
| 83 | public function test_header() |
| 84 | { |
| 85 | $this->assertEquals('application/json', $this->request->header('Accept')); |
| 86 | } |
| 87 | |
| 88 | public function test_header_escaping() |
| 89 | { |
| 90 | $this->type_cast_helper |
| 91 | ->expects($this->once()) |
| 92 | ->method('recursive_set_var') |
| 93 | ->with( |
| 94 | $this->anything(), |
| 95 | '', |
| 96 | true |
| 97 | ); |
| 98 | |
| 99 | $this->request->header('SOMEVAR'); |
| 100 | } |
| 101 | |
| 102 | public function test_file() |
| 103 | { |
| 104 | $file = $this->request->file('test'); |
| 105 | $this->assertEquals('file', $file['name']); |
| 106 | $this->assertEquals('tmp', $file['tmp_name']); |
| 107 | $this->assertEquals(256, $file['size']); |
| 108 | $this->assertEquals('application/octet-stream', $file['type']); |
| 109 | $this->assertEquals(UPLOAD_ERR_OK, $file['error']); |
| 110 | } |
| 111 | |
| 112 | public function test_file_not_exists() |
| 113 | { |
| 114 | $file = $this->request->file('404'); |
| 115 | $this->assertTrue(is_array($file)); |
| 116 | $this->assertTrue(empty($file)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Checks that directly accessing $_POST will trigger |
| 121 | * an error. |
| 122 | */ |
| 123 | public function test_disable_post_super_global() |
| 124 | { |
| 125 | $this->setExpectedTriggerError(E_USER_ERROR); |
| 126 | $_POST['test'] = 3; |
| 127 | } |
| 128 | |
| 129 | public function test_is_set_post() |
| 130 | { |
| 131 | $this->assertTrue($this->request->is_set_post('test')); |
| 132 | $this->assertFalse($this->request->is_set_post('unset')); |
| 133 | } |
| 134 | |
| 135 | public function test_is_ajax_without_ajax() |
| 136 | { |
| 137 | $this->assertFalse($this->request->is_ajax()); |
| 138 | } |
| 139 | |
| 140 | public function test_is_ajax_with_ajax() |
| 141 | { |
| 142 | $this->request->enable_super_globals(); |
| 143 | $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
| 144 | $this->request = new \phpbb\request\request($this->type_cast_helper); |
| 145 | |
| 146 | $this->assertTrue($this->request->is_ajax()); |
| 147 | } |
| 148 | |
| 149 | public static function data_is_secure() |
| 150 | { |
| 151 | return array( |
| 152 | array( |
| 153 | array( |
| 154 | 'HTTPS' => 'on', |
| 155 | ), |
| 156 | true, |
| 157 | ), |
| 158 | array( |
| 159 | array( |
| 160 | 'HTTPS' => '1', |
| 161 | ), |
| 162 | true, |
| 163 | ), |
| 164 | array( |
| 165 | array( |
| 166 | 'HTTPS' => 'yes', |
| 167 | ), |
| 168 | true, |
| 169 | ), |
| 170 | array( |
| 171 | array( |
| 172 | 'HTTPS' => 1, |
| 173 | ), |
| 174 | true, |
| 175 | ), |
| 176 | array( |
| 177 | array( |
| 178 | 'HTTPS' => 'off', |
| 179 | ), |
| 180 | false, |
| 181 | ), |
| 182 | array( |
| 183 | array( |
| 184 | 'HTTPS' => '0', |
| 185 | ), |
| 186 | false, |
| 187 | ), |
| 188 | array( |
| 189 | array( |
| 190 | 'HTTPS' => 0, |
| 191 | ), |
| 192 | false, |
| 193 | ), |
| 194 | array( |
| 195 | array( |
| 196 | 'HTTPS' => '', |
| 197 | ), |
| 198 | false, |
| 199 | ), |
| 200 | array( |
| 201 | array( |
| 202 | 'HTTPS' => 'off', |
| 203 | 'HTTP_X_FORWARDED_PROTO' => 'https', |
| 204 | ), |
| 205 | true, |
| 206 | ), |
| 207 | array( |
| 208 | array( |
| 209 | 'HTTPS' => 'on', |
| 210 | 'HTTP_X_FORWARDED_PROTO' => 'http', |
| 211 | ), |
| 212 | true, |
| 213 | ), |
| 214 | array( |
| 215 | array( |
| 216 | 'HTTPS' => 'off', |
| 217 | 'HTTP_X_FORWARDED_PROTO' => 'http', |
| 218 | ), |
| 219 | false, |
| 220 | ), |
| 221 | array( |
| 222 | array( |
| 223 | 'HTTP_X_FORWARDED_PROTO' => 'http', |
| 224 | ), |
| 225 | false, |
| 226 | ), |
| 227 | array( |
| 228 | array( |
| 229 | 'HTTP_X_FORWARDED_PROTO' => 'https', |
| 230 | ), |
| 231 | true, |
| 232 | ), |
| 233 | array( |
| 234 | array( |
| 235 | 'HTTPS' => 'on', |
| 236 | 'HTTP_X_FORWARDED_PROTO' => 'http', |
| 237 | ), |
| 238 | true, |
| 239 | ), |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @dataProvider data_is_secure |
| 245 | */ |
| 246 | public function test_is_secure($server_data, $expected) |
| 247 | { |
| 248 | $this->assertFalse($this->request->is_secure()); |
| 249 | |
| 250 | $this->request->enable_super_globals(); |
| 251 | $_SERVER = $server_data; |
| 252 | $this->request = new \phpbb\request\request($this->type_cast_helper); |
| 253 | |
| 254 | $this->assertSame($expected, $this->request->is_secure()); |
| 255 | } |
| 256 | |
| 257 | public function test_variable_names() |
| 258 | { |
| 259 | $expected = array('test', 'unset'); |
| 260 | $result = $this->request->variable_names(); |
| 261 | $this->assertEquals($expected, $result); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Makes sure super globals work properly after these tests |
| 266 | */ |
| 267 | protected function tearDown(): void |
| 268 | { |
| 269 | $this->request->enable_super_globals(); |
| 270 | } |
| 271 | } |