From f8ad036dbf8f84c3c5910dcb018097defca39076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 27 Dec 2023 22:41:32 +0100 Subject: [PATCH] fix/adopt some unit tests --- includes/collection/class-followers.php | 4 ++-- includes/handler/class-follow.php | 12 +++++------ includes/model/class-follow-request.php | 24 ++++++++++++++-------- tests/test-class-activitypub-followers.php | 10 +++++---- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 85adc6c..a09e86d 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -224,11 +224,11 @@ class Followers { * * @return array The Term list of Followers. */ - public static function get_all_followers() { + public static function get_all_followers( $user_id ) { $args = array( 'nopaging' => true, ); - return self::get_followers( null, null, null, $args ); + return self::get_followers( $user_id, null, null, $args ); } /** diff --git a/includes/handler/class-follow.php b/includes/handler/class-follow.php index 43ef5c7..d0cab72 100644 --- a/includes/handler/class-follow.php +++ b/includes/handler/class-follow.php @@ -67,24 +67,24 @@ class Follow { /** * Send Follow response * - * @param Activitypub\Model\User $user The Target Users ActivityPub object - * @param Activitypub\Model\Follower $follower The Followers ActivityPub object + * @param int|string $user_id The target users internal user id + * @param Activitypub\Model\Follower $follower The followers ActivityPub object * @param array|object $object The ActivityPub follow object * @param string $type The reponse object type: 'Accept' or 'Reject' * * @return void */ - public static function send_follow_response( $user, $inbox, $object, $type ) { + public static function send_follow_response( $user_id, $inbox, $object, $type ) { // send activity $activity = new Activity(); $activity->set_type( $type ); $activity->set_object( $object ); - $activity->set_actor( $user->get_id() ); + $activity->set_actor( $user_id ); $activity->set_to( $object['actor'] ); - $activity->set_id( $user->get_id() . '#accept-' . \preg_replace( '~^https?://~', '', $object['actor'] ) . '-' . \time() ); + $activity->set_id( $user_id . '#accept-' . \preg_replace( '~^https?://~', '', $object['actor'] ) . '-' . \time() ); $activity = $activity->to_json(); - Http::post( $inbox, $activity, $user->get__id() ); + Http::post( $inbox, $activity, $user_id ); } } diff --git a/includes/model/class-follow-request.php b/includes/model/class-follow-request.php index 310378e..fee8171 100644 --- a/includes/model/class-follow-request.php +++ b/includes/model/class-follow-request.php @@ -198,7 +198,7 @@ class Follow_Request extends Base_Object { $this->save_follow_request_status( self::FOLLOW_REQUEST_STATUS_REJECTED ); - $this->send_response( 'Reject' ); + $this->send_response( 'Reject', $user_id ); $this->delete(); } @@ -214,7 +214,7 @@ class Follow_Request extends Base_Object { $this->save_follow_request_status( self::FOLLOW_REQUEST_STATUS_APPROVED ); - $this->send_response( 'Accept' ); + $this->send_response( 'Accept', $user_id, $follower_id ); } /** @@ -228,12 +228,21 @@ class Follow_Request extends Base_Object { /** * Prepere the sending of the follow request response and hand it over to the sending handler. + * + * @param string $type The Activity type of the response: 'Accept', or 'Reject'. + * @param int|string $user_id The user id of who gets followed. + * @param int $follwer_id The internal follower id. */ - public function send_response( $type ) { - $user_id = get_post_meta( $this->get__id(), 'activitypub_user_id', true ); - $user = Users::get_by_id( $user_id ); + public function send_response( $type, $user_id = null, $follower_id = null ) { + if ( ! $user_id ) { + $user_id = get_post_meta( $this->get__id(), 'activitypub_user_id', true ); + } - $follower_id = $this->get_follower_id(); + $user = Users::get_by_id( $user_id ); + + if ( ! $follower_id ) { + $follower_id = $this->get_follower_id(); + } $follower_inbox = get_post_field( 'post_content_filtered', $follower_id ); @@ -241,9 +250,8 @@ class Follow_Request extends Base_Object { $follow_object = array( 'id' => $this->get_id(), 'type' => $this->get_type(), - 'object' => $user, ); - do_action( 'activitypub_send_follow_response', $user, $follower_inbox, $follow_object, $type ); + do_action( 'activitypub_send_follow_response', $user_id, $follower_inbox, $follow_object, $type ); } } diff --git a/tests/test-class-activitypub-followers.php b/tests/test-class-activitypub-followers.php index 3f31c33..0c2b4cd 100644 --- a/tests/test-class-activitypub-followers.php +++ b/tests/test-class-activitypub-followers.php @@ -251,7 +251,7 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { foreach ( $followers as $follower ) { $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); - \Activitypub\Collection\Followers::add_follow_relationship(1, $follower_id ); + \Activitypub\Collection\Followers::add_follow_relationship( 1, $follower_id ); } $follower = \Activitypub\Collection\Followers::get_follower( 1, 'http://sally.example.org' ); @@ -288,8 +288,8 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $follower2_id = \Activitypub\Collection\Followers::add_follower( $follower ); $follower3_id = \Activitypub\Collection\Followers::add_follower( $follower ); - $this->assertEqual( $follower1_id, $follower2_id ); - $this->assertEqual( $follower1_id, $follower3_id ); + $this->assertEquals( $follower1_id, $follower2_id ); + $this->assertEquals( $follower1_id, $follower3_id ); \Activitypub\Collection\Followers::add_follow_relationship(1, $follower1_id ); \Activitypub\Collection\Followers::add_follow_relationship(1, $follower2_id ); @@ -408,10 +408,12 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $id = $follower->upsert(); + echo $id . '\n'; + add_post_meta( $id, 'activitypub_user_id', 1 ); } - $followers = \Activitypub\Collection\Followers::get_all_followers(); + $followers = \Activitypub\Collection\Followers::get_all_followers( 1 ); $this->assertCount( 30, $followers ); }