Make WordPress Core

Changeset 49605


Ignore:
Timestamp:
11/15/2020 02:57:39 PM (6 years ago)
Author:
johnbillion
Message:

Built/Test Tools: Switch to a data provider for the default user role and capability tests.

This test previously performed 1,010 assertions, and a failure in any one would prevent the other assertions from running. Using a data provider means simultaneous failures will all be reported at once.

See #51344, #32394

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r49604 r49605  
    323323                        'assign_post_tags'            => array( 'administrator', 'editor', 'author', 'contributor' ),
    324324                );
     325        }
     326
     327        public function dataAllCapsAndRoles() {
     328                $data = array();
     329                $caps = $this->getAllCapsAndRoles();
     330
     331                foreach ( self::$users as $role => $null ) {
     332                        foreach ( $caps as $cap => $roles ) {
     333                                $data[] = array(
     334                                        $role,
     335                                        $cap,
     336                                );
     337                        }
     338                }
     339
     340                return $data;
    325341        }
    326342
     
    521537
    522538        /**
    523          * Test the default roles and caps.
    524          */
    525         function test_all_roles_and_caps() {
    526                 $caps = $this->getAllCapsAndRoles();
    527 
     539         * Test the default capabilities of all user roles.
     540         *
     541         * @dataProvider dataAllCapsAndRoles
     542         */
     543        function test_default_caps_for_all_roles( $role, $cap ) {
     544                $user         = self::$users[ $role ];
     545                $roles_by_cap = $this->getAllCapsAndRoles();
     546
     547                if ( in_array( $role, $roles_by_cap[ $cap ], true ) ) {
     548                        $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
     549                        $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
     550                } else {
     551                        $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
     552                        $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
     553                }
     554        }
     555
     556        /**
     557         * Test miscellaneous capabilities of all user roles.
     558         */
     559        function test_other_caps_for_all_roles() {
    528560                foreach ( self::$users as $role => $user ) {
    529 
    530561                        // Make sure the user is valid.
    531562                        $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
     
    533564                        // Make sure the role name is correct.
    534565                        $this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" );
    535 
    536                         foreach ( $caps as $cap => $roles ) {
    537                                 if ( in_array( $role, $roles, true ) ) {
    538                                         $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
    539                                         $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
    540                                 } else {
    541                                         $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
    542                                         $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
    543                                 }
    544                         }
    545566
    546567                        $this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
Note: See TracChangeset for help on using the changeset viewer.