Changeset 50490
- Timestamp:
- 03/04/2021 12:12:55 AM (4 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/capabilities.php
r50138 r50490 680 680 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 681 681 * by adding it to the function signature. 682 * @since 5.8.0 Converted to wrapper for the user_can() function. 682 683 * 683 684 * @see WP_User::has_cap() … … 690 691 */ 691 692 function current_user_can( $capability, ...$args ) { 692 $current_user = wp_get_current_user(); 693 694 if ( empty( $current_user ) ) { 695 return false; 696 } 697 698 return $current_user->has_cap( $capability, ...$args ); 693 return user_can( wp_get_current_user(), $capability, ...$args ); 699 694 } 700 695 … … 715 710 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 716 711 * by adding it to the function signature. 712 * @since 5.8.0 Wraps current_user_can() after switching to blog. 717 713 * 718 714 * @param int $blog_id Site ID. … … 724 720 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false; 725 721 726 $current_user = wp_get_current_user(); 727 728 if ( empty( $current_user ) ) { 729 if ( $switched ) { 730 restore_current_blog(); 731 } 732 return false; 733 } 734 735 $can = $current_user->has_cap( $capability, ...$args ); 722 $can = current_user_can( $capability, ...$args ); 736 723 737 724 if ( $switched ) { … … 806 793 } 807 794 808 if ( ! $user || ! $user->exists() ) { 809 return false; 795 if ( empty( $user ) ) { 796 // User is logged out, create anonymous user object. 797 $user = new WP_User( 0 ); 798 $user->init( new stdClass ); 810 799 } 811 800 -
trunk/src/wp-includes/class-wp-user.php
r49790 r50490 168 168 */ 169 169 public function init( $data, $site_id = '' ) { 170 if ( ! isset( $data->ID ) ) { 171 $data->ID = 0; 172 } 170 173 $this->data = $data; 171 174 $this->ID = (int) $data->ID; -
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.