wordpress-event-bridge-for-.../tests/includes/class-test-event-sources.php

233 lines
7.4 KiB
PHP
Raw Normal View History

<?php
/**
* Test file for the Event Sources feature.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Tests;
use WP_REST_Request;
use WP_REST_Server;
/**
2024-12-21 10:57:42 +01:00
* Test class for the Event Sources Feature.
*
* @coversDefaultClass \Event_Bridge_For_ActivityPub\Event_Sources
*/
class Test_Event_Sources extends \WP_UnitTestCase {
const FOLLOWED_ACTOR = array(
'id' => 'https://remote.example/@organizer',
'type' => 'Person',
'inbox' => 'https://remote.example/@organizer/inbox',
'outbox' => 'https://remote.example/@organizer/outbox',
'name' => 'The Organizer',
'summary' => 'Just a random organizer of events in the Fediverse',
);
/**
* Post ID.
*
* @var int
*/
protected static $event_source_post_id;
/**
* REST Server.
*
* @var WP_REST_Server
*/
protected $server;
/**
* Create fake data before tests run.
*
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
// Follow actor.
2024-12-21 10:57:42 +01:00
$event_source = \Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source::init_from_array( self::FOLLOWED_ACTOR );
$post_id = $event_source->save();
self::$event_source_post_id = $post_id;
}
/**
* Set up the test.
*/
public function set_up() {
\add_option( 'permalink_structure', '/%postname%/' );
2024-12-21 11:31:57 +01:00
\update_option( 'event_bridge_for_activitypub_event_sources_active', true );
\update_option(
'event_bridge_for_activitypub_integration_used_for_event_sources_feature',
\Event_Bridge_For_ActivityPub\Integrations\GatherPress::class );
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
global $wp_rest_server;
$wp_rest_server = new WP_REST_Server();
$this->server = $wp_rest_server;
do_action( 'rest_api_init' );
\Activitypub\Rest\Server::add_hooks();
}
/**
* Tear down the test.
*/
public function tear_down() {
\delete_option( 'permalink_structure' );
\add_filter( 'activitypub_defer_signature_verification', '__return_false' );
}
/**
* Test receiving event from followed actor.
*/
public function test_incoming_event() {
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$json = array(
'id' => 'https://remote.example/@organizer/events/new-year-party#create',
'type' => 'Create',
'actor' => 'https://remote.example/@organizer',
'object' => array(
'id' => 'https://remote.example/@organizer/events/new-year-party',
'type' => 'Event',
'startTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS ),
'name' => 'New Years Party 50/51',
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
),
);
$request = new WP_REST_Request( 'POST', '/activitypub/1.0/users/0/inbox' );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( \wp_json_encode( $json ) );
// Dispatch the request.
$response = \rest_do_request( $request );
$this->assertEquals( 202, $response->get_status() );
}
/**
* Test receiving event from followed actor with missing start time.
*/
public function test_incoming_create_with_missing_start_time() {
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$json = array(
'id' => 'https://remote.example/@organizer/events/new-year-party#create',
'type' => 'Create',
'actor' => 'https://remote.example/@organizer',
'object' => array(
'id' => 'https://remote.example/@organizer/events/new-year-party',
'type' => 'Event',
'name' => 'New Years Party 50/51',
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
),
);
$request = new WP_REST_Request( 'POST', '/activitypub/1.0/users/0/inbox' );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( \wp_json_encode( $json ) );
// Dispatch the request.
$response = \rest_do_request( $request );
$this->assertEquals( 400, $response->get_status() );
}
/**
* Test receiving event from followed actor with wrongly formatted start time.
*/
public function test_incoming_event_with_faulty_start_time() {
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$json = array(
'id' => 'https://remote.example/@organizer/events/new-year-party#create',
'type' => 'Create',
'actor' => 'https://remote.example/@organizer',
'object' => array(
'id' => 'https://remote.example/@organizer/events/new-year-party',
'type' => 'Event',
'name' => 'New Years Party 50/51',
'startTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS ),
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
),
);
$request = new WP_REST_Request( 'POST', '/activitypub/1.0/users/0/inbox' );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( \wp_json_encode( $json ) );
// Dispatch the request.
$response = \rest_do_request( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* We do understand, but do not care about incoming events that happened in the past.
*/
public function test_incoming_event_which_took_place_in_the_past() {
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$json = array(
'id' => 'https://remote.example/@organizer/events/new-year-party#create',
'type' => 'Create',
'actor' => 'https://remote.example/@organizer',
'object' => array(
'id' => 'https://remote.example/@organizer/events/new-year-party',
'type' => 'Event',
'name' => 'New Years Party 50/51',
'startTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() - WEEK_IN_SECONDS ),
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
),
);
$request = new WP_REST_Request( 'POST', '/activitypub/1.0/users/0/inbox' );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( \wp_json_encode( $json ) );
// Dispatch the request.
$response = \rest_do_request( $request );
// This should be 403 but it is not possible without lots of hacks at the moment.
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test receiving event from actor we do not follow.
*/
public function test_incoming_create_from_non_followed_actor() {
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
$json = array(
'id' => 'https://remote.example/@another_organizer/events/new-year-party#create',
'type' => 'Create',
'actor' => 'https://remote.example/@another_organizer',
'object' => array(
'id' => 'https://remote.example/@another_organizer/events/new-year-party',
'type' => 'Event',
'startTime' => '2050-12-31T18:00:00Z',
'name' => 'New Years Party 50/51',
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
),
);
$request = new WP_REST_Request( 'POST', '/activitypub/1.0/users/0/inbox' );
$request->set_header( 'Content-Type', 'application/activity+json' );
$request->set_body( \wp_json_encode( $json ) );
// Dispatch the request.
$response = \rest_do_request( $request );
$this->assertEquals( 401, $response->get_status() );
}
}