Skip to content

Commit b8631aa

Browse files
committed
Tests: Cover private field visibility in core/users-query
Extend two existing tests to check that every private user field is returned only to the user and to callers who can edit them. Until now, only `email` and `roles` were checked for each role. Developed in #16246. Follow-up to r64346. Props hmbashar, lancewillett. See #66549. git-svn-id: https://develop.svn.wordpress.org/trunk@65845 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a9849e4 commit b8631aa

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

‎tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ public static function wpTearDownAfterClass(): void {
161161
}
162162
}
163163

164+
/**
165+
* Sets the public and private profile fields of the public author.
166+
*
167+
* The values are rolled back with the test transaction.
168+
*/
169+
private function set_public_author_profile_fields(): void {
170+
wp_update_user(
171+
array(
172+
'ID' => self::$fixture_ids['public_author'],
173+
'description' => 'A public biography.',
174+
'user_url' => 'https://example.org/author',
175+
'first_name' => 'Private',
176+
'last_name' => 'Person',
177+
'nickname' => 'hidden-nickname',
178+
'locale' => 'de_DE',
179+
)
180+
);
181+
}
182+
164183
/**
165184
* Registers the core/users-query ability inside a faked init action.
166185
*
@@ -540,25 +559,32 @@ public function test_core_users_requires_logged_in_user(): void {
540559
* filter at all.
541560
*
542561
* @ticket 64657
562+
* @ticket 66549
543563
*/
544564
public function test_visible_current_user_reads_their_own_sensitive_fields(): void {
565+
$this->set_public_author_profile_fields();
545566
wp_set_current_user( self::$fixture_ids['public_author'] );
546567
$this->register_ability();
547568

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

550572
$result = $ability->execute(
551573
array(
552574
'where' => array( 'ids' => array( self::$fixture_ids['public_author'] ) ),
553-
'fields' => array( 'id', 'email', 'username', 'roles' ),
575+
'fields' => $fields,
554576
)
555577
);
556578

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

564590
$result = $ability->execute( array( 'where' => array( 'email' => 'core-users-ability-author@example.com' ) ) );
@@ -1865,6 +1891,7 @@ public function test_admin_can_query_by_role_and_receive_roles(): void {
18651891
* Field visibility for another public author matches the current user's role.
18661892
*
18671893
* @ticket 64657
1894+
* @ticket 66549
18681895
*
18691896
* @dataProvider data_roles_for_another_public_author_field_visibility
18701897
*
@@ -1873,23 +1900,32 @@ public function test_admin_can_query_by_role_and_receive_roles(): void {
18731900
* @param bool $can_view_roles Whether the role can view another user's roles.
18741901
*/
18751902
public function test_roles_have_expected_field_visibility_for_another_public_author( string $role, bool $can_view_sensitive, bool $can_view_roles ): void {
1903+
$this->set_public_author_profile_fields();
18761904
wp_set_current_user( self::$fixture_ids[ $role ] );
18771905
$this->register_ability();
18781906

1907+
$sensitive_fields = array( 'username', 'email', 'first_name', 'last_name', 'nickname', 'locale', 'registered' );
1908+
18791909
$result = wp_get_ability( 'core/users-query' )->execute(
18801910
array(
18811911
'where' => array( 'ids' => array( self::$fixture_ids['public_author'] ) ),
1882-
'fields' => array( 'id', 'email', 'roles' ),
1912+
'fields' => array_merge( array( 'id', 'description', 'url', 'roles' ), $sensitive_fields ),
18831913
)
18841914
);
18851915

18861916
$this->assertIsArray( $result, sprintf( 'The %s role should be able to execute a public-author lookup.', $role ) );
18871917
$this->assertSame( self::$fixture_ids['public_author'], $result['items'][0]['id'], sprintf( 'The %s role should receive the requested public author.', $role ) );
1888-
$this->assertSame( $can_view_sensitive, array_key_exists( 'email', $result['items'][0] ), sprintf( 'The %s role email visibility should match expectations.', $role ) );
1918+
$this->assertSame( 'A public biography.', $result['items'][0]['description'], sprintf( 'The %s role should receive the public description.', $role ) );
1919+
$this->assertSame( 'https://example.org/author', $result['items'][0]['url'], sprintf( 'The %s role should receive the public URL.', $role ) );
1920+
foreach ( $sensitive_fields as $field ) {
1921+
$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 ) );
1922+
}
18891923
$this->assertSame( $can_view_roles, array_key_exists( 'roles', $result['items'][0] ), sprintf( 'The %s role roles visibility should match expectations.', $role ) );
18901924

18911925
if ( $can_view_sensitive ) {
18921926
$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 ) );
1927+
$this->assertSame( 'Private', $result['items'][0]['first_name'], sprintf( 'The %s role should receive the public author first name when allowed.', $role ) );
1928+
$this->assertSame( 'hidden-nickname', $result['items'][0]['nickname'], sprintf( 'The %s role should receive the public author nickname when allowed.', $role ) );
18931929
}
18941930

18951931
if ( ! $can_view_roles ) {

0 commit comments

Comments
 (0)