From 3cbe83834210c679a5ab31ab5ec9665141fd6cef Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:21:53 -0300 Subject: [PATCH 1/4] XProfile field: test apostrophes is handled correctly in field update --- .../xprofile/test-data-controller.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/testcases/xprofile/test-data-controller.php b/tests/testcases/xprofile/test-data-controller.php index aea6abc2..8266f865 100644 --- a/tests/testcases/xprofile/test-data-controller.php +++ b/tests/testcases/xprofile/test-data-controller.php @@ -312,6 +312,49 @@ public function test_update_textbox() { $this->assertEquals( $data[0]['value']['raw'], 'textbox field' ); } + /** + * @group update_item + * + * @ticket https://buddypress.trac.wordpress.org/ticket/9127 + */ + public function test_update_textbox_with_apostrophe_value() { + $field_id = $this->bp_factory->xprofile_field->create( + [ + 'type' => 'textbox', + 'field_group_id' => $this->group_id, + 'value' => 'textbox field', + ] + ); + + $this->bp->set_current_user( $this->user ); + + $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $field_id, $this->user ) ); + $request->add_header( 'content-type', 'application/json' ); + + $params = $this->set_field_data( [ 'value' => "I don't travel often" ] ); + $request->set_param( 'context', 'edit' ); + $request->set_body( wp_json_encode( $params ) ); + $response = $this->server->dispatch( $request ); + + $data = $response->get_data(); + + $this->assertNotEmpty( $data ); + $this->assertEquals( $data[0]['value']['raw'], "I don't travel often" ); + + $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $field_id, $this->user ) ); + $request->add_header( 'content-type', 'application/json' ); + + $params = $this->set_field_data( [ 'value' => "I don\\'t travel often" ] ); + $request->set_param( 'context', 'edit' ); + $request->set_body( wp_json_encode( $params ) ); + $response = $this->server->dispatch( $request ); + + $data = $response->get_data(); + + $this->assertNotEmpty( $data ); + $this->assertEquals( $data[0]['value']['raw'], "I don't travel often" ); + } + /** * @group update_item */ From 17aeb9d2cbcec2340b997c4a08de667a0c198cd4 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:37:27 -0300 Subject: [PATCH 2/4] Update tests --- .../xprofile/test-data-controller.php | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/testcases/xprofile/test-data-controller.php b/tests/testcases/xprofile/test-data-controller.php index 8266f865..3655aa58 100644 --- a/tests/testcases/xprofile/test-data-controller.php +++ b/tests/testcases/xprofile/test-data-controller.php @@ -281,6 +281,108 @@ public function test_update_multiselectbox_with_empty_value() { $this->assertEquals( $data[0]['value']['unserialized'], [] ); } + /** + * @group update_item + * + * @ticket https://buddypress.trac.wordpress.org/ticket/9127 + */ + public function test_update_multiselectbox_with_apostrophe_value() { + $field_id = $this->bp_factory->xprofile_field->create( + [ + 'type' => 'multiselectbox', + 'field_group_id' => $this->group_id + ] + ); + xprofile_insert_field( + [ + 'field_group_id' => $this->group_id, + 'parent_id' => $field_id, + 'type' => 'option', + 'name' => 'Option 1', + ] + ); + + xprofile_insert_field( + [ + 'field_group_id' => $this->group_id, + 'parent_id' => $field_id, + 'type' => 'option', + 'name' => "I don't travel often", + ] + ); + + $this->bp->set_current_user( $this->user ); + + $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $field_id, $this->user ) ); + $request->add_header( 'content-type', 'application/json' ); + + $params = $this->set_field_data( [ 'value' => "I don\'t travel often" ] ); + + $request->set_param( 'context', 'edit' ); + $request->set_body( wp_json_encode( $params ) ); + $response = $this->server->dispatch( $request ); + + $this->assertNotInstanceOf( 'WP_Error', $response ); + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertNotEmpty( $data ); + + $this->assertEquals( $data[0]['value']['unserialized'], [ "I don\'t travel often" ] ); + $this->assertEquals( $data[0]['value']['raw'], "a:1:{i:0;s:21:\"I don\\'t travel often\";}" ); + } + + /** + * @group update_item + * + * @ticket https://buddypress.trac.wordpress.org/ticket/9127 + */ + public function test_update_checkbox_with_apostrophe_value() { + $field_id = $this->bp_factory->xprofile_field->create( + [ + 'type' => 'checkbox', + 'field_group_id' => $this->group_id + ] + ); + xprofile_insert_field( + [ + 'field_group_id' => $this->group_id, + 'parent_id' => $field_id, + 'type' => 'option', + 'name' => 'Option 1', + ] + ); + + xprofile_insert_field( + [ + 'field_group_id' => $this->group_id, + 'parent_id' => $field_id, + 'type' => 'option', + 'name' => "I don't travel often", + ] + ); + + $this->bp->set_current_user( $this->user ); + + $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $field_id, $this->user ) ); + $request->add_header( 'content-type', 'application/json' ); + + $params = $this->set_field_data( [ 'value' => "I don\'t travel often" ] ); + + $request->set_param( 'context', 'edit' ); + $request->set_body( wp_json_encode( $params ) ); + $response = $this->server->dispatch( $request ); + + $this->assertNotInstanceOf( 'WP_Error', $response ); + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertNotEmpty( $data ); + + $this->assertEquals( $data[0]['value']['unserialized'], [ "I don\'t travel often" ] ); + $this->assertEquals( $data[0]['value']['raw'], "a:1:{i:0;s:21:\"I don\\'t travel often\";}" ); + } + /** * @group update_item */ From 00f9eba5a3d53002e2570ef1d9ff2db29aaa8c5c Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Tue, 9 Apr 2024 18:56:59 -0300 Subject: [PATCH 3/4] Update branch to test the fix --- .wp-env.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.wp-env.json b/.wp-env.json index 27a80ca1..f93dd016 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,6 +1,6 @@ { "core": "WordPress/WordPress#master", - "plugins": ["buddypress/buddypress#master", "."], + "plugins": ["buddypress/buddypress#feature/9127", "."], "config": { "WP_DEBUG": true, "SCRIPT_DEBUG": true From 41e30395d5d21baf533ee285b9a935e74ae58c23 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:03:34 -0300 Subject: [PATCH 4/4] Test with the lastest from core --- .wp-env.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.wp-env.json b/.wp-env.json index f93dd016..27a80ca1 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,6 +1,6 @@ { "core": "WordPress/WordPress#master", - "plugins": ["buddypress/buddypress#feature/9127", "."], + "plugins": ["buddypress/buddypress#master", "."], "config": { "WP_DEBUG": true, "SCRIPT_DEBUG": true