From 6950d522648426334ae93850efc74a647b85ef9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Fri, 3 Jan 2025 20:12:06 +0100 Subject: [PATCH] Add basic test for parsing of outbox --- tests/includes/class-test-outbox-parser.php | 147 ++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tests/includes/class-test-outbox-parser.php diff --git a/tests/includes/class-test-outbox-parser.php b/tests/includes/class-test-outbox-parser.php new file mode 100644 index 0000000..dab679b --- /dev/null +++ b/tests/includes/class-test-outbox-parser.php @@ -0,0 +1,147 @@ + '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', + ); + + /** + * REST Server. + * + * @var WP_REST_Server + */ + protected $server; + + /** + * Set up the test. + */ + public function set_up() { + if ( ! defined( 'GATHERPRESS_CORE_FILE' ) ) { + self::markTestSkipped( 'GatherPress plugin is not active.' ); + } + + \add_option( 'permalink_structure', '/%postname%/' ); + + 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(); + + // Mock the plugin activation. + \GatherPress\Core\Setup::get_instance()->activate_gatherpress_plugin( false ); + + // Make sure that ActivityPub support is enabled for GatherPress. + $aec = \Event_Bridge_For_ActivityPub\Setup::get_instance(); + $aec->activate_activitypub_support_for_active_event_plugins(); + + // Add event source (ActivityPub follower). + _delete_all_posts(); + \Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source::init_from_array( self::FOLLOWED_ACTOR )->save(); + + \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 ); + } + + /** + * Tear down the test. + */ + public function tear_down() { + \delete_option( 'permalink_structure' ); + } + + /** + * Test the import of events from an items array of an outbox. + */ + public function test_import_events_from_items() { + $items = array( + array( + 'id' => 'https://remote.example/@organizer/events/concert#create', + 'type' => 'Create', + 'actor' => 'https://remote.example/@organizer', + 'object' => array( + 'id' => 'https://remote.example/@organizer/events/concert', + 'type' => 'Event', + 'startTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS ), + 'endTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS + HOUR_IN_SECONDS ), + 'name' => 'Concert', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'published' => \gmdate( 'Y-m-d\TH:i:s\Z', time() - WEEK_IN_SECONDS ), + ), + ), + array( + 'id' => 'https://remote.example/@organizer/events/birthday-party#create', + 'type' => 'Create', + 'actor' => 'https://remote.example/@organizer', + 'object' => array( + 'id' => 'https://remote.example/@organizer/events/birthday-party', + 'type' => 'Event', + 'startTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + 2 * WEEK_IN_SECONDS ), + 'endTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + 2 * WEEK_IN_SECONDS + HOUR_IN_SECONDS ), + 'name' => 'Birthday Party', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'published' => \gmdate( 'Y-m-d\TH:i:s\Z', time() - WEEK_IN_SECONDS ), + ), + ), + array( + 'id' => 'https://remote.example/@organizer/events/status/1#create', + 'type' => 'Create', + 'actor' => 'https://remote.example/@organizer', + 'object' => array( + 'id' => 'https://remote.example/@organizer/status/1', + 'type' => 'Note', + 'content' => 'This is a note', + 'to' => 'https://www.w3.org/ns/activitystreams#Public', + 'published' => \gmdate( 'Y-m-d\TH:i:s\Z', time() - WEEK_IN_SECONDS ), + ), + ), + array( + 'id' => 'https://remote.example/@organizer/likes/1', + 'type' => 'Like', + 'actor' => 'https://remote.example/@organizer', + 'object' => 'https://remote2.example/@actor/status/1', + ), + array( + 'id' => 'https://remote.example/@organizer/shares/1', + 'type' => 'Announce', + 'actor' => 'https://remote.example/@organizer', + 'object' => 'https://remote2.example/@actor/status/2', + ), + ); + $count = \Event_Bridge_For_ActivityPub\Outbox_Parser::import_events_from_items( $items, 'https://remote.example/@organizer' ); + + $this->assertEquals( 2, $count ); + + // Check that we have two event posts. + $event_query = \GatherPress\Core\Event_Query::get_instance(); + $the_query = $event_query->get_upcoming_events(); + $this->assertEquals( 2, $the_query->post_count ); + } +}