fix unit tests

This commit is contained in:
Matthias Pfefferle 2023-04-25 09:31:28 +02:00
parent 377fc94161
commit 764a091046
5 changed files with 80 additions and 15 deletions

View file

@ -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();

View file

@ -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(

View file

@ -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 ) {

View file

@ -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(

View file

@ -1,15 +1,37 @@
<?php
class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
public function test_get_followers() {
$followers = array( 'https://example.com/author/jon', 'https://example.org/author/doe' );
$followers[] = array(
'type' => '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;
}
}