Changeset 39189 for trunk/tests/phpunit/tests/user/mapMetaCap.php
- Timestamp:
- 11/10/2016 01:53:08 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/user/mapMetaCap.php
r38378 r39189 6 6 */ 7 7 class Tests_User_MapMetaCap extends WP_UnitTestCase { 8 var $super_admins = null; 9 10 function setUp() { 11 parent::setUp(); 12 13 $this->user_ids = array(); 14 15 $this->user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); 16 $this->author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); 17 18 if ( isset( $GLOBALS['super_admins'] ) ) 19 $this->super_admins = $GLOBALS['super_admins']; 20 $user = new WP_User( $this->user_id ); 8 9 protected static $post_type = 'mapmetacap'; 10 protected static $super_admins = null; 11 protected static $user_id = null; 12 protected static $author_id = null; 13 protected static $post_id = null; 14 15 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 16 self::$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); 17 self::$author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); 18 19 if ( isset( $GLOBALS['super_admins'] ) ) { 20 self::$super_admins = $GLOBALS['super_admins']; 21 } 22 $user = new WP_User( self::$user_id ); 21 23 $GLOBALS['super_admins'] = array( $user->user_login ); 22 24 23 $this->post_type = rand_str( 20 ); 24 register_post_type( $this->post_type ); 25 26 $this->post_id = wp_insert_post( array( 27 'post_title' => rand_str(), 28 'post_type' => $this->post_type, 25 register_post_type( self::$post_type ); 26 27 self::$post_id = $factory->post->create( array( 28 'post_type' => self::$post_type, 29 29 'post_status' => 'private', 30 'post_author' => $this->author_id, 31 ) ); 32 } 33 34 function tearDown() { 35 $GLOBALS['super_admins'] = $this->super_admins; 36 unset( $GLOBALS['wp_post_types'][ $this->post_type ] ); 37 parent::tearDown(); 30 'post_author' => self::$author_id, 31 ) ); 32 } 33 34 public static function wpTearDownAfterClass() { 35 $GLOBALS['super_admins'] = self::$super_admins; 36 unset( $GLOBALS['wp_post_types'][ self::$post_type ] ); 38 37 } 39 38 … … 44 43 $this->assertEquals( 45 44 array( 'do_not_allow' ), 46 map_meta_cap( 'edit_post', $this->user_id, $this->post_id + 1 )45 map_meta_cap( 'edit_post', self::$user_id, self::$post_id + 1 ) 47 46 ); 48 47 } … … 50 49 function test_capability_type_post_with_no_extra_caps() { 51 50 52 register_post_type( $this->post_type, array(53 'capability_type' => 'post', 54 ) ); 55 $post_type_object = get_post_type_object( $this->post_type );51 register_post_type( self::$post_type, array( 52 'capability_type' => 'post', 53 ) ); 54 $post_type_object = get_post_type_object( self::$post_type ); 56 55 57 56 $this->assertTrue( $post_type_object->map_meta_cap ); 58 57 59 58 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 60 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );61 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 62 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );63 64 $this->assertEquals( array( 'read_private_posts' ), 65 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );66 $this->assertEquals( array( 'read_private_posts' ), 67 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );68 69 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 70 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );71 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 72 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );59 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 60 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 61 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 62 63 $this->assertEquals( array( 'read_private_posts' ), 64 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 65 $this->assertEquals( array( 'read_private_posts' ), 66 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 67 68 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 69 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 70 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 71 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 73 72 } 74 73 75 74 function test_custom_capability_type_with_map_meta_cap() { 76 register_post_type( $this->post_type, array(75 register_post_type( self::$post_type, array( 77 76 'capability_type' => 'book', 78 77 'map_meta_cap' => true, 79 78 ) ); 80 79 81 $post_type_object = get_post_type_object( $this->post_type );80 $post_type_object = get_post_type_object( self::$post_type ); 82 81 83 82 $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ), 84 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );83 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 85 84 $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ), 86 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );85 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 87 86 88 87 $this->assertEquals( array( 'read_private_books' ), 89 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );88 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 90 89 $this->assertEquals( array( 'read_private_books' ), 91 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );90 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 92 91 93 92 $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ), 94 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );93 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 95 94 $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ), 96 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );95 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 97 96 } 98 97 99 98 function test_capability_type_post_with_one_renamed_cap() { 100 register_post_type( $this->post_type, array(99 register_post_type( self::$post_type, array( 101 100 'capability_type' => 'post', 102 101 'capabilities' => array( 'edit_posts' => 'edit_books' ), 103 102 ) ); 104 103 105 $post_type_object = get_post_type_object( $this->post_type );104 $post_type_object = get_post_type_object( self::$post_type ); 106 105 107 106 $this->assertFalse( $post_type_object->map_meta_cap ); 108 107 109 108 $this->assertEquals( array( 'edit_post' ), 110 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );109 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 111 110 $this->assertEquals( array( 'edit_post' ), 112 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );111 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 113 112 114 113 $this->assertEquals( array( 'read_post' ), 115 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );114 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 116 115 $this->assertEquals( array( 'read_post' ), 117 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );116 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 118 117 119 118 $this->assertEquals( array( 'delete_post' ), 120 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );119 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 121 120 $this->assertEquals( array( 'delete_post' ), 122 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );121 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 123 122 } 124 123 125 124 function test_capability_type_post_map_meta_cap_true_with_renamed_cap() { 126 register_post_type( $this->post_type, array(125 register_post_type( self::$post_type, array( 127 126 'capability_type' => 'post', 128 127 'map_meta_cap' => true, … … 133 132 ) ); 134 133 135 $post_type_object = get_post_type_object( $this->post_type );134 $post_type_object = get_post_type_object( self::$post_type ); 136 135 137 136 $this->assertTrue( $post_type_object->map_meta_cap ); 138 137 139 138 $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ), 140 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );139 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 141 140 $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ), 142 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );143 144 $this->assertEquals( array( 'read_private_posts' ), 145 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );146 $this->assertEquals( array( 'read_private_posts' ), 147 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );148 149 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 150 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );151 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 152 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );141 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 142 143 $this->assertEquals( array( 'read_private_posts' ), 144 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 145 $this->assertEquals( array( 'read_private_posts' ), 146 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 147 148 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 149 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 150 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 151 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 153 152 } 154 153 155 154 function test_capability_type_post_with_all_meta_caps_renamed() { 156 register_post_type( $this->post_type, array(155 register_post_type( self::$post_type, array( 157 156 'capability_type' => 'post', 158 157 'capabilities' => array( … … 163 162 ) ); 164 163 165 $post_type_object = get_post_type_object( $this->post_type );164 $post_type_object = get_post_type_object( self::$post_type ); 166 165 167 166 $this->assertFalse( $post_type_object->map_meta_cap ); 168 167 169 168 $this->assertEquals( array( 'edit_book' ), 170 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );169 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 171 170 $this->assertEquals( array( 'edit_book' ), 172 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );171 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 173 172 174 173 $this->assertEquals( array( 'read_book' ), 175 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );174 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 176 175 $this->assertEquals( array( 'read_book' ), 177 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );176 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 178 177 179 178 $this->assertEquals( array( 'delete_book' ), 180 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );179 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 181 180 $this->assertEquals( array( 'delete_book' ), 182 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );181 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 183 182 } 184 183 185 184 function test_capability_type_post_with_all_meta_caps_renamed_mapped() { 186 register_post_type( $this->post_type, array(185 register_post_type( self::$post_type, array( 187 186 'capability_type' => 'post', 188 187 'map_meta_cap' => true, … … 194 193 ) ); 195 194 196 $post_type_object = get_post_type_object( $this->post_type );195 $post_type_object = get_post_type_object( self::$post_type ); 197 196 198 197 $this->assertTrue( $post_type_object->map_meta_cap ); 199 198 200 199 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 201 map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );202 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 203 map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );204 205 $this->assertEquals( array( 'read_private_posts' ), 206 map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );207 $this->assertEquals( array( 'read_private_posts' ), 208 map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );209 210 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 211 map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );212 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 213 map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );200 map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) ); 201 $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ), 202 map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) ); 203 204 $this->assertEquals( array( 'read_private_posts' ), 205 map_meta_cap( 'read_post', self::$user_id, self::$post_id ) ); 206 $this->assertEquals( array( 'read_private_posts' ), 207 map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) ); 208 209 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 210 map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) ); 211 $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ), 212 map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) ); 214 213 } 215 214 … … 219 218 if ( is_multisite() ) { 220 219 $this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) ); 221 $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );220 $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) ); 222 221 } else { 223 $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );222 $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) ); 224 223 } 225 224 } … … 239 238 } 240 239 241 $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );242 $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );240 $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', self::$user_id ) ); 241 $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', self::$user_id ) ); 243 242 } 244 243 … … 269 268 270 269 update_option( 'page_on_front', $post_id ); 271 $caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );270 $caps = map_meta_cap( 'delete_page', self::$user_id, $post_id ); 272 271 delete_option( 'page_on_front' ); 273 272 … … 287 286 288 287 update_option( 'page_for_posts', $post_id ); 289 $caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );288 $caps = map_meta_cap( 'delete_page', self::$user_id, $post_id ); 290 289 delete_option( 'page_for_posts' ); 291 290
Note: See TracChangeset
for help on using the changeset viewer.