Changeset 50490 for trunk/tests/phpunit/tests/user/capabilities.php
- Timestamp:
- 03/04/2021 12:12:55 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/user/capabilities.php
r50131 r50490 13 13 */ 14 14 protected static $users = array( 15 'anonymous' => null, 15 16 'administrator' => null, 16 17 'editor' => null, … … 32 33 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 33 34 self::$users = array( 35 'anonymous' => new WP_User( 0 ), 34 36 'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ), 35 37 'editor' => $factory->user->create_and_get( array( 'role' => 'editor' ) ), … … 343 345 } 344 346 347 /** 348 * Data provider for testing a single site install's roles. 349 * 350 * @return array[] { 351 * Arguments for test. 352 * 353 * @type string $role The role to test for. 354 * } 355 */ 356 function data_single_site_roles_to_check() { 357 return array( 358 array( 'anonymous' ), 359 array( 'administrator' ), 360 array( 'editor' ), 361 array( 'author' ), 362 array( 'contributor' ), 363 array( 'subscriber' ), 364 ); 365 } 366 345 367 protected function getAllCapsAndRoles() { 346 368 return $this->getPrimitiveCapsAndRoles() + $this->getMetaCapsAndRoles(); … … 392 414 393 415 foreach ( self::$users as $role => $user ) { 394 395 // Make sure the user is valid. 396 $this->assertTrue( $user->exists(), "User with {$role} role does not exist" ); 416 if ( 'anonymous' === $role ) { 417 // The anonymous role does not exist. 418 $this->assertFalse( $user->exists(), "User with {$role} role should not exist" ); 419 } else { 420 // Make sure the user is valid. 421 $this->assertTrue( $user->exists(), "User with {$role} role does not exist" ); 422 } 397 423 398 424 $user_caps = $user->allcaps; … … 564 590 /** 565 591 * Test miscellaneous capabilities of all user roles. 566 */ 567 function test_other_caps_for_all_roles() { 568 foreach ( self::$users as $role => $user ) { 569 // Make sure the user is valid. 570 $this->assertTrue( $user->exists(), "User with {$role} role does not exist" ); 571 572 // Make sure the role name is correct. 573 $this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" ); 574 575 $this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability" ); 576 $this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability" ); 577 578 $this->assertFalse( $user->has_cap( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" ); 579 $this->assertFalse( user_can( $user, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" ); 580 581 $this->assertTrue( $user->has_cap( 'exist' ), "User with the {$role} role should have the exist capability" ); 582 $this->assertTrue( user_can( $user, 'exist' ), "User with the {$role} role should have the exist capability" ); 583 } 592 * 593 * @dataProvider data_single_site_roles_to_check 594 */ 595 function test_other_caps_for_all_roles( $role ) { 596 $user = self::$users[ $role ]; 597 $old_id = wp_get_current_user()->ID; 598 wp_set_current_user( $user->ID ); 599 600 // Make sure the role name is correct. 601 $expected_roles = array( $role ); 602 if ( 'anonymous' === $role ) { 603 // Anonymous role does not exist, user roles should be empty. 604 $expected_roles = array(); 605 } 606 $this->assertSame( $expected_roles, $user->roles, "User should only have the {$role} role" ); 607 608 $this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test via WP_User->has_cap() method)." ); 609 $this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by user object)." ); 610 $this->assertFalse( user_can( $user->ID, 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by user ID)." ); 611 $this->assertFalse( current_user_can( 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by current user)." ); 612 613 $this->assertFalse( $user->has_cap( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test via WP_User->has_cap() method)." ); 614 $this->assertFalse( user_can( $user, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by user object)." ); 615 $this->assertFalse( user_can( $user->ID, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by user ID)." ); 616 $this->assertFalse( current_user_can( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by current user)." ); 617 618 $this->assertTrue( $user->has_cap( 'exist' ), "User with the {$role} role should have the exist capability (test via WP_User->has_cap() method)." ); 619 $this->assertTrue( user_can( $user, 'exist' ), "User with the {$role} role should have the exist capability (test by user object)." ); 620 $this->assertTrue( user_can( $user->ID, 'exist' ), "User with the {$role} role should have the exist capability (test by user ID)." ); 621 $this->assertTrue( current_user_can( 'exist' ), "User with the {$role} role should have the exist capability (test by current user)." ); 622 623 wp_set_current_user( $old_id ); 624 } 625 626 /** 627 * Test user exists/does not exist as expected. 628 * 629 * @dataProvider data_single_site_roles_to_check 630 */ 631 function test_user_exists_in_database( $role ) { 632 $user = self::$users[ $role ]; 633 $expected = true; 634 635 if ( 'anonymous' === $role ) { 636 $expected = false; 637 } 638 639 $this->assertSame( $expected, $user->exists() ); 584 640 } 585 641 … … 595 651 $user->remove_cap( 'do_not_allow' ); 596 652 $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" ); 653 654 # Test adding the cap via a filter 655 add_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 ); 656 $has_cap = $user->has_cap( 'do_not_allow' ); 657 remove_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 ); 658 $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" ); 659 660 if ( 'anonymous' === $role ) { 661 // The anonymous role does not exist. 662 continue; 663 } 597 664 598 665 # Test adding the cap to the user's role … … 602 669 $role_obj->remove_cap( 'do_not_allow' ); 603 670 $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" ); 604 605 # Test adding the cap via a filter606 add_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );607 $has_cap = $user->has_cap( 'do_not_allow' );608 remove_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );609 $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" );610 611 671 } 612 672 }
Note: See TracChangeset
for help on using the changeset viewer.