Ticket #32394: ticket-32394.patch
File ticket-32394.patch, 9.5 KB (added by , 8 years ago) |
---|
-
tests/phpunit/tests/user/capabilities.php
From f725b680b07151b75eb71ef92980328c0d95ae26 Mon Sep 17 00:00:00 2001 From: Travis Weston <tweston@bangordailynews.com> Date: Wed, 20 May 2015 21:06:47 -0400 Subject: [PATCH 1/2] Add more methods, and every capability found in the codex --- tests/phpunit/tests/user/capabilities.php | 135 +++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 20 deletions(-) diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 6a1044b..39396dc 100644
a b class Tests_User_Capabilities extends WP_UnitTestCase { 38 38 return $meta_value; 39 39 } 40 40 41 function _administratorRoles( $user, $has = true ) { 42 43 $method = ( $has === true ) ? 'assertTrue' : 'assertFalse'; 44 45 $this->$method( $user->has_cap('activate_plugins') ); 46 $this->$method( $user->has_cap('create_users') ); 47 $this->$method( $user->has_cap('delete_plugins') ); 48 $this->$method( $user->has_cap('delete_themes') ); 49 $this->$method( $user->has_cap('delete_users') ); 50 $this->$method( $user->has_cap('edit_files') ); 51 $this->$method( $user->has_cap('edit_plugins') ); 52 $this->$method( $user->has_cap('edit_theme_options') ); 53 $this->$method( $user->has_cap('edit_themes') ); 54 $this->$method( $user->has_cap('edit_users') ); 55 $this->$method( $user->has_cap('export') ); 56 $this->$method( $user->has_cap('import') ); 57 $this->$method( $user->has_cap('install_plugins') ); 58 $this->$method( $user->has_cap('install_themes') ); 59 $this->$method( $user->has_cap('list_users') ); 60 $this->$method( $user->has_cap('manage_options') ); 61 $this->$method( $user->has_cap('promote_users') ); 62 $this->$method( $user->has_cap('remove_users') ); 63 $this->$method( $user->has_cap('switch_themes') ); 64 $this->$method( $user->has_cap('update_core') ); 65 $this->$method( $user->has_cap('update_plugins') ); 66 $this->$method( $user->has_cap('update_themes') ); 67 $this->$method( $user->has_cap('edit_dashboard') ); 68 69 } 70 71 function _editorRoles( $user, $has = true ) { 72 73 $method = ( $has === true ) ? 'assertTrue' : 'assertFalse'; 74 75 $this->$method($user->has_cap('moderate_comments')); 76 $this->$method($user->has_cap('manage_categories')); 77 $this->$method($user->has_cap('manage_links')); 78 $this->$method($user->has_cap('edit_others_posts')); 79 $this->$method($user->has_cap('edit_pages')); 80 $this->$method($user->has_cap('edit_others_pages')); 81 $this->$method($user->has_cap('edit_published_pages')); 82 $this->$method($user->has_cap('publish_pages')); 83 $this->$method($user->has_cap('delete_pages')); 84 $this->$method($user->has_cap('delete_others_posts')); 85 $this->$method($user->has_cap('delete_published_pages')); 86 $this->$method($user->has_cap('delete_others_posts')); 87 $this->$method($user->has_cap('delete_private_posts')); 88 $this->$method($user->has_cap('edit_private_posts')); 89 $this->$method($user->has_cap('read_private_posts')); 90 $this->$method($user->has_cap('delete_private_pages')); 91 $this->$method($user->has_cap('edit_private_pages')); 92 $this->$method($user->has_cap('read_private_pages')); 93 $this->$method($user->has_cap('unfiltered_html')); 94 95 } 96 97 function _authorRoles( $user, $has = true ) { 98 99 $method = ( $has === true ) ? 'assertTrue' : 'assertFalse'; 100 101 $this->$method($user->has_cap('edit_published_posts')); 102 $this->$method($user->has_cap('upload_files')); 103 $this->$method($user->has_cap('publish_posts')); 104 $this->$method($user->has_cap('delete_published_posts')); 105 106 } 107 108 function _contributorRoles( $user, $has = true ) { 109 110 $method = ( $has === true ) ? 'assertTrue' : 'assertFalse'; 111 112 $this->$method($user->has_cap('edit_posts')); 113 $this->$method($user->has_cap('delete_posts')); 114 115 } 116 117 function _subscriberRoles( $user, $has = true ) { 118 119 $method = ( $has === true ) ? 'assertTrue' : 'assertFalse'; 120 121 $this->$method($user->has_cap('read')); 122 123 } 124 41 125 // test the default roles 42 126 43 127 function test_user_administrator() { 128 44 129 $id = $this->factory->user->create( array( 'role' => 'administrator' ) ); 45 130 $user = new WP_User($id); 46 131 $this->assertTrue($user->exists(), "Problem getting user $id"); … … class Tests_User_Capabilities extends WP_UnitTestCase { 49 134 $this->assertEquals(array('administrator'), $user->roles); 50 135 51 136 // check a few of the main capabilities 52 $this->assertTrue($user->has_cap('switch_themes'));53 $this->assertTrue($user->has_cap('list_users'));54 $this->assertTrue($user->has_cap('manage_options'));55 137 $this->assertTrue($user->has_cap('level_10')); 138 $this->_administratorRoles( $user, true ); 139 $this->_editorRoles( $user, true ); 140 $this->_authorRoles( $user, true ); 141 $this->_contributorRoles( $user, true ); 142 $this->_subscriberRoles( $user, true ); 143 56 144 } 57 145 58 146 function test_user_editor() { … … class Tests_User_Capabilities extends WP_UnitTestCase { 64 152 $this->assertEquals(array('editor'), $user->roles); 65 153 66 154 // check a few of the main capabilities 67 $this->assertTrue($user->has_cap('moderate_comments'));68 $this->assertTrue($user->has_cap('manage_categories'));69 $this->assertTrue($user->has_cap('upload_files'));70 155 $this->assertTrue($user->has_cap('level_7')); 156 $this->_editorRoles( $user, true ); 157 $this->_authorRoles( $user, true ); 158 $this->_contributorRoles( $user, true ); 159 $this->_subscriberRoles( $user, true ); 71 160 72 161 // and a few capabilities this user doesn't have 73 $this->assertFalse($user->has_cap('switch_themes')); 74 $this->assertFalse($user->has_cap('edit_users')); 162 $this->_administratorRoles( $user, false ); 75 163 $this->assertFalse($user->has_cap('level_8')); 76 164 } 77 165 … … class Tests_User_Capabilities extends WP_UnitTestCase { 84 172 $this->assertEquals(array('author'), $user->roles); 85 173 86 174 // check a few of the main capabilities 87 $this->assertTrue($user->has_cap('edit_posts'));88 $this->assertTrue($user->has_cap('edit_published_posts'));89 $this->assertTrue($user->has_cap('upload_files'));90 175 $this->assertTrue($user->has_cap('level_2')); 176 $this->_authorRoles( $user, true ); 177 $this->_contributorRoles( $user, true ); 178 $this->_subscriberRoles( $user, true ); 91 179 92 180 // and a few capabilities this user doesn't have 93 $this->assertFalse($user->has_cap('moderate_comments'));94 $this->assertFalse($user->has_cap('manage_categories'));95 181 $this->assertFalse($user->has_cap('level_3')); 182 $this->_administratorRoles( $user, false ); 183 $this->_editorRoles( $user, false ); 184 96 185 } 97 186 98 187 function test_user_contributor() { … … class Tests_User_Capabilities extends WP_UnitTestCase { 104 193 $this->assertEquals(array('contributor'), $user->roles); 105 194 106 195 // check a few of the main capabilities 107 $this-> assertTrue($user->has_cap('edit_posts'));108 $this-> assertTrue($user->has_cap('read'));196 $this->_contributorRoles( $user, true ); 197 $this->_subscriberRoles( $user, true ); 109 198 $this->assertTrue($user->has_cap('level_1')); 110 199 $this->assertTrue($user->has_cap('level_0')); 111 200 112 201 // and a few capabilities this user doesn't have 113 $this->assertFalse($user->has_cap('upload_files'));114 $this->assertFalse($user->has_cap('edit_published_posts'));115 202 $this->assertFalse($user->has_cap('level_2')); 203 $this->_administratorRoles( $user, false ); 204 $this->_editorRoles( $user, false ); 205 $this->_authorRoles( $user, false ); 206 116 207 } 117 208 118 209 function test_user_subscriber() { 210 119 211 $id = $this->factory->user->create( array( 'role' => 'subscriber' ) ); 120 212 $user = new WP_User($id); 121 213 $this->assertTrue($user->exists(), "Problem getting user $id"); … … class Tests_User_Capabilities extends WP_UnitTestCase { 124 216 $this->assertEquals(array('subscriber'), $user->roles); 125 217 126 218 // check a few of the main capabilities 127 $this-> assertTrue($user->has_cap('read'));219 $this->_subscriberRoles( $user, true ); 128 220 $this->assertTrue($user->has_cap('level_0')); 129 221 130 222 // and a few capabilities this user doesn't have 131 $this->assertFalse($user->has_cap('upload_files'));132 $this->assertFalse($user->has_cap('edit_posts'));133 223 $this->assertFalse($user->has_cap('level_1')); 224 $this->_administratorRoles( $user, false ); 225 $this->_editorRoles( $user, false ); 226 $this->_authorRoles( $user, false ); 227 $this->_contributorRoles( $user, false ); 228 134 229 } 135 230 136 231 // a role that doesn't exist -
tests/phpunit/tests/user/capabilities.php
-- 2.1.4 From f224327ff1a29d0e5c8a93edef5d79619b29f2ea Mon Sep 17 00:00:00 2001 From: Travis Weston <tweston@bangordailynews.com> Date: Wed, 20 May 2015 22:30:37 -0400 Subject: [PATCH 2/2] Properly test manage_links in all it's forms --- tests/phpunit/tests/user/capabilities.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 39396dc..0358d3f 100644
a b class Tests_User_Capabilities extends WP_UnitTestCase { 74 74 75 75 $this->$method($user->has_cap('moderate_comments')); 76 76 $this->$method($user->has_cap('manage_categories')); 77 $this->$method($user->has_cap('manage_links')); 77 78 if( $has === true ) { 79 80 update_option('link_manager_enabled', true); 81 $this->assertTrue( get_option('link_manager_enabled') ); 82 $this->assertTrue( $user->has_cap( 'manage_links' ) ); 83 84 update_option('link_manager_enabled', false); 85 $this->assertFalse( get_option('link_manager_enabled') ); 86 87 } 88 89 $this->assertFalse( $user->has_cap( 'manage_links' ) ); 78 90 $this->$method($user->has_cap('edit_others_posts')); 79 91 $this->$method($user->has_cap('edit_pages')); 80 92 $this->$method($user->has_cap('edit_others_pages'));