mirror of
https://github.com/buddypress/buddypress.git
synced 2026-07-22 20:56:55 +08:00
We are introducing a new `BP_LoggedIn_User` class to fetch data about a BuddyPress logged-in user. This new addition fixes an issue where a user could be de-authenticated when making REST API requests. Props dcavins, DJPaul, johnjamesjacoby, and imath. Closes https://github.com/buddypress/buddypress/pull/395 See #9229 and #9145 Fixes #7658 git-svn-id: https://buddypress.svn.wordpress.org/trunk@14070 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
549 lines
17 KiB
PHP
549 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* XProfile Field Groups Controller Tests.
|
|
*
|
|
* @group xprofile
|
|
* @group xprofile-group
|
|
*/
|
|
class BP_Tests_XProfile_Groups_REST_Controller extends BP_Test_REST_Controller_Testcase {
|
|
protected $group_id;
|
|
protected $controller = 'BP_XProfile_Field_Groups_REST_Controller';
|
|
protected $handle = 'xprofile/groups';
|
|
|
|
public function set_up() {
|
|
parent::set_up();
|
|
|
|
$this->group_id = $this->bp::factory()->xprofile_group->create();
|
|
}
|
|
|
|
public function test_register_routes() {
|
|
$routes = $this->server->get_routes();
|
|
|
|
// Main.
|
|
$this->assertArrayHasKey( $this->endpoint_url, $routes );
|
|
$this->assertCount( 2, $routes[ $this->endpoint_url ] );
|
|
|
|
// Single.
|
|
$this->assertArrayHasKey( $this->endpoint_url . '/(?P<id>[\d]+)', $routes );
|
|
$this->assertCount( 3, $routes[ $this->endpoint_url . '/(?P<id>[\d]+)' ] );
|
|
}
|
|
|
|
/**
|
|
* @group get_items
|
|
*/
|
|
public function test_get_items() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$this->bp::factory()->xprofile_group->create_many( 5 );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
foreach ( $all_data as $data ) {
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] );
|
|
$this->check_group_data( $field_group, $data, 'view', $response->get_links() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @group get_items
|
|
*/
|
|
public function test_get_items_with_support_for_the_community_visibility() {
|
|
toggle_component_visibility();
|
|
|
|
$this->bp::factory()->xprofile_group->create_many( 5 );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group get_items
|
|
*/
|
|
public function test_get_items_include_groups() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$g1 = $this->bp::factory()->xprofile_group->create();
|
|
$g2 = $this->bp::factory()->xprofile_group->create();
|
|
$this->bp::factory()->xprofile_group->create_many( 3 );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
|
|
$request->set_param( 'context', 'view' );
|
|
$request->set_param( 'include_groups', array( $g1, $g2 ) );
|
|
$response = $this->server->dispatch( $request );
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
foreach ( $all_data as $data ) {
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] );
|
|
$this->check_group_data( $field_group, $data, 'view', $response->get_links() );
|
|
}
|
|
|
|
$group_ids = wp_list_pluck( $all_data, 'id' );
|
|
sort( $group_ids );
|
|
$this->assertSame( $group_ids, array( $g1, $g2 ) );
|
|
}
|
|
|
|
/**
|
|
* @group get_items
|
|
*/
|
|
public function test_get_items_publicly() {
|
|
$this->bp::factory()->xprofile_group->create_many( 5 );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint_url );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
foreach ( $all_data as $data ) {
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] );
|
|
$this->check_group_data( $field_group, $data, 'view', $response->get_links() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @group get_item
|
|
*/
|
|
public function test_get_item() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id );
|
|
$this->assertEquals( $this->group_id, $field_group->id );
|
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
$this->check_group_data( $field_group, $all_data, 'view', $response->get_links() );
|
|
}
|
|
|
|
/**
|
|
* @group get_item
|
|
*/
|
|
public function test_get_item_publicly_with_support_for_the_community_visibility() {
|
|
toggle_component_visibility();
|
|
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id );
|
|
$this->assertEquals( $this->group_id, $field_group->id );
|
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group get_item
|
|
*/
|
|
public function test_get_item_publicly() {
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id );
|
|
$this->assertEquals( $this->group_id, $field_group->id );
|
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
$this->check_group_data( $field_group, $all_data, 'view', $response->get_links() );
|
|
}
|
|
|
|
/**
|
|
* @group get_item
|
|
*/
|
|
public function test_get_item_invalid_id() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 );
|
|
}
|
|
|
|
/**
|
|
* @group create_item
|
|
*/
|
|
public function test_create_item() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
|
|
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
|
|
|
|
$params = $this->set_field_group_data();
|
|
$request->set_body_params( $params );
|
|
$request->set_param( 'context', 'edit' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->check_create_field_group_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @group create_item
|
|
*/
|
|
public function test_rest_create_item() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
|
|
$request->add_header( 'content-type', 'application/json' );
|
|
|
|
$params = $this->set_field_group_data();
|
|
$request->set_body( wp_json_encode( $params ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->check_create_field_group_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @group create_item
|
|
*/
|
|
public function test_create_item_user_not_logged_in() {
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
|
|
$request->add_header( 'content-type', 'application/json' );
|
|
|
|
$params = $this->set_field_group_data();
|
|
$request->set_body( wp_json_encode( $params ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group create_item
|
|
*/
|
|
public function test_create_item_user_without_permission() {
|
|
$u = static::factory()->user->create();
|
|
wp_set_current_user( $u );
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
|
|
$request->add_header( 'content-type', 'application/json' );
|
|
|
|
$params = $this->set_field_group_data();
|
|
$request->set_body( wp_json_encode( $params ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group update_item
|
|
*/
|
|
public function test_update_item() {
|
|
$new_name = 'Updated name';
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$request->add_header( 'content-type', 'application/json' );
|
|
$request->set_body( wp_json_encode( array( 'name' => $new_name ) ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$object = $response->get_data();
|
|
$updated = $this->endpoint->get_xprofile_field_group_object( $object['id'] );
|
|
|
|
$this->assertSame( $new_name, $updated->name );
|
|
}
|
|
|
|
/**
|
|
* @group update_item
|
|
*/
|
|
public function test_update_item_invalid_id() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 );
|
|
}
|
|
|
|
/**
|
|
* @group update_item
|
|
*/
|
|
public function test_update_item_user_not_logged_in() {
|
|
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group update_item
|
|
*/
|
|
public function test_update_item_without_permission() {
|
|
$u = static::factory()->user->create();
|
|
wp_set_current_user( $u );
|
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group delete_item
|
|
*/
|
|
public function test_delete_item() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id );
|
|
|
|
$request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $field_group->id ) );
|
|
$request->set_param( 'context', 'edit' );
|
|
$response = $this->server->dispatch( $request );
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
$this->check_group_data( $field_group, $all_data['previous'], 'view', $response->get_links() );
|
|
}
|
|
|
|
/**
|
|
* @group delete_item
|
|
*/
|
|
public function test_delete_item_invalid_id() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 );
|
|
}
|
|
|
|
/**
|
|
* @group delete_item
|
|
*/
|
|
public function test_delete_item_user_not_logged_in() {
|
|
$request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group delete_item
|
|
*/
|
|
public function test_delete_item_without_permission() {
|
|
$u = static::factory()->user->create();
|
|
wp_set_current_user( $u );
|
|
|
|
$request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() );
|
|
}
|
|
|
|
/**
|
|
* @group prepare_item
|
|
*/
|
|
public function test_prepare_item() {
|
|
wp_set_current_user( $this->user );
|
|
|
|
$group = $this->endpoint->get_xprofile_field_group_object( $this->group_id );
|
|
$this->assertEquals( $this->group_id, $group->id );
|
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $group->id ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$all_data = $response->get_data();
|
|
$this->assertNotEmpty( $all_data );
|
|
|
|
$this->check_group_data( $group, $all_data, 'view', $response->get_links() );
|
|
}
|
|
|
|
protected function check_create_field_group_response( $response ) {
|
|
$this->assertNotInstanceOf( 'WP_Error', $response );
|
|
$response = rest_ensure_response( $response );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertNotEmpty( $data );
|
|
|
|
$field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] );
|
|
$this->check_group_data( $field_group, $data, 'view', $response->get_links() );
|
|
}
|
|
|
|
protected function check_group_data( $group, $data, $context, $links ) {
|
|
$this->assertEquals( $group->id, $data['id'] );
|
|
$this->assertEquals( $group->name, $data['name'] );
|
|
$this->assertEquals( $group->group_order, $data['group_order'] );
|
|
$this->assertEquals( $group->can_delete, $data['can_delete'] );
|
|
|
|
if ( 'view' === $context ) {
|
|
$this->assertEquals( $group->description, $data['description']['rendered'] );
|
|
} else {
|
|
$this->assertEquals( $group->description, $data['description']['raw'] );
|
|
}
|
|
}
|
|
|
|
protected function set_field_group_data( $args = array() ) {
|
|
return wp_parse_args(
|
|
$args,
|
|
array(
|
|
'description' => 'Field Group Description',
|
|
'name' => 'Test Field Name',
|
|
'can_delete' => true,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function test_get_item_schema() {
|
|
$request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$response = $this->server->dispatch( $request );
|
|
$data = $response->get_data();
|
|
$properties = $data['schema']['properties'];
|
|
|
|
$this->assertEquals( 6, count( $properties ) );
|
|
$this->assertArrayHasKey( 'id', $properties );
|
|
$this->assertArrayHasKey( 'name', $properties );
|
|
$this->assertArrayHasKey( 'description', $properties );
|
|
$this->assertArrayHasKey( 'group_order', $properties );
|
|
$this->assertArrayHasKey( 'can_delete', $properties );
|
|
$this->assertArrayHasKey( 'fields', $properties );
|
|
}
|
|
|
|
public function test_context_param() {
|
|
// Collection.
|
|
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url );
|
|
$response = $this->server->dispatch( $request );
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
|
$this->assertEquals( array( 'view', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
|
}
|
|
|
|
public function update_additional_field( $value, $data, $attribute ) {
|
|
return bp_xprofile_update_meta( $data->id, 'group', '_' . $attribute, $value );
|
|
}
|
|
|
|
public function get_additional_field( $data, $attribute ) {
|
|
return bp_xprofile_get_meta( $data['id'], 'group', '_' . $attribute );
|
|
}
|
|
|
|
/**
|
|
* @group additional_fields
|
|
*/
|
|
public function test_additional_fields() {
|
|
$registered_fields = $GLOBALS['wp_rest_additional_fields'];
|
|
|
|
bp_rest_register_field(
|
|
'xprofile',
|
|
'foo_group_key',
|
|
array(
|
|
'get_callback' => array( $this, 'get_additional_field' ),
|
|
'update_callback' => array( $this, 'update_additional_field' ),
|
|
'schema' => array(
|
|
'description' => 'xProfile Group Meta Field',
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
'group'
|
|
);
|
|
|
|
wp_set_current_user( $this->user );
|
|
$expected = 'bar_group_value';
|
|
|
|
// POST
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
|
|
$request->set_param( 'context', 'edit' );
|
|
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
|
|
$params = $this->set_field_group_data( array( 'foo_group_key' => $expected ) );
|
|
$request->set_body_params( $params );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$create_data = $response->get_data();
|
|
$this->assertTrue( $expected === $create_data['foo_group_key'] );
|
|
|
|
// GET
|
|
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $create_data['id'] ) );
|
|
$request->set_param( 'context', 'view' );
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$get_data = $response->get_data();
|
|
$this->assertTrue( $expected === $get_data['foo_group_key'] );
|
|
|
|
$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
|
|
}
|
|
|
|
/**
|
|
* @group additional_fields
|
|
*/
|
|
public function test_update_additional_fields() {
|
|
$registered_fields = $GLOBALS['wp_rest_additional_fields'];
|
|
|
|
bp_rest_register_field(
|
|
'xprofile',
|
|
'bar_group_key',
|
|
array(
|
|
'get_callback' => array( $this, 'get_additional_field' ),
|
|
'update_callback' => array( $this, 'update_additional_field' ),
|
|
'schema' => array(
|
|
'description' => 'xProfile Group Meta Field',
|
|
'type' => 'string',
|
|
'context' => array( 'view', 'edit' ),
|
|
),
|
|
),
|
|
'group'
|
|
);
|
|
|
|
wp_set_current_user( $this->user );
|
|
$expected = 'foo_group_value';
|
|
|
|
// PUT
|
|
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) );
|
|
$request->set_param( 'context', 'edit' );
|
|
$request->add_header( 'content-type', 'application/json' );
|
|
$request->set_body(
|
|
wp_json_encode(
|
|
array(
|
|
'name' => $this->endpoint->get_xprofile_field_group_object( $this->group_id )->name,
|
|
'bar_group_key' => $expected,
|
|
)
|
|
)
|
|
);
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$update_data = $response->get_data();
|
|
$this->assertTrue( $expected === $update_data['bar_group_key'] );
|
|
|
|
$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
|
|
}
|
|
}
|