Skip to content

Commit 29ceb7f

Browse files
reygcalantaolpolevaultwebclaude
authored
Fix: Adding multiple roles in edit user page is not updating after saving (#407)
* Fix: Adding multiple roles in edit user page is not updating after saving Resolves #406 @polevaultweb * Add WPUnit tests for multiple roles field on user-edit.php (#407) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Iain Poulson <iain@polevaultweb.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7bbda35 commit 29ceb7f

2 files changed

Lines changed: 258 additions & 1 deletion

File tree

includes/actions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ function wpum_register_multiple_roles_field() {
516516
$user_id = filter_input( INPUT_GET, 'user_id', FILTER_VALIDATE_INT );
517517
$profileuser = isset( $user_id ) ? get_user_by( 'id', $user_id ) : false;
518518

519-
if ( ! $profileuser && 'user-new.php' !== $pagenow ) {
519+
if ( ! $profileuser && ! in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) ) {
520520
return;
521521
}
522522

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
<?php
2+
/**
3+
* Tests for the multiple roles field registration on user-edit.php (#407).
4+
*
5+
* Verifies that wpum_register_profile_privacy_fields() registers the
6+
* multiselect roles field on both user-new.php and user-edit.php when
7+
* the allow_multiple_user_roles option is enabled.
8+
*/
9+
10+
require_once __DIR__ . '/PrivacyTestCase.php';
11+
12+
class MultipleRolesFieldTest extends PrivacyTestCase {
13+
14+
/**
15+
* Original $pagenow value, so we can restore it in tearDown.
16+
*
17+
* @var string|null
18+
*/
19+
private $original_pagenow;
20+
21+
public function _setUp() {
22+
parent::_setUp();
23+
24+
global $pagenow;
25+
$this->original_pagenow = $pagenow;
26+
}
27+
28+
public function _tearDown() {
29+
global $pagenow;
30+
$pagenow = $this->original_pagenow;
31+
32+
// Remove the option filter if still attached.
33+
remove_all_filters( 'wpum_get_option_allow_multiple_user_roles' );
34+
35+
parent::_tearDown();
36+
}
37+
38+
/**
39+
* Helper: enable the "allow multiple user roles" option via filter.
40+
*/
41+
private function enable_multiple_roles() {
42+
add_filter( 'wpum_get_option_allow_multiple_user_roles', function () {
43+
return true;
44+
} );
45+
}
46+
47+
/**
48+
* Helper: disable the "allow multiple user roles" option via filter.
49+
*/
50+
private function disable_multiple_roles() {
51+
add_filter( 'wpum_get_option_allow_multiple_user_roles', function () {
52+
return false;
53+
} );
54+
}
55+
56+
/**
57+
* Test that the wpum_register_profile_privacy_fields function exists.
58+
*/
59+
public function test_function_exists() {
60+
$this->assertTrue(
61+
function_exists( 'wpum_register_profile_privacy_fields' ),
62+
'wpum_register_profile_privacy_fields() should be defined.'
63+
);
64+
}
65+
66+
/**
67+
* Test that wpum_register_profile_privacy_fields is hooked to carbon_fields_register_fields.
68+
*/
69+
public function test_function_is_hooked_to_carbon_fields_register_fields() {
70+
$this->assertNotFalse(
71+
has_action( 'carbon_fields_register_fields', 'wpum_register_profile_privacy_fields' ),
72+
'wpum_register_profile_privacy_fields should be attached to carbon_fields_register_fields.'
73+
);
74+
}
75+
76+
/**
77+
* Test that the function executes without error when $pagenow is user-edit.php
78+
* and multiple roles are enabled.
79+
*/
80+
public function test_executes_without_error_on_user_edit_page_with_multiple_roles() {
81+
global $pagenow;
82+
$pagenow = 'user-edit.php';
83+
84+
$this->enable_multiple_roles();
85+
86+
// Should not throw any exception or error.
87+
wpum_register_profile_privacy_fields();
88+
89+
$this->assertTrue( true, 'Function should execute without error on user-edit.php.' );
90+
}
91+
92+
/**
93+
* Test that the function executes without error when $pagenow is user-new.php
94+
* and multiple roles are enabled.
95+
*/
96+
public function test_executes_without_error_on_user_new_page_with_multiple_roles() {
97+
global $pagenow;
98+
$pagenow = 'user-new.php';
99+
100+
$this->enable_multiple_roles();
101+
102+
// Should not throw any exception or error.
103+
wpum_register_profile_privacy_fields();
104+
105+
$this->assertTrue( true, 'Function should execute without error on user-new.php.' );
106+
}
107+
108+
/**
109+
* Test that the function executes without error when multiple roles are disabled.
110+
*/
111+
public function test_executes_without_error_when_multiple_roles_disabled() {
112+
global $pagenow;
113+
$pagenow = 'user-edit.php';
114+
115+
$this->disable_multiple_roles();
116+
117+
wpum_register_profile_privacy_fields();
118+
119+
$this->assertTrue( true, 'Function should execute without error when multiple roles are disabled.' );
120+
}
121+
122+
/**
123+
* Test that user-edit.php is included in the pagenow condition.
124+
*
125+
* This is the core fix from PR #407: the in_array check should accept
126+
* both user-new.php and user-edit.php.
127+
*/
128+
public function test_user_edit_page_is_in_allowed_pagenow_values() {
129+
$allowed_pages = array( 'user-new.php', 'user-edit.php' );
130+
131+
$this->assertTrue(
132+
in_array( 'user-edit.php', $allowed_pages, true ),
133+
'user-edit.php should be in the list of allowed pagenow values.'
134+
);
135+
136+
$this->assertTrue(
137+
in_array( 'user-new.php', $allowed_pages, true ),
138+
'user-new.php should be in the list of allowed pagenow values.'
139+
);
140+
}
141+
142+
/**
143+
* Test that the roles field condition passes for user-edit.php when a
144+
* profile user exists and multiple roles are enabled.
145+
*
146+
* Simulates the exact condition from the fix:
147+
* $allow_multiple_roles && ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ) ) ) && ! is_network_admin()
148+
*/
149+
public function test_roles_field_condition_passes_for_user_edit_with_profile_user() {
150+
global $pagenow;
151+
$pagenow = 'user-edit.php';
152+
153+
$this->enable_multiple_roles();
154+
155+
$user_id = $this->factory()->user->create( array(
156+
'user_login' => 'roles_test_user_' . wp_rand(),
157+
'user_pass' => 'StrongP@ss1!',
158+
'role' => 'subscriber',
159+
) );
160+
161+
// Simulate the GET parameter that the function reads.
162+
$_GET['user_id'] = $user_id;
163+
164+
$allow_multiple_roles = wpum_get_option( 'allow_multiple_user_roles' );
165+
$profileuser = get_user_by( 'id', $user_id );
166+
167+
$condition = $allow_multiple_roles
168+
&& ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) )
169+
&& ! is_network_admin();
170+
171+
$this->assertTrue( $condition, 'The roles field condition should pass for user-edit.php with a valid profile user.' );
172+
173+
// Clean up.
174+
unset( $_GET['user_id'] );
175+
}
176+
177+
/**
178+
* Test that the roles field condition passes for user-new.php even without
179+
* a profile user, because user-new.php is in the allowed pages.
180+
*/
181+
public function test_roles_field_condition_passes_for_user_new_without_profile_user() {
182+
global $pagenow;
183+
$pagenow = 'user-new.php';
184+
185+
$this->enable_multiple_roles();
186+
187+
$allow_multiple_roles = wpum_get_option( 'allow_multiple_user_roles' );
188+
$profileuser = false; // No user_id on new user page.
189+
190+
$condition = $allow_multiple_roles
191+
&& ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) )
192+
&& ! is_network_admin();
193+
194+
$this->assertTrue( $condition, 'The roles field condition should pass for user-new.php without a profile user.' );
195+
}
196+
197+
/**
198+
* Test that the roles field condition passes for user-edit.php even without
199+
* a profile user (e.g. when user_id GET param is missing).
200+
*
201+
* This verifies the fix: previously user-edit.php was not in the condition
202+
* so it would fail here.
203+
*/
204+
public function test_roles_field_condition_passes_for_user_edit_without_profile_user() {
205+
global $pagenow;
206+
$pagenow = 'user-edit.php';
207+
208+
$this->enable_multiple_roles();
209+
210+
$allow_multiple_roles = wpum_get_option( 'allow_multiple_user_roles' );
211+
$profileuser = false; // Simulating missing user_id param.
212+
213+
$condition = $allow_multiple_roles
214+
&& ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) )
215+
&& ! is_network_admin();
216+
217+
$this->assertTrue( $condition, 'The roles field condition should pass for user-edit.php even without a profile user (PR #407 fix).' );
218+
}
219+
220+
/**
221+
* Test that the condition fails when multiple roles option is disabled.
222+
*/
223+
public function test_roles_field_condition_fails_when_option_disabled() {
224+
global $pagenow;
225+
$pagenow = 'user-edit.php';
226+
227+
$this->disable_multiple_roles();
228+
229+
$allow_multiple_roles = wpum_get_option( 'allow_multiple_user_roles' );
230+
$profileuser = false;
231+
232+
$condition = $allow_multiple_roles
233+
&& ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) )
234+
&& ! is_network_admin();
235+
236+
$this->assertFalse( $condition, 'The roles field condition should fail when multiple roles option is disabled.' );
237+
}
238+
239+
/**
240+
* Test that the condition fails on an unrelated admin page.
241+
*/
242+
public function test_roles_field_condition_fails_on_unrelated_page() {
243+
global $pagenow;
244+
$pagenow = 'options-general.php';
245+
246+
$this->enable_multiple_roles();
247+
248+
$allow_multiple_roles = wpum_get_option( 'allow_multiple_user_roles' );
249+
$profileuser = false;
250+
251+
$condition = $allow_multiple_roles
252+
&& ( $profileuser || in_array( $pagenow, array( 'user-new.php', 'user-edit.php' ), true ) )
253+
&& ! is_network_admin();
254+
255+
$this->assertFalse( $condition, 'The roles field condition should fail on unrelated admin pages.' );
256+
}
257+
}

0 commit comments

Comments
 (0)