| | 497 | |
| | 498 | /** |
| | 499 | * Test that Last-Modified in headers is properly updated from posts and comments where appropriate. |
| | 500 | * |
| | 501 | * @ticket 47968 |
| | 502 | */ |
| | 503 | public function test_feed_last_modified_header() { |
| | 504 | $last_week = gmdate( 'Y-m-d H:i:s', strtotime( '-1 week' ) ); |
| | 505 | $yesterday = gmdate( 'Y-m-d H:i:s', strtotime( '-1 day' ) ); |
| | 506 | $today = gmdate( 'Y-m-d H:i:s' ); |
| | 507 | |
| | 508 | $post_id = $this->factory()->post->create( |
| | 509 | array( |
| | 510 | 'post_date' => $last_week, |
| | 511 | ) |
| | 512 | ); |
| | 513 | |
| | 514 | $filter_post_modified_last_week = function( $headers ) use ( $last_week ) { |
| | 515 | $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $last_week ) ); |
| | 516 | |
| | 517 | return $headers; |
| | 518 | }; |
| | 519 | |
| | 520 | add_filter( 'wp_headers', $filter_post_modified_last_week ); |
| | 521 | |
| | 522 | // Last modified by a post last week. |
| | 523 | $this->go_to( '/?feed=rss2' ); |
| | 524 | |
| | 525 | $this->factory()->comment->create( |
| | 526 | array( |
| | 527 | 'comment_post_ID' => $post_id, |
| | 528 | 'comment_date' => $yesterday, |
| | 529 | ) |
| | 530 | ); |
| | 531 | |
| | 532 | // Last modified by a comment yesterday, however, we are calling a feed without comments, so Last-Modified is still from last week's post. |
| | 533 | $this->go_to( '/?feed=rss2' ); |
| | 534 | |
| | 535 | remove_filter( 'wp_headers', $filter_post_modified_last_week ); |
| | 536 | |
| | 537 | $filter_comment_modified_yesterday = function( $headers ) use ( $yesterday ) { |
| | 538 | $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $yesterday ) ); |
| | 539 | |
| | 540 | return $headers; |
| | 541 | }; |
| | 542 | |
| | 543 | add_filter( 'wp_headers', $filter_comment_modified_yesterday ); |
| | 544 | |
| | 545 | // Last modified by a comment yesterday, we are calling a feed with comments, so Last-Modified is from yesterday's comment. |
| | 546 | $this->go_to( '/?feed=rss2&withcomments=1' ); |
| | 547 | |
| | 548 | remove_filter( 'wp_headers', $filter_comment_modified_yesterday ); |
| | 549 | |
| | 550 | $this->factory()->post->create( |
| | 551 | array( |
| | 552 | 'post_date' => $today, |
| | 553 | ) |
| | 554 | ); |
| | 555 | |
| | 556 | $filter_post_modified_today = function( $headers ) use ( $today ) { |
| | 557 | $this->assertSame( strtotime( $headers['Last-Modified'] ), strtotime( $today ) ); |
| | 558 | |
| | 559 | return $headers; |
| | 560 | }; |
| | 561 | |
| | 562 | add_filter( 'wp_headers', $filter_post_modified_today ); |
| | 563 | |
| | 564 | // Last modified by a post today, so that should be the Last-Modified datetime. |
| | 565 | $this->go_to( '/?feed=rss2&withcomments=1' ); |
| | 566 | |
| | 567 | remove_filter( 'wp_headers', $filter_post_modified_today ); |
| | 568 | } |