WordPress.org

Make WordPress Core

Changeset 1309 in tests


Ignore:
Timestamp:
07/10/13 22:42:57 (12 months ago)
Author:
nacin
Message:

Rewrite revisions unit tests to account for [23842].

wp_insert_post() (and thus the factory) does *not* create a revision by default. When it is desired for the first "save" to be a revision, an auto-draft should be used.

The various cap-checking tests still use wp_insert_post() to test that route as well, hence the decrementing of revision counts.

see #24708.

File:
1 edited

Legend:

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

    r1277 r1309  
    2929        //create a post as Author 
    3030        wp_set_current_user( $author_user_id ); 
    31         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_content' => 'I cant spel werds.' ) ); 
     31        $post = get_default_post_to_edit( 'post', true ); 
     32        $post_id = $post->ID; 
     33 
     34        wp_update_post( array( 'post_status' => 'draft', 'post_content' => 'I cant spel werds.', 'ID' => $post_id ) ); 
    3235 
    3336        //update post as Editor 
     
    3740        //restore back as Admin 
    3841        wp_set_current_user( $admin_user_id ); 
    39         $revisions = wp_get_post_revisions( $post_id ); 
     42        $revisions = wp_get_post_revisions( $post->ID ); 
    4043        $this->assertCount( 2, $revisions ); 
    4144 
     
    5861    */ 
    5962    function test_revision_dont_save_revision_if_unchanged() { 
    60         $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) ); 
     63        $post = get_default_post_to_edit( 'post', true ); 
     64        $post_id = $post->ID; 
     65 
     66        $this->assertCount( 0, wp_get_post_revisions( $post_id ) ); // No revisions on auto-draft creation. 
     67 
     68        wp_update_post( array( 'post_status' => 'draft', 'post_title' => 'some-post', 'post_content' => 'some_content', 'ID' => $post_id ) ); 
     69 
    6170        $this->assertCount( 1, wp_get_post_revisions( $post_id ) ); // Just the initial revision 
    6271 
     
    7281        //next try to save another identical update, tests for patch that prevents storing duplicates 
    7382        wp_update_post( array( 'post_content'   => 'new update for some updated content', 'ID' => $post_id ) ); //content unchanged, shouldn't save 
    74         $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should still be 2 revision 
     83        $this->assertCount( 3, wp_get_post_revisions( $post_id ) ); //should still be 3 revision 
    7584 
    7685        //next try to save another update, same content, but new ttile, should save revision 
    7786        wp_update_post( array( 'post_title' => 'some-post-changed', 'post_content'  => 'new update for some updated content', 'ID' => $post_id ) ); 
    78         $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should  be 3 revision 
     87        $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should  be 4 revision 
    7988 
    8089        //next try to save another identical update 
    8190        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 
    82         $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should still be 3 revision 
     91        $this->assertCount( 4, wp_get_post_revisions( $post_id ) ); //should still be 4 revision 
    8392    } 
    8493 
     
    9099        add_filter( 'wp_save_post_revision_check_for_changes', '__return_false' ); 
    91100 
    92         $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) ); 
     101        $post = get_default_post_to_edit( 'post', true ); 
     102        $post_id = $post->ID; 
     103 
     104        $this->assertCount( 0, wp_get_post_revisions( $post_id ) ); // No revisions on auto-draft creation. 
     105 
     106        wp_update_post( array( 'post_status' => 'draft', 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content', 'ID' => $post_id ) ); 
     107 
    93108        $this->assertCount( 1, wp_get_post_revisions( $post_id ) ); 
    94109 
     
    127142 
    128143        $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 ) ); 
     149        } 
     150 
     151        // Author should be able to view the revisions fine 
     152        foreach ( $revisions as $revision ) { 
     153            $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) ); 
     154        } 
     155    } 
     156 
     157    /** 
     158     * Tests the Caps used in the action=restore case of wp-admin/revision.php 
     159     * @ticket 16847 
     160     */ 
     161    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 ) ); 
     172        } 
     173 
     174        // Author shouldn't be able to restore the revisions 
     175        foreach ( $revisions as $revision ) { 
     176             $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) ); 
     177        } 
     178    } 
     179 
     180    /** 
     181     * Tests the Caps used in the action=diff case of wp-admin/revision.php 
     182     * @ticket 16847 
     183     */ 
     184    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 ) ); 
     189        wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 
     190        wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); 
     191 
     192        // Diff checks if you can read both left and right revisions 
     193        $revisions = wp_get_post_revisions( $post_id ); 
    129194        $this->assertCount( 2, $revisions ); 
    130         $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) ); 
    131  
    132         foreach ( $revisions as $revision ) { 
    133             $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) ); 
    134         } 
    135  
    136         // Author should be able to view the revisions fine 
    137         foreach ( $revisions as $revision ) { 
    138             $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) ); 
    139         } 
    140     } 
    141  
    142     /** 
    143      * Tests the Caps used in the action=restore case of wp-admin/revision.php 
    144      * @ticket 16847 
    145      */ 
    146     function test_revision_restore_caps_post() { 
    147         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
    148         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
    149  
    150         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) ); 
    151         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 
    152  
    153         $revisions = wp_get_post_revisions( $post_id ); 
    154         $this->assertCount( 2, $revisions ); 
    155         foreach ( $revisions as $revision ) { 
    156              $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) ); 
    157         } 
    158  
    159         // Author shouldn't be able to restore the revisions 
    160         foreach ( $revisions as $revision ) { 
    161              $this->assertFalse( user_can( $author_user_id, 'edit_post', $revision->post_parent ) ); 
    162         } 
    163     } 
    164  
    165     /** 
    166      * Tests the Caps used in the action=diff case of wp-admin/revision.php 
    167      * @ticket 16847 
    168      */ 
    169     function test_revision_diff_caps_post() { 
    170         $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 
    171         $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) ); 
    172  
    173         $post_id = $this->factory->post->create( array( 'post_type' => 'post', 'post_author' => $editor_user_id ) ); 
    174         wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 
    175         wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); 
    176  
    177         // Diff checks if you can read both left and right revisions 
    178         $revisions = wp_get_post_revisions( $post_id ); 
    179         $this->assertCount( 3, $revisions ); 
    180195        foreach ( $revisions as $revision ) { 
    181196            $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) ); 
     
    206221 
    207222        $revisions = wp_get_post_revisions( $post_id ); 
    208         $this->assertCount( 2, $revisions ); 
     223        $this->assertCount( 1, $revisions ); 
    209224        $this->assertTrue( user_can( $editor_user_id, 'read_post', $post_id ) ); 
    210225 
     
    242257 
    243258        $revisions = wp_get_post_revisions( $post_id ); 
    244         $this->assertCount( 2, $revisions ); 
     259        $this->assertCount( 1, $revisions ); 
    245260        foreach ( $revisions as $revision ) { 
    246261            $this->assertTrue( user_can( $editor_user_id, 'edit_post', $revision->post_parent ) ); 
     
    277292 
    278293        $revisions = wp_get_post_revisions( $post_id ); 
    279         $this->assertCount( 2, $revisions ); 
     294        $this->assertCount( 1, $revisions ); 
    280295        foreach ( $revisions as $revision ) { 
    281296            $this->assertTrue( current_user_can( 'edit_post', $revision->post_parent ) ); 
     
    286301 
    287302        $revisions = wp_get_post_revisions( $post_id ); 
    288         $this->assertCount( 3, $revisions ); 
     303        $this->assertCount( 2, $revisions ); 
    289304        foreach ( $revisions as $revision ) { 
    290305            $this->assertFalse( current_user_can( 'edit_post', $revision->post_parent ) ); 
     
    314329        // Diff checks if you can read both left and right revisions 
    315330        $revisions = wp_get_post_revisions( $post_id ); 
    316         $this->assertEquals( count( $revisions ), 3 ); 
     331        $this->assertCount( 2, $revisions ); 
    317332        foreach ( $revisions as $revision ) { 
    318333            $this->assertTrue( user_can( $editor_user_id, 'read_post', $revision->ID ) ); 
Note: See TracChangeset for help on using the changeset viewer.