|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test wp_update_post() date handling when changing post status |
| 5 | + * |
| 6 | + * @group post |
| 7 | + * @covers ::wp_update_post |
| 8 | + */ |
| 9 | +class Tests_Post_wpUpdatePostDateStatus extends WP_UnitTestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Data provider for test_update_post_preserves_date |
| 13 | + * |
| 14 | + * @return array[] Test parameters |
| 15 | + */ |
| 16 | + public function data_post_status_transitions() { |
| 17 | + return array( |
| 18 | + 'pending to future' => array( |
| 19 | + 'initial_status' => 'pending', |
| 20 | + ), |
| 21 | + 'draft to future' => array( |
| 22 | + 'initial_status' => 'draft', |
| 23 | + ), |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test that wp_update_post() preserves post_date when changing to future status |
| 29 | + * |
| 30 | + * @ticket 62468 |
| 31 | + * |
| 32 | + * @dataProvider data_post_status_transitions |
| 33 | + * @covers ::wp_update_post |
| 34 | + * |
| 35 | + * @param string $initial_status Initial post status |
| 36 | + */ |
| 37 | + public function test_update_post_preserves_date( $initial_status ) { |
| 38 | + $post_data = array( |
| 39 | + 'post_title' => 'Test Post', |
| 40 | + 'post_content' => 'Test content', |
| 41 | + 'post_status' => $initial_status, |
| 42 | + 'post_author' => self::factory()->user->create( array( 'role' => 'editor' ) ), |
| 43 | + ); |
| 44 | + |
| 45 | + $post_id = wp_insert_post( $post_data ); |
| 46 | + $this->assertIsInt( $post_id ); |
| 47 | + |
| 48 | + // Set future date (1 day from now) |
| 49 | + $future_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) ); |
| 50 | + $future_date_gmt = get_gmt_from_date( $future_date ); |
| 51 | + |
| 52 | + $update_data = array( |
| 53 | + 'ID' => $post_id, |
| 54 | + 'post_status' => 'future', |
| 55 | + 'post_date' => $future_date, |
| 56 | + 'post_date_gmt' => $future_date_gmt, |
| 57 | + ); |
| 58 | + |
| 59 | + $update_result = wp_update_post( $update_data ); |
| 60 | + $this->assertIsInt( $update_result ); |
| 61 | + |
| 62 | + $updated_post = get_post( $post_id ); |
| 63 | + |
| 64 | + $this->assertEquals( 'future', $updated_post->post_status ); |
| 65 | + $this->assertEquals( $future_date, $updated_post->post_date ); |
| 66 | + $this->assertEquals( $future_date_gmt, $updated_post->post_date_gmt ); |
| 67 | + } |
| 68 | +} |
0 commit comments