Skip to content

[41604] Modified update_item() to read only POST body parameters thereby handling global query parameters #8975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
Original file line number Diff line number Diff line change
@@ -144,8 +144,9 @@ protected function prepare_value( $value, $schema ) {
*/
public function update_item( $request ) {
$options = $this->get_registered_options();

$params = $request->get_params();
$query_params = $request->get_query_params();
$params = array_diff_key( $params, $query_params );
Comment on lines 146 to +149

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we need to align the equals to avoid PHPCS warning errors:

	public function update_item( $request ) {
		$options      = $this->get_registered_options();
		$params       = $request->get_params();
		$query_params = $request->get_query_params();
		$params       = array_diff_key( $params, $query_params );

I don't know why it passed CI, but it gives a warning locally.

composer lint src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php
> @php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source 'src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php'
W 1 / 1 (100%)



PHP CODE SNIFFER REPORT SUMMARY
---------------------------------------------------------------------------------------------
FILE                                                                         ERRORS  WARNINGS
---------------------------------------------------------------------------------------------
src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php     0       3
---------------------------------------------------------------------------------------------
A TOTAL OF 0 ERRORS AND 3 WARNINGS WERE FOUND IN 1 FILE
---------------------------------------------------------------------------------------------
PHPCBF CAN FIX 3 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------

Time: 61ms; Memory: 8MB


PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
-----------------------------------------------------------------------
    SOURCE                                                        COUNT
-----------------------------------------------------------------------
[x] Generic.Formatting.MultipleStatementAlignment.NotSameWarning  3
-----------------------------------------------------------------------
A TOTAL OF 3 SNIFF VIOLATIONS WERE FOUND IN 1 SOURCE
-----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SOURCES AUTOMATICALLY (3 VIOLATIONS IN TOTAL)
-----------------------------------------------------------------------

Script @php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source handling the lint event returned with error code 2


if ( empty( $params ) || ! empty( array_diff_key( $params, $options ) ) ) {
$message = empty( $params )
39 changes: 39 additions & 0 deletions tests/phpunit/tests/rest-api/rest-settings-controller.php
Original file line number Diff line number Diff line change
@@ -397,6 +397,9 @@ public function test_create_item() {
$this->assertSame( 400, $response->get_status() );
}

/**
* @ticket 41604
*/
public function test_update_item() {
wp_set_current_user( self::$administrator );

@@ -410,6 +413,39 @@ public function test_update_item() {
$this->assertSame( get_option( 'blogname' ), $data['title'] );
}

/**
* @ticket 41604
*/
public function test_update_item_with_global_parameters_present() {
wp_set_current_user( self::$administrator );

$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$request->set_param( 'title', 'The new title!' );
$request->set_url_params( array( '_locale' => 'user' ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertSame( 'The new title!', $data['title'] );
$this->assertSame( get_option( 'blogname' ), $data['title'] );
}

/**
* @ticket 41604
*/
public function test_update_item_with_empty_body() {
wp_set_current_user( self::$administrator );

$request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 400, $response->get_status() );
}

/**
* @ticket 41604
*/
public function test_update_nonexistent_item() {
wp_set_current_user( self::$administrator );

@@ -420,6 +456,9 @@ public function test_update_nonexistent_item() {
$this->assertSame( 400, $response->get_status() );
}

/**
* @ticket 41604
*/
public function test_update_partially_valid_items() {
wp_set_current_user( self::$administrator );

Loading
Oops, something went wrong.