Make WordPress Core

Ticket #42464: 42464.2.diff

File 42464.2.diff, 6.1 KB (added by peterwilsoncc, 8 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 45fb3573b8..94a450f438 100644
    a b function wp_insert_post( $postarr, $wp_error = false ) { 
    33043304                }
    33053305        }
    33063306
    3307         // Don't allow contributors to set the post slug for pending review posts.
    3308         if ( 'pending' == $post_status && ! current_user_can( 'publish_posts' ) ) {
     3307        /*
     3308         * Don't allow contributors to set the post slug for pending review posts.
     3309         *
     3310         * For new posts check the primitive capability, for updates check the meta capability.
     3311         */
     3312        $post_type_object = get_post_type_object( $post_type );
     3313
     3314        if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
     3315                $post_name = '';
     3316        } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) {
    33093317                $post_name = '';
    33103318        }
    33113319
  • tests/phpunit/tests/post/wpInsertPost.php

    diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php
    index ff38968c0a..9df211ccc8 100644
    a b  
    55 */
    66class Tests_WPInsertPost extends WP_UnitTestCase {
    77
     8        protected static $user_ids = array(
     9                'administrator' => null,
     10                'contributor'   => null,
     11        );
     12
     13        static function wpSetUpBeforeClass( $factory ) {
     14                self::$user_ids = array(
     15                        'administrator' => $factory->user->create( array( 'role' => 'administrator' ) ),
     16                        'contributor'   => $factory->user->create( array( 'role' => 'contributor' ) ),
     17                );
     18
     19                $role = get_role( 'administrator' );
     20                $role->add_cap( 'publish_mapped_meta_caps' );
     21                $role->add_cap( 'publish_unmapped_meta_caps' );
     22        }
     23
     24        function setUp() {
     25                parent::setUp();
     26
     27                register_post_type( 'mapped_meta_caps', array(
     28                        'capability_type' => array( 'mapped_meta_cap', 'mapped_meta_caps' ),
     29                        'map_meta_cap'    => true,
     30                ) );
     31
     32                register_post_type( 'unmapped_meta_caps', array(
     33                        'capability_type' => array( 'unmapped_meta_cap', 'unmapped_meta_caps' ),
     34                        'map_meta_cap'    => false,
     35                ) );
     36
     37                register_post_type( 'no_admin_caps', array(
     38                        'capability_type' => array( 'no_admin_cap', 'no_admin_caps' ),
     39                        'map_meta_cap'    => false,
     40                ) );
     41        }
     42
    843        /**
    944         * @ticket 11863
    1045         */
    class Tests_WPInsertPost extends WP_UnitTestCase { 
    103138                $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name );
    104139                $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name );
    105140        }
     141
     142        /**
     143         * Data for testing the ability for users to set the post slug.
     144         *
     145         * @return array Array of test arguments.
     146         */
     147        function data_various_post_types() {
     148                /*
     149                 * 1. Post type (see setUp).
     150                 */
     151                return array(
     152                        array( 'mapped_meta_caps' ),
     153                        array( 'unmapped_meta_caps' ),
     154                        array( 'post' ),
     155                );
     156        }
     157
     158        /**
     159         * Test contributor making changes to the pending post slug.
     160         *
     161         * @ticket 42464
     162         * @dataProvider data_various_post_types
     163         */
     164        function test_contributor_setting_post_slug( $post_type ) {
     165                wp_set_current_user( self::$user_ids['contributor'] );
     166
     167                $post_id = $this->factory()->post->create( array(
     168                        'post_title'   => 'Jefferson claim: nice to have Washington on your side.',
     169                        'post_content' => "I’m in the cabinet. I am complicit in watching him grabbin’ at power and kiss it.\n\nIf Washington isn’t gon’ listen to disciplined dissidents, this is the difference: this kid is out!",
     170                        'post_type'    => $post_type,
     171                        'post_name'    => 'new-washington',
     172                        'post_status'  => 'pending',
     173                ) );
     174
     175                $expected = '';
     176                $actual = get_post_field( 'post_name', $post_id );
     177
     178                $this->assertEquals( $expected, $actual );
     179
     180                // Now update the post.
     181                wp_update_post( array(
     182                        'ID' => $post_id,
     183                        'post_title' => 'Hamilton has Washington on side: Jefferson',
     184                        'post_name'  => 'edited-washington',
     185                ) );
     186
     187                $expected = '';
     188                $actual = get_post_field( 'post_name', $post_id );
     189
     190                $this->assertEquals( $expected, $actual );
     191        }
     192
     193        /**
     194         * Test administrator making changes to the pending post slug.
     195         *
     196         * @ticket 42464
     197         * @dataProvider data_various_post_types
     198         */
     199        function test_administrator_setting_post_slug( $post_type ) {
     200                wp_set_current_user( self::$user_ids['administrator'] );
     201
     202                $post_id = $this->factory()->post->create( array(
     203                        'post_title'   => 'What is the Conner Project?',
     204                        'post_content' => "Evan Hansen’s last link to his friend Conner is a signature on his broken arm.",
     205                        'post_type'    => $post_type,
     206                        'post_name'    => 'dear-evan-hansen-explainer',
     207                        'post_status'  => 'pending',
     208                ) );
     209
     210                $expected = 'dear-evan-hansen-explainer';
     211                $actual = get_post_field( 'post_name', $post_id );
     212
     213                $this->assertEquals( $expected, $actual );
     214
     215                // Now update the post.
     216                wp_update_post( array(
     217                        'ID' => $post_id,
     218                        'post_title' => 'Conner Project to close',
     219                        'post_name'  => 'dear-evan-hansen-spoiler',
     220                ) );
     221
     222                $expected = 'dear-evan-hansen-spoiler';
     223                $actual = get_post_field( 'post_name', $post_id );
     224
     225                $this->assertEquals( $expected, $actual );
     226        }
     227
     228        /**
     229         * Test administrator making changes tp a pending post slug for a post type they don't
     230         * have permission to publish.
     231         *
     232         * These assertions failed prior to ticket #42464.
     233         *
     234         * @ticket 42464
     235         */
     236        function test_administrator_not_permitted_setting_post_slug() {
     237                wp_set_current_user( self::$user_ids['administrator'] );
     238
     239                $post_id = $this->factory()->post->create( array(
     240                        'post_title'   => 'Everything is legal in New Jersey',
     241                        'post_content' => 'Shortly before his death, Philip Hamilton was heard to claim everything was legal in the garden state.',
     242                        'post_type'    => 'no_admin_caps',
     243                        'post_name'    => 'yet-another-duel',
     244                        'post_status'  => 'pending',
     245                ) );
     246
     247                $expected = '';
     248                $actual = get_post_field( 'post_name', $post_id );
     249
     250                $this->assertEquals( $expected, $actual );
     251
     252                // Now update the post.
     253                wp_update_post( array(
     254                        'ID' => $post_id,
     255                        'post_title' => 'Ten things illegal in New Jersey',
     256                        'post_name'  => 'foreshadowing-in-nj',
     257                ) );
     258
     259                $expected = '';
     260                $actual = get_post_field( 'post_name', $post_id );
     261
     262                $this->assertEquals( $expected, $actual );
     263        }
     264
    106265}