From 764a09104679bddf5b9b8e07141f349d3b2a6eec Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 25 Apr 2023 09:31:28 +0200 Subject: [PATCH] fix unit tests --- includes/class-activity-dispatcher.php | 2 +- includes/collection/class-followers.php | 6 ++ includes/peer/class-followers.php | 2 +- ...-class-activitypub-activity-dispatcher.php | 7 +- tests/test-class-db-activitypub-followers.php | 78 ++++++++++++++++--- 5 files changed, 80 insertions(+), 15 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 73e49b5..1e31616 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -67,7 +67,7 @@ class Activity_Dispatcher { $activitypub_activity = new Activity( $activity_type ); $activitypub_activity->from_post( $activitypub_post ); - $inboxes = FollowerCollection::get_inboxes( $user_id ); + $inboxes = Followers::get_inboxes( $user_id ); foreach ( $inboxes as $inbox ) { $activity = $activitypub_activity->to_json(); diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 78d0139..9669398 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -240,6 +240,8 @@ class Followers { 'object_ids' => $user_id, 'number' => $number, 'offset' => $offset, + 'orderby' => 'id', + 'order' => 'ASC', ) ); @@ -294,6 +296,10 @@ class Followers { $terms = $terms->get_terms(); + if ( ! $terms ) { + return array(); + } + global $wpdb; $results = $wpdb->get_col( $wpdb->prepare( diff --git a/includes/peer/class-followers.php b/includes/peer/class-followers.php index 8941cc4..e0e6ddb 100644 --- a/includes/peer/class-followers.php +++ b/includes/peer/class-followers.php @@ -23,7 +23,7 @@ class Followers { public static function add_follower( $actor, $author_id ) { _deprecated_function( __METHOD__, '1.0.0', '\Activitypub\Collection\Followers::add_follower' ); - return \Activitypub\Collection\Followers::add_followers( $author_id, $actor ); + return \Activitypub\Collection\Followers::add_follower( $author_id, $actor ); } public static function remove_follower( $actor, $author_id ) { diff --git a/tests/test-class-activitypub-activity-dispatcher.php b/tests/test-class-activitypub-activity-dispatcher.php index 0e503e5..bde52ab 100644 --- a/tests/test-class-activitypub-activity-dispatcher.php +++ b/tests/test-class-activitypub-activity-dispatcher.php @@ -5,17 +5,22 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT 'url' => 'https://example.org/users/username', 'inbox' => 'https://example.org/users/username/inbox', 'name' => 'username', + 'prefferedUsername' => 'username', ), 'jon@example.com' => array( 'url' => 'https://example.com/author/jon', 'inbox' => 'https://example.com/author/jon/inbox', 'name' => 'jon', + 'prefferedUsername' => 'jon', ), ); public function test_dispatch_activity() { $followers = array( 'https://example.com/author/jon', 'https://example.org/users/username' ); - \update_user_meta( 1, 'activitypub_followers', $followers ); + + foreach ( $followers as $follower ) { + \Activitypub\Collection\Followers::add_follower( 1, $follower ); + } $post = \wp_insert_post( array( diff --git a/tests/test-class-db-activitypub-followers.php b/tests/test-class-db-activitypub-followers.php index c502bf2..0591178 100644 --- a/tests/test-class-db-activitypub-followers.php +++ b/tests/test-class-db-activitypub-followers.php @@ -1,15 +1,37 @@ 'Person', - 'id' => 'http://sally.example.org', - 'name' => 'Sally Smith', - ); - \update_user_meta( 1, 'activitypub_followers', $followers ); + public static $users = array( + 'username@example.org' => array( + 'url' => 'https://example.org/users/username', + 'inbox' => 'https://example.org/users/username/inbox', + 'name' => 'username', + 'prefferedUsername' => 'username', + ), + 'jon@example.com' => array( + 'url' => 'https://example.com/author/jon', + 'inbox' => 'https://example.com/author/jon/inbox', + 'name' => 'jon', + 'prefferedUsername' => 'jon', + ), + 'sally@example.org' => array( + 'url' => 'http://sally.example.org', + 'inbox' => 'http://sally.example.org/inbox', + 'name' => 'jon', + 'prefferedUsername' => 'jon', + ), + ); - $db_followers = \Activitypub\Peer\Followers::get_followers( 1 ); + public function test_get_followers() { + $followers = array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ); + + $pre_http_request = new MockAction(); + add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); + + foreach ( $followers as $follower ) { + \Activitypub\Collection\Followers::add_follower( 1, $follower ); + } + + $db_followers = \Activitypub\Collection\Followers::get_followers( 1 ); $this->assertEquals( 3, \count( $db_followers ) ); @@ -17,11 +39,43 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { } public function test_add_follower() { - $follower = 'https://example.com/author/' . \time(); - \Activitypub\Peer\Followers::add_follower( $follower, 1 ); + $pre_http_request = new MockAction(); + add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); - $db_followers = \Activitypub\Peer\Followers::get_followers( 1 ); + $follower = 'https://example.com/author/' . \time(); + \Activitypub\Collection\Followers::add_follower( 1, $follower ); + + $db_followers = \Activitypub\Collection\Followers::get_followers( 1 ); $this->assertContains( $follower, $db_followers ); } + + public static function http_request_host_is_external( $in, $host ) { + if ( in_array( $host, array( 'example.com', 'example.org' ), true ) ) { + return true; + } + return $in; + } + public static function http_request_args( $args, $url ) { + if ( in_array( wp_parse_url( $url, PHP_URL_HOST ), array( 'example.com', 'example.org' ), true ) ) { + $args['reject_unsafe_urls'] = false; + } + return $args; + } + + public static function pre_http_request( $preempt, $request, $url ) { + return array( + 'headers' => array( + 'content-type' => 'text/json', + ), + 'body' => '', + 'response' => array( + 'code' => 202, + ), + ); + } + + public static function http_response( $response, $args, $url ) { + return $response; + } }