From 8920c60c61f32e6a412e2d5afd58fd16efa6aec4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 10 Jul 2023 15:14:37 +0200 Subject: [PATCH] final fixes and more tests --- includes/functions.php | 6 ++- includes/transformer/class-post.php | 2 +- ...-class-activitypub-activity-dispatcher.php | 43 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 725d9f6..0c3dc70 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -312,14 +312,16 @@ function is_user_disabled( $user_id ) { * @return boolean True if the blog is in single-user mode, false otherwise. */ function is_single_user() { + $return = false; + if ( false === ACTIVITYPUB_DISABLE_BLOG_USER && true === ACTIVITYPUB_DISABLE_USER ) { - return true; + $return = true; } - return false; + return apply_filters( 'activitypub_is_single_user', $return ); } if ( ! function_exists( 'get_self_link' ) ) { diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index b408484..7c7d990 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -3,7 +3,7 @@ namespace Activitypub\Transformer; use WP_Post; use Activitypub\Collection\Users; -use Activitypub\Collection\Blog_Users; +use Activitypub\Model\Blog_User; use Activitypub\Activity\Base_Object; use function Activitypub\is_single_user; diff --git a/tests/test-class-activitypub-activity-dispatcher.php b/tests/test-class-activitypub-activity-dispatcher.php index c7abf02..54ebda0 100644 --- a/tests/test-class-activitypub-activity-dispatcher.php +++ b/tests/test-class-activitypub-activity-dispatcher.php @@ -47,6 +47,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $json = json_decode( $second_call_args[1]['body'] ); $this->assertEquals( 'Create', $json->type ); + $this->assertEquals( 'http://example.org/?author=1', $json->actor ); + $this->assertEquals( 'http://example.org/?author=1', $json->object->attributedTo ); remove_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10 ); } @@ -125,6 +127,47 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT remove_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10 ); } + public function test_dispatch_blog_activity() { + $followers = array( 'https://example.com/author/jon' ); + + add_filter( + 'activitypub_is_single_user', + function( $return ) { + return true; + } + ); + + foreach ( $followers as $follower ) { + \Activitypub\Collection\Followers::add_follower( \Activitypub\Collection\Users::BLOG_USER_ID, $follower ); + } + + $post = \wp_insert_post( + array( + 'post_author' => 1, + 'post_content' => 'hello', + ) + ); + + $pre_http_request = new MockAction(); + add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); + + \Activitypub\Activity_Dispatcher::send_activity_or_announce( get_post( $post ), 'Create' ); + + $all_args = $pre_http_request->get_args(); + $first_call_args = $all_args[0]; + + $this->assertSame( 1, $pre_http_request->get_call_count() ); + + $user = new \Activitypub\Model\Blog_User(); + + $json = json_decode( $first_call_args[1]['body'] ); + $this->assertEquals( 'Create', $json->type ); + $this->assertEquals( $user->get_url(), $json->actor ); + $this->assertEquals( $user->get_url(), $json->object->attributedTo ); + + remove_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10 ); + } + public function set_up() { parent::set_up(); add_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'pre_get_remote_metadata_by_actor' ), 10, 2 );