| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group media |
| 5 | */ |
| 6 | class Tests_Media_MediaHandleSideload extends WP_UnitTestCase { |
| 7 | |
| 8 | /** |
| 9 | * @dataProvider post_id_provider |
| 10 | * |
| 11 | * @param mixed $post_id The post id to test for. |
| 12 | * @param string $expected_directory Expected created directory. |
| 13 | * |
| 14 | * @throws PHPUnit_Framework_AssertionFailedError |
| 15 | */ |
| 16 | public function test_with_a_existing_post_id( $post_id, $expected_directory ) { |
| 17 | $url = 'https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png'; |
| 18 | $tmp = download_url( $url ); |
| 19 | |
| 20 | $file_array = array( |
| 21 | 'name' => basename( $url ), |
| 22 | 'tmp_name' => $tmp |
| 23 | ); |
| 24 | |
| 25 | if ( is_wp_error( $tmp ) ) { |
| 26 | @unlink( $file_array[ 'tmp_name' ] ); |
| 27 | |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Handles the sideload. |
| 32 | $id = media_handle_sideload( $file_array, $post_id ); |
| 33 | |
| 34 | // Checks if the expected directory is in the file path. |
| 35 | $actual = strpos( get_attached_file( $id ), $expected_directory ); |
| 36 | |
| 37 | $this->assertNotFalse( $actual ); |
| 38 | } |
| 39 | |
| 40 | public function post_id_provider() { |
| 41 | |
| 42 | $default = date( '/Y/m', current_time( 'timestamp' ) ); |
| 43 | |
| 44 | return array( |
| 45 | array ( |
| 46 | 'post_id' => null, |
| 47 | 'expected' => $default, |
| 48 | ), |
| 49 | array ( |
| 50 | 'post_id' => 0, |
| 51 | 'expected' => $default, |
| 52 | ), |
| 53 | array ( |
| 54 | 'post_id' => false, |
| 55 | 'expected' => $default, |
| 56 | ), |
| 57 | array ( |
| 58 | 'post_id' => self::factory()->post->create( array( 'post_date' => '0000-00-00' ) ), |
| 59 | 'expected' => $default, |
| 60 | ), |
| 61 | array ( |
| 62 | 'post_id' => self::factory()->post->create( array( 'post_date' => '2018-01-01' ) ), |
| 63 | 'expected' => '/2018/01', |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | No newline at end of file |