Make WordPress Core

Ticket #42464: 42464.diff

File 42464.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 1dbf0c9867..d5502ea687 100644
    a b function wp_insert_post( $postarr, $wp_error = false ) { 
    31583158                }
    31593159        }
    31603160
    3161         // Don't allow contributors to set the post slug for pending review posts.
    3162         if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) {
     3161        /*
     3162         * Don't allow contributors to set the post slug for pending review posts.
     3163         *
     3164         * For new posts check the primitive capability, for updates check the meta capability.
     3165         */
     3166        $post_type_object = get_post_type_object( $post_type );
     3167
     3168        if (
     3169                ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) OR
     3170                ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) )
     3171        ) {
    31633172                $post_name = '';
    31643173        }
    31653174
  • tests/phpunit/tests/post/wpInsertPost.php

    diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php
    index f0e150a8c8..b04e23c4d6 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 { 
    89124                $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name );
    90125                $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name );
    91126        }
     127
     128        /**
     129         * Data for testing the ability for users to set the post slug.
     130         *
     131         * @return array Array of test arguments.
     132         */
     133        function data_various_post_types() {
     134                /*
     135                 * 1. Post type (see setUp).
     136                 */
     137                return array(
     138                        array( 'mapped_meta_caps' ),
     139                        array( 'unmapped_meta_caps' ),
     140                        array( 'post' ),
     141                );
     142        }
     143
     144        /**
     145         * Test contributor making changes to the pending post slug.
     146         *
     147         * @ticket 42464
     148         * @dataProvider data_various_post_types
     149         */
     150        function test_contributor_setting_post_slug( $post_type ) {
     151                wp_set_current_user( self::$user_ids['contributor'] );
     152
     153                $post_id = $this->factory()->post->create( array(
     154                        'post_title'   => 'Jefferson claim: nice to have Washington on your side.',
     155                        '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!",
     156                        'post_type'    => $post_type,
     157                        'post_name'    => 'new-washington',
     158                        'post_status'  => 'pending',
     159                ) );
     160
     161                $expected = '';
     162                $actual = get_post_field( 'post_name', $post_id );
     163
     164                $this->assertEquals( $expected, $actual );
     165
     166                // Now update the post.
     167                wp_update_post( array(
     168                        'ID' => $post_id,
     169                        'post_title' => 'Hamilton has Washington on side: Jefferson',
     170                        'post_name'  => 'edited-washington',
     171                ) );
     172
     173                $expected = '';
     174                $actual = get_post_field( 'post_name', $post_id );
     175
     176                $this->assertEquals( $expected, $actual );
     177        }
     178
     179        /**
     180         * Test administrator making changes to the pending post slug.
     181         *
     182         * @ticket 42464
     183         * @dataProvider data_various_post_types
     184         */
     185        function test_administrator_setting_post_slug( $post_type ) {
     186                wp_set_current_user( self::$user_ids['administrator'] );
     187
     188                $post_id = $this->factory()->post->create( array(
     189                        'post_title'   => 'What is the Conner Project?',
     190                        'post_content' => "Evan Hansen’s last link to his friend Conner is a signature on his broken arm.",
     191                        'post_type'    => $post_type,
     192                        'post_name'    => 'dear-evan-hansen-explainer',
     193                        'post_status'  => 'pending',
     194                ) );
     195
     196                $expected = 'dear-evan-hansen-explainer';
     197                $actual = get_post_field( 'post_name', $post_id );
     198
     199                $this->assertEquals( $expected, $actual );
     200
     201                // Now update the post.
     202                wp_update_post( array(
     203                        'ID' => $post_id,
     204                        'post_title' => 'Conner Project to close',
     205                        'post_name'  => 'dear-evan-hansen-spoiler',
     206                ) );
     207
     208                $expected = 'dear-evan-hansen-spoiler';
     209                $actual = get_post_field( 'post_name', $post_id );
     210
     211                $this->assertEquals( $expected, $actual );
     212        }
     213
     214        /**
     215         * Test administrator making changes tp a pending post slug for a post type they don't
     216         * have permission to publish.
     217         *
     218         * These assertions failed prior to ticket #42464.
     219         *
     220         * @ticket 42464
     221         */
     222        function test_administrator_not_permitted_setting_post_slug() {
     223                wp_set_current_user( self::$user_ids['administrator'] );
     224
     225                $post_id = $this->factory()->post->create( array(
     226                        'post_title'   => 'Everything is legal in New Jersey',
     227                        'post_content' => 'Shortly before his death, Philip Hamilton was heard to claim everything was legal in the garden state.',
     228                        'post_type'    => 'no_admin_caps',
     229                        'post_name'    => 'yet-another-duel',
     230                        'post_status'  => 'pending',
     231                ) );
     232
     233                $expected = '';
     234                $actual = get_post_field( 'post_name', $post_id );
     235
     236                $this->assertEquals( $expected, $actual );
     237
     238                // Now update the post.
     239                wp_update_post( array(
     240                        'ID' => $post_id,
     241                        'post_title' => 'Ten things illegal in New Jersey',
     242                        'post_name'  => 'foreshadowing-in-nj',
     243                ) );
     244
     245                $expected = '';
     246                $actual = get_post_field( 'post_name', $post_id );
     247
     248                $this->assertEquals( $expected, $actual );
     249        }
     250
    92251}