Skip to content

Commit

Permalink
Merge pull request #497 from buddypress/feature/test-for-9127
Browse files Browse the repository at this point in the history
XProfile field: test string with apostrophes is handled correctly in a field update
  • Loading branch information
renatonascalves authored Apr 12, 2024
2 parents 4bb1a70 + 41e3039 commit e6d1fbb
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions tests/testcases/xprofile/test-data-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -312,6 +414,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
*/
Expand Down

0 comments on commit e6d1fbb

Please sign in to comment.