Make WordPress Core

Ticket #47968: 47968.2.diff

File 47968.2.diff, 5.6 KB (added by costdev, 4 years ago)

Updated patch with some tidying up. Splits tests, adds documentation, @covers annotations, $message parameters.

  • src/wp-includes/class-wp.php

    diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php
    index 23af2fff87..3e72de329b 100644
    a b class WP { 
    413413                $headers       = array();
    414414                $status        = null;
    415415                $exit_required = false;
     416                $date_format   = 'D, d M Y H:i:s';
    416417
    417418                if ( is_user_logged_in() ) {
    418419                        $headers = array_merge( $headers, wp_get_nocache_headers() );
    class WP { 
    420421                        // Unmoderated comments are only visible for 10 minutes via the moderation hash.
    421422                        $expires = 10 * MINUTE_IN_SECONDS;
    422423
    423                         $headers['Expires']       = gmdate( 'D, d M Y H:i:s', time() + $expires );
     424                        $headers['Expires']       = gmdate( $date_format, time() + $expires );
    424425                        $headers['Cache-Control'] = sprintf(
    425426                                'max-age=%d, must-revalidate',
    426427                                $expires
    class WP { 
    459460                                        )
    460461                                )
    461462                        ) {
    462                                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
     463                                $wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
     464                                $wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
     465
     466                                if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
     467                                        $wp_last_modified = $wp_last_modified_post;
     468                                } else {
     469                                        $wp_last_modified = $wp_last_modified_comment;
     470                                }
    463471                        } else {
    464                                 $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
     472                                $wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
    465473                        }
    466474
    467475                        if ( ! $wp_last_modified ) {
    468                                 $wp_last_modified = gmdate( 'D, d M Y H:i:s' );
     476                                $wp_last_modified = gmdate( $date_format );
    469477                        }
    470478
    471479                        $wp_last_modified .= ' GMT';
  • tests/phpunit/tests/feed/rss2.php

    diff --git a/tests/phpunit/tests/feed/rss2.php b/tests/phpunit/tests/feed/rss2.php
    index 742c78f3d7..217b302732 100644
    a b class Tests_Feed_RSS2 extends WP_UnitTestCase { 
    494494                );
    495495
    496496        }
     497
     498        /**
     499         * Test that the Last-Modified is a post's date when a more recent comment exists,
     500         * but the "withcomments=1" query var is not passed.
     501         *
     502         * @ticket 47968
     503         *
     504         * @covers ::send_headers
     505         */
     506        public function test_feed_last_modified_should_be_a_post_date_when_withcomments_is_not_passed() {
     507                $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     508                $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     509
     510                // Create a post dated last week.
     511                $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     512
     513                // Create a comment dated yesterday.
     514                $this->factory()->comment->create(
     515                        array(
     516                                'comment_post_ID' => $post_id,
     517                                'comment_date'    => $yesterday,
     518                        )
     519                );
     520
     521                // The Last-Modified header should have the post's date when "withcomments" is not passed.
     522                add_filter(
     523                        'wp_headers',
     524                        function( $headers ) use ( $last_week ) {
     525                                $this->assertSame(
     526                                        strtotime( $headers['Last-Modified'] ),
     527                                        strtotime( $last_week ),
     528                                        'Last-Modified was not the date of the post'
     529                                );
     530                                return $headers;
     531                        }
     532                );
     533
     534                $this->go_to( '/?feed=rss2' );
     535        }
     536
     537        /**
     538         * Test that the Last-Modified is a comment's date when a more recent comment exists,
     539         * and the "withcomments=1" query var is passed.
     540         *
     541         * @ticket 47968
     542         *
     543         * @covers ::send_headers
     544         */
     545        public function test_feed_last_modified_should_be_the_date_of_a_comment_that_is_the_latest_update_when_withcomments_is_passed() {
     546                $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     547                $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     548
     549                // Create a post dated last week.
     550                $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     551
     552                // Create a comment dated yesterday.
     553                $this->factory()->comment->create(
     554                        array(
     555                                'comment_post_ID' => $post_id,
     556                                'comment_date'    => $yesterday,
     557                        )
     558                );
     559
     560                // The Last-Modified header should have the comment's date when "withcomments=1" is passed.
     561                add_filter(
     562                        'wp_headers',
     563                        function( $headers ) use ( $yesterday ) {
     564                                $this->assertSame(
     565                                        strtotime( $headers['Last-Modified'] ),
     566                                        strtotime( $yesterday ),
     567                                        'Last-Modified was not the date of the comment'
     568                                );
     569                                return $headers;
     570                        }
     571                );
     572
     573                $this->go_to( '/?feed=rss2&withcomments=1' );
     574        }
     575
     576        /**
     577         * Test that the Last-Modified is the latest post's date when an earlier post and comment exist,
     578         * and the "withcomments=1" query var is passed.
     579         *
     580         * @ticket 47968
     581         *
     582         * @covers ::send_headers
     583         */
     584        public function test_feed_last_modified_should_be_the_date_of_a_post_that_is_the_latest_update_when_withcomments_is_passed() {
     585                $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) );
     586                $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) );
     587                $today     = gmdate( 'Y-m-d H:i:s' );
     588
     589                // Create a post dated last week.
     590                $post_id = $this->factory()->post->create( array( 'post_date' => $last_week ) );
     591
     592                // Create a comment dated yesterday.
     593                $this->factory()->comment->create(
     594                        array(
     595                                'comment_post_ID' => $post_id,
     596                                'comment_date'    => $yesterday,
     597                        )
     598                );
     599
     600                // Create a post dated today.
     601                $this->factory()->post->create( array( 'post_date' => $today ) );
     602
     603                // The Last-Modified header should have the date from today's post when it is the latest update.
     604                add_filter(
     605                        'wp_headers',
     606                        function( $headers ) use ( $today ) {
     607                                $this->assertSame(
     608                                        strtotime( $headers['Last-Modified'] ),
     609                                        strtotime( $today ),
     610                                        'Last-Modified was not the date of the most recent most'
     611                                );
     612                                return $headers;
     613                        }
     614                );
     615
     616                $this->go_to( '/?feed=rss2&withcomments=1' );
     617        }
    497618}