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


git-svn-id: https://develop.svn.wordpress.org/trunk@49605 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2020-11-15 14:57:39 +00:00
parent ea852af151
commit cd9ed5e3ad

View File

@ -324,6 +324,22 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
);
}
public function dataAllCapsAndRoles() {
$data = array();
$caps = $this->getAllCapsAndRoles();
foreach ( self::$users as $role => $null ) {
foreach ( $caps as $cap => $roles ) {
$data[] = array(
$role,
$cap,
);
}
}
return $data;
}
protected function getAllCapsAndRoles() {
return $this->getPrimitiveCapsAndRoles() + $this->getMetaCapsAndRoles();
}
@ -520,21 +536,15 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
/**
* Test the default roles and caps.
* Test the default capabilities of all user roles.
*
* @dataProvider dataAllCapsAndRoles
*/
function test_all_roles_and_caps() {
$caps = $this->getAllCapsAndRoles();
function test_default_caps_for_all_roles( $role, $cap ) {
$user = self::$users[ $role ];
$roles_by_cap = $this->getAllCapsAndRoles();
foreach ( self::$users as $role => $user ) {
// Make sure the user is valid.
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
// Make sure the role name is correct.
$this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" );
foreach ( $caps as $cap => $roles ) {
if ( in_array( $role, $roles, true ) ) {
if ( in_array( $role, $roles_by_cap[ $cap ], true ) ) {
$this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
$this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
} else {
@ -543,6 +553,17 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
}
}
/**
* Test miscellaneous capabilities of all user roles.
*/
function test_other_caps_for_all_roles() {
foreach ( self::$users as $role => $user ) {
// Make sure the user is valid.
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
// Make sure the role name is correct.
$this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" );
$this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
$this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );