WordPress.org

Make WordPress Core

Changeset 1214 in tests


Ignore:
Timestamp:
02/14/2013 04:43:55 PM (7 years ago)
Author:
westi
Message:

Revisions: Add a new test for a force save filter in wp_save_post_revision

Also Update the tests to have correct ordering, be less complex and use things like assertCount.
See #7392 and #9843

File:
1 edited

Legend:

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

    r1213 r1214  
    3838        wp_set_current_user( $admin_user_id );
    3939        $revisions = wp_get_post_revisions( $post_id );
    40         $this->assertEquals( count( $revisions ), 1 );
     40        $this->assertCount( 1, $revisions );
    4141
    4242        $lastrevision = end( $revisions );
    43         $this->assertEquals( $lastrevision->post_content, 'I cant spel werds.' );
     43        $this->assertEquals( 'I cant spel werds.', $lastrevision->post_content );
    4444        // #16215
    45         $this->assertEquals( $lastrevision->post_author, $author_user_id );
     45        $this->assertEquals( $author_user_id , $lastrevision->post_author);
    4646
    4747        wp_restore_post_revision( $lastrevision->ID );
    4848
    4949        //is post_meta correctly set to revision author
    50         $this->assertEquals( get_post_meta( $post_id, '_edit_last', true ), $author_user_id ); //after restoring user
     50        $this->assertEquals( $author_user_id, get_post_meta( $post_id, '_edit_last', true ) ); //after restoring user
     51
     52        wp_set_current_user( 0 );
    5153    }
    5254
     
    5961
    6062        wp_update_post( array( 'post_content'   => 'some updated content', 'ID' => $post_id ) );    //1st revision
    61         $this->assertEquals( 1, count( wp_get_post_revisions( $post_id ) ) ); //should be 1 revision so far
     63        $this->assertCount( 1, wp_get_post_revisions( $post_id ) ); //should be 1 revision so far
    6264
    6365        //update the post
    6466        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision
    65         $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should be 2 revision so far
     67        $this->assertCount( 2, wp_get_post_revisions( $post_id ) ); //should be 2 revision so far
    6668
    6769        //next try to save another identical update, tests for patch that prevents storing duplicates
    6870        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
    69         $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should still be 2 revision
     71        $this->assertCount( 2, wp_get_post_revisions( $post_id ) ); //should still be 2 revision
    7072
    7173        //next try to save another update, same content, but new ttile, should save revision
    7274        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) );
    73         $this->assertEquals( 3, count( wp_get_post_revisions( $post_id ) ) ); //should  be 3 revision
     75        $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should  be 3 revision
    7476
    7577        //next try to save another identical update
    7678        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
    77         $this->assertEquals( 3, count( wp_get_post_revisions( $post_id ) ) ); //should still be 3 revision
     79        $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should still be 3 revision
     80    }
     81
     82    /**
     83    * @ticket 7392
     84    * @ticket 9843
     85    */
     86    function test_revision_force_save_revision_even_if_unchanged() {
     87        add_filter( 'wp_save_post_revision_check_for_changes', '__return_false' );
     88
     89        $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
     90       
     91        wp_update_post( array( 'post_content'   => 'some updated content', 'ID' => $post_id ) );    //1st revision
     92        $this->assertCount( 1, wp_get_post_revisions( $post_id ) );
     93       
     94        //update the post
     95        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision
     96        $this->assertCount( 2, wp_get_post_revisions( $post_id ) );
     97       
     98        //next try to save another identical update, tests for patch that prevents storing duplicates
     99        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save
     100        $this->assertCount( 3, wp_get_post_revisions( $post_id ) );
     101       
     102        //next try to save another update, same content, but new ttile, should save revision
     103        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) );
     104        $this->assertCount( 4, wp_get_post_revisions( $post_id ) );
     105       
     106        //next try to save another identical update
     107        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
     108        $this->assertCount( 5, wp_get_post_revisions( $post_id ) );
     109
     110        remove_filter( 'wp_save_post_revision_check_for_changes', '__return_false' );
    78111    }
    79112
     
    86119        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    87120
    88         //create a post as Editor
    89         wp_set_current_user( $editor_user_id );
    90         $post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
    91         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    92 
    93         $revisions = wp_get_post_revisions( $post_id );
    94         $this->assertEquals( count( $revisions ), 1 );
    95         $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 ) );
     121        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
     122        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     123
     124        $revisions = wp_get_post_revisions( $post_id );
     125        $this->assertCount( 1, $revisions );
     126        $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
     127
     128        foreach ( $revisions as $revision ) {
     129            $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
    99130        }
    100131
    101132        // Author should be able to view the revisions fine
    102         wp_set_current_user( $author_user_id );
    103         foreach ( $revisions as $revision ) {
    104             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     133        foreach ( $revisions as $revision ) {
     134            $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
    105135        }
    106136    }
     
    114144        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    115145
    116         //create a post as Editor
    117         wp_set_current_user( $editor_user_id );
    118         $post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
    119         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    120 
    121         $revisions = wp_get_post_revisions( $post_id );
    122         $this->assertEquals( count( $revisions ), 1 );
    123         foreach ( $revisions as $revision ) {
    124              $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
     146        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
     147        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     148
     149        $revisions = wp_get_post_revisions( $post_id );
     150        $this->assertCount( 1, $revisions );
     151        foreach ( $revisions as $revision ) {
     152             $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
    125153        }
    126154
    127155        // Author shouldn't be able to restore the revisions
    128         wp_set_current_user( $author_user_id );
    129         foreach ( $revisions as $revision ) {
    130              $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) );
     156        foreach ( $revisions as $revision ) {
     157             $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
    131158        }
    132159    }
     
    140167        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    141168
    142         //create a post as Editor
    143         wp_set_current_user( $editor_user_id );
    144         $post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
     169        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
    145170        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    146171        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
     
    148173        // Diff checks if you can read both left and right revisions
    149174        $revisions = wp_get_post_revisions( $post_id );
    150         $this->assertEquals( count( $revisions ), 2 );
    151         foreach ( $revisions as $revision ) {
    152             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     175        $this->assertCount( 2, $revisions );
     176        foreach ( $revisions as $revision ) {
     177            $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
    153178        }
    154179
    155180        // Author should be able to diff the revisions fine
    156         wp_set_current_user( $author_user_id );
    157         foreach ( $revisions as $revision ) {
    158             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     181        foreach ( $revisions as $revision ) {
     182            $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
    159183        }
    160184    }
     
    174198        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    175199
    176         //create a post as Editor
    177         wp_set_current_user( $editor_user_id );
    178         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) );
    179         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    180 
    181         $revisions = wp_get_post_revisions( $post_id );
    182         $this->assertEquals( count( $revisions ), 1 );
    183         $this->assertTrue( current_user_can( 'read_post', $post_id ) );
    184 
    185         foreach ( $revisions as $revision ) {
    186              $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     200        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
     201        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     202
     203        $revisions = wp_get_post_revisions( $post_id );
     204        $this->assertCount( 1, $revisions );
     205        $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
     206
     207        foreach ( $revisions as $revision ) {
     208             $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
    187209        }
    188210
    189211        // Author should be able to view the revisions fine
    190         wp_set_current_user( $author_user_id );
    191         foreach ( $revisions as $revision ) {
    192              $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     212        foreach ( $revisions as $revision ) {
     213             $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
    193214        }
    194215    }
     
    209230
    210231        // The minimum extra caps needed for this test normally you would give the role all the relevant caps.
    211         $cpt_cap_map = array(
    212             'edit_published_posts' => 'edit_published_events',
    213         );
    214 
    215232        $editor_user = new WP_User( $editor_user_id );
    216         foreach( $cpt_cap_map as $post_cap => $cpt_cap ) {
    217             if ( $editor_user->has_cap( $post_cap ) )
    218                 $editor_user->add_cap( $cpt_cap );
    219         }
     233        $editor_user->add_cap( 'edit_published_events' );
    220234
    221235        //create a post as Editor
    222         wp_set_current_user( $editor_user_id );
    223         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) );
    224         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    225 
    226         $revisions = wp_get_post_revisions( $post_id );
    227         $this->assertEquals( count( $revisions ), 1 );
    228         foreach ( $revisions as $revision ) {
    229             $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) );
     236        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
     237        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     238
     239        $revisions = wp_get_post_revisions( $post_id );
     240        $this->assertCount( 1, $revisions );
     241        foreach ( $revisions as $revision ) {
     242            $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
    230243        }
    231244
     
    233246        wp_set_current_user( $author_user_id );
    234247        foreach ( $revisions as $revision ) {
    235             $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) );
     248            $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
    236249        }
    237250    }
     
    291304        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    292305
    293         //create a post as Editor
    294         wp_set_current_user( $editor_user_id );
    295         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) );
     306        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
    296307        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    297308        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
     
    301312        $this->assertEquals( count( $revisions ), 2 );
    302313        foreach ( $revisions as $revision ) {
    303             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     314            $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
    304315        }
    305316
    306317        // Author should be able to diff the revisions fine
    307         wp_set_current_user( $author_user_id );
    308         foreach ( $revisions as $revision ) {
    309             $this->assertTrue( current_user_can( 'read_post', $revision->ID ) );
     318        foreach ( $revisions as $revision ) {
     319            $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
    310320        }
    311321    }
Note: See TracChangeset for help on using the changeset viewer.