diff --git a/tests/phpunit/tests/admin/includesMedia.php b/tests/phpunit/tests/admin/includesMedia.php new file mode 100644 index 0000000000000..4c90ed738b98f --- /dev/null +++ b/tests/phpunit/tests/admin/includesMedia.php @@ -0,0 +1,91 @@ +post->create_and_get(); + self::$different_post = $factory->post->create_and_get(); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post->ID, true ); + wp_delete_post( self::$different_post->ID, true ); + self::$post = null; + self::$different_post = null; + } + + public function setUp() { + parent::setUp(); + + add_filter( 'pre_http_request', [ $this, 'internal_fake_download_url' ], 10, 3 ); + } + + public function tearDown() { + parent::tearDown(); + + $this->remove_added_uploads(); + remove_filter( 'pre_http_request', [ $this, 'internal_fake_download_url' ] ); + } + + /** + * Filters whether to preempt an HTTP request's return value. Mocking the + * response. Since ticket 49631 + * + * @param false $preempt Whether to preempt an HTTP request's return value. Default false. + * @param array $parsed_args HTTP request arguments. + * @param string $url The request URL. + * + * @return array + */ + public function internal_fake_download_url( $preempt, array $parsed_args, $url = '' ) { + // Just need an image content string to fill the new image, keeping the same mime type. + file_put_contents( $parsed_args['filename'], file_get_contents( DIR_TESTDATA . '/images/canola.jpg' ) ); + + return [ + 'response' => [ + 'code' => 200, + ], + ]; + } + + /** + * @ticket 49631 + */ + public function test_media_sideload_image() { + $year = gmdate( 'Y' ); + $month = gmdate( 'm' ); + + $image_source = 'http://' . WP_TESTS_DOMAIN . '/external/source/image1.jpg'; + $image = 'http://' . WP_TESTS_DOMAIN . "/wp-content/uploads/{$year}/{$month}/image1.jpg"; + $image_html = addslashes( $image ); + + $media_html = media_sideload_image( $image_source, self::$post->ID ); + + $this->assertNotWPError( $media_html ); + $this->assertRegExp( "~~", $media_html ); + + $image_source = 'http://' . WP_TESTS_DOMAIN . '/external/source/image2.jpg'; + $image = 'http://' . WP_TESTS_DOMAIN . "/wp-content/uploads/{$year}/{$month}/image2.jpg"; + + $media_src = media_sideload_image( $image_source, self::$different_post->ID, null, 'src' ); + + $this->assertEquals( $image, $media_src ); + + $image_source = 'http://' . WP_TESTS_DOMAIN . '/external/source/image3.jpg'; + + $media_id = media_sideload_image( $image_source, self::$different_post->ID, null, 'id' ); + $attachment_meta_source_url = get_post_meta( $media_id, '_source_url', true ); + + $this->assertInternalType( 'numeric', $media_id ); + $this->assertEquals( $image_source, $attachment_meta_source_url ); + } + +}