Make WordPress Core

Ticket #32394: ticket-32394.patch

File ticket-32394.patch, 9.5 KB (added by anubisthejackle, 8 years ago)

Add all capabilities from Codex.

  • 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 { 
    3838                return $meta_value;
    3939        }
    4040
     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
    41125        // test the default roles
    42126
    43127        function test_user_administrator() {
     128
    44129                $id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    45130                $user = new WP_User($id);
    46131                $this->assertTrue($user->exists(), "Problem getting user $id");
    class Tests_User_Capabilities extends WP_UnitTestCase { 
    49134                $this->assertEquals(array('administrator'), $user->roles);
    50135
    51136                // 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'));
    55137                $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
    56144        }
    57145
    58146        function test_user_editor() {
    class Tests_User_Capabilities extends WP_UnitTestCase { 
    64152                $this->assertEquals(array('editor'), $user->roles);
    65153
    66154                // 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'));
    70155                $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 );
    71160
    72161                // 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 );
    75163                $this->assertFalse($user->has_cap('level_8'));
    76164        }
    77165
    class Tests_User_Capabilities extends WP_UnitTestCase { 
    84172                $this->assertEquals(array('author'), $user->roles);
    85173
    86174                // 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'));
    90175                $this->assertTrue($user->has_cap('level_2'));
     176                $this->_authorRoles( $user, true );
     177                $this->_contributorRoles( $user, true );
     178                $this->_subscriberRoles( $user, true );
    91179
    92180                // 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'));
    95181                $this->assertFalse($user->has_cap('level_3'));
     182                $this->_administratorRoles( $user, false );
     183                $this->_editorRoles( $user, false );
     184
    96185        }
    97186
    98187        function test_user_contributor() {
    class Tests_User_Capabilities extends WP_UnitTestCase { 
    104193                $this->assertEquals(array('contributor'), $user->roles);
    105194
    106195                // 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 );
    109198                $this->assertTrue($user->has_cap('level_1'));
    110199                $this->assertTrue($user->has_cap('level_0'));
    111200
    112201                // 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'));
    115202                $this->assertFalse($user->has_cap('level_2'));
     203                $this->_administratorRoles( $user, false );
     204                $this->_editorRoles( $user, false );
     205                $this->_authorRoles( $user, false );
     206
    116207        }
    117208
    118209        function test_user_subscriber() {
     210
    119211                $id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    120212                $user = new WP_User($id);
    121213                $this->assertTrue($user->exists(), "Problem getting user $id");
    class Tests_User_Capabilities extends WP_UnitTestCase { 
    124216                $this->assertEquals(array('subscriber'), $user->roles);
    125217
    126218                // check a few of the main capabilities
    127                 $this->assertTrue($user->has_cap('read'));
     219                $this->_subscriberRoles( $user, true );
    128220                $this->assertTrue($user->has_cap('level_0'));
    129221
    130222                // 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'));
    133223                $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
    134229        }
    135230
    136231        // 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 { 
    7474
    7575                $this->$method($user->has_cap('moderate_comments'));
    7676                $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' ) );
    7890                $this->$method($user->has_cap('edit_others_posts'));
    7991                $this->$method($user->has_cap('edit_pages'));
    8092                $this->$method($user->has_cap('edit_others_pages'));