| 281 | /** |
| 282 | * @ticket 15928 |
| 283 | */ |
| 284 | public function test_wp_get_attachment_with_https_off() { |
| 285 | $filename = ( DIR_TESTDATA . '/images/test-image.jpg' ); |
| 286 | $contents = file_get_contents( $filename ); |
| 287 | |
| 288 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 289 | $this->assertTrue( empty( $upload['error'] ) ); |
| 290 | |
| 291 | // Set attachment ID |
| 292 | $attachment_id = $this->_make_attachment( $upload ); |
| 293 | |
| 294 | // Save server data for cleanup |
| 295 | $is_ssl = is_ssl(); |
| 296 | $_SERVER['HTTPS'] = 'off'; |
| 297 | |
| 298 | // Find out what scheme is coming out of wp_upload_dir() |
| 299 | $upload_dir = wp_upload_dir(); |
| 300 | $scheme = parse_url( $upload_dir['baseurl'] , PHP_URL_SCHEME ); |
| 301 | |
| 302 | // Test that wp_get_attachemt_url returns the same scheme as the uploads dir |
| 303 | $url = wp_get_attachment_url( $attachment_id ); |
| 304 | $this->assertSame( set_url_scheme( $url, $scheme ), $url ); |
| 305 | |
| 306 | // Cleanup |
| 307 | $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off'; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @ticket 15928 |
| 312 | */ |
| 313 | public function test_wp_get_attachment_url_with_https_on_same_host() { |
| 314 | $filename = ( DIR_TESTDATA . '/images/test-image.jpg' ); |
| 315 | $contents = file_get_contents( $filename ); |
| 316 | |
| 317 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 318 | $this->assertTrue( empty( $upload['error'] ) ); |
| 319 | |
| 320 | // Set attachment ID |
| 321 | $attachment_id = $this->_make_attachment( $upload ); |
| 322 | |
| 323 | // Save server data for cleanup |
| 324 | $is_ssl = is_ssl(); |
| 325 | $http_host = $_SERVER['HTTP_HOST']; |
| 326 | |
| 327 | $_SERVER['HTTPS'] = 'on'; |
| 328 | |
| 329 | // Set server host to match the host of wp_upload_dir() |
| 330 | $upload_dir = wp_upload_dir(); |
| 331 | $_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST ); |
| 332 | |
| 333 | // Test that wp_get_attachemt_url returns with https scheme |
| 334 | $url = wp_get_attachment_url( $attachment_id ); |
| 335 | $this->assertSame( set_url_scheme( $url, 'https' ), $url ); |
| 336 | |
| 337 | // Cleanup |
| 338 | $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off'; |
| 339 | $_SERVER['HTTP_HOST'] = $http_host; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @ticket 15928 |
| 344 | */ |
| 345 | public function test_wp_get_attachment_url_with_https_on_diff_host() { |
| 346 | $filename = ( DIR_TESTDATA . '/images/test-image.jpg' ); |
| 347 | $contents = file_get_contents( $filename ); |
| 348 | |
| 349 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 350 | $this->assertTrue( empty( $upload['error'] ) ); |
| 351 | |
| 352 | // Set attachment ID |
| 353 | $attachment_id = $this->_make_attachment( $upload ); |
| 354 | |
| 355 | // Save server data for cleanup |
| 356 | $is_ssl = is_ssl(); |
| 357 | $http_host = $_SERVER['HTTP_HOST']; |
| 358 | |
| 359 | $_SERVER['HTTPS'] = 'on'; |
| 360 | |
| 361 | // Set server host to something random |
| 362 | $_SERVER['HTTP_HOST'] = 'some.otherhostname.com'; |
| 363 | |
| 364 | // Find out what scheme is coming out of wp_upload_dir() |
| 365 | $upload_dir = wp_upload_dir(); |
| 366 | $scheme = parse_url( $upload_dir['baseurl'] , PHP_URL_SCHEME ); |
| 367 | |
| 368 | // Test that wp_get_attachemt_url returns the same scheme as the uploads dir |
| 369 | $url = wp_get_attachment_url( $attachment_id ); |
| 370 | $this->assertSame( set_url_scheme( $url, $scheme ), $url ); |
| 371 | |
| 372 | // Cleanup |
| 373 | $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off'; |
| 374 | $_SERVER['HTTP_HOST'] = $http_host; |
| 375 | } |
| 376 | |