wordpress-event-bridge-for-.../tests/includes/activitypub/transmogrifier/class-test-the-events-calendar.php

216 lines
7.7 KiB
PHP
Raw Normal View History

2024-12-21 10:57:42 +01:00
<?php
/**
* Test file for the Transmogrifier (import of ActivityPub Event objects) of GatherPress.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Tests\ActivityPub\Transmogrifier;
use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source;
use WP_REST_Request;
use WP_REST_Server;
/**
* Test class for the Transmogrifier (import of ActivityPub Event objects) of GatherPress.
*
* @coversDefaultClass \Event_Bridge_For_ActivityPub\ActivityPub\Transmogrifier\The_Events_Calendar
*/
class Test_The_Events_Calendar 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',
);
/**
* REST Server.
*
* @var WP_REST_Server
*/
protected $server;
/**
* Set up the test.
*/
public function set_up() {
if ( ! class_exists( '\Tribe__Events__Main' ) ) {
self::markTestSkipped( 'The Events Calendar 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();
// Make sure that ActivityPub support is enabled for The Events Calendar.
$aec = \Event_Bridge_For_ActivityPub\Setup::get_instance();
$aec->activate_activitypub_support_for_active_event_plugins();
2024-12-21 15:42:04 +01:00
// Add event source (ActivityPub follower).
_delete_all_posts();
\Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source::init_from_array( self::FOLLOWED_ACTOR )->save();
2024-12-21 10:57:42 +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\The_Events_Calendar::class
);
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
}
/**
* Tear down the test.
*/
public function tear_down() {
\delete_option( 'permalink_structure' );
}
/**
* 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 ),
'endTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS + HOUR_IN_SECONDS ),
2024-12-21 15:42:04 +01:00
'name' => 'Fediverse Party for The Events Calendar',
2024-12-21 10:57:42 +01:00
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
'location' => array(
'type' => 'Place',
'name' => 'Fediverse Concert Hall',
'address' => 'Fedistreet 13, Feditown 1337',
),
),
);
$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() );
// Check if post has been created.
2024-12-21 15:42:04 +01:00
$events = tribe_get_events();
2024-12-21 10:57:42 +01:00
2024-12-21 15:42:04 +01:00
$this->assertEquals( 1, count( $events ) );
2024-12-21 10:57:42 +01:00
// Initialize new GatherPress Event object.
2024-12-21 15:42:04 +01:00
$event = tribe_get_event( $events[0] );
2024-12-21 10:57:42 +01:00
$this->assertEquals( $json['object']['name'], $event->post_title );
2024-12-21 15:42:04 +01:00
$this->assertEquals( $json['object']['startTime'], $event->dates->start->format( 'Y-m-d\TH:i:s\Z' ) );
$this->assertEquals( $json['object']['endTime'], $event->dates->end->format( 'Y-m-d\TH:i:s\Z' ) );
2024-12-21 10:57:42 +01:00
$venues = $event->venues;
// Get first venue. We currently only support a single venue.
if ( $venues instanceof \Tribe\Events\Collections\Lazy_Post_Collection ) {
$venue = $venues->first();
} elseif ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) {
return null;
} else {
$venue = $venues[0];
}
$this->assertEquals( $json['object']['location']['address'], $venue->address );
$this->assertEquals( $json['object']['location']['name'], $venue->post_title );
\remove_filter( 'activitypub_defer_signature_verification', '__return_true' );
}
/**
* Test receiving event from followed actor.
*/
public function test_incoming_event_with_postal_address() {
\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 ),
'endTime' => \gmdate( 'Y-m-d\TH:i:s\Z', time() + WEEK_IN_SECONDS + HOUR_IN_SECONDS ),
'name' => 'Fediverse Party for The Events Calendar',
'to' => 'https://www.w3.org/ns/activitystreams#Public',
'published' => '2020-01-01T00:00:00Z',
'location' => array(
'type' => 'Place',
'name' => 'Fediverse Concert Hall',
'address' => array(
'type' => 'PostalAddress',
'streetAddress' => 'FediStreet 13',
'postalCode' => '1337',
'addressLocality' => 'Feditown',
'addressState' => 'Fediverse State',
'addressCountry' => 'Fediverse World',
'url' => 'https://fedidevs.org/',
),
),
),
);
$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() );
// Check if post has been created.
$events = tribe_get_events();
$this->assertEquals( 1, count( $events ) );
// Initialize new GatherPress Event object.
$event = tribe_get_event( $events[0] );
$this->assertEquals( $json['object']['name'], $event->post_title );
$this->assertEquals( $json['object']['startTime'], $event->dates->start->format( 'Y-m-d\TH:i:s\Z' ) );
$this->assertEquals( $json['object']['endTime'], $event->dates->end->format( 'Y-m-d\TH:i:s\Z' ) );
$venues = $event->venues;
// Get first venue. We currently only support a single venue.
if ( $venues instanceof \Tribe\Events\Collections\Lazy_Post_Collection ) {
$venue = $venues->first();
} elseif ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) {
return null;
} else {
$venue = $venues[0];
}
$this->assertEquals( $json['object']['location']['name'], $venue->post_title );
$this->assertEquals( $json['object']['location']['address']['streetAddress'], $venue->address );
$this->assertEquals( $json['object']['location']['address']['postalCode'], $venue->zip );
$this->assertEquals( $json['object']['location']['address']['addressLocality'], $venue->city );
$this->assertEquals( $json['object']['location']['address']['addressState'], $venue->state );
$this->assertEquals( $json['object']['location']['address']['addressCountry'], $venue->country );
$this->assertEquals( $json['object']['location']['address']['url'], $venue->website );
2024-12-21 10:57:42 +01:00
\remove_filter( 'activitypub_defer_signature_verification', '__return_true' );
}
}