Make WordPress Core

Changeset 1213 in tests for trunk/tests/post/revisions.php


Ignore:
Timestamp:
02/14/2013 03:27:37 PM (12 years ago)
Author:
nacin
Message:

Add a revisions capability test for when the post cannot be edited once published because the edit_published_posts cap is denied. See [1212/tests]. See #16847. Trims westi's whitespace.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/post/revisions.php

    r1212 r1213  
    2626        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    2727        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    28        
     28
    2929        //create a post as Author
    3030        wp_set_current_user( $author_user_id );
    3131        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_content' => 'I cant spel werds.' ) );
    32        
     32
    3333        //update post as Editor
    3434        wp_set_current_user( $editor_user_id );
    3535        wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) );
    36        
     36
    3737        //restore back as Admin
    3838        wp_set_current_user( $admin_user_id );
    3939        $revisions = wp_get_post_revisions( $post_id );
    4040        $this->assertEquals( count( $revisions ), 1 );
    41        
     41
    4242        $lastrevision = end( $revisions );
    4343        $this->assertEquals( $lastrevision->post_content, 'I cant spel werds.' );
    4444        // #16215
    4545        $this->assertEquals( $lastrevision->post_author, $author_user_id );
    46        
     46
    4747        wp_restore_post_revision( $lastrevision->ID );
    48        
     48
    4949        //is post_meta correctly set to revision author
    5050        $this->assertEquals( get_post_meta( $post_id, '_edit_last', true ), $author_user_id ); //after restoring user
     
    5757    function test_revision_dont_save_revision_if_unchanged() {
    5858        $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
    59        
     59
    6060        wp_update_post( array( 'post_content'   => 'some updated content', 'ID' => $post_id ) );    //1st revision
    6161        $this->assertEquals( 1, count( wp_get_post_revisions( $post_id ) ) ); //should be 1 revision so far
    62        
     62
    6363        //update the post
    6464        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision
    6565        $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should be 2 revision so far
    66        
     66
    6767        //next try to save another identical update, tests for patch that prevents storing duplicates
    6868        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
    6969        $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should still be 2 revision
    70        
     70
    7171        //next try to save another update, same content, but new ttile, should save revision
    7272        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) );
    7373        $this->assertEquals( 3, count( wp_get_post_revisions( $post_id ) ) ); //should  be 3 revision
    74        
     74
    7575        //next try to save another identical update
    7676        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
     
    8585        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    8686        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    87        
     87
    8888        //create a post as Editor
    8989        wp_set_current_user( $editor_user_id );
    9090        $post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
    9191        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    92        
     92
    9393        $revisions = wp_get_post_revisions( $post_id );
    9494        $this->assertEquals( count( $revisions ), 1 );
    9595        $this->assertTrue( current_user_can( 'read_post', $post_id ) );
    96        
    97         foreach ( $revisions as $revision ) {
    98             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
    99         }
    100        
     96
     97        foreach ( $revisions as $revision ) {
     98            $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     99        }
     100
    101101        // Author should be able to view the revisions fine
    102102        wp_set_current_user( $author_user_id );
     
    113113        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    114114        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    115        
     115
    116116        //create a post as Editor
    117117        wp_set_current_user( $editor_user_id );
    118118        $post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
    119119        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    120        
     120
    121121        $revisions = wp_get_post_revisions( $post_id );
    122122        $this->assertEquals( count( $revisions ), 1 );
     
    124124             $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
    125125        }
    126        
     126
    127127        // Author shouldn't be able to restore the revisions
    128128        wp_set_current_user( $author_user_id );
     
    139139        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    140140        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    141        
     141
    142142        //create a post as Editor
    143143        wp_set_current_user( $editor_user_id );
     
    145145        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    146146        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
    147        
     147
    148148        // Diff checks if you can read both left and right revisions
    149149        $revisions = wp_get_post_revisions( $post_id );
     
    152152            $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
    153153        }
    154        
     154
    155155        // Author should be able to diff the revisions fine
    156156        wp_set_current_user( $author_user_id );
     
    178178        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) );
    179179        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    180        
     180
    181181        $revisions = wp_get_post_revisions( $post_id );
    182182        $this->assertEquals( count( $revisions ), 1 );
    183183        $this->assertTrue( current_user_can( 'read_post', $post_id ) );
    184        
     184
    185185        foreach ( $revisions as $revision ) {
    186186             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
    187187        }
    188        
     188
    189189        // Author should be able to view the revisions fine
    190190        wp_set_current_user( $author_user_id );
     
    223223        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) );
    224224        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    225        
     225
    226226        $revisions = wp_get_post_revisions( $post_id );
    227227        $this->assertEquals( count( $revisions ), 1 );
     
    229229            $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
    230230        }
    231        
     231
    232232        // Author shouldn't be able to restore the revisions
    233233        wp_set_current_user( $author_user_id );
     
    235235            $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) );
    236236        }
     237    }
     238
     239    /**
     240     * Tests the Caps used in the action=restore case of wp-admin/revision.php
     241     * @ticket 16847
     242     */
     243    function test_revision_restore_caps_before_publish() {
     244        register_post_type( $this->post_type, array(
     245            'capability_type' => 'post',
     246            'capabilities' => array(
     247                // No one can edit this post type once published.
     248                // So, revisions cannot be restored, either.
     249                'edit_published_posts' => 'do_not_allow',
     250            ),
     251            'map_meta_cap' => true,
     252            'supports' => array( 'revisions' ),
     253        ) );
     254
     255        $old_id = get_current_user_id();
     256        wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
     257
     258        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_status' => 'draft' ) );
     259        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     260
     261        $revisions = wp_get_post_revisions( $post_id );
     262        $this->assertCount( 1, $revisions );
     263        foreach ( $revisions as $revision ) {
     264            $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
     265            $this->assertTrue( current_user_can( 'edit_post', $revision->ID ) );
     266        }
     267
     268        wp_update_post( array( 'post_status' => 'publish', 'ID' => $post_id ) );
     269
     270        $revisions = wp_get_post_revisions( $post_id );
     271        $this->assertCount( 2, $revisions );
     272        foreach ( $revisions as $revision ) {
     273            $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) );
     274            $this->assertFalse( current_user_can( 'edit_post', $revision->ID ) );
     275        }
     276        wp_set_current_user( $old_id );
    237277    }
    238278
     
    256296        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    257297        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
    258        
     298
    259299        // Diff checks if you can read both left and right revisions
    260300        $revisions = wp_get_post_revisions( $post_id );
     
    263303            $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
    264304        }
    265        
     305
    266306        // Author should be able to diff the revisions fine
    267307        wp_set_current_user( $author_user_id );
Note: See TracChangeset for help on using the changeset viewer.