From 0e046bf350b11bfd429733c3061f44b9f7ee922f Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Tue, 21 May 2024 23:17:37 -0300 Subject: [PATCH] Unit tests cleanup --- .github/workflows/unit-tests.yml | 2 +- ...rest-group-membership-request-endpoint.php | 16 +- tests/bootstrap.php | 3 - tests/define-constants.php | 5 +- tests/env-wp-phpunit-config.php | 83 ----- tests/testcases/activity/test-controller.php | 325 +++++++++--------- .../test-blog-avatar-controller.php | 79 ++--- .../test-group-avatar-controller.php | 30 +- .../test-group-cover-controller.php | 28 +- .../test-member-avatar-controller.php | 48 +-- .../test-member-cover-controller.php | 22 +- tests/testcases/blogs/test-controller.php | 86 ++--- .../testcases/components/test-controller.php | 36 +- tests/testcases/friends/test-controller.php | 68 ++-- tests/testcases/groups/test-controller.php | 230 ++++++------- .../groups/test-group-invites-controller.php | 168 +++++---- tests/testcases/members/test-controller.php | 164 ++++----- .../test-group-membership-controller.php | 241 ++++++------- ...st-group-membership-request-controller.php | 154 ++++----- tests/testcases/messages/test-controller.php | 272 +++++++-------- .../notifications/test-controller.php | 112 +++--- tests/testcases/signup/test-controller.php | 70 ++-- .../sitewide-notices/test-controller.php | 113 +++--- .../xprofile/test-data-controller.php | 96 +++--- .../xprofile/test-field-controller.php | 62 ++-- .../xprofile/test-group-controller.php | 56 ++- 26 files changed, 1153 insertions(+), 1416 deletions(-) delete mode 100644 tests/env-wp-phpunit-config.php diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 12f5c290..0b1a38d3 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.4', '8.0', '8.1', '8.2'] + php: ['7.4', '8.0', '8.1', '8.2', '8.3'] wp_version: ['master'] include: - php: '8.0' diff --git a/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php b/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php index ca1046b4..a0ae22e4 100644 --- a/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php +++ b/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php @@ -433,25 +433,19 @@ public function create_item_permissions_check( $request ) { $retval = new WP_Error( 'bp_rest_authorization_required', __( 'Sorry, you need to be logged in to create a membership request.', 'buddypress' ), - array( - 'status' => rest_authorization_required_code(), - ) + array( 'status' => rest_authorization_required_code() ) ); } elseif ( ! $user instanceof WP_User ) { $retval = new WP_Error( 'bp_rest_group_member_invalid_id', __( 'Invalid member ID.', 'buddypress' ), - array( - 'status' => 404, - ) + array( 'status' => 404 ) ); } elseif ( ! $group instanceof BP_Groups_Group ) { $retval = new WP_Error( 'bp_rest_group_invalid_id', __( 'Invalid group ID.', 'buddypress' ), - array( - 'status' => 404, - ) + array( 'status' => 404 ) ); } elseif ( bp_current_user_can( 'bp_moderate' ) || $user_id === $user_id_arg ) { $retval = true; @@ -459,9 +453,7 @@ public function create_item_permissions_check( $request ) { $retval = new WP_Error( 'bp_rest_group_membership_requests_cannot_create_item', __( 'User may not extend requests on behalf of another user.', 'buddypress' ), - array( - 'status' => rest_authorization_required_code(), - ) + array( 'status' => rest_authorization_required_code() ) ); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8ea6a8ec..3d67389f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -38,9 +38,6 @@ function _manually_load_plugins() { echo "Loading WP Testing environment...\n"; require_once WP_TESTS_DIR . '/includes/bootstrap.php'; -echo "Loading REST controllers...\n"; -require_once WP_TESTS_DIR . '/includes/testcase-rest-controller.php'; - echo "Loading BuddyPress testcases...\n"; require_once BP_TESTS_DIR . '/includes/testcase.php'; require_once BP_TESTS_DIR . '/includes/testcase-emails.php'; diff --git a/tests/define-constants.php b/tests/define-constants.php index 7740c46e..7fbd59a7 100644 --- a/tests/define-constants.php +++ b/tests/define-constants.php @@ -17,10 +17,7 @@ * - Assume that we are inside of a develop.svn.wordpress.org setup, and walk * up the directory tree */ -if ( false !== getenv( 'WP_PHPUNIT__DIR' ) ) { - define( 'WP_TESTS_DIR', getenv( 'WP_PHPUNIT__DIR' ) ); - define( 'WP_ROOT_DIR', '/var/www/html' ); -} elseif ( false !== getenv( 'WP_TESTS_DIR' ) ) { +if ( false !== getenv( 'WP_TESTS_DIR' ) ) { define( 'WP_TESTS_DIR', getenv( 'WP_TESTS_DIR' ) ); define( 'WP_ROOT_DIR', WP_TESTS_DIR ); } else { diff --git a/tests/env-wp-phpunit-config.php b/tests/env-wp-phpunit-config.php deleted file mode 100644 index 25f0abd8..00000000 --- a/tests/env-wp-phpunit-config.php +++ /dev/null @@ -1,83 +0,0 @@ -bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Activity_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->activity->id; - $this->activity_id = $this->bp_factory->activity->create(); + $this->activity_id = $this->bp::factory()->activity->create(); - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -33,13 +29,6 @@ public function set_up() { if ( ! $this->server ) { $this->server = rest_get_server(); } - - $this->old_current_user = get_current_user_id(); - } - - public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); } public function test_register_routes() { @@ -58,9 +47,9 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->activity->create_many( 3 ); + $this->bp::factory()->activity->create_many( 3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -80,7 +69,7 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $this->bp_factory->activity->create_many( 3 ); + $this->bp::factory()->activity->create_many( 3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -93,11 +82,11 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_multiple_types() { - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'public', ) ); - $this->bp_factory->activity->create( array( + $this->bp::factory()->activity->create( array( 'component' => buddypress()->groups->id, 'type' => 'created_group', 'user_id' => $this->user, @@ -105,7 +94,7 @@ public function test_get_items_multiple_types() { 'hide_sitewide' => true, ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -126,7 +115,7 @@ public function test_get_items_multiple_types() { * @group get_items */ public function test_get_items_with_invalid_type() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -145,15 +134,15 @@ public function test_get_public_groups_items() { $component = buddypress()->groups->id; // Current user is $this->user. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', ) ); - $g2 = $this->bp_factory->group->create( array( + $g2 = $this->bp::factory()->group->create( array( 'status' => 'public', ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -161,15 +150,15 @@ public function test_get_public_groups_items() { 'hide_sitewide' => true, ) ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, 'item_id' => $g2, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -191,24 +180,24 @@ public function test_get_public_groups_items() { public function test_get_items_from_a_specific_group() { $component = buddypress()->groups->id; - $g1 = $this->bp_factory->group->create( array( 'status' => 'public' ) ); - $g2 = $this->bp_factory->group->create( array( 'status' => 'public' ) ); + $g1 = $this->bp::factory()->group->create( array( 'status' => 'public' ) ); + $g2 = $this->bp::factory()->group->create( array( 'status' => 'public' ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, 'item_id' => $g2, ) ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, 'item_id' => $g2, ) ); - $a3 = $this->bp_factory->activity->create( array( + $a3 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -216,15 +205,15 @@ public function test_get_items_from_a_specific_group() { 'hide_sitewide' => true, ) ); - $a4 = $this->bp_factory->activity->create( array( + $a4 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, 'item_id' => $g1, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'group_id' => $g2 ) ); @@ -246,21 +235,21 @@ public function test_get_items_from_a_specific_group() { public function test_get_private_group_items() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); // Current user is $u. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u, ) ); - $g2 = $this->bp_factory->group->create( array( + $g2 = $this->bp::factory()->group->create( array( 'status' => 'public', 'creator_id' => $this->user, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u, @@ -268,7 +257,7 @@ public function test_get_private_group_items() { 'hide_sitewide' => true, ) ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -297,16 +286,16 @@ public function test_get_private_group_items() { public function test_get_private_group_items_without_access() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); // Current user is $u. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $this->user, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -336,21 +325,21 @@ public function test_get_private_group_items_without_access() { public function test_get_private_group_items_with_the_group_id_param() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); // Current user is $u. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u, ) ); - $g2 = $this->bp_factory->group->create( array( + $g2 = $this->bp::factory()->group->create( array( 'status' => 'public', 'creator_id' => $this->user, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u, @@ -358,7 +347,7 @@ public function test_get_private_group_items_with_the_group_id_param() { 'hide_sitewide' => true, ) ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -386,10 +375,10 @@ public function test_get_private_group_items_with_the_group_id_param() { public function test_get_private_group_items_for_mod() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', 'creator_id' => $this->user, ) ); @@ -397,7 +386,7 @@ public function test_get_private_group_items_for_mod() { groups_join_group( $g1, $u ); groups_promote_member( $u, $g1, 'mod' ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'activity_update', 'user_id' => $this->user, @@ -405,7 +394,7 @@ public function test_get_private_group_items_for_mod() { 'hide_sitewide' => true, ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -428,10 +417,10 @@ public function test_get_private_group_items_for_mod() { public function test_get_private_group_items_for_admin() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); - $g2 = $this->bp_factory->group->create( array( + $g2 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $this->user, ) ); @@ -439,7 +428,7 @@ public function test_get_private_group_items_for_admin() { groups_join_group( $g2, $u ); groups_promote_member( $u, $g2, 'admin' ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' =>'activity_update', 'user_id' => $this->user, @@ -447,7 +436,7 @@ public function test_get_private_group_items_for_admin() { 'hide_sitewide' => true, ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -470,16 +459,16 @@ public function test_get_private_group_items_for_admin() { public function test_get_private_group_items_with_group_id_param_without_access() { $component = buddypress()->groups->id; - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); // Current user is $u. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $this->user, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -506,11 +495,11 @@ public function test_get_private_group_items_with_group_id_param_without_access( * @group get_items */ public function test_get_paginated_items() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); - $a = $this->bp_factory->activity->create( array( 'user_id' => $u ) ); - $this->bp_factory->activity->create_many( 5, array( 'user_id' => $u ) ); + $a = $this->bp::factory()->activity->create( array( 'user_id' => $u ) ); + $this->bp::factory()->activity->create_many( 5, array( 'user_id' => $u ) ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -535,24 +524,24 @@ public function test_get_paginated_items() { * @group get_items */ public function test_get_items_with_the_groups_scope() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $component = buddypress()->groups->id; // Current user is $this->user. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'public', ) ); - $a2 = $this->bp_factory->activity->create( array( + $a2 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u, 'item_id' => $g1, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'activity_update', 'user_id' => $u, @@ -580,13 +569,13 @@ public function test_get_items_with_the_groups_scope() { * @group get_items */ public function test_get_items_with_favorite() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->activity->create_many( 2 ); - $a1 = $this->bp_factory->activity->create(); + $this->bp::factory()->activity->create_many( 2 ); + $a1 = $this->bp::factory()->activity->create(); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); bp_activity_add_user_favorite( $a1, $u ); @@ -605,10 +594,10 @@ public function test_get_items_with_favorite() { * @group get_items */ public function test_get_items_with_no_favorite() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); - $this->bp_factory->activity->create_many( 3, array( 'user_id' => $u ) ); + $this->bp::factory()->activity->create_many( 3, array( 'user_id' => $u ) ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -627,7 +616,7 @@ public function test_get_items_with_no_favorite() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $activity = $this->endpoint->get_activity_object( $this->activity_id ); $this->assertEquals( $this->activity_id, $activity->id ); @@ -665,16 +654,16 @@ public function test_get_item_with_support_for_the_community_visibility() { */ public function test_get_embedded_hidden_group_from_activity() { $component = buddypress()->groups->id; - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', 'creator_id' => $u1, ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u1, @@ -699,16 +688,16 @@ public function test_get_embedded_hidden_group_from_activity() { */ public function test_get_embedded_hidden_group_from_activity_without_permission() { $component = buddypress()->groups->id; - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $g1 = $this->bp_factory->group->create( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', 'creator_id' => $u1, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u2, @@ -728,16 +717,16 @@ public function test_get_embedded_hidden_group_from_activity_without_permission( */ public function test_get_embedded_private_group_from_activity_without_permission() { $component = buddypress()->groups->id; - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $g1 = $this->bp_factory->group->create( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', 'creator_id' => $u1, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $u2, @@ -759,11 +748,11 @@ public function test_get_item_for_item_belonging_to_private_group() { $component = buddypress()->groups->id; // Current user is $this->user. - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', ) ); - $a1 = $this->bp_factory->activity->create( array( + $a1 = $this->bp::factory()->activity->create( array( 'component' => $component, 'type' => 'created_group', 'user_id' => $this->user, @@ -774,13 +763,13 @@ public function test_get_item_for_item_belonging_to_private_group() { $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $a1 ); // Non-authenticated. - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $response = $this->server->dispatch( $request ); $this->assertEquals( 401, $response->get_status() ); // Not a member of the group. - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $response = $this->server->dispatch( $request ); @@ -802,9 +791,9 @@ public function test_get_item_for_item_belonging_to_private_group() { * @group render_item */ public function test_render_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'user_id' => $this->user, 'content' => 'links should be clickable: https://buddypress.org', ) ); @@ -825,10 +814,10 @@ public function test_render_item() { * @group render_item */ public function test_render_item_with_embed_post() { - $this->bp->set_current_user( $this->user ); - $p = $this->factory->post->create(); + $this->bp::set_current_user( $this->user ); + $p = static::factory()->post->create(); - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'user_id' => $this->user, 'content' => get_post_embed_url( $p ), ) ); @@ -849,7 +838,7 @@ public function test_render_item_with_embed_post() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -866,7 +855,7 @@ public function test_create_item() { * @group create_item */ public function test_rest_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -883,7 +872,7 @@ public function test_rest_create_item() { * @group create_item */ public function test_create_item_with_no_content() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -915,8 +904,8 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_in_a_group() { - $this->bp->set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $this->bp::set_current_user( $this->user ); + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, ) ); @@ -939,8 +928,8 @@ public function test_create_item_in_a_group() { * @group create_item */ public function test_create_item_in_a_private_group() { - $this->bp->set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $this->bp::set_current_user( $this->user ); + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, 'status' => 'private', ) ); @@ -964,8 +953,8 @@ public function test_create_item_in_a_private_group() { * @group create_item */ public function test_create_item_in_an_hidden_group() { - $this->bp->set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $this->bp::set_current_user( $this->user ); + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, 'status' => 'hidden', ) ); @@ -989,17 +978,17 @@ public function test_create_item_in_an_hidden_group() { * @group create_item */ public function test_create_item_and_get_comment() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'component' => 'activity', 'type' => 'activity_update', 'user_id' => $this->user, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -1038,9 +1027,9 @@ public function test_create_item_and_get_comment() { * @group create_item */ public function test_create_item_and_get_reply() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'component' => 'activity', 'type' => 'activity_update', 'user_id' => $u, @@ -1053,7 +1042,7 @@ public function test_create_item_and_get_reply() { 'content' => 'Activity comment', ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); // Add a reply to c $request = new WP_REST_Request( 'POST', $this->endpoint_url ); @@ -1094,22 +1083,22 @@ public function test_create_item_and_get_reply() { * @group create_item */ public function test_create_item_and_get_comment_in_a_group() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, ) ); - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'component' => buddypress()->groups->id, 'type' => 'activity_update', 'user_id' => $this->user, 'item_id' => $g, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); groups_join_group( $g, $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); @@ -1147,8 +1136,8 @@ public function test_create_item_and_get_comment_in_a_group() { * @group create_item */ public function test_create_item_with_no_content_in_a_group() { - $this->bp->set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $this->bp::set_current_user( $this->user ); + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, ) ); @@ -1172,8 +1161,8 @@ public function test_create_item_with_no_content_in_a_group() { * @group create_item */ public function test_create_blog_post_item() { - $this->bp->set_current_user( $this->user ); - $p = $this->factory->post->create(); + $this->bp::set_current_user( $this->user ); + $p = static::factory()->post->create(); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -1212,7 +1201,7 @@ public function test_create_blog_post_item() { * @group update_item */ public function test_update_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $activity = $this->endpoint->get_activity_object( $this->activity_id ); $this->assertEquals( $this->activity_id, $activity->id ); @@ -1244,9 +1233,9 @@ public function test_update_item() { */ public function test_update_item_but_keep_date_the_same() { $activity_date = '1968-12-25 01:23:45'; - $activity_id = $this->bp_factory->activity->create( [ 'recorded_time' => $activity_date ] ); + $activity_id = $this->bp::factory()->activity->create( [ 'recorded_time' => $activity_date ] ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $activity_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -1268,16 +1257,16 @@ public function test_update_item_but_keep_date_the_same() { * @group PR448 */ public function test_update_item_posted_in_a_group() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $g = $this->bp_factory->group->create( + $g = $this->bp::factory()->group->create( array( 'creator_id' => $this->user, 'status' => 'hidden', ) ); - $a = $this->bp_factory->activity->create_and_get( + $a = $this->bp::factory()->activity->create_and_get( array( 'user_id' => $this->user, 'component' => buddypress()->groups->id, @@ -1320,7 +1309,7 @@ public function test_update_item_posted_in_a_group() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->add_header( 'content-type', 'application/json' ); @@ -1352,11 +1341,11 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $a = $this->bp_factory->activity->create( array( 'user_id' => $u ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $a = $this->bp::factory()->activity->create( array( 'user_id' => $u ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u2 ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u2 ); $activity = $this->endpoint->get_activity_object( $a ); $this->assertEquals( $a, $activity->id ); @@ -1375,10 +1364,10 @@ public function test_update_item_without_permission() { * @group update_item */ public function test_update_item_without_content() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $a = $this->bp_factory->activity->create( array( 'user_id' => $u ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $a = $this->bp::factory()->activity->create( array( 'user_id' => $u ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $activity = $this->endpoint->get_activity_object( $a ); $this->assertEquals( $a, $activity->id ); @@ -1397,10 +1386,10 @@ public function test_update_item_without_content() { * @group update_item */ public function test_update_item_without_type() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $a = $this->bp_factory->activity->create( array( 'user_id' => $u ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $a = $this->bp::factory()->activity->create( array( 'user_id' => $u ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $activity = $this->endpoint->get_activity_object( $a ); $this->assertEquals( $a, $activity->id ); @@ -1422,9 +1411,9 @@ public function test_update_item_without_type() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $activity_id = $this->bp_factory->activity->create( array( + $activity_id = $this->bp::factory()->activity->create( array( 'content' => 'Deleted activity', ) ); @@ -1448,7 +1437,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -1473,11 +1462,11 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $activity_id = $this->bp_factory->activity->create( array( 'user_id' => $u ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $activity_id = $this->bp::factory()->activity->create( array( 'user_id' => $u ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u2 ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u2 ); $activity = $this->endpoint->get_activity_object( $activity_id ); $this->assertEquals( $activity_id, $activity->id ); @@ -1492,12 +1481,12 @@ public function test_delete_item_without_permission() { * @group update_favorite */ public function test_update_favorite() { - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'user_id' => $this->user, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d/favorite', $a ) ); $request->add_header( 'content-type', 'application/json' ); @@ -1514,12 +1503,12 @@ public function test_update_favorite() { * @group update_favorite */ public function test_update_favorite_remove() { - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'user_id' => $this->user, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); bp_activity_add_user_favorite( $a, $u ); @@ -1537,12 +1526,12 @@ public function test_update_favorite_remove() { * @group update_favorite */ public function test_update_favorite_when_disabled() { - $a = $this->bp_factory->activity->create( array( + $a = $this->bp::factory()->activity->create( array( 'user_id' => $this->user, ) ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); add_filter( 'bp_activity_can_favorite', '__return_false' ); @@ -1556,7 +1545,7 @@ public function test_update_favorite_when_disabled() { } public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $activity = $this->endpoint->get_activity_object( $this->activity_id ); $this->assertEquals( $this->activity_id, $activity->id ); @@ -1589,7 +1578,7 @@ public function test_additional_fields() { ), ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_value'; // POST @@ -1630,9 +1619,9 @@ public function test_update_additional_fields() { ), ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'foo_value'; - $a_id = $this->bp_factory->activity->create(); + $a_id = $this->bp::factory()->activity->create(); // PUT $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $a_id ) ); diff --git a/tests/testcases/attachments/test-blog-avatar-controller.php b/tests/testcases/attachments/test-blog-avatar-controller.php index f4cd47b0..7b9b9f96 100644 --- a/tests/testcases/attachments/test-blog-avatar-controller.php +++ b/tests/testcases/attachments/test-blog-avatar-controller.php @@ -7,7 +7,6 @@ * @group blog-avatar */ class BP_Test_REST_Attachments_Blog_Avatar_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -15,8 +14,6 @@ class BP_Test_REST_Attachments_Blog_Avatar_Endpoint extends WP_Test_REST_Control public function set_up() { parent::set_up(); - - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Attachments_Blog_Avatar_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->blogs->id . '/'; @@ -26,10 +23,6 @@ public function set_up() { } } - public function tear_down() { - parent::tear_down(); - } - public function test_register_routes() { $routes = $this->server->get_routes(); $endpoint = $this->endpoint_url . '(?P[\d]+)/avatar'; @@ -50,24 +43,22 @@ public function test_get_items() { * @group get_item */ public function test_get_item() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $u = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); $expected = array( 'full' => get_avatar_url( $u, array( 'size' => 150 ) ), 'thumb' => get_avatar_url( $u, array( 'size' => 50 ) ), ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data[0], $expected ); @@ -77,9 +68,11 @@ public function test_get_item() { * @group get_item */ public function test_get_item_with_support_for_the_community_visibility() { + $this->skipWithoutMultisite(); + toggle_component_visibility(); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -91,26 +84,24 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_site_icon() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $expected = array( 'full' => bp_blogs_default_avatar( '', array( 'object' => 'blog', 'type' => 'full' ) ), 'thumb' => bp_blogs_default_avatar( '', array( 'object' => 'blog', 'type' => 'thumb' ) ), ); - $u = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); $request->set_param( 'no_user_gravatar', true ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data[0], $expected ); @@ -120,15 +111,13 @@ public function test_get_item_site_icon() { * @group get_item */ public function test_get_item_no_grav() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $u = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); $expected = array( 'full' => bp_get_blog_avatar( array( 'blog_id' => $blog_id, 'html' => false, 'type' => 'full' ) ), 'thumb' => bp_get_blog_avatar( array( 'blog_id' => $blog_id, 'html' => false, 'type' => 'thumb' ) ), @@ -138,7 +127,7 @@ public function test_get_item_no_grav() { $request->set_param( 'context', 'view' ); $request->set_param( 'no_user_gravatar', true ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data[0], $expected ); @@ -148,25 +137,23 @@ public function test_get_item_no_grav() { * @group get_item */ public function test_get_item_invalid_user_id() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $current_user = get_current_user_id(); - $u = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); - $blog_id = $this->bp_factory->blog->create( array( 'meta' => array( 'public' => 1 ) ) ); + $blog_id = $this->bp::factory()->blog->create( array( 'meta' => array( 'public' => 1 ) ) ); - $this->bp->set_current_user( $current_user ); + $this->bp::set_current_user( $current_user ); // Remove admins. add_filter( 'bp_blogs_get_blogs', array( $this, 'filter_admin_user_id' ), 10, 1 ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); remove_filter( 'bp_blogs_get_blogs', array( $this, 'filter_admin_user_id' ), 10, 1 ); @@ -183,12 +170,10 @@ public function filter_admin_user_id( $blog_results ) { * @group get_item */ public function test_get_item_invalid_blog_id() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_blog_invalid_id', $response, 404 ); } @@ -221,11 +206,9 @@ public function test_prepare_item() { } public function test_get_item_schema() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); // Single. $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); @@ -239,11 +222,9 @@ public function test_get_item_schema() { } public function test_context_param() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $blog_id = $this->bp_factory->blog->create(); + $blog_id = $this->bp::factory()->blog->create(); // Single. $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '%d/avatar', $blog_id ) ); diff --git a/tests/testcases/attachments/test-group-avatar-controller.php b/tests/testcases/attachments/test-group-avatar-controller.php index 79d3c87c..9370338c 100644 --- a/tests/testcases/attachments/test-group-avatar-controller.php +++ b/tests/testcases/attachments/test-group-avatar-controller.php @@ -7,7 +7,6 @@ * @group group-avatar */ class BP_Test_REST_Attachments_Group_Avatar_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -15,22 +14,20 @@ class BP_Test_REST_Attachments_Group_Avatar_Endpoint extends WP_Test_REST_Contro protected $user_id; protected $group_id; protected $server; - protected $old_current_user; public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Attachments_Group_Avatar_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id . '/'; $this->image_file = __DIR__ . '/assets/test-image.jpg'; - $this->user_id = $this->bp_factory->user->create( array( + $this->user_id = $this->bp::factory()->user->create( array( 'role' => 'administrator', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'creator_id' => $this->user_id, @@ -39,13 +36,6 @@ public function set_up() { if ( ! $this->server ) { $this->server = rest_get_server(); } - - $this->old_current_user = get_current_user_id(); - } - - public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); } public function test_register_routes() { @@ -68,7 +58,7 @@ public function test_get_items() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $this->group_id ) ); $request->set_param( 'context', 'view' ); @@ -129,7 +119,7 @@ public function test_create_item() { $reset_files = $_FILES; $reset_post = $_POST; - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); add_filter( 'pre_move_uploaded_file', array( $this, 'copy_file' ), 10, 3 ); add_filter( 'bp_core_avatar_dimension', array( $this, 'return_100' ), 10, 1 ); @@ -196,7 +186,7 @@ public function test_create_item_with_image_upload_disabled() { $reset_files = $_FILES; $reset_post = $_POST; - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); // Disabling group avatar upload. add_filter( 'bp_disable_group_avatar_uploads', '__return_true' ); @@ -225,7 +215,7 @@ public function test_create_item_with_image_upload_disabled() { * @group create_item */ public function test_create_item_empty_image() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -245,9 +235,9 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_invalid_group() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -272,7 +262,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_failed() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/avatar', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -293,7 +283,7 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_invalid_group() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/attachments/test-group-cover-controller.php b/tests/testcases/attachments/test-group-cover-controller.php index 8b6157b7..ec0cc652 100644 --- a/tests/testcases/attachments/test-group-cover-controller.php +++ b/tests/testcases/attachments/test-group-cover-controller.php @@ -7,7 +7,6 @@ * @group group-cover */ class BP_Test_REST_Attachments_Group_Cover_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -19,17 +18,16 @@ class BP_Test_REST_Attachments_Group_Cover_Endpoint extends WP_Test_REST_Control public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Attachments_Group_Cover_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id . '/'; $this->image_file = __DIR__ . '/assets/test-image.jpg'; - $this->user_id = $this->bp_factory->user->create( array( + $this->user_id = $this->bp::factory()->user->create( array( 'role' => 'administrator', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'creator_id' => $this->user_id, @@ -110,7 +108,7 @@ public function test_create_item_no_valid_image_directory() { $this->markTestSkipped(); } - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $reset_files = $_FILES; $_FILES['file'] = array( @@ -123,7 +121,7 @@ public function test_create_item_no_valid_image_directory() { $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', $this->group_id ) ); $request->set_file_params( $_FILES ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_group_cover_upload_error', $response, 500 ); @@ -138,7 +136,7 @@ public function test_create_item_image_upload_disabled() { $this->markTestSkipped(); } - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $reset_files = $_FILES; // Disabling group cover upload. @@ -165,7 +163,7 @@ public function test_create_item_image_upload_disabled() { * @group create_item */ public function test_create_item_empty_image() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -185,9 +183,9 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_unauthorized_user() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -198,7 +196,7 @@ public function test_create_item_unauthorized_user() { * @group create_item */ public function test_create_item_invalid_group_id() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -232,9 +230,9 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_unauthorized_user() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -245,7 +243,7 @@ public function test_delete_item_unauthorized_user() { * @group delete_item */ public function test_delete_item_invalid_group_id() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -256,7 +254,7 @@ public function test_delete_item_invalid_group_id() { * @group delete_item */ public function test_delete_item_failed() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', $this->group_id ) ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/attachments/test-member-avatar-controller.php b/tests/testcases/attachments/test-member-avatar-controller.php index d6117fa6..83d50da1 100644 --- a/tests/testcases/attachments/test-member-avatar-controller.php +++ b/tests/testcases/attachments/test-member-avatar-controller.php @@ -7,38 +7,28 @@ * @group member-avatar */ class BP_Test_REST_Attachments_Member_Avatar_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; protected $image_file; protected $user_id; protected $server; - protected $old_current_user; public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Attachments_Member_Avatar_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/members/'; $this->image_file = __DIR__ . '/assets/test-image.jpg'; - $this->user_id = $this->bp_factory->user->create( array( + $this->user_id = $this->bp::factory()->user->create( array( 'role' => 'administrator', ) ); if ( ! $this->server ) { $this->server = rest_get_server(); } - - $this->old_current_user = get_current_user_id(); - } - - public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); } public function test_register_routes() { @@ -61,13 +51,13 @@ public function test_get_items() { * @group get_item */ public function test_get_item() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $u1 ) ); $request->set_param( 'context', 'view' ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -96,7 +86,7 @@ public function test_get_item_with_support_for_the_community_visibility() { public function test_get_item_publicly() { $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); $request->set_param( 'context', 'view' ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -111,7 +101,7 @@ public function test_get_item_publicly() { */ public function test_get_item_invalid_member_id() { $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } @@ -126,7 +116,7 @@ public function test_create_item() { $reset_files = $_FILES; $reset_post = $_POST; - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); add_filter( 'pre_move_uploaded_file', array( $this, 'copy_file' ), 10, 3 ); add_filter( 'bp_core_avatar_dimension', array( $this, 'return_100' ), 10, 1 ); @@ -189,13 +179,13 @@ public function return_100() { * @group create_item */ public function test_create_item_with_upload_disabled() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); // Disabling member avatar upload. add_filter( 'bp_disable_avatar_uploads', '__return_true' ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_disabled', $response, 500 ); } @@ -203,10 +193,10 @@ public function test_create_item_with_upload_disabled() { * @group create_item */ public function test_create_item_empty_image() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_no_image_file', $response, 500 ); } @@ -215,7 +205,7 @@ public function test_create_item_empty_image() { */ public function test_create_item_user_not_logged_in() { $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } @@ -223,12 +213,12 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_invalid_member_id() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } @@ -250,10 +240,10 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_failed() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_no_uploaded_avatar', $response, 404 ); } @@ -263,7 +253,7 @@ public function test_delete_item_failed() { */ public function test_delete_item_user_not_logged_in() { $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/avatar', $this->user_id ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } @@ -272,7 +262,7 @@ public function test_delete_item_user_not_logged_in() { */ public function test_delete_item_invalid_member_id() { $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } diff --git a/tests/testcases/attachments/test-member-cover-controller.php b/tests/testcases/attachments/test-member-cover-controller.php index b839e685..7644d2e0 100644 --- a/tests/testcases/attachments/test-member-cover-controller.php +++ b/tests/testcases/attachments/test-member-cover-controller.php @@ -7,7 +7,6 @@ * @group member-cover */ class BP_Test_REST_Attachments_Member_Cover_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -17,12 +16,11 @@ class BP_Test_REST_Attachments_Member_Cover_Endpoint extends WP_Test_REST_Contro public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Attachments_Member_Cover_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/members/'; - $this->user_id = $this->bp_factory->user->create( array( + $this->user_id = $this->bp::factory()->user->create( array( 'role' => 'administrator', ) ); @@ -96,7 +94,7 @@ public function test_create_item() { * @group create_item */ public function test_create_item_with_upload_disabled() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); // Disabling cover image upload. add_filter( 'bp_disable_cover_image_uploads', '__return_true' ); @@ -112,7 +110,7 @@ public function test_create_item_with_upload_disabled() { * @group create_item */ public function test_create_item_empty_image() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', $this->user_id ) ); $response = $this->server->dispatch( $request ); @@ -132,9 +130,9 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_unauthorized_user() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', $this->user_id ) ); $response = $this->server->dispatch( $request ); @@ -145,7 +143,7 @@ public function test_create_item_unauthorized_user() { * @group create_item */ public function test_create_item_invalid_member_id() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/cover', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -179,9 +177,9 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_unauthorized_user() { - $u1 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', $this->user_id ) ); $response = $this->server->dispatch( $request ); @@ -192,7 +190,7 @@ public function test_delete_item_unauthorized_user() { * @group delete_item */ public function test_delete_item_invalid_member_id() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -203,7 +201,7 @@ public function test_delete_item_invalid_member_id() { * @group delete_item */ public function test_delete_item_failed() { - $this->bp->set_current_user( $this->user_id ); + $this->bp::set_current_user( $this->user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/cover', $this->user_id ) ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/blogs/test-controller.php b/tests/testcases/blogs/test-controller.php index 416abc55..b4d97c29 100644 --- a/tests/testcases/blogs/test-controller.php +++ b/tests/testcases/blogs/test-controller.php @@ -7,7 +7,6 @@ * @group blogs */ class BP_Test_REST_Blogs_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -17,11 +16,10 @@ class BP_Test_REST_Blogs_Endpoint extends WP_Test_REST_Controller_Testcase { public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Blogs_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->blogs->id; - $this->admin = $this->factory->user->create( array( + $this->admin = static::factory()->user->create( array( 'role' => 'administrator', ) ); @@ -30,10 +28,6 @@ public function set_up() { } } - public function tear_down() { - parent::tear_down(); - } - public function test_register_routes() { $routes = $this->server->get_routes(); @@ -50,13 +44,11 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); - $this->bp_factory->blog->create_many( 2 ); + $this->bp::factory()->blog->create_many( 2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -77,13 +69,11 @@ public function test_get_items() { * @group get_item */ public function test_get_item() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( array( 'title' => 'The Foo Bar Blog' ) ); @@ -109,7 +99,7 @@ public function test_get_item_with_support_for_the_community_visibility() { toggle_component_visibility(); - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( array( 'title' => 'The Foo Bar Blog' ) ); @@ -125,9 +115,7 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_invalid_blog_id() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -139,13 +127,11 @@ public function test_get_item_invalid_blog_id() { * @group get_item */ public function test_get_embedded_latest_post_from_blog_using_subdirectory() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( [ 'title' => 'The Foo Bar Blog', 'domain' => 'foo-bar', @@ -155,8 +141,8 @@ public function test_get_embedded_latest_post_from_blog_using_subdirectory() { switch_to_blog( $blog_id ); - $this->factory->post->create(); - $latest_post = $this->factory->post->create(); + static::factory()->post->create(); + $latest_post = static::factory()->post->create(); $permalink = get_permalink( $latest_post ); $title = get_the_title( $latest_post ); @@ -186,14 +172,12 @@ public function test_get_embedded_latest_post_from_blog_using_subdirectory() { * @group get_item */ public function test_get_embedded_latest_post_from_blog_using_subdomain() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); $subdomain = 'cool-site.foo-bar'; - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( [ 'title' => 'The Foo Bar Blog', 'domain' => $subdomain, @@ -203,8 +187,8 @@ public function test_get_embedded_latest_post_from_blog_using_subdomain() { switch_to_blog( $blog_id ); - $this->factory->post->create(); - $latest_post = $this->factory->post->create(); + static::factory()->post->create(); + $latest_post = static::factory()->post->create(); $permalink = get_permalink( $latest_post ); $title = get_the_title( $latest_post ); @@ -235,9 +219,7 @@ public function test_get_embedded_latest_post_from_blog_using_subdomain() { * @group create_item */ public function test_create_item() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $old_settings = $settings = buddypress()->site_options; @@ -248,7 +230,7 @@ public function test_create_item() { $settings['registration'] = 'blog'; buddypress()->site_options = $settings; - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -271,9 +253,7 @@ public function test_create_item() { * @group create_item */ public function test_create_item_user_not_logged_in() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -290,9 +270,7 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_signup_disabled() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $old_settings = $settings = buddypress()->site_options; @@ -303,7 +281,7 @@ public function test_create_item_signup_disabled() { $settings['registration'] = 'none'; buddypress()->site_options = $settings; - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -322,11 +300,9 @@ public function test_create_item_signup_disabled() { * @group create_item */ public function test_create_item_without_required_field() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); - $this->bp->set_current_user( $this->admin ); + $this->bp::set_current_user( $this->admin ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -386,9 +362,7 @@ public function get_additional_field( $data, $attribute ) { * @group additional_fields */ public function test_get_additional_fields() { - if ( ! is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithoutMultisite(); $registered_fields = $GLOBALS['wp_rest_additional_fields']; @@ -401,10 +375,10 @@ public function test_get_additional_fields() { ), ) ); - $u = $this->bp_factory->user->create(); - $this->bp->set_current_user( $u ); + $u = $this->bp::factory()->user->create(); + $this->bp::set_current_user( $u ); - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( array( 'title' => 'The Foo Bar Blog', 'user_id' => $u, diff --git a/tests/testcases/components/test-controller.php b/tests/testcases/components/test-controller.php index 92c467d9..1df1b093 100644 --- a/tests/testcases/components/test-controller.php +++ b/tests/testcases/components/test-controller.php @@ -7,7 +7,6 @@ * @group components */ class BP_Test_REST_Components_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -17,11 +16,10 @@ class BP_Test_REST_Components_Endpoint extends WP_Test_REST_Controller_Testcase public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Components_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/components'; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -43,7 +41,7 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -64,7 +62,7 @@ public function test_get_items() { * @group get_items */ public function test_get_items_paginated() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( @@ -90,7 +88,7 @@ public function test_get_items_paginated() { * @group get_items */ public function test_get_items_invalid_status() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( @@ -117,9 +115,9 @@ public function test_get_items_user_is_not_logged_in() { * @group get_items */ public function test_get_items_without_permission() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -132,13 +130,13 @@ public function test_get_items_without_permission() { * @group get_items */ public function test_get_items_active_permission() { - $u = $this->factory->user->create( + $u = static::factory()->user->create( array( 'role' => 'author', ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'status', 'active' ); @@ -156,7 +154,7 @@ public function test_get_items_active_permission() { * @group get_items */ public function test_get_items_active_component_features() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -177,7 +175,7 @@ public function test_get_items_active_component_features() { * @group get_items */ public function test_get_items_inactive_component_features() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); add_filter( 'bp_is_messages_star_active', '__return_false' ); @@ -210,7 +208,7 @@ public function deactivate_activity_component( $retval, $component ) { * @group get_items */ public function test_get_items_inactive_component() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); add_filter( 'bp_is_active', array( $this, 'deactivate_activity_component' ), 10, 2 ); @@ -250,7 +248,7 @@ public function test_create_item() { * @group update_item */ public function test_update_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( @@ -271,7 +269,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_nonexistent_component() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( @@ -287,7 +285,7 @@ public function test_update_item_nonexistent_component() { * @group update_item */ public function test_update_item_empty_action() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( @@ -303,7 +301,7 @@ public function test_update_item_empty_action() { * @group update_item */ public function test_update_item_invalid_action() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( @@ -333,9 +331,9 @@ public function test_update_item_user_is_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( diff --git a/tests/testcases/friends/test-controller.php b/tests/testcases/friends/test-controller.php index 2794dc38..0f1e4478 100644 --- a/tests/testcases/friends/test-controller.php +++ b/tests/testcases/friends/test-controller.php @@ -7,7 +7,6 @@ * @group friends */ class BP_Test_REST_Friends_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -19,12 +18,11 @@ class BP_Test_REST_Friends_Endpoint extends WP_Test_REST_Controller_Testcase { public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Friends_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->friends->id; - $this->friend = $this->factory->user->create(); - $this->user = $this->factory->user->create( + $this->friend = static::factory()->user->create(); + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', @@ -39,10 +37,6 @@ public function set_up() { } } - public function tear_down() { - parent::tear_down(); - } - public function test_register_routes() { $routes = $this->server->get_routes(); @@ -64,7 +58,7 @@ public function test_get_items() { $this->create_friendship(); $this->create_friendship(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( @@ -99,7 +93,7 @@ public function test_get_items_user_not_logged_in() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $response = $this->server->dispatch( $request ); @@ -119,7 +113,7 @@ public function test_get_item() { * @group get_item */ public function test_get_item_with_invalid_friend_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -142,7 +136,7 @@ public function test_get_item_user_not_logged_in() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -167,10 +161,10 @@ public function test_create_item() { * @group create_item */ public function test_create_item_to_myself_from_someone_else() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -190,9 +184,9 @@ public function test_create_item_to_myself_from_someone_else() { * @group create_item */ public function test_admins_create_item_to_myself_from_someone_else() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -212,10 +206,10 @@ public function test_admins_create_item_to_myself_from_someone_else() { * @group create_item */ public function test_create_item_already_friends() { - $user = $this->factory->user->create(); + $user = static::factory()->user->create(); $this->create_friendship( $user ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -249,7 +243,7 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_regular_user_can_not_create_friendship_to_others() { - $this->bp->set_current_user( $this->factory->user->create() ); + $this->bp::set_current_user( static::factory()->user->create() ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -265,7 +259,7 @@ public function test_regular_user_can_not_create_friendship_to_others() { * @group create_item */ public function test_admins_can_create_friendship_to_others() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -285,7 +279,7 @@ public function test_admins_can_create_friendship_to_others() { * @group create_item */ public function test_admins_can_force_friendship_creation() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -307,8 +301,8 @@ public function test_admins_can_force_friendship_creation() { * @group create_item */ public function test_regular_users_can_not_force_friendship_creation() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -362,7 +356,7 @@ public function test_create_item_without_friend_id() { public function test_update_item() { $this->create_friendship(); - $this->bp->set_current_user( $this->friend ); + $this->bp::set_current_user( $this->friend ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->user ) ); $response = $this->server->dispatch( $request ); @@ -381,7 +375,7 @@ public function test_update_item() { public function test_initiator_can_not_accept_its_own_friendship_request() { $this->create_friendship(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $response = $this->server->dispatch( $request ); @@ -393,7 +387,7 @@ public function test_initiator_can_not_accept_its_own_friendship_request() { * @group update_item */ public function test_update_item_invalid_friend_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -417,7 +411,7 @@ public function test_update_item_user_not_logged_in() { public function test_delete_item() { $this->create_friendship(); - $this->bp->set_current_user( $this->friend ); + $this->bp::set_current_user( $this->friend ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->user ) ); $response = $this->server->dispatch( $request ); @@ -436,7 +430,7 @@ public function test_delete_item() { public function test_delete_item_using_the_initiator() { $this->create_friendship(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $response = $this->server->dispatch( $request ); @@ -455,7 +449,7 @@ public function test_delete_item_using_the_initiator() { public function test_reject_and_remove_item_from_database() { $this->create_friendship(); - $this->bp->set_current_user( $this->friend ); + $this->bp::set_current_user( $this->friend ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->user ) ); $request->set_body_params( [ 'force' => true ] ); @@ -475,7 +469,7 @@ public function test_reject_and_remove_item_from_database() { public function test_reject_and_remove_item_from_database_using_initiator() { $this->create_friendship(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $request->set_body_params( [ 'force' => true ] ); @@ -495,7 +489,7 @@ public function test_reject_and_remove_item_from_database_using_initiator() { public function test_reject_and_remove_item_from_database_using_initiator_and_testing_force() { $this->create_friendship(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $request->set_body_params( [ 'force' => 'true' ] ); @@ -515,7 +509,7 @@ public function test_reject_and_remove_item_from_database_using_initiator_and_te public function test_reject_friendship_request() { $this->create_friendship(); - $this->bp->set_current_user( $this->friend ); + $this->bp::set_current_user( $this->friend ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->user ) ); $response = $this->server->dispatch( $request ); @@ -534,7 +528,7 @@ public function test_reject_friendship_request() { public function test_reject_friendship_with_invalid_friendship_id() { $this->create_friendship(); - $this->bp->set_current_user( $this->friend ); + $this->bp::set_current_user( $this->friend ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -556,7 +550,7 @@ public function test_reject_friendship_with_user_not_logged_in() { * @group prepare_item */ public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $this->friend ) ); $response = $this->server->dispatch( $request ); @@ -609,8 +603,8 @@ protected function set_friendship_data( $args = array() ) { return wp_parse_args( $args, array( - 'initiator_id' => $this->factory->user->create(), - 'friend_id' => $this->factory->user->create(), + 'initiator_id' => static::factory()->user->create(), + 'friend_id' => static::factory()->user->create(), ) ); } diff --git a/tests/testcases/groups/test-controller.php b/tests/testcases/groups/test-controller.php index 1fc9dadf..da4b0395 100644 --- a/tests/testcases/groups/test-controller.php +++ b/tests/testcases/groups/test-controller.php @@ -7,7 +7,6 @@ * @group group */ class BP_Test_REST_Group_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -18,16 +17,15 @@ class BP_Test_REST_Group_Endpoint extends WP_Test_REST_Controller_Testcase { public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Groups_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'creator_id' => $this->user, @@ -54,9 +52,9 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->group->create_many( 3 ); + $this->bp::factory()->group->create_many( 3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -73,7 +71,7 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $this->bp_factory->group->create_many( 3 ); + $this->bp::factory()->group->create_many( 3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -86,16 +84,16 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_including_hidden_groups() { - $u = $this->factory->user->create(); - $g1 = $this->bp_factory->group->create(); - $g2 = $this->bp_factory->group->create( array( + $u = static::factory()->user->create(); + $g1 = $this->bp::factory()->group->create(); + $g2 = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); - $this->bp->add_user_to_group( $u, $g1 ); - $this->bp->add_user_to_group( $u, $g2 ); + $this->bp::add_user_to_group( $u, $g1 ); + $this->bp::add_user_to_group( $u, $g2 ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -121,18 +119,18 @@ public function test_get_items_including_hidden_groups() { * @group get_items */ public function test_get_items_including_private_groups() { - $u = $this->factory->user->create(); - $g1 = $this->bp_factory->group->create( array( + $u = static::factory()->user->create(); + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', ) ); - $g2 = $this->bp_factory->group->create( array( + $g2 = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); - $this->bp->add_user_to_group( $u, $g1 ); - $this->bp->add_user_to_group( $u, $g2 ); + $this->bp::add_user_to_group( $u, $g1 ); + $this->bp::add_user_to_group( $u, $g2 ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -158,13 +156,13 @@ public function test_get_items_including_private_groups() { * @group get_items */ public function test_get_items_not_including_hidden_groups_when_not_using_user_id_param() { - $u = $this->factory->user->create(); - $g = $this->bp_factory->group->create( array( + $u = static::factory()->user->create(); + $g = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); - $this->bp->add_user_to_group( $u, $g ); - $this->bp->set_current_user( $u ); + $this->bp::add_user_to_group( $u, $g ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -188,22 +186,22 @@ public function test_get_items_not_including_hidden_groups_when_not_using_user_i * @group get_items */ public function test_get_paginated_items() { - $u = $this->factory->user->create(); - $g1 = $this->bp_factory->group->create(); - $g2 = $this->bp_factory->group->create(); - $g3 = $this->bp_factory->group->create(); - $g4 = $this->bp_factory->group->create(); - $g5 = $this->bp_factory->group->create(); - $g6 = $this->bp_factory->group->create(); - - $this->bp->add_user_to_group( $u, $g1 ); - $this->bp->add_user_to_group( $u, $g2 ); - $this->bp->add_user_to_group( $u, $g3 ); - $this->bp->add_user_to_group( $u, $g4 ); - $this->bp->add_user_to_group( $u, $g5 ); - $this->bp->add_user_to_group( $u, $g6 ); - - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $g1 = $this->bp::factory()->group->create(); + $g2 = $this->bp::factory()->group->create(); + $g3 = $this->bp::factory()->group->create(); + $g4 = $this->bp::factory()->group->create(); + $g5 = $this->bp::factory()->group->create(); + $g6 = $this->bp::factory()->group->create(); + + $this->bp::add_user_to_group( $u, $g1 ); + $this->bp::add_user_to_group( $u, $g2 ); + $this->bp::add_user_to_group( $u, $g3 ); + $this->bp::add_user_to_group( $u, $g4 ); + $this->bp::add_user_to_group( $u, $g5 ); + $this->bp::add_user_to_group( $u, $g6 ); + + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -244,9 +242,9 @@ public function test_get_paginated_items() { * @group get_items */ public function test_get_items_edit_context() { - $this->bp_factory->group->create(); - $this->bp_factory->group->create(); - $this->bp_factory->group->create(); + $this->bp::factory()->group->create(); + $this->bp::factory()->group->create(); + $this->bp::factory()->group->create(); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); @@ -269,11 +267,11 @@ public function test_get_items_edit_context() { * @group get_items */ public function test_get_items_edit_context_users_private_data() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->group->create(); - $this->bp_factory->group->create(); - $this->bp_factory->group->create(); + $this->bp::factory()->group->create(); + $this->bp::factory()->group->create(); + $this->bp::factory()->group->create(); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); @@ -297,10 +295,10 @@ public function test_get_items_edit_context_users_private_data() { * @group get_items */ public function test_get_items_extra() { - $u1 = $this->bp_factory->user->create(); - $u2 = $this->bp_factory->user->create(); + $u1 = $this->bp::factory()->user->create(); + $u2 = $this->bp::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $now = time(); @@ -309,11 +307,11 @@ public function test_get_items_extra() { $d3 = gmdate( 'Y-m-d H:i:s', $now - 100 ); $d4 = gmdate( 'Y-m-d H:i:s', $now - 500 ); - $a1 = $this->bp_factory->group->create( array( 'date_created' => $d1 ) ); - $a2 = $this->bp_factory->group->create( array( 'date_created' => $d2 ) ); - $a3 = $this->bp_factory->group->create( array( 'date_created' => $d3 ) ); + $a1 = $this->bp::factory()->group->create( array( 'date_created' => $d1 ) ); + $a2 = $this->bp::factory()->group->create( array( 'date_created' => $d2 ) ); + $a3 = $this->bp::factory()->group->create( array( 'date_created' => $d3 ) ); - $this->bp->add_user_to_group( $u2, $a3 ); + $this->bp::add_user_to_group( $u2, $a3 ); groups_update_groupmeta( $a1, 'last_activity', $d4 ); @@ -345,14 +343,14 @@ public function test_get_items_extra() { * @group get_items */ public function test_get_items_with_group_types() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); bp_groups_register_group_type( 'foo' ); bp_groups_register_group_type( 'bar' ); - $a1 = $this->bp_factory->group->create(); - $a2 = $this->bp_factory->group->create(); - $a3 = $this->bp_factory->group->create(); + $a1 = $this->bp::factory()->group->create(); + $a2 = $this->bp::factory()->group->create(); + $a3 = $this->bp::factory()->group->create(); $expected_types = array( $a1 => array( 'foo' ), @@ -380,8 +378,8 @@ public function test_get_items_with_group_types() { * @group get_item */ public function test_get_item() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $group = $this->endpoint->get_group_object( $this->group_id ); @@ -432,7 +430,7 @@ public function test_get_item_unauthenticated() { * @group get_item */ public function test_get_item_invalid_group_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -444,15 +442,15 @@ public function test_get_item_invalid_group_id() { * @group get_item */ public function test_get_hidden_group() { - $u = $this->factory->user->create(); - $g = $this->bp_factory->group->create( array( + $u = static::factory()->user->create(); + $g = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); $group = $this->endpoint->get_group_object( $g ); - $this->bp->add_user_to_group( $u, $group->id ); - $this->bp->set_current_user( $u ); + $this->bp::add_user_to_group( $u, $group->id ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $group->id ) ); $request->set_param( 'context', 'view' ); @@ -469,14 +467,14 @@ public function test_get_hidden_group() { * @group get_item */ public function test_get_hidden_group_without_being_from_group() { - $u = $this->factory->user->create(); - $g = $this->bp_factory->group->create( array( + $u = static::factory()->user->create(); + $g = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); $group = $this->endpoint->get_group_object( $g ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $group->id ) ); $request->set_param( 'context', 'view' ); @@ -490,8 +488,8 @@ public function test_get_hidden_group_without_being_from_group() { * @group avatar */ public function test_get_item_with_avatar() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $group = $this->endpoint->get_group_object( $this->group_id ); @@ -510,8 +508,8 @@ public function test_get_item_with_avatar() { * @group avatar */ public function test_get_item_without_avatar() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $group = $this->endpoint->get_group_object( $this->group_id ); @@ -532,8 +530,8 @@ public function test_get_item_without_avatar() { * @group get_item */ public function test_get_item_extra() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $group = $this->endpoint->get_group_object( $this->group_id ); @@ -556,9 +554,9 @@ public function test_get_item_extra() { * @group render_item */ public function test_render_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $g = $this->bp_factory->group->create( array( + $g = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'links should be clickable: https://buddypress.org', ) ); @@ -577,7 +575,7 @@ public function test_render_item() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -594,7 +592,7 @@ public function test_create_item() { * @group create_item */ public function test_rest_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -613,7 +611,7 @@ public function test_rest_create_item() { public function test_create_item_with_group_type() { bp_groups_register_group_type( 'foo' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -630,7 +628,7 @@ public function test_create_item_with_group_type() { * @group create_item */ public function test_create_item_with_no_name() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -680,7 +678,7 @@ public function test_update_item() { $group = $this->endpoint->get_group_object( $this->group_id ); $this->assertEquals( $this->group_id, $group->id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $group->id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -711,7 +709,7 @@ public function test_update_group_type() { bp_groups_set_group_type( $this->group_id, 'bar' ); - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -732,7 +730,7 @@ public function test_remove_group_type() { bp_groups_set_group_type( $this->group_id, 'bar' ); - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -754,7 +752,7 @@ public function test_append_group_type() { bp_groups_set_group_type( $this->group_id, 'bar' ); - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -771,7 +769,7 @@ public function test_append_group_type() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -797,11 +795,11 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $a = $this->bp_factory->group->create( array( 'creator_id' => $u ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $a = $this->bp::factory()->group->create( array( 'creator_id' => $u ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u2 ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u2 ); $group = $this->endpoint->get_group_object( $a ); $this->assertEquals( $a, $group->id ); @@ -820,7 +818,7 @@ public function test_update_item_without_permission() { * @group update_item */ public function test_site_admins_can_update_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -846,12 +844,12 @@ public function test_site_admins_can_update_item() { * @group update_item */ public function test_group_admins_can_update_item() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Add user to group as an group admin. - $this->bp->add_user_to_group( $u, $this->group_id, [ 'is_admin' => true ] ); + $this->bp::add_user_to_group( $u, $this->group_id, [ 'is_admin' => true ] ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -877,12 +875,12 @@ public function test_group_admins_can_update_item() { * @group update_item */ public function test_group_moderators_can_not_update_item() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Add user to group as a moderator. - $this->bp->add_user_to_group( $u, $this->group_id, [ 'is_mod' => true ] ); + $this->bp::add_user_to_group( $u, $this->group_id, [ 'is_mod' => true ] ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -900,7 +898,7 @@ public function test_group_moderators_can_not_update_item() { */ public function test_update_item_invalid_status() { $group = $this->endpoint->get_group_object( $this->group_id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $group->id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -917,7 +915,7 @@ public function test_update_item_invalid_status() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->set_param( 'context', 'edit' ); @@ -933,7 +931,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -955,7 +953,7 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $this->bp->set_current_user( $this->factory->user->create() ); + $this->bp::set_current_user( static::factory()->user->create() ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -967,12 +965,12 @@ public function test_delete_item_without_permission() { * @group delete_item */ public function test_group_moderators_can_not_delete_group() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Add user to group as a moderator. - $this->bp->add_user_to_group( $u, $this->group_id, [ 'is_mod' => true ] ); + $this->bp::add_user_to_group( $u, $this->group_id, [ 'is_mod' => true ] ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -984,7 +982,7 @@ public function test_group_moderators_can_not_delete_group() { * @group delete_item */ public function test_site_admins_can_delete_group() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->set_param( 'context', 'edit' ); @@ -1000,12 +998,12 @@ public function test_site_admins_can_delete_group() { * @group delete_item */ public function test_group_admins_can_delete_group() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Add user to group as a group admin. - $this->bp->add_user_to_group( $u, $this->group_id, [ 'is_admin' => true ] ); + $this->bp::add_user_to_group( $u, $this->group_id, [ 'is_admin' => true ] ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->set_param( 'context', 'edit' ); @@ -1021,12 +1019,12 @@ public function test_group_admins_can_delete_group() { * @group get_current_user_groups */ public function test_get_current_user_groups() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $groups = array(); foreach ( array( 'public', 'private', 'hidden' ) as $status ) { - $groups[ $status ] = $this->bp_factory->group->create( array( + $groups[ $status ] = $this->bp::factory()->group->create( array( 'status' => $status, 'creator_id' => $u, ) ); @@ -1046,12 +1044,12 @@ public function test_get_current_user_groups() { * @group get_current_user_groups */ public function test_get_current_user_groups_max_one() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $groups = array(); foreach ( array( 'public', 'private', 'hidden' ) as $status ) { - $groups[ $status ] = $this->bp_factory->group->create( array( + $groups[ $status ] = $this->bp::factory()->group->create( array( 'status' => $status, 'creator_id' => $u, ) ); @@ -1083,7 +1081,7 @@ public function test_get_current_user_groups_not_loggedin() { } public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $group = $this->endpoint->get_group_object( $this->group_id ); $this->assertEquals( $this->group_id, $group->id ); @@ -1265,7 +1263,7 @@ public function test_additional_fields() { ), ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_value'; // POST @@ -1306,9 +1304,9 @@ public function test_update_additional_fields() { ), ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'foo_value'; - $g_id = $this->bp_factory->group->create(); + $g_id = $this->bp::factory()->group->create(); // PUT $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $g_id ) ); diff --git a/tests/testcases/groups/test-group-invites-controller.php b/tests/testcases/groups/test-group-invites-controller.php index fd113df9..4e07a23d 100644 --- a/tests/testcases/groups/test-group-invites-controller.php +++ b/tests/testcases/groups/test-group-invites-controller.php @@ -7,7 +7,6 @@ * @group group-invites */ class BP_Test_REST_Group_Invites_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -20,16 +19,15 @@ class BP_Test_REST_Group_Invites_Endpoint extends WP_Test_REST_Controller_Testca public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Group_Invites_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id . '/invites'; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'status' => 'private', @@ -37,11 +35,11 @@ public function set_up() { ) ); // Create a group with a group admin that is not a site admin. - $this->g1admin = $this->factory->user->create( array( + $this->g1admin = static::factory()->user->create( array( 'role' => 'subscriber', 'user_email' => 'sub@example.com', ) ); - $this->g1 = $this->bp_factory->group->create( array( + $this->g1 = $this->bp::factory()->group->create( array( 'name' => 'Group Test 1', 'description' => 'Group Description 1', 'status' => 'private', @@ -72,16 +70,16 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); - $u5 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); + $u5 = static::factory()->user->create(); $this->populate_group_with_invites( [ $u1, $u2, $u3, $u4 ], $this->group_id ); // As site admin - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'group_id' => $this->group_id, @@ -105,10 +103,10 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); $this->populate_group_with_invites( [ $u1, $u2, $u3, $u4 ], $this->group_id ); @@ -126,17 +124,17 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_as_group_admin() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); - $u5 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); + $u5 = static::factory()->user->create(); $this->populate_group_with_invites( [ $u1, $u2, $u3, $u5 ], $this->g1 ); // Red herring $this->populate_group_with_invites( [ $u4 ], $this->group_id ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'group_id' => $this->g1, @@ -158,8 +156,8 @@ public function test_get_items_as_group_admin() { * @group get_items */ public function test_get_items_as_user() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); $inv1 = groups_invite_user( array( 'user_id' => $u1, @@ -181,7 +179,7 @@ public function test_get_items_as_user() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'user_id' => $u1, @@ -203,12 +201,12 @@ public function test_get_items_as_user() { * @group get_items */ public function test_get_items_as_inviter() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); - $this->bp->add_user_to_group( $u4, $this->g1 ); + $this->bp::add_user_to_group( $u4, $this->g1 ); groups_invite_user( array( 'user_id' => $u1, @@ -230,7 +228,7 @@ public function test_get_items_as_inviter() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u4 ); + $this->bp::set_current_user( $u4 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'inviter_id' => $u4, @@ -252,7 +250,7 @@ public function test_get_items_as_inviter() { * @group get_items */ public function test_get_items_user_not_logged_in() { - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -268,8 +266,8 @@ public function test_get_items_user_not_logged_in() { * @group get_items */ public function test_get_items_without_permission() { - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -285,7 +283,7 @@ public function test_get_items_without_permission() { * @group get_item */ public function test_get_item() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -294,7 +292,7 @@ public function test_get_item() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $invite_id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -310,7 +308,7 @@ public function test_get_item() { public function test_get_item_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -330,7 +328,7 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_as_user() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -339,7 +337,7 @@ public function test_get_item_as_user() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $invite_id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -353,7 +351,7 @@ public function test_get_item_as_user() { * @group get_item */ public function test_get_item_as_inviter() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -362,7 +360,7 @@ public function test_get_item_as_inviter() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $invite_id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -376,9 +374,9 @@ public function test_get_item_as_inviter() { * @group create_item */ public function test_create_item() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -401,9 +399,9 @@ public function test_create_item() { * @group create_item */ public function test_create_item_as_group_admin() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -428,7 +426,7 @@ public function test_create_item_as_group_admin() { */ public function test_inviter_cannot_invite_member_to_group_if_already_member() { // $this->user is a creator of the group. - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -445,9 +443,9 @@ public function test_inviter_cannot_invite_member_to_group_if_already_member() { * @group create_item */ public function test_create_item_user_not_logged_in() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( 'user_id' => $u, @@ -463,7 +461,7 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_invalid_member_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -480,9 +478,9 @@ public function test_create_item_invalid_member_id() { * @group create_item */ public function test_create_item_invalid_inviter_id() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -499,9 +497,9 @@ public function test_create_item_invalid_inviter_id() { * @group create_item */ public function test_create_item_invalid_group_id() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -519,10 +517,10 @@ public function test_create_item_invalid_group_id() { * @group create_item */ public function test_create_item_without_permission() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -539,7 +537,7 @@ public function test_create_item_without_permission() { * @group update_item */ public function test_update_item() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -548,7 +546,7 @@ public function test_update_item() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -564,7 +562,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_as_invitee() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, 'group_id' => $this->group_id, @@ -572,7 +570,7 @@ public function test_update_item_as_invitee() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -588,7 +586,7 @@ public function test_update_item_as_invitee() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); @@ -600,7 +598,7 @@ public function test_update_item_invalid_id() { * @group update_item */ public function test_update_item_user_not_logged_in() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -608,7 +606,7 @@ public function test_update_item_user_not_logged_in() { 'inviter_id' => $this->user, 'send_invite' => 1, ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -619,8 +617,8 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -629,7 +627,7 @@ public function test_update_item_without_permission() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -641,7 +639,7 @@ public function test_update_item_without_permission() { * @group delete_item */ public function test_delete_item() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, 'group_id' => $this->group_id, @@ -650,7 +648,7 @@ public function test_delete_item() { ) ); // Delete as site admin. - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -666,7 +664,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_as_user() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -675,7 +673,7 @@ public function test_delete_item_as_user() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -691,12 +689,12 @@ public function test_delete_item_as_user() { * @group delete_item */ public function test_delete_item_as_inviter() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->add_user_to_group( $u2, $this->g1 ); + $this->bp::add_user_to_group( $u2, $this->g1 ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -719,12 +717,12 @@ public function test_delete_item_as_inviter() { * @group delete_item */ public function test_delete_item_as_group_admin() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->add_user_to_group( $u2, $this->g1 ); + $this->bp::add_user_to_group( $u2, $this->g1 ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -733,7 +731,7 @@ public function test_delete_item_as_group_admin() { 'send_invite' => 1, ) ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $invite_id ); $response = $this->server->dispatch( $request ); @@ -748,7 +746,7 @@ public function test_delete_item_as_group_admin() { * @group delete_item */ public function test_delete_item_user_not_logged_in() { - $u1 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -756,7 +754,7 @@ public function test_delete_item_user_not_logged_in() { 'inviter_id' => $this->user, 'send_invite' => 1, ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $invite_id ); $request->set_query_params( array( 'user_id' => $u1, @@ -771,8 +769,8 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); $invite_id = groups_invite_user( array( 'user_id' => $u1, @@ -780,7 +778,7 @@ public function test_delete_item_without_permission() { 'inviter_id' => $this->user, 'send_invite' => 1, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $invite_id ); $request->set_query_params( array( @@ -796,7 +794,7 @@ public function test_delete_item_without_permission() { * @group update_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/members/test-controller.php b/tests/testcases/members/test-controller.php index 9921050b..5da7c46b 100644 --- a/tests/testcases/members/test-controller.php +++ b/tests/testcases/members/test-controller.php @@ -7,7 +7,6 @@ * @group members */ class BP_Test_REST_Members_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -47,7 +46,6 @@ public function set_up() { buddypress()->members->types = array(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Members_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/members'; @@ -74,9 +72,9 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -103,9 +101,9 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -121,13 +119,13 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_extra() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); // Set current user. $current_user = get_current_user_id(); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); // u2 is the only one to have a latest_update. $a1 = bp_activity_post_update( @@ -143,7 +141,7 @@ public function test_get_items_extra() { // u1 is the only one to have a last activity bp_update_user_last_activity( $u1, $date_last_activity ); - $this->bp->set_current_user( $current_user ); + $this->bp::set_current_user( $current_user ); // u1 and u3 are friends. friends_add_friend( $u1, $u3, true ); @@ -178,10 +176,10 @@ public function test_get_items_extra() { * @group get_items */ public function test_get_paginated_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -211,9 +209,9 @@ public function test_get_paginated_items() { * @group get_items */ public function test_get_items_with_types() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); bp_register_member_type( 'foo' ); bp_register_member_type( 'bar' ); @@ -248,10 +246,10 @@ public function test_get_items_with_types() { * @group get_items */ public function test_get_items_filtered_by_xprofile() { - $u = $this->bp_factory->user->create(); - $u2 = $this->bp_factory->user->create(); - $g = $this->bp_factory->xprofile_group->create(); - $f = $this->bp_factory->xprofile_field->create( [ + $u = $this->bp::factory()->user->create(); + $u2 = $this->bp::factory()->user->create(); + $g = $this->bp::factory()->xprofile_group->create(); + $f = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g, 'type' => 'textbox', 'name' => 'foo', @@ -290,17 +288,17 @@ public function test_get_items_filtered_by_xprofile() { * @group get_items */ public function test_get_items_filtered_by_xprofile_with_and_relation() { - $u = $this->bp_factory->user->create(); - $u2 = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); + $u2 = $this->bp::factory()->user->create(); - $g = $this->bp_factory->xprofile_group->create(); + $g = $this->bp::factory()->xprofile_group->create(); - $f = $this->bp_factory->xprofile_field->create( [ + $f = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g, 'type' => 'textbox', 'name' => 'foo', ] ); - $f2 = $this->bp_factory->xprofile_field->create( [ + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g, 'type' => 'textbox', 'name' => 'bar', @@ -345,18 +343,18 @@ public function test_get_items_filtered_by_xprofile_with_and_relation() { * @group get_items */ public function test_get_items_filtered_by_xprofile_with_or_relation() { - $u = $this->bp_factory->user->create(); - $u2 = $this->bp_factory->user->create(); - $u3 = $this->bp_factory->user->create(); + $u = $this->bp::factory()->user->create(); + $u2 = $this->bp::factory()->user->create(); + $u3 = $this->bp::factory()->user->create(); - $g = $this->bp_factory->xprofile_group->create(); + $g = $this->bp::factory()->xprofile_group->create(); - $f = $this->bp_factory->xprofile_field->create( [ + $f = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g, 'type' => 'textbox', 'name' => 'foo', ] ); - $f2 = $this->bp_factory->xprofile_field->create( [ + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g, 'type' => 'textbox', 'name' => 'bar', @@ -405,7 +403,7 @@ public function test_get_items_filtered_by_xprofile_with_or_relation() { * @group get_item */ public function test_get_item() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Register and set member types. bp_register_member_type( 'foo' ); @@ -426,7 +424,7 @@ public function test_get_item() { public function test_get_item_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Register and set member types. bp_register_member_type( 'foo' ); @@ -444,12 +442,12 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_extras() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Set current user. $current_user = get_current_user_id(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $a1 = bp_activity_post_update( array( @@ -484,7 +482,7 @@ public function test_get_item_extras() { $this->assertEquals( $member['latest_update']['id'], $a1 ); $this->assertEquals( 1, $member['total_friend_count'] ); - $this->bp->set_current_user( $current_user ); + $this->bp::set_current_user( $current_user ); } /** @@ -493,7 +491,7 @@ public function test_get_item_extras() { public function test_get_item_me_extras() { // Set current user. $current_user = get_current_user_id(); - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/me' ); $request->set_query_params( @@ -510,7 +508,7 @@ public function test_get_item_me_extras() { $this->assertEquals( 'right now', $me['last_activity']['timediff'] ); - $this->bp->set_current_user( $current_user ); + $this->bp::set_current_user( $current_user ); } /** @@ -518,7 +516,7 @@ public function test_get_item_me_extras() { * @group avatar */ public function test_get_item_without_avatar() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); // Register and set member types. bp_register_member_type( 'foo' ); @@ -551,7 +549,7 @@ function test_get_item_schema_show_avatar() { $GLOBALS['wp_rest_server']->override_by_default = false; $request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; @@ -574,10 +572,7 @@ public function test_get_item_invalid_id() { * @group create_item */ public function test_create_item() { - - if ( is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithMultisite(); $this->allow_user_to_manage_multisite(); @@ -604,8 +599,8 @@ public function test_create_item() { * @group create_item */ public function test_create_item_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $params = array( 'password' => 'testpassword', @@ -626,7 +621,7 @@ public function test_create_item_without_permission() { * @group update_item */ public function test_update_item() { - $u = $this->factory->user->create( array( + $u = static::factory()->user->create( array( 'email' => 'test@example.com', 'name' => 'User Name', ) ); @@ -655,7 +650,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_user_not_logged_in() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $u ) ); $request->set_param( 'username', 'test_json_user' ); @@ -668,7 +663,7 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -687,10 +682,10 @@ public function test_update_item_invalid_id() { * @group update_item */ public function test_update_item_without_permission() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $u2 ) ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -708,17 +703,14 @@ public function test_update_item_without_permission() { * @group update_item */ public function test_update_item_types() { + $this->skipWithMultisite(); - if ( is_multisite() ) { - $this->markTestSkipped(); - } - - $u = $this->factory->user->create( array( + $u = static::factory()->user->create( array( 'email' => 'member@type.com', 'name' => 'User Name', ) ); - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); bp_register_member_type( 'membertypeone' ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $u ) ); @@ -737,12 +729,12 @@ public function test_update_item_types() { * @group update_item */ public function test_update_item_member_type_as_regular_user() { - $u = $this->factory->user->create( array( + $u = static::factory()->user->create( array( 'email' => 'member@type.com', 'name' => 'User Name', ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); bp_register_member_type( 'membertypeone' ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $u ) ); @@ -760,7 +752,7 @@ public function test_update_item_member_type_as_regular_user() { * @group update_item */ public function test_update_item_member_type_as_admin_user() { - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); bp_register_member_type( 'membertypeone' ); bp_register_member_type( 'membertypetwo' ); @@ -782,12 +774,9 @@ public function test_update_item_member_type_as_admin_user() { * @group delete_item */ public function test_delete_item() { + $this->skipWithMultisite(); - if ( is_multisite() ) { - $this->markTestSkipped(); - } - - $u = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); + $u = static::factory()->user->create( array( 'display_name' => 'Deleted User' ) ); $this->allow_user_to_manage_multisite(); @@ -796,12 +785,6 @@ public function test_delete_item() { $request->set_param( 'reassign', false ); $response = $this->server->dispatch( $request ); - // Not implemented in multisite. - if ( is_multisite() ) { - $this->assertErrorResponse( 'bp_rest_authorization_required_code', $response, 501 ); - return; - } - $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); @@ -815,7 +798,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'force', true ); @@ -829,7 +812,7 @@ public function test_delete_item_invalid_id() { * @group delete_item */ public function test_delete_item_user_not_logged_in() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $u ) ); $request->set_param( 'force', true ); @@ -843,10 +826,10 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $u2 ) ); $request->set_param( 'force', true ); @@ -860,9 +843,9 @@ public function test_delete_item_without_permission() { * @group delete_item */ public function test_delete_current_item() { - $u = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) ); + $u = static::factory()->user->create( array( 'display_name' => 'Deleted User' ) ); $current_user = get_current_user_id(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/me' ); $request->set_param( 'force', true ); @@ -877,11 +860,11 @@ public function test_delete_current_item() { $this->assertTrue( $data['deleted'] ); $this->assertEquals( 'Deleted User', $data['previous']['name'] ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); } public function test_prepare_item() { - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); $request = new WP_REST_Request(); $request->set_param( 'context', 'view' ); @@ -942,7 +925,7 @@ protected function check_user_data( $user, $data, $context ) { } protected function allow_user_to_manage_multisite() { - $this->bp->set_current_user( self::$user ); + $this->bp::set_current_user( self::$user ); if ( is_multisite() ) { update_site_option( 'site_admins', array( wp_get_current_user()->user_login ) ); @@ -1033,10 +1016,7 @@ public function get_additional_field( $data, $attribute ) { * @group additional_fields */ public function test_additional_fields() { - - if ( is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithMultisite(); $registered_fields = $GLOBALS['wp_rest_additional_fields']; @@ -1097,7 +1077,7 @@ public function test_update_additional_fields() { ), ) ); - $u = $this->factory->user->create( array( + $u = static::factory()->user->create( array( 'email' => 'test@example.com', 'name' => 'User Name', ) ); diff --git a/tests/testcases/membership/test-group-membership-controller.php b/tests/testcases/membership/test-group-membership-controller.php index bbda90d2..a80fe172 100644 --- a/tests/testcases/membership/test-group-membership-controller.php +++ b/tests/testcases/membership/test-group-membership-controller.php @@ -7,29 +7,26 @@ * @group group-membership */ class BP_Test_REST_Group_Membership_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; protected $user; protected $group_id; protected $server; - protected $old_current_user; protected $search_terms; public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Group_Membership_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id . '/'; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'creator_id' => $this->user, @@ -38,13 +35,6 @@ public function set_up() { if ( ! $this->server ) { $this->server = rest_get_server(); } - - $this->old_current_user = get_current_user_id(); - } - - public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); } public function test_register_routes() { @@ -66,17 +56,17 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . $g1 . '/members' ); @@ -107,12 +97,12 @@ public function test_get_items() { * @group get_items */ public function test_get_items_by_specific_group_role() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator' => $u1, ) ); @@ -159,10 +149,10 @@ public function test_get_items_by_specific_group_role() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator' => $u1, ) ); @@ -179,20 +169,20 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_paginated_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $u4 = $this->factory->user->create(); - $u5 = $this->factory->user->create(); - $u6 = $this->factory->user->create(); - - $g1 = $this->bp_factory->group->create( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $u4 = static::factory()->user->create(); + $u5 = static::factory()->user->create(); + $u6 = static::factory()->user->create(); + + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); $this->populate_group_with_members( [ $u1, $u2, $u3, $u4, $u5, $u6 ], $g1 ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . $g1 . '/members' ); $request->set_query_params( array( @@ -225,17 +215,17 @@ public function test_get_paginated_items() { * @group get_items */ public function test_get_items_no_search_terms() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'public', ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); groups_promote_member( $u1, $g1, 'admin' ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); add_filter( 'bp_rest_group_members_get_items_query_args', array( $this, 'group_members_query_args' ) ); @@ -253,22 +243,22 @@ public function test_get_items_no_search_terms() { * @group get_items */ public function test_get_items_has_search_terms() { - $u1 = $this->factory->user->create( array( + $u1 = static::factory()->user->create( array( 'user_nicename' => 'foo', ) ); - $u2 = $this->factory->user->create( array( + $u2 = static::factory()->user->create( array( 'user_nicename' => 'foo bar', ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'public', ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); groups_promote_member( $u1, $g1, 'admin' ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); add_filter( 'bp_rest_group_members_get_items_query_args', array( $this, 'group_members_query_args' ) ); @@ -309,10 +299,10 @@ public function test_get_item() { * @group create_item */ public function test_create_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $g = $this->bp_factory->group->create(); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $g = $this->bp::factory()->group->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $g . '/members' ); $request->set_param( 'context', 'view' ); @@ -332,9 +322,9 @@ public function test_create_item() { * @group create_item */ public function test_create_item_as_admin() { - $u = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $u = static::factory()->user->create( array( 'role' => 'administrator' ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $this->group_id . '/members' ); $request->set_param( 'context', 'edit' ); @@ -361,9 +351,9 @@ public function test_create_item_as_admin() { * @group create_item */ public function test_member_can_add_himself_to_public_group() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $this->group_id . '/members' ); @@ -392,13 +382,13 @@ public function test_member_can_add_himself_to_public_group() { * @group create_item */ public function test_member_can_not_add_himself_to_private_group() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'private', ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $g1 . '/members' ); $request->set_param( 'context', 'view' ); @@ -414,13 +404,13 @@ public function test_member_can_not_add_himself_to_private_group() { * @group create_item */ public function test_member_can_not_add_himself_to_hidden_group() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'status' => 'hidden', ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $g1 . '/members' ); $request->set_param( 'context', 'edit' ); @@ -436,10 +426,10 @@ public function test_member_can_not_add_himself_to_hidden_group() { * @group create_item */ public function test_member_cannot_add_others_to_public_group() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url . $this->group_id . '/members' ); $request->set_param( 'context', 'view' ); @@ -455,11 +445,11 @@ public function test_member_cannot_add_others_to_public_group() { * @group update_item */ public function test_update_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $this->populate_group_with_members( [ $u ], $this->group_id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $this->group_id . '/members/' . $u ); $request->set_query_params( array( @@ -486,11 +476,11 @@ public function test_update_item() { * @group update_item */ public function test_administrator_can_ban_member() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $this->populate_group_with_members( [ $u ], $this->group_id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $this->group_id . '/members/' . $u ); $request->set_query_params( array( @@ -518,16 +508,16 @@ public function test_administrator_can_ban_member() { * @group update_item */ public function test_group_admin_can_ban_member() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1 ], $g1 ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u1 ); $request->set_query_params( array( @@ -555,11 +545,11 @@ public function test_group_admin_can_ban_member() { * @group update_item */ public function test_administrator_can_promote_member() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $this->populate_group_with_members( [ $u ], $this->group_id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $this->group_id . '/members/' . $u ); $request->set_query_params( array( @@ -587,16 +577,16 @@ public function test_administrator_can_promote_member() { * @group update_item */ public function test_group_admin_can_promote_member() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1 ], $this->group_id ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u1 ); $request->set_query_params( array( @@ -624,17 +614,17 @@ public function test_group_admin_can_promote_member() { * @group update_item */ public function test_member_can_not_promote_other_member() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1, $u3 ], $g1 ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u1 ); $request->set_query_params( array( @@ -652,18 +642,18 @@ public function test_member_can_not_promote_other_member() { * @group update_item */ public function test_group_mods_can_not_promote_members() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u1, ) ); - $this->bp->add_user_to_group( $u2, $g1, array( + $this->bp::add_user_to_group( $u2, $g1, array( 'is_mod' => true, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u1 ); $request->set_query_params( array( @@ -679,17 +669,17 @@ public function test_group_mods_can_not_promote_members() { * @group update_item */ public function test_admin_can_demote_group_admin_to_member() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1, $u3 ], $g1 ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u2 ); $request->set_query_params( array( @@ -717,11 +707,11 @@ public function test_admin_can_demote_group_admin_to_member() { * @group update_item */ public function test_group_admin_can_demote_another_group_admin_to_mod() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u3, ) ); @@ -733,7 +723,7 @@ public function test_group_admin_can_demote_another_group_admin_to_mod() { $group_member_2 = new BP_Groups_Member( $u2, $g1 ); $group_member_2->promote( 'admin' ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u2 ); $request->set_query_params( array( @@ -763,17 +753,17 @@ public function test_group_admin_can_demote_another_group_admin_to_mod() { * @group update_item */ public function test_member_can_not_demote_other_member() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1, $u3 ], $g1 ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . $g1 . '/members/' . $u2 ); $request->set_query_params( array( @@ -790,9 +780,9 @@ public function test_member_can_not_demote_other_member() { * @group update_item */ public function test_update_item_invalid_group_id() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/members/' . $u ); $request->set_query_params( array( @@ -820,17 +810,17 @@ public function test_update_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u3, ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . $g1 . '/members/' . $u1 ); @@ -850,17 +840,17 @@ public function test_delete_item() { * @group delete_item */ public function test_member_can_remove_himself_from_group() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u3, ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . $g1 . '/members/' . $u1 ); @@ -880,17 +870,17 @@ public function test_member_can_remove_himself_from_group() { * @group delete_item */ public function test_member_can_not_remove_others_from_group() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u3, ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . $g1 . '/members/' . $u2 ); @@ -904,17 +894,17 @@ public function test_member_can_not_remove_others_from_group() { * @group delete_item */ public function test_group_admin_can_remove_member_from_group() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u3, ) ); $this->populate_group_with_members( [ $u1, $u2 ], $g1 ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . $g1 . '/members/' . $u1 ); @@ -934,16 +924,16 @@ public function test_group_admin_can_remove_member_from_group() { * @group delete_item */ public function test_group_admin_can_not_remove_himself_from_group() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $g1 = $this->bp_factory->group->create( array( + $g1 = $this->bp::factory()->group->create( array( 'creator_id' => $u2, ) ); $this->populate_group_with_members( [ $u1 ], $g1 ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . $g1 . '/members/' . $u2 ); @@ -965,7 +955,7 @@ public function test_prepare_item() { */ protected function populate_group_with_members( $members, $group_id ) { foreach ( $members as $member_id ) { - $this->bp->add_user_to_group( $member_id, $group_id ); + $this->bp::add_user_to_group( $member_id, $group_id ); } } @@ -1000,10 +990,7 @@ protected function check_user_data( $user, $data, $member_object, $context = 'vi } public function test_get_item_schema() { - - if ( is_multisite() ) { - $this->markTestSkipped(); - } + $this->skipWithMultisite(); $request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url . $this->group_id . '/members' ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/membership/test-group-membership-request-controller.php b/tests/testcases/membership/test-group-membership-request-controller.php index 682dc4b4..31305727 100644 --- a/tests/testcases/membership/test-group-membership-request-controller.php +++ b/tests/testcases/membership/test-group-membership-request-controller.php @@ -7,7 +7,6 @@ * @group group-membership-request */ class BP_Test_REST_Group_Membership_Request_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -16,21 +15,19 @@ class BP_Test_REST_Group_Membership_Request_Endpoint extends WP_Test_REST_Contro protected $g1admin; protected $g1; protected $server; - protected $old_current_user; public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Group_Membership_Request_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->groups->id . '/membership-requests'; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); - $this->group_id = $this->bp_factory->group->create( array( + $this->group_id = $this->bp::factory()->group->create( array( 'name' => 'Group Test', 'description' => 'Group Description', 'creator_id' => $this->user, @@ -38,11 +35,11 @@ public function set_up() { ) ); // Create a group with a group admin that is not a site admin. - $this->g1admin = $this->factory->user->create( array( + $this->g1admin = static::factory()->user->create( array( 'role' => 'subscriber', 'user_email' => 'sub@example.com', ) ); - $this->g1 = $this->bp_factory->group->create( array( + $this->g1 = $this->bp::factory()->group->create( array( 'name' => 'Group Test 1', 'description' => 'Group Description 1', 'status' => 'private', @@ -52,13 +49,6 @@ public function set_up() { if ( ! $this->server ) { $this->server = rest_get_server(); } - - $this->old_current_user = get_current_user_id(); - } - - public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); } public function test_register_routes() { @@ -79,15 +69,15 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u2 ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u3 ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -108,9 +98,9 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u2 ) ); @@ -130,13 +120,13 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_as_group_admin() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->g1, 'user_id' => $u ) ); groups_send_membership_request( array( 'group_id' => $this->g1, 'user_id' => $u2 ) ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -155,11 +145,11 @@ public function test_get_items_as_group_admin() { * @group get_items */ public function test_get_items_as_requestor() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -178,7 +168,7 @@ public function test_get_items_as_requestor() { * @group get_items */ public function test_get_items_user_is_not_logged_in() { - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -194,12 +184,12 @@ public function test_get_items_user_is_not_logged_in() { * @group get_items */ public function test_get_items_user_has_no_access_to_group() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'group_id' => $this->group_id, @@ -214,12 +204,12 @@ public function test_get_items_user_has_no_access_to_group() { * @group get_items */ public function test_get_items_user_has_no_access_to_user() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'user_id' => $u, @@ -234,7 +224,7 @@ public function test_get_items_user_has_no_access_to_user() { * @group get_items */ public function test_get_items_invalid_group() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( @@ -250,11 +240,11 @@ public function test_get_items_invalid_group() { * @group get_item */ public function test_get_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/'. $request_id ); $request->set_param( 'context', 'view' ); @@ -275,7 +265,7 @@ public function test_get_item() { public function test_get_item_with_support_for_the_community_visibility() { toggle_component_visibility(); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/'. $request_id ); @@ -289,11 +279,11 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_user_is_not_logged_in() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/'. $request_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -305,7 +295,7 @@ public function test_get_item_user_is_not_logged_in() { * @group get_item */ public function test_get_item_invalid_membership_request() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'context', 'view' ); @@ -318,12 +308,12 @@ public function test_get_item_invalid_membership_request() { * @group get_item */ public function test_get_item_no_access() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); @@ -336,8 +326,8 @@ public function test_get_item_no_access() { * @group create_item */ public function test_create_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -357,12 +347,14 @@ public function test_create_item() { * @group create_item */ public function test_create_item_as_subscriber() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( 'group_id' => $this->group_id, + 'user_id' => $u, ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); @@ -377,9 +369,9 @@ public function test_create_item_as_subscriber() { * @group create_item */ public function test_create_item_user_is_not_logged_in() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( 'user_id' => $u, @@ -395,7 +387,7 @@ public function test_create_item_user_is_not_logged_in() { * @group create_item */ public function test_create_item_invalid_member() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -412,8 +404,8 @@ public function test_create_item_invalid_member() { * @group create_item */ public function test_create_item_invalid_group() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -430,10 +422,10 @@ public function test_create_item_invalid_group() { * @group create_item */ public function test_create_item_with_an_already_group_member() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->add_user_to_group( $u, $this->group_id ); - $this->bp->set_current_user( $u ); + $this->bp::add_user_to_group( $u, $this->group_id ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -450,10 +442,10 @@ public function test_create_item_with_an_already_group_member() { * @group create_item */ public function test_create_item_fails_with_pending_request() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( @@ -470,8 +462,8 @@ public function test_create_item_fails_with_pending_request() { * @group update_item */ public function test_update_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); @@ -493,11 +485,11 @@ public function test_update_item() { * @group update_item */ public function test_update_item_as_group_admin() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request_id = groups_send_membership_request( array( 'group_id' => $this->g1, 'user_id' => $u ) ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -515,11 +507,11 @@ public function test_update_item_as_group_admin() { * @group update_item */ public function test_update_item_user_is_not_logged_in() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -531,12 +523,12 @@ public function test_update_item_user_is_not_logged_in() { * @group update_item */ public function test_update_item_user_has_no_access() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); @@ -549,9 +541,9 @@ public function test_update_item_user_has_no_access() { * @group update_item */ public function test_update_item_invalid_id() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'context', 'view' ); @@ -564,8 +556,8 @@ public function test_update_item_invalid_id() { * @group delete_item */ public function test_delete_item() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $this->user ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); @@ -584,8 +576,8 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_as_requestor() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); @@ -604,11 +596,11 @@ public function test_delete_item_as_requestor() { * @group delete_item */ public function test_delete_item_as_group_admin() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request_id = groups_send_membership_request( array( 'group_id' => $this->g1, 'user_id' => $u ) ); - $this->bp->set_current_user( $this->g1admin ); + $this->bp::set_current_user( $this->g1admin ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -624,11 +616,11 @@ public function test_delete_item_as_group_admin() { * @group delete_item */ public function test_delete_item_user_is_not_logged_in() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->g1, 'user_id' => $u ) ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); @@ -640,12 +632,12 @@ public function test_delete_item_user_is_not_logged_in() { * @group delete_item */ public function test_delete_item_user_has_no_access() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); $request_id = groups_send_membership_request( array( 'group_id' => $this->group_id, 'user_id' => $u ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $request_id ); $request->set_param( 'context', 'view' ); diff --git a/tests/testcases/messages/test-controller.php b/tests/testcases/messages/test-controller.php index 76915d54..f884c201 100644 --- a/tests/testcases/messages/test-controller.php +++ b/tests/testcases/messages/test-controller.php @@ -8,7 +8,6 @@ * @group messages */ class BP_Test_REST_Messages_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -18,21 +17,16 @@ class BP_Test_REST_Messages_Endpoint extends WP_Test_REST_Controller_Testcase { public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Messages_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->messages->id; - $this->user = $this->factory->user->create( array( 'role' => 'administrator' ) ); + $this->user = static::factory()->user->create( array( 'role' => 'administrator' ) ); if ( ! $this->server ) { $this->server = rest_get_server(); } } - public function tear_down() { - parent::tear_down(); - } - public function test_register_routes() { $routes = $this->server->get_routes(); $endpoint = $this->endpoint_url; @@ -58,22 +52,22 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp_factory->message->create( array( + $this->bp::factory()->message->create( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp_factory->message->create( array( + $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -98,37 +92,37 @@ public function test_get_items() { * @group get_items */ public function test_get_items_and_paginate_messages_recipients() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); - $thread = $this->bp_factory->message->create_and_get( array( + $thread = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp_factory->message->create( array( + $this->bp::factory()->message->create( array( 'thread_id' => $thread->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); - $thread2 = $this->bp_factory->message->create_and_get( array( + $thread2 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u3 ), 'subject' => 'Foo', ) ); - $this->bp_factory->message->create( array( + $this->bp::factory()->message->create( array( 'thread_id' => $thread2->thread_id, 'sender_id' => $u3, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -159,16 +153,16 @@ public function test_get_items_and_paginate_messages_recipients() { * @group get_item */ public function test_get_item() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', 'content' => 'Content', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); @@ -187,9 +181,9 @@ public function test_get_item() { * @group get_item */ public function test_get_thread_messages_paginated() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -197,7 +191,7 @@ public function test_get_thread_messages_paginated() { ) ); // create several messages. - $this->bp_factory->message->create_many( 10, array( + $this->bp::factory()->message->create_many( 10, array( 'thread_id' => $m->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), @@ -205,14 +199,14 @@ public function test_get_thread_messages_paginated() { ) ); // create a reply. - $message_id = $this->bp_factory->message->create( array( + $message_id = $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'thread_id' => $m->thread_id, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); @@ -236,15 +230,15 @@ public function test_get_thread_messages_paginated() { * @group get_item */ public function test_get_item_admin_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); @@ -265,16 +259,16 @@ public function test_get_item_admin_access() { * @group get_item */ public function test_get_item_user_with_no_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); @@ -288,11 +282,11 @@ public function test_get_item_user_with_no_access() { * @group get_item */ public function test_get_item_user_is_not_logged_in() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + static::factory()->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -311,7 +305,7 @@ public function test_get_item_user_is_not_logged_in() { * @group get_item */ public function test_get_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); @@ -323,9 +317,9 @@ public function test_get_item_invalid_id() { * @group create_item */ public function test_create_item() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -351,7 +345,7 @@ public function test_create_item() { * @group create_item */ public function test_create_item_user_is_not_logged_in() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -371,13 +365,13 @@ public function test_create_item_user_is_not_logged_in() { * @group create_item */ public function test_create_item_with_no_content() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( array( 'sender_id' => $this->user, - 'recipients' => [ $this->factory->user->create() ], + 'recipients' => [ static::factory()->user->create() ], 'subject' => 'Foo', ) ); @@ -393,7 +387,7 @@ public function test_create_item_with_no_content() { * @group create_item */ public function test_create_item_with_no_receipts() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -415,16 +409,16 @@ public function test_create_item_with_no_receipts() { * @group update_item */ public function test_update_item() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', 'content' => 'Content', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'read', true ); @@ -446,9 +440,9 @@ public function test_update_item() { * @group update_item */ public function test_update_thread_to_unread() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -458,7 +452,7 @@ public function test_update_thread_to_unread() { // Update to read. messages_mark_thread_read( $m->thread_id, $u2 ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'unread', true ); @@ -486,9 +480,9 @@ public function test_update_thread_to_unread() { * @group update_item */ public function test_update_item_user_is_not_logged_in() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -508,7 +502,7 @@ public function test_update_item_user_is_not_logged_in() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -520,17 +514,17 @@ public function test_update_item_invalid_id() { * @group update_item */ public function test_update_item_with_user_with_no_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', 'content' => 'Content', ) ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); @@ -545,16 +539,16 @@ public function test_update_item_with_user_with_no_access() { * @group delete_item */ public function test_delete_item() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', 'content' => 'Content', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_query_params( array( 'user_id' => $u2 ) ); @@ -573,15 +567,15 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_admin_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); $request->set_query_params( array( 'user_id' => $u2 ) ); @@ -600,16 +594,16 @@ public function test_delete_item_admin_access() { * @group delete_item */ public function test_delete_item_with_user_with_no_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); @@ -624,9 +618,9 @@ public function test_delete_item_with_user_with_no_access() { * @group delete_item */ public function test_delete_item_user_is_not_logged_in() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m = $this->bp_factory->message->create( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -645,32 +639,32 @@ public function test_delete_item_user_is_not_logged_in() { * @group starred */ public function test_get_starred_items() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); // Init another thread. - $m2_id = $this->bp_factory->message->create( array( + $m2_id = $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Taz', ) ); // Create a reply. - $r1 = $this->bp_factory->message->create_and_get( array( + $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); bp_messages_star_set_action( array( 'user_id' => $u1, @@ -702,25 +696,25 @@ public function test_get_starred_items() { * @group starred */ public function test_update_starred_add_star() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); // Create a reply. - $r1 = $this->bp_factory->message->create_and_get( array( + $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id ); $request->add_header( 'content-type', 'application/json' ); @@ -735,11 +729,11 @@ public function test_update_starred_add_star() { * @group starred */ public function test_update_starred_remove_star() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m = $this->bp_factory->message->create_and_get( array( + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -750,7 +744,7 @@ public function test_update_starred_remove_star() { 'message_id' => $m->id, ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id ); $response = $this->server->dispatch( $request ); @@ -763,11 +757,11 @@ public function test_update_starred_remove_star() { * @group starred */ public function test_update_starred_user_is_not_logged_in() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m = $this->bp_factory->message->create_and_get( array( + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -786,18 +780,18 @@ public function test_update_starred_user_is_not_logged_in() { * @group starred */ public function test_update_starred_user_with_no_access() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); // Init a thread. - $m = $this->bp_factory->message->create_and_get( array( + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id ); @@ -812,7 +806,7 @@ public function test_update_starred_user_with_no_access() { * @group starred */ public function test_update_starred_using_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $response = $this->server->dispatch( $request ); @@ -844,11 +838,11 @@ public function test_additional_fields_for_get_item() { ), ) ); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', @@ -856,7 +850,7 @@ public function test_additional_fields_for_get_item() { $expected = 'boz_value'; bp_messages_update_meta( $m1->id, '_taz_field', $expected ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); // GET $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id ); @@ -888,8 +882,8 @@ public function test_additional_fields_for_created_thread() { ), ) ); - $u = $this->factory->user->create(); - $this->bp->set_current_user( $this->user ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $this->user ); $expected = 'bar_value'; // POST @@ -929,17 +923,17 @@ public function test_additional_fields_for_created_reply() { ), ) ); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Foo', ) ); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $expected = 'foo_value'; // POST a reply. @@ -982,31 +976,31 @@ public function test_additional_fields_for_last_message_updated() { ), ) ); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Foo', ) ); - $this->bp_factory->message->create_and_get( array( + $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Bar', ) ); - $this->bp_factory->message->create_and_get( array( + $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Taz', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $expected = 'taz_value'; // Update the last message. @@ -1042,31 +1036,31 @@ public function test_additional_fields_for_specific_message_updated() { ), ) ); - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); // Init a thread. - $m1 = $this->bp_factory->message->create_and_get( array( + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Top', ) ); - $r1 = $this->bp_factory->message->create_and_get( array( + $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Up', ) ); - $r1 = $this->bp_factory->message->create_and_get( array( + $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Upper', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $expected = 'up_value'; // Update the last message. @@ -1091,17 +1085,17 @@ public function test_additional_fields_for_specific_message_updated() { * @group prepare_recipient_for_response */ public function test_prepare_prepare_recipient_for_response() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $u3 = $this->factory->user->create(); - $m = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $u3 = static::factory()->user->create(); + $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2, $u3 ), 'subject' => 'Foo', 'content' => 'Content', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); @@ -1132,22 +1126,22 @@ public function test_prepare_prepare_recipient_for_response() { * @group prepare_links */ public function test_prepare_add_links_to_response() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); - $m1 = $this->bp_factory->message->create_and_get( array( + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); + $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Bar', 'content' => 'Content', ) ); - $r1 = $this->bp_factory->message->create_and_get( array( + $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'content' => 'Reply', ) ); - $this->bp->set_current_user( $u2 ); + $this->bp::set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id ); diff --git a/tests/testcases/notifications/test-controller.php b/tests/testcases/notifications/test-controller.php index 00742c76..1a714565 100644 --- a/tests/testcases/notifications/test-controller.php +++ b/tests/testcases/notifications/test-controller.php @@ -7,7 +7,6 @@ * @group notifications */ class BP_Test_REST_Notifications_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -18,12 +17,11 @@ class BP_Test_REST_Notifications_Endpoint extends WP_Test_REST_Controller_Testca public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Notifications_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->notifications->id; - $this->notification_id = $this->bp_factory->notification->create(); - $this->user = $this->factory->user->create( array( + $this->notification_id = $this->bp::factory()->notification->create(); + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -49,8 +47,8 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $notification_id = $this->bp_factory->notification->create( array( 'user_id' => $this->user ) ); - $this->bp->set_current_user( $this->user ); + $notification_id = $this->bp::factory()->notification->create( array( 'user_id' => $this->user ) ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'user_id' => $this->user ) ); @@ -69,13 +67,13 @@ public function test_get_items() { * @group get_items */ public function test_admin_can_get_items_from_multiple_users() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp_factory->notification->create( array( 'user_id' => $u1, ) ); - $this->bp_factory->notification->create( array( 'user_id' => $u2, ) ); + $this->bp::factory()->notification->create( array( 'user_id' => $u1, ) ); + $this->bp::factory()->notification->create( array( 'user_id' => $u2, ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'user_ids' => array( $u1, $u2 ) ) ); @@ -97,14 +95,14 @@ public function test_admin_can_get_items_from_multiple_users() { * @group get_items */ public function test_user_can_not_get_items_from_multiple_users() { - $u1 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u2 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $u3 = $this->factory->user->create( array( 'role' => 'subscriber' ) ); + $u1 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u2 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $u3 = static::factory()->user->create( array( 'role' => 'subscriber' ) ); - $this->bp_factory->notification->create( array( 'user_id' => $u1 ) ); - $this->bp_factory->notification->create( array( 'user_id' => $u2 ) ); + $this->bp::factory()->notification->create( array( 'user_id' => $u1 ) ); + $this->bp::factory()->notification->create( array( 'user_id' => $u2 ) ); - $this->bp->set_current_user( $u3 ); + $this->bp::set_current_user( $u3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_query_params( array( 'user_ids' => array( $u1, $u2 ) ) ); @@ -129,8 +127,8 @@ public function test_get_items_user_not_logged_in() { * @group get_items */ public function test_get_items_user_cannot_see_notifications_from_others() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -143,7 +141,7 @@ public function test_get_items_user_cannot_see_notifications_from_others() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $notification = $this->endpoint->get_notification_object( $this->notification_id ); $this->assertEquals( $this->notification_id, $notification->id ); @@ -164,9 +162,9 @@ public function test_get_item() { * @group get_item */ public function test_get_embedded_user_from_notification_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $notification_id = $this->bp_factory->notification->create( array( 'user_id' => $this->user ) ); + $notification_id = $this->bp::factory()->notification->create( array( 'user_id' => $this->user ) ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_query_params( array( '_embed' => 'user' ) ); @@ -190,8 +188,8 @@ public function test_get_embedded_user_from_notification_item() { * @group get_item */ public function test_get_embedded_group_from_notification_item() { - $group_id = $this->bp_factory->group->create(); - $notification_id = $this->bp_factory->notification->create( + $group_id = $this->bp::factory()->group->create(); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data( array( 'component_name' => buddypress()->groups->id, @@ -200,7 +198,7 @@ public function test_get_embedded_group_from_notification_item() { ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_query_params( array( '_embed' => 'group' ) ); @@ -226,8 +224,8 @@ public function test_get_embedded_group_from_notification_item() { * @group get_item */ public function test_get_embedded_activity_from_notification_item() { - $activity_id = $this->bp_factory->activity->create(); - $notification_id = $this->bp_factory->notification->create( + $activity_id = $this->bp::factory()->activity->create(); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data( array( 'component_name' => buddypress()->activity->id, @@ -236,7 +234,7 @@ public function test_get_embedded_activity_from_notification_item() { ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_query_params( array( '_embed' => 'activity' ) ); @@ -269,13 +267,13 @@ public function test_get_embedded_blog_from_notification_item() { $blog_title = 'The Foo Bar Blog'; - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $blog_id = $this->bp_factory->blog->create( + $blog_id = $this->bp::factory()->blog->create( array( 'title' => $blog_title ) ); - $notification_id = $this->bp_factory->notification->create( + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data( array( 'component_name' => buddypress()->blogs->id, @@ -309,7 +307,7 @@ public function test_get_embedded_blog_from_notification_item() { * @group get_item */ public function test_get_item_user_not_logged_in() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_param( 'context', 'view' ); @@ -322,10 +320,10 @@ public function test_get_item_user_not_logged_in() { * @group get_item */ public function test_get_item_user_cannot_see_notification() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); - $u = $this->factory->user->create(); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_param( 'context', 'view' ); @@ -338,7 +336,7 @@ public function test_get_item_user_cannot_see_notification() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -355,7 +353,7 @@ public function test_create_item() { * @group create_item */ public function test_rest_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -387,8 +385,8 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_user_cannot_create() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -405,9 +403,9 @@ public function test_create_item_user_cannot_create() { * @group update_item */ public function test_update_item() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -430,7 +428,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->add_header( 'content-type', 'application/json' ); @@ -457,10 +455,10 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_user_without_access() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_param( 'context', 'edit' ); @@ -473,9 +471,9 @@ public function test_update_item_user_without_access() { * @group update_item */ public function test_update_item_same_status() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -492,11 +490,11 @@ public function test_update_item_same_status() { * @group delete_item */ public function test_delete_item() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); $notification = $this->endpoint->get_notification_object( $notification_id ); $this->assertEquals( $notification_id, $notification->id ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_param( 'context', 'edit' ); @@ -515,7 +513,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'edit' ); @@ -539,10 +537,10 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_user_without_access() { - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $notification_id ) ); $request->set_param( 'context', 'edit' ); @@ -555,7 +553,7 @@ public function test_delete_item_user_without_access() { * @group prepare_item */ public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $notification = $this->endpoint->get_notification_object( $this->notification_id ); @@ -683,7 +681,7 @@ public function test_additional_fields() { ), ) ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_value'; // POST @@ -725,8 +723,8 @@ public function test_update_additional_fields() { ), ) ); - $notification_id = $this->bp_factory->notification->create( $this->set_notification_data() ); - $this->bp->set_current_user( $this->user ); + $notification_id = $this->bp::factory()->notification->create( $this->set_notification_data() ); + $this->bp::set_current_user( $this->user ); $expected = 'foo_value'; // Put diff --git a/tests/testcases/signup/test-controller.php b/tests/testcases/signup/test-controller.php index 98d55ad0..44ec6be5 100644 --- a/tests/testcases/signup/test-controller.php +++ b/tests/testcases/signup/test-controller.php @@ -7,7 +7,6 @@ * @group signup */ class BP_Test_REST_Signup_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -27,11 +26,10 @@ public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Signup_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/signup'; - $this->user = $this->factory->user->create( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', @@ -78,7 +76,7 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $s1 = $this->create_signup(); $signup = $this->endpoint->get_signup_object( $s1 ); @@ -98,7 +96,7 @@ public function test_get_items() { * @group get_items */ public function test_get_paginated_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $s1 = $this->create_signup(); $s2 = $this->create_signup(); @@ -139,9 +137,9 @@ public function test_get_items_user_not_logged_in() { * @group get_items */ public function test_get_items_unauthorized_user() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -154,7 +152,7 @@ public function test_get_items_unauthorized_user() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $signup = $this->endpoint->get_signup_object( $this->signup_id ); $this->assertEquals( $this->signup_id, $signup->id ); @@ -174,7 +172,7 @@ public function test_get_item() { * @group get_item */ public function test_get_item_with_invalid_signup_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -197,9 +195,9 @@ public function test_get_item_user_not_logged_in() { * @group get_item */ public function test_get_item_unauthorized_user() { - $u = $this->factory->user->create(); + $u = static::factory()->user->create(); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%s', $this->signup_id ) ); $request->set_param( 'context', 'view' ); @@ -232,9 +230,9 @@ public function test_create_item() { * @group create_item */ public function test_create_item_with_signup_fields() { - $g1 = $this->bp_factory->xprofile_group->create(); + $g1 = $this->bp::factory()->xprofile_group->create(); - $f1 = $this->bp_factory->xprofile_field->create( + $f1 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'textbox', @@ -242,7 +240,7 @@ public function test_create_item_with_signup_fields() { ] ); - $f2 = $this->bp_factory->xprofile_field->create( + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'checkbox', @@ -253,7 +251,7 @@ public function test_create_item_with_signup_fields() { bp_xprofile_update_field_meta( $f1, 'signup_position', 2 ); bp_xprofile_update_field_meta( $f2, 'signup_position', 3 ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -262,7 +260,7 @@ public function test_create_item_with_signup_fields() { ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -326,9 +324,9 @@ public function test_create_item_with_signup_fields() { * @group create_item */ public function test_create_item_without_the_required_field_name_field() { - $g1 = $this->bp_factory->xprofile_group->create(); + $g1 = $this->bp::factory()->xprofile_group->create(); - $f1 = $this->bp_factory->xprofile_field->create( + $f1 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'textbox', @@ -336,7 +334,7 @@ public function test_create_item_without_the_required_field_name_field() { ] ); - $f2 = $this->bp_factory->xprofile_field->create( + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'checkbox', @@ -347,7 +345,7 @@ public function test_create_item_without_the_required_field_name_field() { bp_xprofile_update_field_meta( $f1, 'signup_position', 2 ); bp_xprofile_update_field_meta( $f2, 'signup_position', 3 ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -356,7 +354,7 @@ public function test_create_item_without_the_required_field_name_field() { ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -395,9 +393,9 @@ public function test_create_item_without_the_required_field_name_field() { * @group create_item */ public function test_create_item_without_a_custom_required_field_name_field() { - $g1 = $this->bp_factory->xprofile_group->create(); + $g1 = $this->bp::factory()->xprofile_group->create(); - $f1 = $this->bp_factory->xprofile_field->create( + $f1 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'textbox', @@ -406,7 +404,7 @@ public function test_create_item_without_a_custom_required_field_name_field() { ] ); - $f2 = $this->bp_factory->xprofile_field->create( + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'checkbox', @@ -417,7 +415,7 @@ public function test_create_item_without_a_custom_required_field_name_field() { bp_xprofile_update_field_meta( $f1, 'signup_position', 2 ); bp_xprofile_update_field_meta( $f2, 'signup_position', 3 ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -426,7 +424,7 @@ public function test_create_item_without_a_custom_required_field_name_field() { ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -466,9 +464,9 @@ public function test_create_item_without_a_custom_required_field_name_field() { * @group create_item */ public function test_create_item_a_custom_required_field_name_field_value_missing() { - $g1 = $this->bp_factory->xprofile_group->create(); + $g1 = $this->bp::factory()->xprofile_group->create(); - $f1 = $this->bp_factory->xprofile_field->create( + $f1 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'textbox', @@ -477,7 +475,7 @@ public function test_create_item_a_custom_required_field_name_field_value_missin ] ); - $f2 = $this->bp_factory->xprofile_field->create( + $f2 = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'type' => 'checkbox', @@ -488,7 +486,7 @@ public function test_create_item_a_custom_required_field_name_field_value_missin bp_xprofile_update_field_meta( $f1, 'signup_position', 2 ); bp_xprofile_update_field_meta( $f2, 'signup_position', 3 ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -497,7 +495,7 @@ public function test_create_item_a_custom_required_field_name_field_value_missin ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g1, 'parent_id' => $f2, @@ -614,7 +612,7 @@ public function test_update_item_invalid_invalid_activation_key() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $signup = $this->endpoint->get_signup_object( $this->signup_id ); $this->assertEquals( $this->signup_id, $signup->id ); @@ -634,7 +632,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_signup_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'edit' ); @@ -658,8 +656,8 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_unauthorized_user() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->signup_id ) ); $request->set_param( 'context', 'edit' ); @@ -724,7 +722,7 @@ public function test_resend_activation_email_invalid_signup_id() { } public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $signup = $this->endpoint->get_signup_object( $this->signup_id ); $this->assertEquals( $this->signup_id, $signup->id ); diff --git a/tests/testcases/sitewide-notices/test-controller.php b/tests/testcases/sitewide-notices/test-controller.php index 4652f115..794e19d3 100644 --- a/tests/testcases/sitewide-notices/test-controller.php +++ b/tests/testcases/sitewide-notices/test-controller.php @@ -7,23 +7,20 @@ * @group notices */ class BP_Test_REST_Sitewide_Notices_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; protected $user; - protected $old_current_user; protected $server; protected $last_inserted_notice_id; public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_Sitewide_Notices_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/sitewide-notices'; - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -32,18 +29,15 @@ public function set_up() { $this->server = rest_get_server(); } - $this->old_current_user = get_current_user_id(); - add_action( 'messages_notice_before_save', array( $this, 'add_filter_update_last_active_query' ), 10, 0 ); add_action( 'messages_notice_after_save', array( $this, 'set_last_inserted_notice_id' ), 10, 1 ); } public function tear_down() { - parent::tear_down(); - $this->bp->set_current_user( $this->old_current_user ); + remove_action( 'messages_notice_before_save', array( $this, 'filter_update_last_active_query' ), 10 ); + remove_action( 'messages_notice_after_save', array( $this, 'set_last_inserted_notice_id' ), 10 ); - remove_action( 'messages_notice_before_save', array( $this, 'filter_update_last_active_query' ), 10, 0 ); - remove_action( 'messages_notice_after_save', array( $this, 'set_last_inserted_notice_id' ), 10, 1 ); + parent::tear_down(); } public function catch_inserted_id( $query ) { @@ -121,7 +115,7 @@ public function create_notice( $notices = array() ) { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -155,7 +149,7 @@ public function test_get_items() { * @group get_items */ public function test_get_items_no_edit_access() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -166,8 +160,8 @@ public function test_get_items_no_edit_access() { ); $created = $this->create_notice( $tested ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); @@ -180,7 +174,7 @@ public function test_get_items_no_edit_access() { * @group get_items */ public function test_get_items_view_active() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -195,8 +189,8 @@ public function test_get_items_view_active() { $n = wp_filter_object_list( $created, array( 'is_active' => 1 ), 'and', 'id' ); $key = key( $n ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -213,20 +207,10 @@ public function test_get_items_view_active() { * @group get_items */ public function test_get_items_no_active() { - $this->bp->set_current_user( $this->user ); - $tested = array( - 'n1' => array( - 'subject' => 'foo', - 'is_active' => 0, - ), - 'n2' => array( - 'subject' => 'bar', - 'is_active' => 0, - ), - ); + $this->bp::set_current_user( $this->user ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -240,7 +224,7 @@ public function test_get_items_no_active() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -257,8 +241,8 @@ public function test_get_item() { $id = current( $n ); $key = key( $n ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $id ); $request->set_param( 'context', 'view' ); @@ -276,7 +260,7 @@ public function test_get_item() { * @group get_item */ public function test_get_item_admin_access() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -309,7 +293,7 @@ public function test_get_item_admin_access() { * @group get_item */ public function test_get_item_no_access() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'foo', @@ -324,10 +308,9 @@ public function test_get_item_no_access() { $n = wp_filter_object_list( $created, array( 'is_active' => 0 ), 'and', 'id' ); $id = current( $n ); - $key = key( $n ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $id ); $request->set_param( 'context', 'view' ); @@ -340,7 +323,7 @@ public function test_get_item_no_access() { * @group get_item */ public function test_get_item_view_active() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'bar', @@ -357,8 +340,8 @@ public function test_get_item_view_active() { $id = current( $n ); $key = key( $n ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $id ); $request->set_param( 'context', 'view' ); @@ -376,7 +359,7 @@ public function test_get_item_view_active() { * @group get_item */ public function test_get_item_with_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'context', 'view' ); @@ -389,7 +372,7 @@ public function test_get_item_with_invalid_id() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -413,8 +396,8 @@ public function test_create_item() { * @group create_item */ public function test_create_item_no_access() { - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -433,7 +416,7 @@ public function test_create_item_no_access() { * @group create_item */ public function test_create_item_no_subject() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_query_params( @@ -452,7 +435,7 @@ public function test_create_item_no_subject() { * @group update_item */ public function test_update_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Foo Bar', @@ -481,7 +464,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_no_access() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Foo Bar', @@ -491,8 +474,8 @@ public function test_update_item_no_access() { $created = $this->create_notice( $tested ); $n = current( $created ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $n->id ) ); $request->set_param( 'message', 'Ouch!' ); @@ -505,7 +488,7 @@ public function test_update_item_no_access() { * @group update_item */ public function test_update_item_no_message() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Foo Bar', @@ -526,7 +509,7 @@ public function test_update_item_no_message() { * @group update_item */ public function test_update_item_with_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'message', 'Ouch!' ); @@ -539,7 +522,7 @@ public function test_update_item_with_invalid_id() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Foo Bar', @@ -565,7 +548,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_no_access() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Ouch!', @@ -575,8 +558,8 @@ public function test_delete_item_no_access() { $created = $this->create_notice( $tested ); $n = current( $created ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $n->id ) ); $response = $this->server->dispatch( $request ); @@ -588,7 +571,7 @@ public function test_delete_item_no_access() { * @group delete_item */ public function test_delete_item_with_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -600,7 +583,7 @@ public function test_delete_item_with_invalid_id() { * @group dismiss_item */ public function test_dismiss_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Taz', @@ -610,8 +593,8 @@ public function test_dismiss_item() { $created = $this->create_notice( $tested ); $n = current( $created ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/dismiss' ); $response = $this->server->dispatch( $request ); @@ -630,7 +613,7 @@ public function test_dismiss_item() { * @group dismiss_item */ public function test_dismiss_item_no_actives() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Taz', @@ -640,8 +623,8 @@ public function test_dismiss_item_no_actives() { $this->create_notice( $tested ); - $u1 = $this->factory->user->create(); - $this->bp->set_current_user( $u1 ); + $u1 = static::factory()->user->create(); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/dismiss' ); $response = $this->server->dispatch( $request ); @@ -653,7 +636,7 @@ public function test_dismiss_item_no_actives() { * @group dismiss_item */ public function test_dismiss_item_not_logged_in() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $tested = array( 'n1' => array( 'subject' => 'Taz', @@ -662,7 +645,7 @@ public function test_dismiss_item_not_logged_in() { $this->create_notice( $tested ); - $this->bp->set_current_user( 0 ); + $this->bp::set_current_user( 0 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/dismiss' ); $response = $this->server->dispatch( $request ); diff --git a/tests/testcases/xprofile/test-data-controller.php b/tests/testcases/xprofile/test-data-controller.php index 56968332..eabe9747 100644 --- a/tests/testcases/xprofile/test-data-controller.php +++ b/tests/testcases/xprofile/test-data-controller.php @@ -7,7 +7,6 @@ * @group xprofile-data */ class BP_Test_REST_XProfile_Data_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -20,15 +19,14 @@ class BP_Test_REST_XProfile_Data_Endpoint extends WP_Test_REST_Controller_Testca public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_XProfile_Data_Endpoint(); $this->field = new BP_REST_XProfile_Fields_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->profile->id . '/'; - $this->group_id = $this->bp_factory->xprofile_group->create(); - $this->field_id = $this->bp_factory->xprofile_field->create( [ 'field_group_id' => $this->group_id ] ); + $this->group_id = $this->bp::factory()->xprofile_group->create(); + $this->field_id = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $this->group_id ] ); - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -58,7 +56,7 @@ public function test_get_items() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); xprofile_set_field_data( $this->field_id, $this->user, 'foo' ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $this->user ) ); @@ -91,7 +89,7 @@ public function test_get_item_with_support_for_the_community_visibility() { * @group get_item */ public function test_get_item_hidden_for_user() { - $f = $this->bp_factory->xprofile_field->create( array( 'field_group_id' => $this->group_id ) ); + $f = $this->bp::factory()->xprofile_field->create( array( 'field_group_id' => $this->group_id ) ); xprofile_set_field_data( $f, $this->user, 'bar' ); xprofile_set_field_visibility_level( $f, $this->user, 'adminsonly' ); @@ -105,7 +103,7 @@ public function test_get_item_hidden_for_user() { * @group update_item */ public function test_update_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $this->user ) ); $request->add_header( 'content-type', 'application/json' ); @@ -122,7 +120,7 @@ public function test_update_item() { * @group update_item */ public function test_update_checkbox() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'checkbox', 'field_group_id' => $this->group_id @@ -147,7 +145,7 @@ public function test_update_checkbox() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -170,7 +168,7 @@ public function test_update_checkbox() { * @group update_item */ public function test_update_multiselectbox() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'multiselectbox', 'field_group_id' => $this->group_id @@ -204,7 +202,7 @@ public function test_update_multiselectbox() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -227,7 +225,7 @@ public function test_update_multiselectbox() { * @group update_item */ public function test_update_multiselectbox_with_invalid_item() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'multiselectbox', 'field_group_id' => $this->group_id @@ -243,7 +241,7 @@ public function test_update_multiselectbox_with_invalid_item() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -260,7 +258,7 @@ public function test_update_multiselectbox_with_invalid_item() { * @group update_item */ public function test_update_multiselectbox_with_empty_value() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'multiselectbox', 'field_group_id' => $this->group_id @@ -278,7 +276,7 @@ public function test_update_multiselectbox_with_empty_value() { xprofile_set_field_data( $field_id, $this->user, 'Option 1' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); // Clear selected options. $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $field_id, $this->user ) ); @@ -301,7 +299,7 @@ public function test_update_multiselectbox_with_empty_value() { * @ticket https://buddypress.trac.wordpress.org/ticket/9127 */ public function test_update_multiselectbox_with_apostrophe_value() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'multiselectbox', 'field_group_id' => $this->group_id @@ -325,7 +323,7 @@ public function test_update_multiselectbox_with_apostrophe_value() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -352,14 +350,14 @@ public function test_update_multiselectbox_with_apostrophe_value() { * @ticket https://buddypress.trac.wordpress.org/ticket/9127 */ public function test_update_checkbox_with_apostrophe_value() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'checkbox', 'field_group_id' => $this->group_id ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $this->group_id, 'parent_id' => $field_id, @@ -368,7 +366,7 @@ public function test_update_checkbox_with_apostrophe_value() { ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $this->group_id, 'parent_id' => $field_id, @@ -377,7 +375,7 @@ public function test_update_checkbox_with_apostrophe_value() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -402,14 +400,14 @@ public function test_update_checkbox_with_apostrophe_value() { * @group update_item */ public function test_update_textbox() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'textbox', 'field_group_id' => $this->group_id ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -435,7 +433,7 @@ public function test_update_textbox() { * @ticket https://buddypress.trac.wordpress.org/ticket/9127 */ public function test_update_textbox_with_apostrophe_value() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'textbox', 'field_group_id' => $this->group_id, @@ -443,7 +441,7 @@ public function test_update_textbox_with_apostrophe_value() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -476,7 +474,7 @@ public function test_update_textbox_with_apostrophe_value() { * @group update_item */ public function test_update_selectbox() { - $field_id = $this->bp_factory->xprofile_field->create( + $field_id = $this->bp::factory()->xprofile_field->create( [ 'type' => 'selectbox', 'name' => 'Test Field Name', @@ -484,7 +482,7 @@ public function test_update_selectbox() { ] ); - $this->bp_factory->xprofile_field->create( + $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $this->group_id, 'parent_id' => $field_id, @@ -493,7 +491,7 @@ public function test_update_selectbox() { ] ); - $this->bp->set_current_user( $this->user ); + $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' ); @@ -530,8 +528,8 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_user_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $this->user ) ); $request->add_header( 'content-type', 'application/json' ); @@ -546,10 +544,10 @@ public function test_update_item_user_without_permission() { * @group update_item */ public function test_update_item_user_without_permission_with_param() { - $u1 = $this->factory->user->create(); - $u2 = $this->factory->user->create(); + $u1 = static::factory()->user->create(); + $u2 = static::factory()->user->create(); - $this->bp->set_current_user( $u1 ); + $this->bp::set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $u2 ) ); $request->add_header( 'content-type', 'application/json' ); @@ -564,7 +562,7 @@ public function test_update_item_user_without_permission_with_param() { * @group update_item */ public function test_update_item_invalid_field_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, $this->user ) ); $request->add_header( 'content-type', 'application/json' ); @@ -580,7 +578,7 @@ public function test_update_item_invalid_field_id() { * @group update_item */ public function test_update_item_invalid_member_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->add_header( 'content-type', 'application/json' ); @@ -603,12 +601,12 @@ public function test_create_item() { * @group delete_item */ public function test_delete_item() { - $g = $this->bp_factory->xprofile_group->create(); - $f = $this->bp_factory->xprofile_field->create( [ 'field_group_id' => $g ] ); + $g = $this->bp::factory()->xprofile_group->create(); + $f = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $g ] ); xprofile_set_field_data( $f, $this->user, 'foo' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/data/%d', $f, $this->user ) ); $response = $this->server->dispatch( $request ); @@ -631,15 +629,15 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_field_owner_can_delete() { - $u = $this->bp_factory->user->create(); - $g = $this->bp_factory->xprofile_group->create(); - $f = $this->bp_factory->xprofile_field->create( array( + $u = $this->bp::factory()->user->create(); + $g = $this->bp::factory()->xprofile_group->create(); + $f = $this->bp::factory()->xprofile_field->create( array( 'field_group_id' => $g, ) ); xprofile_set_field_data( $f, $u, 'bar' ); - $this->bp->set_current_user( $u ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/data/%d', $f, $u ) ); $response = $this->server->dispatch( $request ); @@ -662,7 +660,7 @@ public function test_delete_item_field_owner_can_delete() { * @group delete_item */ public function test_delete_item_invalid_field_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/data/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, $this->user ) ); $response = $this->server->dispatch( $request ); @@ -674,7 +672,7 @@ public function test_delete_item_invalid_field_id() { * @group delete_item */ public function test_delete_item_invalid_user_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -696,8 +694,8 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $this->user ) ); $response = $this->server->dispatch( $request ); @@ -709,7 +707,7 @@ public function test_delete_item_without_permission() { * @group prepare_item */ public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); xprofile_set_field_data( $this->field_id, $this->user, 'foo' ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '%d/data/%d', $this->field_id, $this->user ) ); @@ -738,7 +736,7 @@ public function test_additional_fields() { ), ), 'data' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_metadata_value'; // POST diff --git a/tests/testcases/xprofile/test-field-controller.php b/tests/testcases/xprofile/test-field-controller.php index b71c3619..39c1c47f 100644 --- a/tests/testcases/xprofile/test-field-controller.php +++ b/tests/testcases/xprofile/test-field-controller.php @@ -7,7 +7,6 @@ * @group xprofile-field */ class BP_Test_REST_XProfile_Fields_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -19,14 +18,13 @@ class BP_Test_REST_XProfile_Fields_Endpoint extends WP_Test_REST_Controller_Test public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_XProfile_Fields_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->profile->id . '/fields'; - $this->group_id = $this->bp_factory->xprofile_group->create(); - $this->field_id = $this->bp_factory->xprofile_field->create( [ 'field_group_id' => $this->group_id ] ); + $this->group_id = $this->bp::factory()->xprofile_group->create(); + $this->field_id = $this->bp::factory()->xprofile_field->create( [ 'field_group_id' => $this->group_id ] ); - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -52,9 +50,9 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); + $this->bp::factory()->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $response = $this->server->dispatch( $request ); @@ -75,7 +73,7 @@ public function test_get_items() { * @group get_items */ public function test_public_get_items() { - $this->bp_factory->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); + $this->bp::factory()->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $response = $this->server->dispatch( $request ); @@ -98,7 +96,7 @@ public function test_public_get_items() { public function test_public_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $this->bp_factory->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); + $this->bp::factory()->xprofile_field->create_many( 5, [ 'field_group_id' => $this->group_id ] ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $response = $this->server->dispatch( $request ); @@ -110,10 +108,10 @@ public function test_public_get_items_with_support_for_the_community_visibility( * @group get_items */ public function test_get_items_include_groups() { - $g1 = $this->bp_factory->xprofile_group->create(); - $g2 = $this->bp_factory->xprofile_group->create(); - $this->bp_factory->xprofile_field->create_many( 3, [ 'field_group_id' => $g1 ] ); - $this->bp_factory->xprofile_field->create_many( 2, [ 'field_group_id' => $g2 ] ); + $g1 = $this->bp::factory()->xprofile_group->create(); + $g2 = $this->bp::factory()->xprofile_group->create(); + $this->bp::factory()->xprofile_field->create_many( 3, [ 'field_group_id' => $g1 ] ); + $this->bp::factory()->xprofile_field->create_many( 2, [ 'field_group_id' => $g2 ] ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'include_groups', array( $g2 ) ); @@ -138,7 +136,7 @@ public function test_get_items_include_groups() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $field = $this->endpoint->get_xprofile_field_object( $this->field_id ); $this->assertEquals( $this->field_id, $field->id ); @@ -193,7 +191,7 @@ public function test_get_public_item_with_support_for_the_community_visibility() * @group get_item */ public function test_get_item_with_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); @@ -205,7 +203,7 @@ public function test_get_item_with_invalid_id() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -222,7 +220,7 @@ public function test_create_item() { * @group create_item */ public function test_rest_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -239,7 +237,7 @@ public function test_rest_create_item() { * @group create_item */ public function test_create_item_with_without_required_field() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -256,7 +254,7 @@ public function test_create_item_with_without_required_field() { * @group create_item */ public function test_create_item_with_invalid_type() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -288,8 +286,8 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_user_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -307,7 +305,7 @@ public function test_create_item_user_without_permission() { */ public function test_update_item() { $new_name = 'Updated name'; - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->field_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -332,7 +330,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->add_header( 'content-type', 'application/json' ); @@ -361,8 +359,8 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->field_id ) ); $request->add_header( 'content-type', 'application/json' ); @@ -377,7 +375,7 @@ public function test_update_item_without_permission() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $field = $this->endpoint->get_xprofile_field_object( $this->field_id ); @@ -397,7 +395,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -419,8 +417,8 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u = $this->factory->user->create( array( 'role' => 'subscriber' ) ); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create( array( 'role' => 'subscriber' ) ); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->field_id ) ); $response = $this->server->dispatch( $request ); @@ -432,7 +430,7 @@ public function test_delete_item_without_permission() { * @group prepare_item */ public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $field = $this->endpoint->get_xprofile_field_object( $this->field_id ); @@ -464,7 +462,7 @@ public function test_additional_fields() { ), ), 'field' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_field_value'; // POST @@ -505,7 +503,7 @@ public function test_update_additional_fields() { ), ), 'field' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'foo_field_value'; // PUT diff --git a/tests/testcases/xprofile/test-group-controller.php b/tests/testcases/xprofile/test-group-controller.php index 4f19986a..af2bd897 100644 --- a/tests/testcases/xprofile/test-group-controller.php +++ b/tests/testcases/xprofile/test-group-controller.php @@ -7,7 +7,6 @@ * @group xprofile-group */ class BP_Test_REST_XProfile_Groups_Endpoint extends WP_Test_REST_Controller_Testcase { - protected $bp_factory; protected $endpoint; protected $bp; protected $endpoint_url; @@ -18,13 +17,12 @@ class BP_Test_REST_XProfile_Groups_Endpoint extends WP_Test_REST_Controller_Test public function set_up() { parent::set_up(); - $this->bp_factory = new BP_UnitTest_Factory(); $this->endpoint = new BP_REST_XProfile_Field_Groups_Endpoint(); $this->bp = new BP_UnitTestCase(); $this->endpoint_url = '/' . bp_rest_namespace() . '/' . bp_rest_version() . '/' . buddypress()->profile->id . '/groups'; - $this->group_id = $this->bp_factory->xprofile_group->create(); + $this->group_id = $this->bp::factory()->xprofile_group->create(); - $this->user = $this->factory->user->create( array( + $this->user = static::factory()->user->create( array( 'role' => 'administrator', 'user_email' => 'admin@example.com', ) ); @@ -50,9 +48,9 @@ public function test_register_routes() { * @group get_items */ public function test_get_items() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); - $this->bp_factory->xprofile_group->create_many( 5 ); + $this->bp::factory()->xprofile_group->create_many( 5 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -76,7 +74,7 @@ public function test_get_items() { public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); - $this->bp_factory->xprofile_group->create_many( 5 ); + $this->bp::factory()->xprofile_group->create_many( 5 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -89,11 +87,11 @@ public function test_get_items_with_support_for_the_community_visibility() { * @group get_items */ public function test_get_items_include_groups() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); + $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' ); @@ -120,7 +118,7 @@ public function test_get_items_include_groups() { * @group get_items */ public function test_get_items_publicly() { - $this->bp_factory->xprofile_group->create_many( 5 ); + $this->bp::factory()->xprofile_group->create_many( 5 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); @@ -142,7 +140,7 @@ public function test_get_items_publicly() { * @group get_item */ public function test_get_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -198,7 +196,7 @@ public function test_get_item_publicly() { * @group get_item */ public function test_get_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -211,7 +209,7 @@ public function test_get_item_invalid_id() { * @group create_item */ public function test_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); @@ -228,7 +226,7 @@ public function test_create_item() { * @group create_item */ public function test_rest_create_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -258,8 +256,8 @@ public function test_create_item_user_not_logged_in() { * @group create_item */ public function test_create_item_user_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); @@ -276,7 +274,7 @@ public function test_create_item_user_without_permission() { */ public function test_update_item() { $new_name = 'Updated name'; - $this->bp->set_current_user( $this->user ); + $this->bp::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' ); @@ -299,7 +297,7 @@ public function test_update_item() { * @group update_item */ public function test_update_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -321,8 +319,8 @@ public function test_update_item_user_not_logged_in() { * @group update_item */ public function test_update_item_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -334,7 +332,7 @@ public function test_update_item_without_permission() { * @group delete_item */ public function test_delete_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); @@ -355,7 +353,7 @@ public function test_delete_item() { * @group delete_item */ public function test_delete_item_invalid_id() { - $this->bp->set_current_user( $this->user ); + $this->bp::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 ); @@ -377,8 +375,8 @@ public function test_delete_item_user_not_logged_in() { * @group delete_item */ public function test_delete_item_without_permission() { - $u = $this->factory->user->create(); - $this->bp->set_current_user( $u ); + $u = static::factory()->user->create(); + $this->bp::set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); @@ -390,7 +388,7 @@ public function test_delete_item_without_permission() { * @group prepare_item */ public function test_prepare_item() { - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $this->assertEquals( $this->group_id, $group->id ); @@ -490,7 +488,7 @@ public function test_additional_fields() { ), ), 'group' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'bar_group_value'; // POST @@ -531,7 +529,7 @@ public function test_update_additional_fields() { ), ), 'group' ); - $this->bp->set_current_user( $this->user ); + $this->bp::set_current_user( $this->user ); $expected = 'foo_group_value'; // PUT