-
Notifications
You must be signed in to change notification settings - Fork 3.1k
wp_update_post overwrites post_date when updating post_status #9699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SirLouen
wants to merge
5
commits into
WordPress:trunk
Choose a base branch
from
SirLouen:patch/62468
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
399d2fe
wp_update_post overwrites post_date when updating post_status
SirLouen 5d912fe
Better docs
SirLouen bc8f8c5
Returning back the insert part of a merged test
SirLouen 405eb5a
Improved Test Cases
SirLouen 47c1ae6
mukesh review
SirLouen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
<?php | ||
/** | ||
* Test wp_update_post() function | ||
* | ||
* @package WordPress | ||
* @subpackage Post | ||
* | ||
* @since 6.9.0 | ||
*/ | ||
|
||
/** | ||
* Class to Test wp_update_post() function | ||
* | ||
* @group post | ||
* @covers ::wp_update_post | ||
*/ | ||
class Tests_Post_WpUpdatePost extends WP_UnitTestCase { | ||
|
||
/** | ||
* User IDs for the test. | ||
* | ||
* @var array{administrator: null, editor: null, contributor: null} | ||
*/ | ||
protected static $user_ids = array( | ||
'administrator' => null, | ||
'editor' => null, | ||
'contributor' => null, | ||
); | ||
|
||
/** | ||
* Set up before class. | ||
* | ||
* @param WP_UnitTest_Factory $factory The Unit Test Factory. | ||
*/ | ||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
self::$user_ids = array( | ||
'administrator' => $factory->user->create( | ||
array( | ||
'role' => 'administrator', | ||
) | ||
), | ||
'editor' => $factory->user->create( | ||
array( | ||
'role' => 'editor', | ||
) | ||
), | ||
'contributor' => $factory->user->create( | ||
array( | ||
'role' => 'contributor', | ||
) | ||
), | ||
); | ||
|
||
$role = get_role( 'administrator' ); | ||
$role->add_cap( 'publish_mapped_meta_caps' ); | ||
$role->add_cap( 'publish_unmapped_meta_caps' ); | ||
} | ||
|
||
/** | ||
* Tear down after class. | ||
*/ | ||
public static function wpTearDownAfterClass() { | ||
$role = get_role( 'administrator' ); | ||
$role->remove_cap( 'publish_mapped_meta_caps' ); | ||
$role->remove_cap( 'publish_unmapped_meta_caps' ); | ||
} | ||
|
||
/** | ||
* Set up. | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
|
||
register_post_type( | ||
'mapped_meta_caps', | ||
array( | ||
'capability_type' => array( 'mapped_meta_cap', 'mapped_meta_caps' ), | ||
'map_meta_cap' => true, | ||
) | ||
); | ||
|
||
register_post_type( | ||
'unmapped_meta_caps', | ||
array( | ||
'capability_type' => array( 'unmapped_meta_cap', 'unmapped_meta_caps' ), | ||
'map_meta_cap' => false, | ||
) | ||
); | ||
|
||
register_post_type( | ||
'no_admin_caps', | ||
array( | ||
'capability_type' => array( 'no_admin_cap', 'no_admin_caps' ), | ||
'map_meta_cap' => false, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Test updating a post with an invalid ID. | ||
* | ||
* @ticket 23474 | ||
*/ | ||
public function test_update_invalid_post_id() { | ||
$post_id = self::factory()->post->create(); | ||
$post = get_post( $post_id, ARRAY_A ); | ||
|
||
$post['ID'] = 123456789; | ||
|
||
$this->assertSame( 0, wp_update_post( $post ) ); | ||
|
||
$this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) ); | ||
} | ||
|
||
/** | ||
* Test ensuring that wp_update_post() does not unintentionally modify post tags | ||
* if the post has several tags with the same name but different slugs. | ||
* | ||
* Tags should only be modified if 'tags_input' parameter was explicitly provided, | ||
* and is different from the existing tags. | ||
* | ||
* @ticket 45121 | ||
*/ | ||
public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() { | ||
$tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) ); | ||
$tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) ); | ||
$tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) ); | ||
|
||
$post_id = self::factory()->post->create( | ||
array( | ||
'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ), | ||
) | ||
); | ||
|
||
$post = get_post( $post_id ); | ||
|
||
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); | ||
$this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); | ||
|
||
wp_update_post( $post ); | ||
|
||
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); | ||
$this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); | ||
|
||
wp_update_post( | ||
array( | ||
'ID' => $post->ID, | ||
'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ), | ||
) | ||
); | ||
|
||
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); | ||
$this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags ); | ||
} | ||
|
||
/** | ||
* Test the wp_update_post() filters post content when 'post_status' is 'draft'. | ||
* | ||
* @ticket 22944 | ||
*/ | ||
public function test_wp_update_post_with_content_filtering() { | ||
kses_remove_filters(); | ||
|
||
$post_id = wp_insert_post( | ||
array( | ||
'post_title' => '<script>Test</script>', | ||
) | ||
); | ||
$post = get_post( $post_id ); | ||
$this->assertSame( '<script>Test</script>', $post->post_title ); | ||
$this->assertSame( 'draft', $post->post_status ); | ||
|
||
kses_init_filters(); | ||
|
||
wp_update_post( | ||
array( | ||
'ID' => $post->ID, | ||
'post_status' => 'publish', | ||
) | ||
); | ||
|
||
kses_remove_filters(); | ||
|
||
$post = get_post( $post->ID ); | ||
$this->assertSame( 'Test', $post->post_title ); | ||
} | ||
|
||
/** | ||
* Test updating a post and preserving post_date when changing to future status. | ||
* | ||
* @ticket 62468 | ||
* | ||
* @dataProvider data_update_post_preserves_date_for_future_posts | ||
* | ||
* @param string $initial_status Initial post status. | ||
* @param string $time_offset Time offset. | ||
* @param string $expected_status Expected post status. | ||
*/ | ||
public function test_update_post_preserves_date_for_future_posts( $initial_status, $time_offset, $expected_status ) { | ||
|
||
$post_id = self::factory()->post->create( | ||
array( | ||
'post_status' => $initial_status, | ||
) | ||
); | ||
|
||
$future_date = gmdate( 'Y-m-d H:i:s', strtotime( $time_offset ) ); | ||
$update_data = array( | ||
'ID' => $post_id, | ||
'post_status' => 'future', | ||
'post_date' => $future_date, | ||
); | ||
|
||
wp_update_post( $update_data ); | ||
$updated_post = get_post( $post_id ); | ||
|
||
$this->assertSame( $future_date, $updated_post->post_date ); | ||
$this->assertSame( $expected_status, $updated_post->post_status ); | ||
} | ||
SirLouen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Data provider for test_update_post_preserves_date_for_future_posts. | ||
* | ||
* @return array[] Test parameters. | ||
*/ | ||
public function data_update_post_preserves_date_for_future_posts() { | ||
return array( | ||
'pending to future with 1 day more' => array( | ||
'initial_status' => 'pending', | ||
'time_offset' => '+1 day', | ||
'expected_status' => 'future', | ||
), | ||
'draft to future with 1 day more' => array( | ||
'initial_status' => 'draft', | ||
'time_offset' => '+1 day', | ||
'expected_status' => 'future', | ||
), | ||
'publish to future with 1 day more' => array( | ||
'initial_status' => 'publish', | ||
'time_offset' => '+1 day', | ||
'expected_status' => 'publish', | ||
), | ||
'draft to future with 1 day less' => array( | ||
'initial_status' => 'draft', | ||
'time_offset' => '-1 day', | ||
'expected_status' => 'publish', | ||
), | ||
'pending to future with 1 day less' => array( | ||
'initial_status' => 'pending', | ||
'time_offset' => '-1 day', | ||
'expected_status' => 'publish', | ||
), | ||
'publish to future with 1 day less' => array( | ||
'initial_status' => 'publish', | ||
'time_offset' => '-1 day', | ||
'expected_status' => 'publish', | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.