Make WordPress Core


Ignore:
Timestamp:
11/17/2020 03:27:07 AM (6 years ago)
Author:
peterwilsoncc
Message:

Permalinks: Prevent attachment pages 404ing following [49563].

This largely reverts [49563] due to attachment pages returning 404: File not found errors when they use the inherit status.

Permalink changes to attachment pages are retained when they are descendants of trashed or deleted posts.

Props Toro_Unit, helen, johnbillion, peterwilsoncc.
Fixes #51776.
See #5272.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r49603 r49622  
    99        protected static $_sizes;
    1010        protected static $large_filename = 'test-image-large.jpg';
     11        protected static $post_ids;
    1112
    1213        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    1617                $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
    1718                self::$large_id = $factory->attachment->create_upload_object( $filename );
     19
     20                $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
     21                foreach ( $post_statuses as $post_status ) {
     22                        $date = '';
     23                        if ( 'future' === $post_status ) {
     24                                strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     25                        }
     26
     27                        self::$post_ids[ $post_status ] = $factory->post->create(
     28                                array(
     29                                        'post_status' => 'trash' === $post_status ? 'publish' : $post_status,
     30                                        'post_date'   => $date,
     31                                        'post_name'   => "$post_status-post",
     32                                )
     33                        );
     34
     35                        // Attachments without media.
     36                        self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
     37                                array(
     38                                        'post_parent' => self::$post_ids[ $post_status ],
     39                                        'post_status' => 'inherit',
     40                                        'post_name'   => "$post_status-attachment",
     41                                        'post_date'   => $date,
     42                                )
     43                        );
     44                }
     45
     46                // Trash the trash post.
     47                wp_trash_post( self::$post_ids['trash'] );
    1848        }
    1949
     
    30243054        }
    30253055
     3056        /**
     3057         * Test attachment permalinks based on parent post status.
     3058         *
     3059         * @dataProvider data_attachment_permalinks_based_on_parent_status
     3060         * @ticket 51776
     3061         *
     3062         * @param string $post_key     Post as keyed in the shared fixture array.
     3063         * @param string $expected     Expected result.
     3064         * @param bool   $expected_404 Whether the page is expected to return a 404 result.
     3065         *
     3066         */
     3067        function test_attachment_permalinks_based_on_parent_status( $post_key, $expected, $expected_404 ) {
     3068                $this->set_permalink_structure( '/%postname%' );
     3069                $post = get_post( self::$post_ids[ $post_key ] );
     3070
     3071                /*
     3072                 * The dataProvider runs before the fixures are set up, therefore the
     3073                 * post object IDs are placeholders that needs to be replaced.
     3074                 */
     3075                $expected = home_url( str_replace( '%ID%', $post->ID, $expected ) );
     3076
     3077                $this->assertSame( $expected, get_permalink( $post ) );
     3078                $this->go_to( get_permalink( $post ) );
     3079                $this->assertSame( $expected_404, is_404() );
     3080        }
     3081
     3082        /**
     3083         * Data provider for test_attachment_permalinks_based_on_parent_status().
     3084         *
     3085         * @return array[] {
     3086         *     @type string $post_key     Post as keyed in the shared fixture array.
     3087         *     @type string $expected     Expected result.
     3088         *     $type bool   $expected_404 Whether the page is expected to return a 404 result.
     3089         * }
     3090         */
     3091        function data_attachment_permalinks_based_on_parent_status() {
     3092                return array(
     3093                        array( 'draft-attachment', '/?attachment_id=%ID%', true ),
     3094                        array( 'publish-attachment', '/publish-post/publish-attachment', false ),
     3095                        array( 'future-attachment', '/future-post/future-attachment', false ),
     3096                        array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ),
     3097                        array( 'trash-attachment', '/?attachment_id=%ID%', false ),
     3098                );
     3099        }
    30263100}
    30273101
Note: See TracChangeset for help on using the changeset viewer.