Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ public static function wpTearDownAfterClass(): void {
}
}

/**
* Sets the public and private profile fields of the public author.
*
* The values are rolled back with the test transaction.
*/
private function set_public_author_profile_fields(): void {
wp_update_user(
array(
'ID' => self::$fixture_ids['public_author'],
'description' => 'A public biography.',
'user_url' => 'https://example.org/author',
'first_name' => 'Private',
'last_name' => 'Person',
'nickname' => 'hidden-nickname',
'locale' => 'de_DE',
)
);
}

/**
* Registers the core/users-query ability inside a faked init action.
*
Expand Down Expand Up @@ -540,25 +559,32 @@ public function test_core_users_requires_logged_in_user(): void {
* filter at all.
*
* @ticket 64657
* @ticket 66549
*/
public function test_visible_current_user_reads_their_own_sensitive_fields(): void {
$this->set_public_author_profile_fields();
wp_set_current_user( self::$fixture_ids['public_author'] );
$this->register_ability();

$ability = wp_get_ability( 'core/users-query' );
$fields = array( 'id', 'email', 'username', 'first_name', 'last_name', 'nickname', 'locale', 'registered', 'roles' );

$result = $ability->execute(
array(
'where' => array( 'ids' => array( self::$fixture_ids['public_author'] ) ),
'fields' => array( 'id', 'email', 'username', 'roles' ),
'fields' => $fields,
)
);

$this->assertIsArray( $result, 'A user should be able to read themselves by ID.' );
$this->assertSameSets( array( 'id', 'email', 'username', 'roles' ), array_keys( $result['items'][0] ), 'The current user should receive only the requested fields.' );
$this->assertSameSets( $fields, array_keys( $result['items'][0] ), 'The current user should receive only the requested fields.' );
$this->assertSame( self::$fixture_ids['public_author'], $result['items'][0]['id'], 'The ID lookup should return the current user.' );
$this->assertSame( 'core-users-ability-author@example.com', $result['items'][0]['email'], 'The current user should receive their own email.' );
$this->assertSame( 'core_users_ability_author', $result['items'][0]['username'], 'The current user should receive their own username.' );
$this->assertSame( 'Private', $result['items'][0]['first_name'], 'The current user should receive their own first name.' );
$this->assertSame( 'Person', $result['items'][0]['last_name'], 'The current user should receive their own last name.' );
$this->assertSame( 'hidden-nickname', $result['items'][0]['nickname'], 'The current user should receive their own nickname.' );
$this->assertSame( 'de_DE', $result['items'][0]['locale'], 'The current user should receive their own locale.' );
$this->assertContains( 'author', $result['items'][0]['roles'], 'The current user should receive their own roles.' );

$result = $ability->execute( array( 'where' => array( 'email' => 'core-users-ability-author@example.com' ) ) );
Expand Down Expand Up @@ -1865,6 +1891,7 @@ public function test_admin_can_query_by_role_and_receive_roles(): void {
* Field visibility for another public author matches the current user's role.
*
* @ticket 64657
* @ticket 66549
*
* @dataProvider data_roles_for_another_public_author_field_visibility
*
Expand All @@ -1873,23 +1900,32 @@ public function test_admin_can_query_by_role_and_receive_roles(): void {
* @param bool $can_view_roles Whether the role can view another user's roles.
*/
public function test_roles_have_expected_field_visibility_for_another_public_author( string $role, bool $can_view_sensitive, bool $can_view_roles ): void {
$this->set_public_author_profile_fields();
wp_set_current_user( self::$fixture_ids[ $role ] );
$this->register_ability();

$sensitive_fields = array( 'username', 'email', 'first_name', 'last_name', 'nickname', 'locale', 'registered' );

$result = wp_get_ability( 'core/users-query' )->execute(
array(
'where' => array( 'ids' => array( self::$fixture_ids['public_author'] ) ),
'fields' => array( 'id', 'email', 'roles' ),
'fields' => array_merge( array( 'id', 'description', 'url', 'roles' ), $sensitive_fields ),
)
);

$this->assertIsArray( $result, sprintf( 'The %s role should be able to execute a public-author lookup.', $role ) );
$this->assertSame( self::$fixture_ids['public_author'], $result['items'][0]['id'], sprintf( 'The %s role should receive the requested public author.', $role ) );
$this->assertSame( $can_view_sensitive, array_key_exists( 'email', $result['items'][0] ), sprintf( 'The %s role email visibility should match expectations.', $role ) );
$this->assertSame( 'A public biography.', $result['items'][0]['description'], sprintf( 'The %s role should receive the public description.', $role ) );
$this->assertSame( 'https://example.org/author', $result['items'][0]['url'], sprintf( 'The %s role should receive the public URL.', $role ) );
foreach ( $sensitive_fields as $field ) {
$this->assertSame( $can_view_sensitive, array_key_exists( $field, $result['items'][0] ), sprintf( 'The %1$s role %2$s visibility should match expectations.', $role, $field ) );
}
$this->assertSame( $can_view_roles, array_key_exists( 'roles', $result['items'][0] ), sprintf( 'The %s role roles visibility should match expectations.', $role ) );

if ( $can_view_sensitive ) {
$this->assertSame( 'core-users-ability-author@example.com', $result['items'][0]['email'], sprintf( 'The %s role should receive the public author email when allowed.', $role ) );
$this->assertSame( 'Private', $result['items'][0]['first_name'], sprintf( 'The %s role should receive the public author first name when allowed.', $role ) );
$this->assertSame( 'hidden-nickname', $result['items'][0]['nickname'], sprintf( 'The %s role should receive the public author nickname when allowed.', $role ) );
}

if ( ! $can_view_roles ) {
Expand Down
Loading