WordPress.org

Make WordPress Core

Changeset 1212 in tests


Ignore:
Timestamp:
02/14/13 14:07:04 (14 months ago)
Author:
westi
Message:

Revisions: Test the capability checks used in wp-admin/revision.php against how we expect them to behave both with a built-in and a custom post type

See #16847 - Trying to explore CPTs and Custom Capabilities with revisions edit/view/restore

File:
1 edited

Legend:

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

    r1211 r1212  
    66 */ 
    77class Tests_Post_Revisions extends WP_UnitTestCase { 
     8 
     9    function setUp() { 
     10        parent::setUp(); 
     11        $this->post_type = rand_str( 20 ); 
     12    } 
     13 
     14    function tearDown() { 
     15        parent::tearDown(); 
     16        unset( $GLOBALS['wp_post_types'][ $this->post_type ] ); 
     17    } 
     18 
    819    /** 
    920     * Note: Test needs reviewing when #16215 is fixed because I'm not sure the test current tests the "correct" behavior 
     
    1223     */ 
    1324    function test_revision_restore_updates_edit_last_post_meta() { 
    14          $admin_user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 
    15          $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
    16          $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
    17  
    18          //create a post as Author 
    19          wp_set_current_user( $author_user_id ); 
    20          $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_content' => 'I cant spel werds.' ) ); 
    21  
    22          //update post as Editor 
    23          wp_set_current_user( $editor_user_id ); 
    24          wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) ); 
    25  
    26          //restore back as Admin 
    27          wp_set_current_user( $admin_user_id ); 
    28          $revisions = wp_get_post_revisions( $post_id ); 
    29          $this->assertEquals( count( $revisions ), 1 ); 
    30  
    31          $lastrevision = end( $revisions ); 
    32          $this->assertEquals( $lastrevision->post_content, 'I cant spel werds.' ); 
    33          // #16215 
    34          $this->assertEquals( $lastrevision->post_author, $author_user_id ); 
    35  
    36          wp_restore_post_revision( $lastrevision->ID ); 
    37  
    38          //is post_meta correctly set to revision author 
    39          $this->assertEquals( get_post_meta( $post_id, '_edit_last', true ), $author_user_id ); //after restoring user 
     25        $admin_user_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 
     26        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     27        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     28         
     29        //create a post as Author 
     30        wp_set_current_user( $author_user_id ); 
     31        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_content' => 'I cant spel werds.' ) ); 
     32         
     33        //update post as Editor 
     34        wp_set_current_user( $editor_user_id ); 
     35        wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) ); 
     36         
     37        //restore back as Admin 
     38        wp_set_current_user( $admin_user_id ); 
     39        $revisions = wp_get_post_revisions( $post_id ); 
     40        $this->assertEquals( count( $revisions ), 1 ); 
     41         
     42        $lastrevision = end( $revisions ); 
     43        $this->assertEquals( $lastrevision->post_content, 'I cant spel werds.' ); 
     44        // #16215 
     45        $this->assertEquals( $lastrevision->post_author, $author_user_id ); 
     46         
     47        wp_restore_post_revision( $lastrevision->ID ); 
     48         
     49        //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 
    4051    } 
    4152 
     
    4657    function test_revision_dont_save_revision_if_unchanged() { 
    4758        $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) ); 
    48  
     59         
    4960        wp_update_post( array( 'post_content'   => 'some updated content', 'ID' => $post_id ) );    //1st revision 
    5061        $this->assertEquals( 1, count( wp_get_post_revisions( $post_id ) ) ); //should be 1 revision so far 
    51  
     62         
    5263        //update the post 
    5364        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //2nd revision 
    5465        $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should be 2 revision so far 
    55  
     66         
    5667        //next try to save another identical update, tests for patch that prevents storing duplicates 
    5768        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save 
    5869        $this->assertEquals( 2, count( wp_get_post_revisions( $post_id ) ) ); //should still be 2 revision 
    59  
     70         
    6071        //next try to save another update, same content, but new ttile, should save revision 
    6172        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) ); 
    6273        $this->assertEquals( 3, count( wp_get_post_revisions( $post_id ) ) ); //should  be 3 revision 
    63  
     74         
    6475        //next try to save another identical update 
    6576        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 
    6677        $this->assertEquals( 3, count( wp_get_post_revisions( $post_id ) ) ); //should still be 3 revision 
    6778    } 
     79 
     80    /** 
     81     * Tests the Caps used in the action=view case of wp-admin/revision.php 
     82     * @ticket 16847 
     83     */ 
     84    function test_revision_view_caps_post() { 
     85        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     86        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     87         
     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 ) ); 
     99        } 
     100         
     101        // 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 ) ); 
     105        } 
     106    } 
     107 
     108    /** 
     109     * Tests the Caps used in the action=restore case of wp-admin/revision.php 
     110     * @ticket 16847 
     111     */ 
     112    function test_revision_restore_caps_post() { 
     113        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     114        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     115         
     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 ) ); 
     125        } 
     126         
     127        // 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 ) ); 
     131        } 
     132    } 
     133 
     134    /** 
     135     * Tests the Caps used in the action=diff case of wp-admin/revision.php 
     136     * @ticket 16847 
     137     */ 
     138    function test_revision_diff_caps_post() { 
     139        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     140        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     141         
     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' ) ); 
     145        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 
     146        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); 
     147         
     148        // Diff checks if you can read both left and right revisions 
     149        $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 ) ); 
     153        } 
     154         
     155        // 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 ) ); 
     159        } 
     160    } 
     161 
     162    /** 
     163     * Tests the Caps used in the action=view case of wp-admin/revision.php with a CPT with Custom Capabilities 
     164     * @ticket 16847 
     165     */ 
     166    function test_revision_view_caps_cpt() { 
     167        register_post_type( $this->post_type, array( 
     168            'capability_type' => 'event', 
     169            'map_meta_cap' => true, 
     170            'supports' => array( 'revisions' ), 
     171        ) ); 
     172 
     173        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     174        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     175 
     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 ) ); 
     187        } 
     188         
     189        // 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 ) ); 
     193        } 
     194    } 
     195 
     196    /** 
     197     * Tests the Caps used in the action=restore case of wp-admin/revision.php 
     198     * @ticket 16847 
     199     */ 
     200    function test_revision_restore_caps_cpt() { 
     201        register_post_type( $this->post_type, array( 
     202            'capability_type' => 'event', 
     203            'map_meta_cap' => true, 
     204            'supports' => array( 'revisions' ), 
     205        ) ); 
     206 
     207        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     208        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     209 
     210        // 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 
     215        $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        } 
     220 
     221        //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 ) ); 
     230        } 
     231         
     232        // Author shouldn't be able to restore the revisions 
     233        wp_set_current_user( $author_user_id ); 
     234        foreach ( $revisions as $revision ) { 
     235            $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) ); 
     236        } 
     237    } 
     238 
     239    /** 
     240     * Tests the Caps used in the action=diff case of wp-admin/revision.php 
     241     * @ticket 16847 
     242     */ 
     243    function test_revision_diff_caps_cpt() { 
     244        register_post_type( $this->post_type, array( 
     245            'capability_type' => 'event', 
     246            'map_meta_cap' => true, 
     247            'supports' => array( 'revisions' ), 
     248        ) ); 
     249 
     250        $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
     251        $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
     252 
     253        //create a post as Editor 
     254        wp_set_current_user( $editor_user_id ); 
     255        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type ) ); 
     256        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 
     257        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); 
     258         
     259        // Diff checks if you can read both left and right revisions 
     260        $revisions = wp_get_post_revisions( $post_id ); 
     261        $this->assertEquals( count( $revisions ), 2 ); 
     262        foreach ( $revisions as $revision ) { 
     263            $this->assertTrue( current_user_can( 'read_post', $revision->ID ) ); 
     264        } 
     265         
     266        // Author should be able to diff the revisions fine 
     267        wp_set_current_user( $author_user_id ); 
     268        foreach ( $revisions as $revision ) { 
     269            $this->assertTrue( current_user_can( 'read_post', $revision->ID ) ); 
     270        } 
     271    } 
    68272} 
Note: See TracChangeset for help on using the changeset viewer.