From 2bcd4c503921bff9cb5974e522689f5e07ed7917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 20:31:48 +0200 Subject: [PATCH] add test templates for event-manager and gatherpress --- .forgejo/workflows/phpunit.yml | 10 ++ composer.json | 8 +- docker-compose.yml | 2 +- .../activitypub/transformer/class-event.php | 7 ++ .../transformer/class-gatherpress.php | 99 ++++++++--------- tests/test-class-plugin-events-manger.php | 63 +++++++++++ tests/test-class-plugin-gatherpress.php | 103 ++++++++++++++++++ ... test-class-plugin-vs-event-list copy.php} | 0 8 files changed, 233 insertions(+), 59 deletions(-) create mode 100644 tests/test-class-plugin-events-manger.php create mode 100644 tests/test-class-plugin-gatherpress.php rename tests/{test-class-plugin-vs-event-list.php => test-class-plugin-vs-event-list copy.php} (100%) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index f4a03dd..e8810ce 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -83,3 +83,13 @@ jobs: run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=vs_event_list env: PHP_VERSION: ${{ matrix.php-version }} + + - name: Run Integration tests for GatherPress + run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=gatherpress + env: + PHP_VERSION: ${{ matrix.php-version }} + + - name: Run Integration tests for Events Manager + run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=events_manager + env: + PHP_VERSION: ${{ matrix.php-version }} \ No newline at end of file diff --git a/composer.json b/composer.json index d4048d0..ee97807 100644 --- a/composer.json +++ b/composer.json @@ -52,9 +52,13 @@ "@test-gatherpress", "@test-events-manager" ], - "test-vs-event_list": "phpunit --filter=vs_event_list", + "test-debug": [ + "@prepare-test", + "@test-events-manager" + ], + "test-vs-event-list": "phpunit --filter=vs_event_list", "test-the-events-calendar": "phpunit --filter=the_events_calendar", "test-gatherpress": "phpunit --filter=gatherpress", - "test-events-manager": "phpunit --filter=events-manager" + "test-events-manager": "phpunit --filter=events_manager" } } diff --git a/docker-compose.yml b/docker-compose.yml index a1d0658..78aacdd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,6 +42,6 @@ services: - test-db volumes: - .:/app - command: ["composer", "run-script", "test"] + command: ["composer", "run-script", "test-debug"] extra_hosts: - "host.docker.internal:host-gateway" diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index 67c8cf7..994f119 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -50,6 +50,13 @@ class Event extends Post { return 'Event'; } + /** + * Get a sane default for whether comments are enabled. + */ + protected function get_comments_enabled() { + 'open'; + } + /** * Returns the title of the event. * diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index d04e4a6..2d700d6 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -49,29 +49,18 @@ final class GatherPress extends Event { protected $gp_venue; /** - * Get transformer name. + * Extend the constructor, to also set the GatherPress objects. * - * Retrieve the transformers name. + * This is a special class object form The Events Calendar which + * has a lot of useful functions, we make use of our getter functions. * - * @since 1.0.0 - * @access public - * @return string Widget name. + * @param WP_Post $wp_object The WordPress object. + * @param string $wp_taxonomy The taxonomy slug of the event post type. */ - public function get_transformer_name() { - return 'gatherpress/gp-event'; - } - - /** - * Get transformer title. - * - * Retrieve the transformers label. - * - * @since 1.0.0 - * @access public - * @return string Widget title. - */ - public function get_transformer_label() { - return 'GatherPress Event'; + public function __construct( $wp_object, $wp_taxonomy ) { + parent::__construct( $wp_object, $wp_taxonomy ); + $this->gp_event = new GatherPress_Event( $this->wp_object->ID ); + $this->gp_venue = $this->gp_event->get_venue_information(); } /** @@ -90,15 +79,20 @@ final class GatherPress extends Event { /** * Get the event location. * - * @return array The Place. + * @return Place|null The place objector null if not place set. */ - public function get_location() { + public function get_location(): Place|null { $address = $this->gp_venue['full_address']; - $place = new Place(); - $place->set_type( 'Place' ); - $place->set_name( $address ); - $place->set_address( $address ); - return $place; + if ( $address ) { + $place = new Place(); + $place->set_type( 'Place' ); + $place->set_name( $address ); + $place->set_address( $address ); + return $place; + } else { + return null; + } + } /** @@ -134,7 +128,7 @@ final class GatherPress extends Event { /** * Overrides/extends the get_attachments function to also add the event Link. */ - protected function get_attachment() { + protected function get_attachment(): array { $attachments = parent::get_attachment(); if ( count( $attachments ) ) { $attachments[0]['type'] = 'Document'; @@ -154,7 +148,7 @@ final class GatherPress extends Event { * * @return string The User-URL. */ - protected function get_attributed_to() { + protected function get_attributed_to(): string { $user = new Blog(); return $user->get_url(); } @@ -167,7 +161,7 @@ final class GatherPress extends Event { * * @return string $summary The custom event summary. */ - public function get_summary() { + public function get_summary(): string { if ( $this->wp_object->excerpt ) { $excerpt = $this->wp_object->post_excerpt; } elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { @@ -184,38 +178,31 @@ final class GatherPress extends Event { return $summary; } + /** + * Get the content. + */ + public function get_content(): string { + return $this->wp_object->post_content; + } + + /** + * Determine whether the event is online. + * + * @return bool + */ + public function get_is_online(): bool { + return $this->gp_event->maybe_get_online_event_link() ? true : false; + } + + /** * Transform the WordPress Object into an ActivityPub Object. * * @return Activitypub\Activity\Event */ public function to_object() { - $this->ap_object = new Event_Object(); - $this->gp_event = new GatherPress_Event( $this->wp_object->ID ); - $this->gp_venue = $this->gp_event->get_venue_information(); + $activitypub_object = parent::to_object(); - $this->ap_object = parent::to_object(); - - $this->ap_object->set_comments_enabled( 'open' === $this->wp_object->comment_status ? true : false ); - - $this->ap_object->set_external_participation_url( $this->get_url() ); - - $online_event_link = $this->gp_event->maybe_get_online_event_link(); - - if ( $online_event_link ) { - $this->ap_object->set_is_online( true ); - } else { - $this->ap_object->set_is_online( false ); - } - - $this->ap_object->set_status( 'CONFIRMED' ); - - $this->ap_object->set_name( get_the_title( $this->wp_object->ID ) ); - - $this->ap_object->set_actor( get_rest_url_by_path( 'application' ) ); - $this->ap_object->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) ); - - $this->ap_object->set_location(); - return $this->ap_object; + return $activitypub_object; } } diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php new file mode 100644 index 0000000..2037866 --- /dev/null +++ b/tests/test-class-plugin-events-manger.php @@ -0,0 +1,63 @@ +activate_activitypub_support_for_active_event_plugins(); + + // Delete all posts afterwards. + _delete_all_posts(); + } + + /** + * Test that the right transformer gets applied. + */ + public function test_transformer_class() { + // We only test for one event plugin being active at the same time, + // even though we support multiple onces in theory. + // But testing all combinations is beyond scope. + $active_event_plugins = \Activitypub_Event_Extensions\Setup::get_instance()->get_active_event_plugins(); + $this->assertEquals( 1, count( $active_event_plugins ) ); + + // Enable ActivityPub support for the event plugin. + $this->assertContains( EM_POST_TYPE_EVENT, get_option( 'activitypub_support_post_types' ) ); + + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'Events Manager Test event', + 'post_status' => 'published', + 'post_type' => EM_POST_TYPE_EVENT, + 'meta_input' => array( + 'event_start_time' => strtotime( '+10 days 15:00:00' ), + ), + ) + ); + + $wp_object = get_post( $wp_post_id ); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + // Check that we got the right transformer. + $this->assertInstanceOf( \Activitypub_Event_Extensions\Activitypub\Transformer\Events_Manager::class, $transformer ); + } +} diff --git a/tests/test-class-plugin-gatherpress.php b/tests/test-class-plugin-gatherpress.php new file mode 100644 index 0000000..504dc9c --- /dev/null +++ b/tests/test-class-plugin-gatherpress.php @@ -0,0 +1,103 @@ +activate_gatherpress_plugin( false ); + + // Make sure that ActivityPub support is enabled for The Events Calendar. + $aec = \Activitypub_Event_Extensions\Setup::get_instance(); + $aec->activate_activitypub_support_for_active_event_plugins(); + + // Delete all posts afterwards. + _delete_all_posts(); + } + + /** + * Test that the right transformer gets applied. + */ + public function test_transformer_class() { + // We only test for one event plugin being active at the same time, + // even though we support multiple onces in theory. + // But testing all combinations is beyond scope. + $active_event_plugins = \Activitypub_Event_Extensions\Setup::get_instance()->get_active_event_plugins(); + $this->assertEquals( 1, count( $active_event_plugins ) ); + + // Enable ActivityPub support for the event plugin. + $this->assertContains( 'gatherpress_event', get_option( 'activitypub_support_post_types' ) ); + + // Mock GatherPress Event. + $post_id = wp_insert_post( + array( + 'post_title' => 'Unit Test Event', + 'post_type' => 'gatherpress_event', + 'post_content' => 'Unit Test description.', + ) + ); + $event = new \GatherPress\Core\Event( $post_id ); + $params = array( + 'datetime_start' => '+10 days 15:00:00', + 'datetime_end' => '+10 days 16:00:00', + 'timezone' => 'America/New_York', + ); + + $event->save_datetimes( $params ); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $event->event ); + + // Check that we got the right transformer. + $this->assertInstanceOf( \Activitypub_Event_Extensions\Activitypub\Transformer\GatherPress::class, $transformer ); + } + + /** + * Test transformation to ActivityPUb for basic event. + */ + public function test_transform_of_basic_event() { + // Mock GatherPress Event. + $post_id = wp_insert_post( + array( + 'post_title' => 'Unit Test Event', + 'post_type' => 'gatherpress_event', + 'post_content' => 'Unit Test description.', + 'post_status' => 'published', + ) + ); + $event = new \GatherPress\Core\Event( $post_id ); + $params = array( + 'datetime_start' => '+10 days 15:00:00', + 'datetime_end' => '+10 days 16:00:00', + 'timezone' => 'America/New_York', + ); + $event->save_datetimes( $params ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $event->event )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'Unit Test Event', $event_array['name'] ); + $this->assertEquals( 'Unit Test description.', $event_array['content'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertArrayNotHasKey( 'location', $event_array ); + } +} diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list copy.php similarity index 100% rename from tests/test-class-plugin-vs-event-list.php rename to tests/test-class-plugin-vs-event-list copy.php