| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group media |
| 5 | * @group sideload |
| 6 | */ |
| 7 | class Tests_Media_Sideload extends WP_UnitTestCase { |
| 8 | private $attachment_id = 0; |
| 9 | |
| 10 | public function setUp() { |
| 11 | parent::setUp(); |
| 12 | |
| 13 | add_filter( 'pre_http_request', array( $this, 'mock_sideload_request' ), 10, 3 ); |
| 14 | add_filter( 'wp_update_attachment_metadata', array( $this, 'get_attachment_metadata' ), 10, 2 ); |
| 15 | } |
| 16 | |
| 17 | public function tearDown() { |
| 18 | parent::tearDown(); |
| 19 | |
| 20 | remove_filter( 'pre_http_request', array( $this, 'mock_sideload_request' ), 10, 3 ); |
| 21 | remove_filter( 'wp_update_attachment_metadata', array( $this, 'get_attachment_metadata' ), 10, 2 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Catch the attachment ID as attachment meta is updated so that it |
| 26 | * can be used to check against `media_sideload_image` output. |
| 27 | * |
| 28 | * @param array $data Attachment metadata. |
| 29 | * @param int $id Attachment ID. |
| 30 | * @return array Attachment metadata. |
| 31 | */ |
| 32 | public function get_attachment_metadata( $data, $id ) { |
| 33 | $this->attachment_id = $id; |
| 34 | return $data; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Intercept image sideload requests and mock responses. |
| 39 | * |
| 40 | * @param mixed $preempt Whether to preempt an HTTP request's return value. Default false. |
| 41 | * @param mixed $r HTTP request arguments. |
| 42 | * @param string $url The request URL. |
| 43 | * @return array Response data. |
| 44 | */ |
| 45 | public function mock_sideload_request( $preempt, $r, $url ) { |
| 46 | // If `$url` is set to false, continue the request to |
| 47 | // simulate failure using an improper URL. |
| 48 | if ( false == $url ) { |
| 49 | return $preempt; |
| 50 | } |
| 51 | |
| 52 | // Copy the existing file to the expected temporary location to |
| 53 | // simulate a successful upload. |
| 54 | copy( DIR_TESTDATA . '/images/test-image.jpg', $r['filename'] ); |
| 55 | return array( |
| 56 | 'response' => array( |
| 57 | 'code' => 200, |
| 58 | ), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Tests basic functionality and error handling of `media_sideload_image()`. |
| 64 | * |
| 65 | * @ticket 19629 |
| 66 | */ |
| 67 | function test_wp_media_sideload_image() { |
| 68 | $source = 'https://example.com/test-image.jpg'; |
| 69 | $alt = 'alt text'; |
| 70 | |
| 71 | // Test default return value, which should return the same as html. |
| 72 | $result = media_sideload_image( $source, 0, $alt ); |
| 73 | $dest = wp_get_attachment_url( $this->attachment_id ); |
| 74 | |
| 75 | $expected = "<img src='$dest' alt='$alt' />"; |
| 76 | $this->assertEquals( $expected, $result ); |
| 77 | wp_delete_file( get_attached_file( $this->attachment_id ) ); |
| 78 | |
| 79 | // Test 'html' return value |
| 80 | $result = media_sideload_image( $source, 0, $alt, 'html' ); |
| 81 | $dest = wp_get_attachment_url( $this->attachment_id ); |
| 82 | |
| 83 | $expected = "<img src='$dest' alt='$alt' />"; |
| 84 | $this->assertEquals( $expected, $result ); |
| 85 | wp_delete_file( get_attached_file( $this->attachment_id ) ); |
| 86 | |
| 87 | // Test 'id' return value |
| 88 | $result = media_sideload_image( $source, 0, $alt, 'id' ); |
| 89 | $this->assertEquals( $this->attachment_id, $result); |
| 90 | wp_delete_file( get_attached_file( $this->attachment_id ) ); |
| 91 | |
| 92 | // Test 'src' return value |
| 93 | $result = media_sideload_image( $source, 0, $alt, 'src' ); |
| 94 | $expected = wp_get_attachment_url( $this->attachment_id ); |
| 95 | $this->assertEquals( $expected, $result); |
| 96 | wp_delete_file( get_attached_file( $this->attachment_id ) ); |
| 97 | |
| 98 | // Test WP_Error on invalid URL. |
| 99 | $result = media_sideload_image( false, 0, $alt, 'id' ); |
| 100 | $this->assertInstanceOf( 'WP_Error', $result); |
| 101 | |
| 102 | // Test WP_Error if file can't be sanitized to image. |
| 103 | $result = media_sideload_image( 'invalid-image.html', 0, $alt, 'id' ); |
| 104 | $this->assertInstanceOf( 'WP_Error', $result); |
| 105 | } |
| 106 | } |