Skip to content

Commit

Permalink
Add test for user preference checkbox handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukhendu2002 committed Jan 3, 2025
1 parent 016bbec commit 31498f1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/wp-admin/includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ function edit_user( $user_id = 0 ) {
}

if ( $update ) {
$user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true';
$user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true';
$user->rich_editing = isset( $_POST['rich_editing'] ) ? 'true' : 'false';
$user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) ? 'true' : 'false';
$user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
$user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
}
Expand Down
10 changes: 5 additions & 5 deletions src/wp-admin/user-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@
<tr class="user-rich-editing-wrap">
<th scope="row"><?php _e( 'Visual Editor' ); ?></th>
<td>
<label for="rich_editing"><input name="rich_editing" type="checkbox" id="rich_editing" value="false" <?php checked( 'false', $profile_user->rich_editing ); ?> />
<?php _e( 'Disable the visual editor when writing' ); ?>
<label for="rich_editing"><input name="rich_editing" type="checkbox" id="rich_editing" value="true"<?php checked( 'true', $profile_user->rich_editing ); ?> />
<?php _e( 'Enable the visual editor when writing' ); ?>
</label>
</td>
</tr>
Expand All @@ -324,8 +324,8 @@
<tr class="user-syntax-highlighting-wrap">
<th scope="row"><?php _e( 'Syntax Highlighting' ); ?></th>
<td>
<label for="syntax_highlighting"><input name="syntax_highlighting" type="checkbox" id="syntax_highlighting" value="false" <?php checked( 'false', $profile_user->syntax_highlighting ); ?> />
<?php _e( 'Disable syntax highlighting when editing code' ); ?>
<label for="syntax_highlighting"><input name="syntax_highlighting" type="checkbox" id="syntax_highlighting" value="true"<?php checked( 'true', $profile_user->syntax_highlighting ); ?> />
<?php _e( 'Enable syntax highlighting when editing code' ); ?>
</label>
</td>
</tr>
Expand Down Expand Up @@ -370,7 +370,7 @@
<th scope="row"><?php _e( 'Toolbar' ); ?></th>
<td>
<label for="admin_bar_front">
<input name="admin_bar_front" type="checkbox" id="admin_bar_front" value="1"<?php checked( _get_admin_bar_pref( 'front', $profile_user->ID ) ); ?> />
<input name="admin_bar_front" type="checkbox" id="admin_bar_front" value="true"<?php checked( _get_admin_bar_pref( 'front', $profile_user->ID ) ); ?> />
<?php _e( 'Show Toolbar when viewing site' ); ?>
</label><br />
</td>
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2289,4 +2289,46 @@ function ( $meta_id, $object_id, $meta_key ) use ( &$db_update_count ) {
// Verify there are no updates to 'use_ssl' user meta.
$this->assertSame( 1, $db_update_count );
}

/**
* Test user preference checkbox handling.
*
* @ticket 57393
*
* @covers ::edit_user
*/
public function test_user_preferences_checkbox_handling() {
wp_set_current_user( self::$admin_id );

// Test when preferences are enabled.
$_POST = array(
'nickname' => 'Test User',

Check warning on line 2305 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 12 space(s) between "'nickname'" and double arrow, but found 11.
'email' => '[email protected]',

Check warning on line 2306 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 15 space(s) between "'email'" and double arrow, but found 13.
'rich_editing' => 'true',

Check warning on line 2307 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 8 space(s) between "'rich_editing'" and double arrow, but found 6.
'syntax_highlighting' => 'true',
'admin_bar_front' => 'true',

Check warning on line 2309 in tests/phpunit/tests/user.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 5 space(s) between "'admin_bar_front'" and double arrow, but found 3.
);

$result = edit_user( self::$author_id );
$this->assertNotWPError( $result );

$user = get_user_by( 'id', self::$author_id );
$this->assertSame( 'true', $user->rich_editing );
$this->assertSame( 'true', $user->syntax_highlighting );
$this->assertSame( 'true', $user->show_admin_bar_front );

// Test when preferences are disabled.
$_POST = array(
'nickname' => 'Test User',
'email' => '[email protected]',
);

$result = edit_user( self::$author_id );
$this->assertNotWPError( $result );

$user = get_user_by( 'id', self::$author_id );
$this->assertSame( 'false', $user->rich_editing );
$this->assertSame( 'false', $user->syntax_highlighting );
$this->assertSame( 'false', $user->show_admin_bar_front );
}
}

0 comments on commit 31498f1

Please sign in to comment.