wordpress-activitypub/tests/test-class-db-activitypub-followers.php

99 lines
3.1 KiB
PHP
Raw Normal View History

2019-02-19 22:43:14 +01:00
<?php
class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
2023-04-25 09:31:28 +02:00
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',
),
);
2019-02-19 22:43:14 +01:00
public function test_get_followers() {
2023-04-25 09:31:28 +02:00
$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 );
}
2019-02-19 22:43:14 +01:00
2023-04-25 09:31:28 +02:00
$db_followers = \Activitypub\Collection\Followers::get_followers( 1 );
2019-02-19 22:43:14 +01:00
$this->assertEquals( 3, \count( $db_followers ) );
2019-02-19 22:43:14 +01:00
$this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers );
}
2019-02-28 19:32:03 +01:00
public function test_add_follower() {
2023-04-25 09:31:28 +02:00
$pre_http_request = new MockAction();
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
$follower = 'https://example.com/author/' . \time();
2023-04-25 09:31:28 +02:00
\Activitypub\Collection\Followers::add_follower( 1, $follower );
2019-02-28 19:32:03 +01:00
2023-04-25 09:31:28 +02:00
$db_followers = \Activitypub\Collection\Followers::get_followers( 1 );
2019-02-28 19:32:03 +01:00
$this->assertContains( $follower, $db_followers );
}
2023-04-25 09:31:28 +02:00
2023-04-26 17:22:44 +02:00
public function test_get_follower() {
$followers = array( 'https://example.com/author/jon' );
$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 );
}
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' );
$this->assertEquals( 'https://example.com/author/jon', $follower->get_actor() );
$follower = \Activitypub\Collection\Followers::get_follower( 1, 'http://sally.example.org' );
$this->assertNull( $follower );
}
2023-04-25 09:31:28 +02:00
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;
}
2019-02-19 22:43:14 +01:00
}