WordPress.org

Make WordPress Core

Changeset 35193


Ignore:
Timestamp:
10/15/2015 06:31:16 AM (5 years ago)
Author:
wonderboymusic
Message:

Unit Tests: upgrade the fixtures in Tests_Post_Revisions.

See #30017, #33968.

File:
1 edited

Legend:

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

    r31622 r35193  
    66 */
    77class Tests_Post_Revisions extends WP_UnitTestCase {
     8    protected static $admin_user_id;
     9    protected static $editor_user_id;
     10    protected static $author_user_id;
     11
     12    public static function wpSetUpBeforeClass( $factory ) {
     13        self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
     14        self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) );
     15        self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) );
     16    }
     17
     18    public static function wpTearDownAfterClass() {
     19        $ids = array( self::$admin_user_id, self::$editor_user_id, self::$author_user_id );
     20        foreach ( $ids as $id ) {
     21            if ( is_multisite() ) {
     22                wpmu_delete_user( $id );
     23            } else {
     24                wp_delete_user( $id );
     25            }
     26        }
     27    }
    828
    929    function setUp() {
     
    2343     */
    2444    function test_revision_restore_updates_edit_last_post_meta() {
    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 
    2945        //create a post as Author
    30         wp_set_current_user( $author_user_id );
     46        wp_set_current_user( self::$author_user_id );
    3147        $post = get_default_post_to_edit( 'post', true );
    3248        $post_id = $post->ID;
     
    3551
    3652        //update post as Editor
    37         wp_set_current_user( $editor_user_id );
     53        wp_set_current_user( self::$editor_user_id );
    3854        wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) );
    3955
    4056        //restore back as Admin
    41         wp_set_current_user( $admin_user_id );
     57        wp_set_current_user( self::$admin_user_id );
    4258        $revisions = wp_get_post_revisions( $post->ID );
    4359        $this->assertCount( 2, $revisions );
     
    4662        $this->assertEquals( 'I cant spel werds.', $lastrevision->post_content );
    4763        // #16215
    48         $this->assertEquals( $author_user_id , $lastrevision->post_author);
     64        $this->assertEquals( self::$author_user_id , $lastrevision->post_author);
    4965
    5066        wp_restore_post_revision( $lastrevision->ID );
    5167
    5268        //is post_meta correctly set to revision author
    53         $this->assertEquals( $admin_user_id, get_post_meta( $post_id, '_edit_last', true ) ); //after restoring user
    54 
    55         wp_set_current_user( 0 );
     69        $this->assertEquals( self::$admin_user_id, get_post_meta( $post_id, '_edit_last', true ) ); //after restoring user
    5670    }
    5771
     
    135149     */
    136150    function test_revision_view_caps_post() {
    137         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    138         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    139 
    140         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
    141         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    142 
    143         $revisions = wp_get_post_revisions( $post_id );
    144         $this->assertCount( 1, $revisions );
    145         $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
    146 
    147         foreach ( $revisions as $revision ) {
    148             $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
     151        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => self::$editor_user_id ) );
     152        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     153
     154        $revisions = wp_get_post_revisions( $post_id );
     155        $this->assertCount( 1, $revisions );
     156        $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) );
     157
     158        foreach ( $revisions as $revision ) {
     159            $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
    149160        }
    150161
    151162        // Author should be able to view the revisions fine
    152163        foreach ( $revisions as $revision ) {
    153             $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
     164            $this->assertTrue( user_can( self::$author_user_id, 'read_post', $revision->ID ) );
    154165        }
    155166    }
     
    160171     */
    161172    function test_revision_restore_caps_post() {
    162         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    163         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    164 
    165         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
    166         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    167 
    168         $revisions = wp_get_post_revisions( $post_id );
    169         $this->assertCount( 1, $revisions );
    170         foreach ( $revisions as $revision ) {
    171              $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
     173        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => self::$editor_user_id ) );
     174        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     175
     176        $revisions = wp_get_post_revisions( $post_id );
     177        $this->assertCount( 1, $revisions );
     178        foreach ( $revisions as $revision ) {
     179             $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) );
    172180        }
    173181
    174182        // Author shouldn't be able to restore the revisions
    175183        foreach ( $revisions as $revision ) {
    176              $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
     184             $this->assertFalse( user_can( self::$author_user_id, 'edit_post', $revision->post_parent ) );
    177185        }
    178186    }
     
    183191     */
    184192    function test_revision_diff_caps_post() {
    185         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    186         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    187 
    188         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) );
     193        $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => self::$editor_user_id ) );
    189194        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    190195        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
     
    194199        $this->assertCount( 2, $revisions );
    195200        foreach ( $revisions as $revision ) {
    196             $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
     201            $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
    197202        }
    198203
    199204        // Author should be able to diff the revisions fine
    200205        foreach ( $revisions as $revision ) {
    201             $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
     206            $this->assertTrue( user_can( self::$author_user_id, 'read_post', $revision->ID ) );
    202207        }
    203208    }
     
    214219        ) );
    215220
    216         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    217         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    218 
    219         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
    220         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    221 
    222         $revisions = wp_get_post_revisions( $post_id );
    223         $this->assertCount( 1, $revisions );
    224         $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) );
    225 
    226         foreach ( $revisions as $revision ) {
    227              $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
     221        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => self::$editor_user_id ) );
     222        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     223
     224        $revisions = wp_get_post_revisions( $post_id );
     225        $this->assertCount( 1, $revisions );
     226        $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $post_id ) );
     227
     228        foreach ( $revisions as $revision ) {
     229             $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
    228230        }
    229231
    230232        // Author should be able to view the revisions fine
    231233        foreach ( $revisions as $revision ) {
    232              $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
     234             $this->assertTrue( user_can( self::$author_user_id, 'read_post', $revision->ID ) );
    233235        }
    234236    }
     
    245247        ) );
    246248
    247         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    248         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    249 
    250249        // The minimum extra caps needed for this test normally you would give the role all the relevant caps.
    251         $editor_user = new WP_User( $editor_user_id );
     250        $editor_user = new WP_User( self::$editor_user_id );
    252251        $editor_user->add_cap( 'edit_published_events' );
    253252
    254253        //create a post as Editor
    255         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
    256         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    257 
    258         $revisions = wp_get_post_revisions( $post_id );
    259         $this->assertCount( 1, $revisions );
    260         foreach ( $revisions as $revision ) {
    261             $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) );
     254        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => self::$editor_user_id ) );
     255        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
     256
     257        $revisions = wp_get_post_revisions( $post_id );
     258        $this->assertCount( 1, $revisions );
     259        foreach ( $revisions as $revision ) {
     260            $this->assertTrue( user_can( self::$editor_user_id, 'edit_post', $revision->post_parent ) );
    262261        }
    263262
    264263        // Author shouldn't be able to restore the revisions
    265         wp_set_current_user( $author_user_id );
    266         foreach ( $revisions as $revision ) {
    267             $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) );
     264        wp_set_current_user( self::$author_user_id );
     265        foreach ( $revisions as $revision ) {
     266            $this->assertFalse( user_can( self::$author_user_id, 'edit_post', $revision->post_parent ) );
    268267        }
    269268    }
     
    286285
    287286        $old_id = get_current_user_id();
    288         wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
     287        wp_set_current_user( self::$editor_user_id );
    289288
    290289        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_status' => 'draft' ) );
     
    320319        ) );
    321320
    322         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    323         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    324 
    325         $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => $editor_user_id ) );
     321        $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_author' => self::$editor_user_id ) );
    326322        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) );
    327323        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) );
     
    331327        $this->assertCount( 2, $revisions );
    332328        foreach ( $revisions as $revision ) {
    333             $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) );
     329            $this->assertTrue( user_can( self::$editor_user_id, 'read_post', $revision->ID ) );
    334330        }
    335331
    336332        // Author should be able to diff the revisions fine
    337333        foreach ( $revisions as $revision ) {
    338             $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) );
     334            $this->assertTrue( user_can( self::$author_user_id, 'read_post', $revision->ID ) );
    339335        }
    340336    }
     
    374370     */
    375371    function test_wp_get_post_revisions_should_order_by_ID_when_post_date_matches() {
    376         global $wpdb;
    377 
    378372        $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
    379373
Note: See TracChangeset for help on using the changeset viewer.