| 1 | <?php |
| 2 | /** |
| 3 | * Tests for media side-loaded files. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Tests for media side-loaded files. |
| 10 | * |
| 11 | * @group media |
| 12 | */ |
| 13 | class Tests_Media_MediaHandleSideload extends WP_UnitTestCase { |
| 14 | |
| 15 | /** |
| 16 | * Original file path. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected static $orig_file; |
| 21 | |
| 22 | /** |
| 23 | * Test file path. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected static $test_file; |
| 28 | |
| 29 | /** |
| 30 | * Setup before class. |
| 31 | */ |
| 32 | public static function setUpBeforeClass() { |
| 33 | parent::setUpBeforeClass(); |
| 34 | |
| 35 | self::$orig_file = DIR_TESTDATA . '/images/test-image.jpg'; |
| 36 | self::$test_file = get_temp_dir() . 'test-image-media-sideload.jpg'; |
| 37 | |
| 38 | if ( ! file_exists( self::$test_file ) ) { |
| 39 | copy( self::$orig_file, self::$test_file ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Tear down after class. |
| 45 | */ |
| 46 | public static function tearDownAfterClass() { |
| 47 | if ( file_exists( self::$test_file ) ) { |
| 48 | unlink( self::$test_file ); |
| 49 | } |
| 50 | parent::tearDownAfterClass(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Testing media_handle_sideload() with an existing post ID. |
| 55 | * |
| 56 | * @dataProvider data_post_id_provider |
| 57 | * |
| 58 | * @param mixed $post_id The post id to test for. |
| 59 | * @param string $expected_directory Expected created directory. |
| 60 | */ |
| 61 | public function test_with_existing_post_id( $post_id, $expected_directory ) { |
| 62 | |
| 63 | // The media_handle_sideload() deletes the tmp file. |
| 64 | if ( ! file_exists( self::$test_file ) ) { |
| 65 | copy( self::$orig_file, self::$test_file ); |
| 66 | } |
| 67 | |
| 68 | $file_array = array( |
| 69 | 'name' => basename( self::$test_file ), |
| 70 | 'tmp_name' => self::$test_file, |
| 71 | ); |
| 72 | |
| 73 | // Handles the sideload. |
| 74 | $attachment_id = media_handle_sideload( $file_array, $post_id ); |
| 75 | |
| 76 | // Checks if the expected directory is in the file path. |
| 77 | $actual = strpos( get_attached_file( $attachment_id ), $expected_directory ); |
| 78 | |
| 79 | // Cleanup. |
| 80 | if ( ! is_wp_error( $attachment_id ) ) { |
| 81 | wp_delete_attachment( $attachment_id ); |
| 82 | } |
| 83 | |
| 84 | $this->assertNotFalse( $actual ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Data provider for test_with_existing_post_id(). |
| 89 | * |
| 90 | * @return array { |
| 91 | * @type array { |
| 92 | * @mixed mixed $post_id The post id to test for. |
| 93 | * @string $expected Expected created directory. |
| 94 | * } |
| 95 | * } |
| 96 | */ |
| 97 | public function data_post_id_provider() { |
| 98 | |
| 99 | $default = current_time( '/Y/m/' ); |
| 100 | |
| 101 | return array( |
| 102 | array( |
| 103 | 'post_id' => null, |
| 104 | 'expected' => $default, |
| 105 | ), |
| 106 | array( |
| 107 | 'post_id' => 0, |
| 108 | 'expected' => $default, |
| 109 | ), |
| 110 | array( |
| 111 | 'post_id' => false, |
| 112 | 'expected' => $default, |
| 113 | ), |
| 114 | array( |
| 115 | 'post_id' => self::factory()->post->create( array( 'post_date' => '0000-00-00' ) ), |
| 116 | 'expected' => $default, |
| 117 | ), |
| 118 | array( |
| 119 | 'post_id' => self::factory()->post->create( array( 'post_date' => '2018-01-01' ) ), |
| 120 | 'expected' => '/2018/01', |
| 121 | ), |
| 122 | ); |
| 123 | } |
| 124 | } |