| | 953 | |
| | 954 | /** |
| | 955 | * If a post is sticky and is updated by a user that does not have the publish_post capability, it should _stay_ |
| | 956 | * sticky. |
| | 957 | * |
| | 958 | * @ticket 24153 |
| | 959 | */ |
| | 960 | function test_user_without_publish_cannot_affect_sticky() { |
| | 961 | // Create a role with edit_others_posts |
| | 962 | add_role( 'grammarian', 'Grammarian', array( |
| | 963 | 'read' => true, |
| | 964 | 'edit_posts' => true, |
| | 965 | 'edit_others_posts' => true, |
| | 966 | 'edit_published_posts' => true, |
| | 967 | ) ); |
| | 968 | $editor_user = $this->factory->user->create( array( 'role' => 'grammarian' ) ); |
| | 969 | $old_uid = get_current_user_id(); |
| | 970 | wp_set_current_user( $editor_user ); |
| | 971 | |
| | 972 | // Sanity Check |
| | 973 | $this->assertFalse( current_user_can( 'publish_posts' ) ); |
| | 974 | $this->assertTrue( current_user_can( 'edit_others_posts' ) ); |
| | 975 | $this->assertTrue( current_user_can( 'edit_published_posts' ) ); |
| | 976 | |
| | 977 | // Create a sticky post |
| | 978 | $post = $this->factory->post->create_and_get( array( |
| | 979 | 'post_title' => 'Will be changed', |
| | 980 | 'post_content' => 'Will be changed', |
| | 981 | ) ); |
| | 982 | stick_post( $post->ID ); |
| | 983 | |
| | 984 | // Sanity Check |
| | 985 | $this->assertTrue( is_sticky( $post->ID ) ); |
| | 986 | |
| | 987 | // Edit the post |
| | 988 | $post->post_title = 'Updated'; |
| | 989 | $post->post_content = 'Updated'; |
| | 990 | wp_update_post( $post ); |
| | 991 | |
| | 992 | // Make sure it's still sticky |
| | 993 | $saved_post = get_post( $post->ID ); |
| | 994 | $this->assertTrue( is_sticky( $saved_post->ID ) ); |
| | 995 | $this->assertEquals( 'Updated', $saved_post->post_title ); |
| | 996 | $this->assertEquals( 'Updated', $saved_post->post_content ); |
| | 997 | |
| | 998 | // Teardown |
| | 999 | wp_set_current_user( $old_uid ); |
| | 1000 | } |
| | 1001 | |
| | 1002 | /** |
| | 1003 | * If the `edit_post()` method is invoked by a user without publish_posts permission, the sticky status of the post |
| | 1004 | * should not be changed. |
| | 1005 | * |
| | 1006 | * @ticket 24153 |
| | 1007 | */ |
| | 1008 | function test_user_without_publish_cannot_affect_sticky_with_edit_post() { |
| | 1009 | // Create a sticky post |
| | 1010 | $post = $this->factory->post->create_and_get( array( |
| | 1011 | 'post_title' => 'Will be changed', |
| | 1012 | 'post_content' => 'Will be changed', |
| | 1013 | ) ); |
| | 1014 | stick_post( $post->ID ); |
| | 1015 | |
| | 1016 | // Sanity Check |
| | 1017 | $this->assertTrue( is_sticky( $post->ID ) ); |
| | 1018 | |
| | 1019 | // Create a role with edit_others_posts |
| | 1020 | add_role( 'grammarian', 'Grammarian', array( |
| | 1021 | 'read' => true, |
| | 1022 | 'edit_posts' => true, |
| | 1023 | 'edit_others_posts' => true, |
| | 1024 | 'edit_published_posts' => true, |
| | 1025 | ) ); |
| | 1026 | $editor_user = $this->factory->user->create( array( 'role' => 'grammarian' ) ); |
| | 1027 | $old_uid = get_current_user_id(); |
| | 1028 | wp_set_current_user( $editor_user ); |
| | 1029 | |
| | 1030 | // Sanity Check |
| | 1031 | $this->assertFalse( current_user_can( 'publish_posts' ) ); |
| | 1032 | $this->assertTrue( current_user_can( 'edit_others_posts' ) ); |
| | 1033 | $this->assertTrue( current_user_can( 'edit_published_posts' ) ); |
| | 1034 | |
| | 1035 | // Edit the post - The key 'sticky' is intentionally unset. |
| | 1036 | $data = array( |
| | 1037 | 'post_ID' => $post->ID, |
| | 1038 | 'post_title' => 'Updated', |
| | 1039 | 'post_content' => 'Updated', |
| | 1040 | ); |
| | 1041 | edit_post( $data ); |
| | 1042 | |
| | 1043 | // Make sure it's still sticky |
| | 1044 | $saved_post = get_post( $post->ID ); |
| | 1045 | $this->assertTrue( is_sticky( $saved_post->ID ) ); |
| | 1046 | $this->assertEquals( 'Updated', $saved_post->post_title ); |
| | 1047 | $this->assertEquals( 'Updated', $saved_post->post_content ); |
| | 1048 | |
| | 1049 | // Teardown |
| | 1050 | wp_set_current_user( $old_uid ); |
| | 1051 | } |