Changeset 35193
- Timestamp:
- 10/15/2015 06:31:16 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/revisions.php
r31622 r35193 6 6 */ 7 7 class 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 } 8 28 9 29 function setUp() { … … 23 43 */ 24 44 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 29 45 //create a post as Author 30 wp_set_current_user( $author_user_id );46 wp_set_current_user( self::$author_user_id ); 31 47 $post = get_default_post_to_edit( 'post', true ); 32 48 $post_id = $post->ID; … … 35 51 36 52 //update post as Editor 37 wp_set_current_user( $editor_user_id );53 wp_set_current_user( self::$editor_user_id ); 38 54 wp_update_post( array( 'post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id ) ); 39 55 40 56 //restore back as Admin 41 wp_set_current_user( $admin_user_id );57 wp_set_current_user( self::$admin_user_id ); 42 58 $revisions = wp_get_post_revisions( $post->ID ); 43 59 $this->assertCount( 2, $revisions ); … … 46 62 $this->assertEquals( 'I cant spel werds.', $lastrevision->post_content ); 47 63 // #16215 48 $this->assertEquals( $author_user_id , $lastrevision->post_author);64 $this->assertEquals( self::$author_user_id , $lastrevision->post_author); 49 65 50 66 wp_restore_post_revision( $lastrevision->ID ); 51 67 52 68 //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 56 70 } 57 71 … … 135 149 */ 136 150 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 ) ); 149 160 } 150 161 151 162 // Author should be able to view the revisions fine 152 163 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 ) ); 154 165 } 155 166 } … … 160 171 */ 161 172 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 ) ); 172 180 } 173 181 174 182 // Author shouldn't be able to restore the revisions 175 183 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 ) ); 177 185 } 178 186 } … … 183 191 */ 184 192 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 ) ); 189 194 wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 190 195 wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); … … 194 199 $this->assertCount( 2, $revisions ); 195 200 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 ) ); 197 202 } 198 203 199 204 // Author should be able to diff the revisions fine 200 205 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 ) ); 202 207 } 203 208 } … … 214 219 ) ); 215 220 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 ) ); 228 230 } 229 231 230 232 // Author should be able to view the revisions fine 231 233 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 ) ); 233 235 } 234 236 } … … 245 247 ) ); 246 248 247 $author_user_id = $this->factory->user->create( array( 'role' => 'author' ) );248 $editor_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );249 250 249 // 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 ); 252 251 $editor_user->add_cap( 'edit_published_events' ); 253 252 254 253 //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 ) ); 262 261 } 263 262 264 263 // 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 ) ); 268 267 } 269 268 } … … 286 285 287 286 $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 ); 289 288 290 289 $post_id = $this->factory->post->create( array( 'post_type' => $this->post_type, 'post_status' => 'draft' ) ); … … 320 319 ) ); 321 320 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 ) ); 326 322 wp_update_post( array( 'post_content' => 'This content is much better', 'ID' => $post_id ) ); 327 323 wp_update_post( array( 'post_content' => 'This content is even better', 'ID' => $post_id ) ); … … 331 327 $this->assertCount( 2, $revisions ); 332 328 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 ) ); 334 330 } 335 331 336 332 // Author should be able to diff the revisions fine 337 333 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 ) ); 339 335 } 340 336 } … … 374 370 */ 375 371 function test_wp_get_post_revisions_should_order_by_ID_when_post_date_matches() { 376 global $wpdb;377 378 372 $post = $this->factory->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) ); 379 373
Note: See TracChangeset
for help on using the changeset viewer.