final fixes and more tests

This commit is contained in:
Matthias Pfefferle 2023-07-10 15:14:37 +02:00
parent 0fab95bfff
commit 8920c60c61
3 changed files with 48 additions and 3 deletions

View file

@ -312,14 +312,16 @@ function is_user_disabled( $user_id ) {
* @return boolean True if the blog is in single-user mode, false otherwise. * @return boolean True if the blog is in single-user mode, false otherwise.
*/ */
function is_single_user() { function is_single_user() {
$return = false;
if ( if (
false === ACTIVITYPUB_DISABLE_BLOG_USER && false === ACTIVITYPUB_DISABLE_BLOG_USER &&
true === ACTIVITYPUB_DISABLE_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' ) ) { if ( ! function_exists( 'get_self_link' ) ) {

View file

@ -3,7 +3,7 @@ namespace Activitypub\Transformer;
use WP_Post; use WP_Post;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Collection\Blog_Users; use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;

View file

@ -47,6 +47,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT
$json = json_decode( $second_call_args[1]['body'] ); $json = json_decode( $second_call_args[1]['body'] );
$this->assertEquals( 'Create', $json->type ); $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 ); 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 ); 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() { public function set_up() {
parent::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 ); add_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'pre_get_remote_metadata_by_actor' ), 10, 2 );