diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index a30f9f7931..bbb6dd41ae 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -467,9 +467,9 @@ class WP_Http { return null !== $attr; } ); - $cookie_jar[ $value->name ] = new WpOrg\Requests\Cookie( $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) ); + $cookie_jar[ $value->name ] = new WpOrg\Requests\Cookie( (string) $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) ); } elseif ( is_scalar( $value ) ) { - $cookie_jar[ $name ] = new WpOrg\Requests\Cookie( $name, (string) $value ); + $cookie_jar[ $name ] = new WpOrg\Requests\Cookie( (string) $name, (string) $value ); } } diff --git a/tests/phpunit/tests/http/http.php b/tests/phpunit/tests/http/http.php index 1648cca4ea..77a5239819 100644 --- a/tests/phpunit/tests/http/http.php +++ b/tests/phpunit/tests/http/http.php @@ -661,4 +661,56 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase { $this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' ); $this->assertTrue( $pre_http_request_filter_has_run, 'The pre_http_request filter is expected to run.' ); } + + /** + * Test that WP_Http::normalize_cookies method correctly casts integer keys to string. + * @ticket 58566 + * + * @covers WP_Http::normalize_cookies + */ + public function test_normalize_cookies_casts_integer_keys_to_string() { + $http = _wp_http_get_object(); + + $cookies = array( + '1' => 'foo', + 2 => 'bar', + 'qux' => 7, + ); + + $cookie_jar = $http->normalize_cookies( $cookies ); + + $this->assertInstanceOf( 'WpOrg\Requests\Cookie\Jar', $cookie_jar ); + + foreach ( array_keys( $cookies ) as $cookie ) { + if ( is_string( $cookie ) ) { + $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar[ $cookie ] ); + } else { + $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar[ (string) $cookie ] ); + } + } + } + + /** + * Test that WP_Http::normalize_cookies method correctly casts integer cookie names to strings. + * @ticket 58566 + * + * @covers WP_Http::normalize_cookies + */ + public function test_normalize_cookies_casts_cookie_name_integer_to_string() { + $http = _wp_http_get_object(); + + $cookies = array( + 'foo' => new WP_Http_Cookie( + array( + 'name' => 1, + 'value' => 'foo', + ) + ), + ); + + $cookie_jar = $http->normalize_cookies( $cookies ); + + $this->assertInstanceOf( 'WpOrg\Requests\Cookie\Jar', $cookie_jar ); + $this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar['1'] ); + } }