| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Tests the WP_Http_Cookie class. |
| | 5 | * |
| | 6 | * @group http |
| | 7 | */ |
| | 8 | class Tests_HTTP_Cookie extends WP_UnitTestCase { |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests the constructor. |
| | 12 | * |
| | 13 | * @dataProvider constructorValues |
| | 14 | * @covers WP_Http_Cookie::__construct |
| | 15 | * |
| | 16 | * @param array|string $data Raw cookie data. |
| | 17 | * @param string $requested_url The requested url. |
| | 18 | * @param array $expected_attributes Expected attribute values. |
| | 19 | */ |
| | 20 | public function test_contruct( $data, $requested_url, $expected_attributes ) { |
| | 21 | $instance = new WP_Http_Cookie( $data, $requested_url ); |
| | 22 | |
| | 23 | foreach ( $expected_attributes as $attribute => $expected ) { |
| | 24 | $this->assertSame( $expected, $instance->$attribute ); |
| | 25 | } |
| | 26 | } |
| | 27 | |
| | 28 | /** |
| | 29 | * Provides data for the constructor test. |
| | 30 | * |
| | 31 | * @return array[] Test data. |
| | 32 | */ |
| | 33 | public function constructorValues() { |
| | 34 | return array( |
| | 35 | 'with_request_url_containing_trailing_slash' => array( |
| | 36 | 'data' => array( |
| | 37 | 'name' => 'Cookiename', |
| | 38 | 'value' => 'Cookievalue', |
| | 39 | ), |
| | 40 | 'requested_url' => 'https://example.org/path/', |
| | 41 | 'expected_attributes' => array( |
| | 42 | 'name' => 'Cookiename', |
| | 43 | 'value' => 'Cookievalue', |
| | 44 | 'domain' => 'example.org', |
| | 45 | 'path' => '/path/', |
| | 46 | ), |
| | 47 | ), |
| | 48 | 'with_requested_url_not_containing_trailing_slash' => array( |
| | 49 | 'data' => array( |
| | 50 | 'name' => 'Cookiename', |
| | 51 | 'value' => 'Cookievalue', |
| | 52 | ), |
| | 53 | 'requested_url' => 'https://example.org/path/route', |
| | 54 | 'expected_attributes' => array( |
| | 55 | 'name' => 'Cookiename', |
| | 56 | 'value' => 'Cookievalue', |
| | 57 | 'domain' => 'example.org', |
| | 58 | 'path' => '/path/', |
| | 59 | ), |
| | 60 | ), |
| | 61 | 'with_data_array_given' => array( |
| | 62 | 'data' => array( |
| | 63 | 'name' => 'Cookiename', |
| | 64 | 'value' => 'Cookievalue', |
| | 65 | 'domain' => 'example.org', |
| | 66 | 'path' => '/path/', |
| | 67 | 'port' => 80, |
| | 68 | 'host_only' => true, |
| | 69 | ), |
| | 70 | 'requested_url' => '', |
| | 71 | 'expected_attributes' => array( |
| | 72 | 'name' => 'Cookiename', |
| | 73 | 'value' => 'Cookievalue', |
| | 74 | 'domain' => 'example.org', |
| | 75 | 'path' => '/path/', |
| | 76 | 'port' => 80, |
| | 77 | 'host_only' => true, |
| | 78 | ), |
| | 79 | ), |
| | 80 | 'with_data_array_and_requested_url_given' => array( |
| | 81 | 'data' => array( |
| | 82 | 'name' => 'Cookiename', |
| | 83 | 'value' => 'Cookievalue', |
| | 84 | 'domain' => 'example.org', |
| | 85 | 'path' => '/path/', |
| | 86 | 'port' => 80, |
| | 87 | 'host_only' => true, |
| | 88 | ), |
| | 89 | 'requested_url' => 'https://another_example.org/other_path/', |
| | 90 | 'expected_attributes' => array( |
| | 91 | 'name' => 'Cookiename', |
| | 92 | 'value' => 'Cookievalue', |
| | 93 | 'domain' => 'example.org', |
| | 94 | 'path' => '/path/', |
| | 95 | 'port' => 80, |
| | 96 | 'host_only' => true, |
| | 97 | 'expires' => null, |
| | 98 | ), |
| | 99 | ), |
| | 100 | 'with_data_array_containing_no_name' => array( |
| | 101 | 'data' => array( |
| | 102 | 'value' => 'Cookievalue', |
| | 103 | ), |
| | 104 | 'requested_url' => '', |
| | 105 | 'expected_attributes' => array( |
| | 106 | 'name' => null, |
| | 107 | 'value' => null, |
| | 108 | ), |
| | 109 | ), |
| | 110 | 'with_int_expires_value_given' => array( |
| | 111 | 'data' => array( |
| | 112 | 'name' => 'Cookiename', |
| | 113 | 'value' => 'Cookievalue', |
| | 114 | 'expires' => 123456789, |
| | 115 | ), |
| | 116 | 'requested_url' => '', |
| | 117 | 'expected_attributes' => array( |
| | 118 | 'name' => 'Cookiename', |
| | 119 | 'value' => 'Cookievalue', |
| | 120 | 'expires' => 123456789, |
| | 121 | ), |
| | 122 | ), |
| | 123 | 'with_string_expires_value_given' => array( |
| | 124 | 'data' => array( |
| | 125 | 'name' => 'Cookiename', |
| | 126 | 'value' => 'Cookievalue', |
| | 127 | 'expires' => '2020-11-13 14:15:00', |
| | 128 | ), |
| | 129 | 'requested_url' => '', |
| | 130 | 'expected_attributes' => array( |
| | 131 | 'name' => 'Cookiename', |
| | 132 | 'value' => 'Cookievalue', |
| | 133 | 'expires' => strtotime( '2020-11-13 14:15:00' ), |
| | 134 | ), |
| | 135 | ), |
| | 136 | 'with_data_string_given' => array( |
| | 137 | 'data' => 'Cookiename=Cookievalue;domain=example.org;path=/path/;port=80;host_only=true', |
| | 138 | 'requested_url' => '', |
| | 139 | 'expected_attributes' => array( |
| | 140 | 'name' => 'Cookiename', |
| | 141 | 'value' => 'Cookievalue', |
| | 142 | 'domain' => 'example.org', |
| | 143 | 'path' => '/path/', |
| | 144 | 'port' => '80', |
| | 145 | 'host_only' => 'true', |
| | 146 | ), |
| | 147 | ), |
| | 148 | 'with_data_string_contain_empty_pair_given' => array( |
| | 149 | 'data' => 'Cookiename=Cookievalue;', |
| | 150 | 'requested_url' => '', |
| | 151 | 'expected_attributes' => array( |
| | 152 | 'name' => 'Cookiename', |
| | 153 | 'value' => 'Cookievalue', |
| | 154 | ), |
| | 155 | ), |
| | 156 | 'with_data_string_containing_expires_value' => array( |
| | 157 | 'data' => 'Cookiename=Cookievalue;expires=2020-11-13 14:15:00', |
| | 158 | 'requested_url' => '', |
| | 159 | 'expected_attributes' => array( |
| | 160 | 'name' => 'Cookiename', |
| | 161 | 'value' => 'Cookievalue', |
| | 162 | 'expires' => strtotime( '2020-11-13 14:15:00' ), |
| | 163 | ), |
| | 164 | ), |
| | 165 | 'with_data_string_containing_url_encoded_value' => array( |
| | 166 | 'data' => 'Cookiename=Cookie%20value', |
| | 167 | 'requested_url' => '', |
| | 168 | 'expected_attributes' => array( |
| | 169 | 'name' => 'Cookiename', |
| | 170 | 'value' => 'Cookie value', |
| | 171 | ), |
| | 172 | ), |
| | 173 | 'with_data_string_containing_spaces_after' => array( |
| | 174 | 'data' => 'Cookiename =Cookievalue', |
| | 175 | 'requested_url' => '', |
| | 176 | 'expected_attributes' => array( |
| | 177 | 'name' => 'Cookiename', |
| | 178 | 'value' => 'Cookievalue', |
| | 179 | ), |
| | 180 | ), |
| | 181 | 'with_data_string_containing_only_a_key' => array( |
| | 182 | 'data' => 'Cookiename=Cookievalue;domain', |
| | 183 | 'requested_url' => '', |
| | 184 | 'expected_attributes' => array( |
| | 185 | 'name' => 'Cookiename', |
| | 186 | 'value' => 'Cookievalue', |
| | 187 | 'domain' => '' |
| | 188 | ), |
| | 189 | ), |
| | 190 | ); |
| | 191 | } |
| | 192 | |
| | 193 | /** |
| | 194 | * Tests the test method. |
| | 195 | * |
| | 196 | * @dataProvider validateCookieProvider |
| | 197 | * |
| | 198 | * @param array $data Raw cookie data to use. |
| | 199 | * @param string $url URL to test. |
| | 200 | * @param bool $expected Expected value. |
| | 201 | * |
| | 202 | * @covers WP_Http_Cookie::test |
| | 203 | */ |
| | 204 | public function test_the_test_method( $data, $url, $expected ) { |
| | 205 | $instance = new WP_Http_Cookie( $data ); |
| | 206 | |
| | 207 | $this->assertSame( $expected, $instance->test( $url ) ); |
| | 208 | } |
| | 209 | |
| | 210 | /** |
| | 211 | * Provides data for the test method. |
| | 212 | * |
| | 213 | * @return array[] Test data. |
| | 214 | */ |
| | 215 | public function validateCookieProvider() { |
| | 216 | return array( |
| | 217 | 'with_no_name_present' => array( |
| | 218 | 'data' => array( |
| | 219 | 'value' => 'Cookievalue', |
| | 220 | ), |
| | 221 | 'url' => 'https://example.org', |
| | 222 | 'expected' => false, |
| | 223 | ), |
| | 224 | 'with_expired_cookie' => array( |
| | 225 | 'data' => array( |
| | 226 | 'name' => 'Cookiename', |
| | 227 | 'value' => 'Cookievalue', |
| | 228 | 'expires' => strtotime( "-1week" ), |
| | 229 | ), |
| | 230 | 'url' => 'https://example.org', |
| | 231 | 'expected' => false, |
| | 232 | ), |
| | 233 | 'happy_path' => array( |
| | 234 | 'data' => array( |
| | 235 | 'name' => 'Cookiename', |
| | 236 | 'value' => 'Cookievalue', |
| | 237 | 'domain' => 'example.org', |
| | 238 | 'path' => '/path/', |
| | 239 | 'port' => 80, |
| | 240 | ), |
| | 241 | 'url' => 'https://example.org:80/path/', |
| | 242 | 'expected' => true, |
| | 243 | ), |
| | 244 | 'url_not_containing_the_port_on_https' => array( |
| | 245 | 'data' => array( |
| | 246 | 'name' => 'Cookiename', |
| | 247 | 'value' => 'Cookievalue', |
| | 248 | 'domain' => 'example.org', |
| | 249 | 'path' => '/path/', |
| | 250 | 'port' => 443, |
| | 251 | ), |
| | 252 | 'url' => 'https://example.org/path/', |
| | 253 | 'expected' => true, |
| | 254 | ), |
| | 255 | 'url_not_containing_the_port_on_http' => array( |
| | 256 | 'data' => array( |
| | 257 | 'name' => 'Cookiename', |
| | 258 | 'value' => 'Cookievalue', |
| | 259 | 'domain' => 'example.org', |
| | 260 | 'path' => '/path/', |
| | 261 | 'port' => 80, |
| | 262 | ), |
| | 263 | 'url' => 'http://example.org/path/', |
| | 264 | 'expected' => true, |
| | 265 | ), |
| | 266 | 'url_not_containing_the_path' => array( |
| | 267 | 'data' => array( |
| | 268 | 'name' => 'Cookiename', |
| | 269 | 'value' => 'Cookievalue', |
| | 270 | 'domain' => 'example.org', |
| | 271 | 'path' => '/', |
| | 272 | 'port' => 80, |
| | 273 | ), |
| | 274 | 'url' => 'http://example.org', |
| | 275 | 'expected' => true, |
| | 276 | ), |
| | 277 | 'data_not_containing_the_path' => array( |
| | 278 | 'data' => array( |
| | 279 | 'name' => 'Cookiename', |
| | 280 | 'value' => 'Cookievalue', |
| | 281 | 'domain' => 'example.org', |
| | 282 | 'port' => 80, |
| | 283 | ), |
| | 284 | 'url' => 'http://example.org', |
| | 285 | 'expected' => true, |
| | 286 | ), |
| | 287 | 'data_not_containing_the_port' => array( |
| | 288 | 'data' => array( |
| | 289 | 'name' => 'Cookiename', |
| | 290 | 'value' => 'Cookievalue', |
| | 291 | 'domain' => 'example.org', |
| | 292 | ), |
| | 293 | 'url' => 'http://example.org', |
| | 294 | 'expected' => true, |
| | 295 | ), |
| | 296 | 'data_not_containing_the_domain' => array( |
| | 297 | 'data' => array( |
| | 298 | 'name' => 'Cookiename', |
| | 299 | 'value' => 'Cookievalue', |
| | 300 | ), |
| | 301 | 'url' => 'http://example.org', |
| | 302 | 'expected' => true, |
| | 303 | ), |
| | 304 | 'data_not_containing_the_domain_and_url_is_localhost' => array( |
| | 305 | 'data' => array( |
| | 306 | 'name' => 'Cookiename', |
| | 307 | 'value' => 'Cookievalue', |
| | 308 | 'domain' => 'localhost' |
| | 309 | ), |
| | 310 | 'url' => 'http://localhost.local', |
| | 311 | 'expected' => true, |
| | 312 | ), |
| | 313 | 'data_containing_all_domains_and_url_is_subdomain' => array( |
| | 314 | 'data' => array( |
| | 315 | 'name' => 'Cookiename', |
| | 316 | 'value' => 'Cookievalue', |
| | 317 | 'domain' => '.example.org', |
| | 318 | ), |
| | 319 | 'url' => 'http://sub.example.org', |
| | 320 | 'expected' => true, |
| | 321 | ), |
| | 322 | 'data_containing_all_domains_and_url_is_main_domain' => array( |
| | 323 | 'data' => array( |
| | 324 | 'name' => 'Cookiename', |
| | 325 | 'value' => 'Cookievalue', |
| | 326 | 'domain' => '.example.org', |
| | 327 | ), |
| | 328 | 'url' => 'http://example.org', |
| | 329 | 'expected' => true, |
| | 330 | ), |
| | 331 | |
| | 332 | 'data_containing_subdomain_and_url_is_main_domain' => array( |
| | 333 | 'data' => array( |
| | 334 | 'name' => 'Cookiename', |
| | 335 | 'value' => 'Cookievalue', |
| | 336 | 'domain' => 'sub.example.org' |
| | 337 | ), |
| | 338 | 'url' => 'http://example.org', |
| | 339 | 'expected' => false, |
| | 340 | ), |
| | 341 | 'data_containing_port_list' => array( |
| | 342 | 'data' => array( |
| | 343 | 'name' => 'Cookiename', |
| | 344 | 'value' => 'Cookievalue', |
| | 345 | 'domain' => 'example.org', |
| | 346 | 'path' => '/path/', |
| | 347 | 'port' => "80,8080", |
| | 348 | ), |
| | 349 | 'url' => 'https://example.org:8081/path/', |
| | 350 | 'expected' => false, |
| | 351 | ), |
| | 352 | 'data_containing_path_url_contains_other_path' => array( |
| | 353 | 'data' => array( |
| | 354 | 'name' => 'Cookiename', |
| | 355 | 'value' => 'Cookievalue', |
| | 356 | 'domain' => 'example.org', |
| | 357 | 'path' => '/path/', |
| | 358 | ), |
| | 359 | 'url' => 'https://example.org/other_path', |
| | 360 | 'expected' => false, |
| | 361 | ), |
| | 362 | ); |
| | 363 | } |
| | 364 | |
| | 365 | /** |
| | 366 | * Tests the getHeaderValue method. |
| | 367 | * |
| | 368 | * @dataProvider getHeaderValues |
| | 369 | * |
| | 370 | * @covers WP_Http_Cookie::getHeaderValue |
| | 371 | * |
| | 372 | * @param array $data Raw cookie data. |
| | 373 | * @param string $expected Expected value. |
| | 374 | */ |
| | 375 | public function test_getHeaderValue( $data, $expected ) { |
| | 376 | $instance = new WP_Http_Cookie( $data ); |
| | 377 | |
| | 378 | $this->assertSame( $expected, $instance->getHeaderValue() ); |
| | 379 | } |
| | 380 | |
| | 381 | /** |
| | 382 | * Provides data for the getHeaderValueTest |
| | 383 | * |
| | 384 | * @return array[] |
| | 385 | */ |
| | 386 | public function getHeaderValues() { |
| | 387 | return array( |
| | 388 | 'happy_path' => array( |
| | 389 | 'data' => array( |
| | 390 | 'name' => 'Cookiename', |
| | 391 | 'value' => 'Cookievalue' |
| | 392 | ), |
| | 393 | 'expected' => 'Cookiename=Cookievalue', |
| | 394 | ), |
| | 395 | 'with_no_name_set' => array( |
| | 396 | 'data' => array( |
| | 397 | 'value' => 'Cookievalue' |
| | 398 | ), |
| | 399 | 'expected' => '', |
| | 400 | ), |
| | 401 | 'with_no_value_set' => array( |
| | 402 | 'data' => array( |
| | 403 | 'name' => 'Cookiename' |
| | 404 | ), |
| | 405 | 'expected' => '', |
| | 406 | ), |
| | 407 | ); |
| | 408 | } |
| | 409 | |
| | 410 | /** |
| | 411 | * Tests the getHeaderValue method with having the filter applied. |
| | 412 | * |
| | 413 | * @covers WP_Http_Cookie::getHeaderValue |
| | 414 | */ |
| | 415 | public function test_getHeaderValue_with_the_filter_applied() { |
| | 416 | $instance = new WP_Http_Cookie( array( |
| | 417 | 'name' => 'Cookiename', |
| | 418 | 'value' => 'Cookievalue' |
| | 419 | ) ); |
| | 420 | |
| | 421 | add_filter( 'wp_http_cookie_value', [ $this, '_filter_cookie_value' ], 10, 2 ); |
| | 422 | |
| | 423 | $this->assertSame( 'Cookiename=FilteredCookievalue', $instance->getHeaderValue() ); |
| | 424 | |
| | 425 | remove_filter( 'wp_http_cookie_value', [ $this, '_filter_cookie_value' ], 10 ); |
| | 426 | } |
| | 427 | |
| | 428 | /** |
| | 429 | * Filters the cookie value. |
| | 430 | * |
| | 431 | * @param string $value The cookie value. |
| | 432 | * @param string $name The cookie name. |
| | 433 | * |
| | 434 | * @return string The filtered value. |
| | 435 | */ |
| | 436 | public function _filter_cookie_value( $value, $name ) { |
| | 437 | $this->assertSame( 'Cookievalue', $value ); |
| | 438 | $this->assertSame( 'Cookiename', $name ); |
| | 439 | |
| | 440 | return 'FilteredCookievalue'; |
| | 441 | } |
| | 442 | |
| | 443 | /** |
| | 444 | * Tests the getFullHeader method. |
| | 445 | * |
| | 446 | * @covers WP_Http_Cookie::getFullHeader |
| | 447 | */ |
| | 448 | public function test_getFullHeader() { |
| | 449 | $instance = new WP_Http_Cookie( array( |
| | 450 | 'name' => 'Cookiename', |
| | 451 | 'value' => 'Cookievalue' |
| | 452 | ) ); |
| | 453 | |
| | 454 | $this->assertSame( 'Cookie: Cookiename=Cookievalue', $instance->getFullHeader() ); |
| | 455 | } |
| | 456 | |
| | 457 | /** |
| | 458 | * Tests the get_attributes method. |
| | 459 | * |
| | 460 | * @covers WP_Http_Cookie::get_attributes() |
| | 461 | */ |
| | 462 | public function test_get_attributes() { |
| | 463 | $expires = time(); |
| | 464 | |
| | 465 | $instance = new WP_Http_Cookie( array( |
| | 466 | 'name' => 'Cookiename', |
| | 467 | 'value' => 'Cookievalue', |
| | 468 | 'expires' => $expires, |
| | 469 | 'path' => '/path', |
| | 470 | 'domain' => 'example.org', |
| | 471 | ) ); |
| | 472 | |
| | 473 | $this->assertSame( |
| | 474 | array( |
| | 475 | 'expires' => $expires, |
| | 476 | 'path' => '/path', |
| | 477 | 'domain' => 'example.org', |
| | 478 | ), |
| | 479 | $instance->get_attributes() |
| | 480 | ); |
| | 481 | } |
| | 482 | } |