Make WordPress Core

Ticket #43119: 43119.2.diff

File 43119.2.diff, 3.5 KB (added by birgire, 7 years ago)
  • src/wp-admin/includes/media.php

    diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php
    index 8a356dd..b005065 100644
    function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data 
    419419        $overrides = array( 'test_form' => false );
    420420
    421421        $time = current_time( 'mysql' );
    422         if ( $post = get_post( $post_id ) ) {
     422        if ( $post_id && $post = get_post( $post_id ) ) {
    423423                if ( substr( $post->post_date, 0, 4 ) > 0 ) {
    424424                        $time = $post->post_date;
    425425                }
  • new file tests/phpunit/tests/media/media-handle-sideload.php

    diff --git tests/phpunit/tests/media/media-handle-sideload.php tests/phpunit/tests/media/media-handle-sideload.php
    new file mode 100644
    index 0000000..9315eb9
    - +  
     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 */
     13class 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}