From 92fff2220dc559f9e8abd088a2da22b4183393dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 18 Nov 2024 17:36:27 +0100 Subject: [PATCH 01/10] wip --- .../transformer/class-eventprime.php | 61 +++++++++++++++++ .../transformer/class-gatherpress.php | 2 +- includes/class-setup.php | 1 + includes/plugins/class-eventprime.php | 65 +++++++++++++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 includes/activitypub/transformer/class-eventprime.php create mode 100644 includes/plugins/class-eventprime.php diff --git a/includes/activitypub/transformer/class-eventprime.php b/includes/activitypub/transformer/class-eventprime.php new file mode 100644 index 0000000..05546ee --- /dev/null +++ b/includes/activitypub/transformer/class-eventprime.php @@ -0,0 +1,61 @@ +gp_event = new GatherPress_Event( $this->wp_object->ID ); + $this->gp_venue = $this->gp_event->get_venue_information(); + } + + /** + * Get the end time from the event object. + */ + protected function get_end_time(): ?string { + return $this->gp_event->get_datetime_end( 'Y-m-d\TH:i:s\Z' ); + } + + /** + * Get the end time from the event object. + */ + protected function get_start_time(): string { + return $this->gp_event->get_datetime_start( 'Y-m-d\TH:i:s\Z' ); + } +} diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index 756524a..ea742b3 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -1,6 +1,6 @@ Date: Mon, 25 Nov 2024 18:39:41 +0100 Subject: [PATCH 02/10] add custom template filter for EventPrime --- .../transformer/class-eventprime.php | 37 ++--- includes/plugins/class-eventprime.php | 154 ++++++++++++++++++ tests/test-class-plugin-eventprime.php | 111 +++++++++++++ 3 files changed, 277 insertions(+), 25 deletions(-) create mode 100644 tests/test-class-plugin-eventprime.php diff --git a/includes/activitypub/transformer/class-eventprime.php b/includes/activitypub/transformer/class-eventprime.php index 05546ee..2551b17 100644 --- a/includes/activitypub/transformer/class-eventprime.php +++ b/includes/activitypub/transformer/class-eventprime.php @@ -22,40 +22,27 @@ use GatherPress\Core\Event as GatherPress_Event; * @since 1.0.0 */ final class EventPrime extends Event { - - /** - * The current GatherPress Event object. - * - * @var GatherPress_Event - */ - protected $gp_event; - - /** - * Extend the constructor, to also set the GatherPress objects. - * - * This is a special class object form The Events Calendar which - * has a lot of useful functions, we make use of our getter functions. - * - * @param WP_Post $wp_object The WordPress object. - * @param string $wp_taxonomy The taxonomy slug of the event post type. - */ - 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(); - } - /** * Get the end time from the event object. */ protected function get_end_time(): ?string { - return $this->gp_event->get_datetime_end( 'Y-m-d\TH:i:s\Z' ); + $timestamp = get_post_meta( $this->wp_object->ID, 'em_end_date', true ); + if ( $timestamp ) { + return \gmdate( 'Y-m-d\TH:i:s\Z', $timestamp ); + } else { + return null; + } } /** * Get the end time from the event object. */ protected function get_start_time(): string { - return $this->gp_event->get_datetime_start( 'Y-m-d\TH:i:s\Z' ); + $timestamp = get_post_meta( $this->wp_object->ID, 'em_start_date', true ); + if ( $timestamp ) { + return \gmdate( 'Y-m-d\TH:i:s\Z', $timestamp ); + } else { + return ''; + } } } diff --git a/includes/plugins/class-eventprime.php b/includes/plugins/class-eventprime.php index a725b37..17a944e 100644 --- a/includes/plugins/class-eventprime.php +++ b/includes/plugins/class-eventprime.php @@ -9,6 +9,9 @@ namespace ActivityPub_Event_Bridge\Plugins; +use Activitypub\Signature; +use Eventprime_Basic_Functions; + // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore @@ -18,6 +21,13 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore * @since 1.0.0 */ final class EventPrime extends Event_Plugin { + /** + * Add filter for the template inclusion. + */ + public function __construct() { + \add_filter( 'template_include', array( self::class, 'render_activitypub_template' ), 100 ); + } + /** * Returns the full plugin file. * @@ -62,4 +72,148 @@ final class EventPrime extends Event_Plugin { public static function get_event_category_taxonomy(): string { return 'em_event_type'; } + + /** + * Determine whether the current request is an EventPrime ActivityPub request. + */ + private static function is_eventprime_activitypub_request() { + global $wp_query; + + /* + * ActivityPub requests are currently only made for + * author archives, singular posts, and the homepage. + */ + if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( '\REST_REQUEST' ) ) { + return false; + } + + // Check if the current post type supports ActivityPub. + if ( \is_singular() ) { + $queried_object = \get_queried_object(); + + if ( ! $queried_object instanceof \WP_Post ) { + return false; + } + + if ( '[em_event]' !== $queried_object->post_content && '[em_events]' !== $queried_object->post_content ) { + return false; + } + } + + // Check if header already sent. + if ( ! \headers_sent() && ACTIVITYPUB_SEND_VARY_HEADER ) { + // Send Vary header for Accept header. + \header( 'Vary: Accept' ); + } + + // One can trigger an ActivityPub request by adding ?activitypub to the URL. + if ( isset( $wp_query->query_vars['activitypub'] ) ) { + return true; + } + + /* + * The other (more common) option to make an ActivityPub request + * is to send an Accept header. + */ + if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) { + $accept = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT'] ) ); + + /* + * $accept can be a single value, or a comma separated list of values. + * We want to support both scenarios, + * and return true when the header includes at least one of the following: + * - application/activity+json + * - application/ld+json + * - application/json + */ + if ( preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) { + return true; + } + } + + return false; + } + + /** + * Extract the post id of the event for an EventPrime event query. + * + * @return bool|int The post ID if an event could be identified, false otherwise. + */ + private static function get_eventprime_post_id() { + $event = get_query_var( 'event' ); + if ( ! $event ) { + if ( ! empty( filter_input( INPUT_GET, 'event', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) { + $event = rtrim( filter_input( INPUT_GET, 'event', FILTER_SANITIZE_FULL_SPECIAL_CHARS ), '/\\' ); + } + } + + if ( $event ) { + $ep_basic_functions = new Eventprime_Basic_Functions(); + return $ep_basic_functions->ep_get_id_by_slug( $event, 'em_event' ); + } + + return false; + } + + /** + * Add the ActivityPub template for EventPrime. + * + * @param string $template The path to the template object. + * @return string The new path to the JSON template. + */ + public static function render_activitypub_template( $template ) { + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + return $template; + } + + // Check if the request is a page with (solely) the eventprime shortcode in it. + if ( ! self::is_eventprime_activitypub_request() ) { + return $template; + } + + if ( ! \is_singular() ) { + return $template; + } + + $post_id = self::get_eventprime_post_id(); + + if ( $post_id ) { + $preview = \get_query_var( 'preview' ); + if ( $preview ) { + $activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php'; + } else { + $activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php'; + } + } + + /* + * Check if the request is authorized. + * + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch + */ + if ( $activitypub_template && ACTIVITYPUB_AUTHORIZED_FETCH ) { + $verification = Signature::verify_http_signature( $_SERVER ); + if ( \is_wp_error( $verification ) ) { + header( 'HTTP/1.1 401 Unauthorized' ); + + // Fallback as template_loader can't return http headers. + return $template; + } + } + + if ( $activitypub_template ) { + global $post; + + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited + $post = get_post( $post_id ); + + // Ensure WordPress functions use the new post data. + setup_postdata( $post ); + // Return the default ActivityPub template. + return $activitypub_template; + } + + return $template; + } } diff --git a/tests/test-class-plugin-eventprime.php b/tests/test-class-plugin-eventprime.php new file mode 100644 index 0000000..98217fb --- /dev/null +++ b/tests/test-class-plugin-eventprime.php @@ -0,0 +1,111 @@ + array( + 'venue' => 'Minimal Venue', + 'status' => 'publish', + ), + 'complex_venue' => array( + 'venue' => 'Complex Venue', + 'status' => 'publish', + 'show_map' => false, + 'show_map_link' => false, + 'address' => 'Venue address', + 'city' => 'Venue city', + 'country' => 'Venue country', + 'province' => 'Venue province', + 'state' => 'Venue state', + 'stateprovince' => 'Venue stateprovince', + 'zip' => 'Venue zip', + 'phone' => 'Venue phone', + 'website' => 'http://venue.com', + ), + ); + + public const MOCKUP_EVENTS = array( + 'minimal_event' => array( + 'title' => 'My Event', + 'content' => 'Come to my event!', + 'start_date' => '+10 days 15:00:00', + 'duration' => HOUR_IN_SECONDS, + 'status' => 'publish', + ), + 'complex_event' => array( + 'title' => 'My Event', + 'content' => 'Come to my event!', + 'start_date' => '+10 days 15:00:00', + 'duration' => HOUR_IN_SECONDS, + 'status' => 'publish', + ), + ); + + /** + * Override the setup function, so that tests don't run if the Events Calendar is not active. + */ + public function set_up() { + parent::set_up(); + + if ( ! class_exists( '\Eventprime_Basic_Functions' ) ) { + self::markTestSkipped( 'The EventPrime Calendar management plugin is not active.' ); + } + + // Make sure that ActivityPub support is enabled for The Events Calendar. + $aeb = \ActivityPub_Event_Bridge\Setup::get_instance(); + $aeb->activate_activitypub_support_for_active_event_plugins(); + + // Delete all posts afterwards. + _delete_all_posts(); + } + + /** + * Test that the right transformer gets applied. + */ + public function test_the_events_calendar_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_Bridge\Setup::get_instance()->get_active_event_plugins(); + $this->assertEquals( 1, count( $active_event_plugins ) ); + + // Enable ActivityPub support for the event plugin. + $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); + + $event_data = array(); + $event_data['name'] = 'EventPrime Event title'; + $event_data['description'] = 'EventPrime event description'; + $event_data['status'] = 'Publish'; + $event_data['em_event_type'] = ''; + $event_data['em_venue'] = ''; + $event_data['em_organizer'] = ''; + $event_data['em_performer'] = ''; + $event_data['em_start_date'] = strtotime( '+10 days 15:00:00' ); + $event_data['em_end_date'] = strtotime( '+10 days 16:00:00' ); + $event_data['em_enable_booking'] = 'bookings_off'; + $event_data['em_ticket_price'] = 0; + + // Create an EventPrime Event without content. + $ep_functions = new Eventprime_Basic_Functions(); + + $post_id = $ep_functions->insert_event_post_data( $event_data ); + + $wp_object = get_post( $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_Bridge\Activitypub\Transformer\EventPrime::class, $transformer ); + } +} -- 2.39.5 From aa353c37958ac7188ac24d015ad32dbb360373bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 19:06:11 +0100 Subject: [PATCH 03/10] add basic venue support and tests for eventprime --- .forgejo/workflows/phpunit.yml | 5 +++ composer.json | 6 ++-- .../transformer/class-eventprime.php | 34 +++++++++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index f8e08ac..151c527 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -107,5 +107,10 @@ jobs: - name: Run Integration tests for Modern Events Calendar Lite run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=modern_events_calendar_lite + env: + PHP_VERSION: ${{ matrix.php-version }} + + - name: Run Integration tests for EventPrime + run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=eventprime env: PHP_VERSION: ${{ matrix.php-version }} \ No newline at end of file diff --git a/composer.json b/composer.json index 4da8b48..d4c2b0b 100644 --- a/composer.json +++ b/composer.json @@ -54,11 +54,12 @@ "@test-events-manager", "@test-wp-event-manager", "@test-eventin", - "@test-modern-events-calendar-lite" + "@test-modern-events-calendar-lite", + "@test-eventprime" ], "test-debug": [ "@prepare-test", - "@test-gatherpress" + "@test-eventprime" ], "test-vs-event-list": "phpunit --filter=vs_event_list", "test-the-events-calendar": "phpunit --filter=the_events_calendar", @@ -67,6 +68,7 @@ "test-wp-event-manager": "phpunit --filter=wp_event_manager", "test-eventin": "phpunit --filter=eventin", "test-modern-events-calendar-lite": "phpunit --filter=modern_events_calendar_lite", + "test-eventprime": "phpunit --filter=eventprime", "test-all": "phpunit" } } diff --git a/includes/activitypub/transformer/class-eventprime.php b/includes/activitypub/transformer/class-eventprime.php index 2551b17..c247ebe 100644 --- a/includes/activitypub/transformer/class-eventprime.php +++ b/includes/activitypub/transformer/class-eventprime.php @@ -11,10 +11,8 @@ namespace ActivityPub_Event_Bridge\Activitypub\Transformer; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore -use Activitypub\Activity\Extended_Object\Event as Event_Object; use Activitypub\Activity\Extended_Object\Place; use ActivityPub_Event_Bridge\Activitypub\Transformer\Event; -use GatherPress\Core\Event as GatherPress_Event; /** * ActivityPub Transformer for VS Event @@ -45,4 +43,36 @@ final class EventPrime extends Event { return ''; } } + + /** + * Get location from the event object. + */ + protected function get_location(): ?Place { + $venue_term_id = get_post_meta( $this->wp_object->ID, 'em_venue', true ); + if ( ! $venue_term_id ) { + return null; + } + + $venue = wp_get_post_terms( $this->wp_object->ID, 'em_venue' ); + + if ( empty( $venue ) ) { + return null; + } else { + $venue = $venue[0]; + } + + $place = new Place(); + + $place->set_name( $venue->name ); + $place->set_content( $venue->description ); + + $address = get_term_meta( $venue->term_id, 'em_address', true ); + $display_address = get_term_meta( $venue->term_id, 'em_display_address_on_frontend', true ); + + if ( $address && $display_address ) { + $place->set_address( get_term_meta( $venue->term_id, 'em_address', true ) ); + } + + return $place; + } } -- 2.39.5 From ed236a70c57ed23b00601c52fa7c941cc29e8072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 19:10:57 +0100 Subject: [PATCH 04/10] allow global variable override for EventPrime --- includes/plugins/class-eventprime.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/plugins/class-eventprime.php b/includes/plugins/class-eventprime.php index 17a944e..e5fb8d6 100644 --- a/includes/plugins/class-eventprime.php +++ b/includes/plugins/class-eventprime.php @@ -205,8 +205,7 @@ final class EventPrime extends Event_Plugin { if ( $activitypub_template ) { global $post; - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited - $post = get_post( $post_id ); + $post = get_post( $post_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // Ensure WordPress functions use the new post data. setup_postdata( $post ); -- 2.39.5 From fe9bc524f4ef639728bd544ebb2638382e02034d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 19:14:44 +0100 Subject: [PATCH 05/10] install eventprime-event-calendar-management in tests pipeline --- .forgejo/workflows/phpunit.yml | 2 +- bin/install-wp-tests.sh | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 151c527..c79e189 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -38,7 +38,7 @@ jobs: path: | ${{ env.WP_CORE_DIR }} ${{ env.WP_TESTS_DIR }} - key: cache-wordpress-67-2 + key: cache-wordpress-67-3 - name: Cache Composer id: cache-composer-phpunit diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 3cc440f..7cbf282 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -257,6 +257,7 @@ install_wp_plugins() { install_wp_plugin the-events-calendar "6.8.1" install_wp_plugin very-simple-event-list install_wp_plugin gatherpress + install_wp_plugin eventprime-event-calendar-management install_wp_plugin events-manager "6.6.3" install_wp_plugin wp-event-manager "3.1.45.1" install_wp_plugin wp-event-solution "4.0.14" -- 2.39.5 From 8a5408f978b5d4c47a3bcd325d7fc8258c5050f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 19:15:57 +0100 Subject: [PATCH 06/10] activate eventprime plugin in ci --- tests/bootstrap.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4262f2b..fef1717 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -71,6 +71,9 @@ function _manually_load_plugin() { case 'wp_event_manager': $plugin_file = 'wp-event-manager/wp-event-manager.php'; break; + case 'eventprime': + $plugin_file = 'eventprime-event-calendar-management/event-prime.php'; + break; } if ( $plugin_file ) { -- 2.39.5 From c283dc41ea6c054cb1372dd5c93c533024510961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 19:17:49 +0100 Subject: [PATCH 07/10] fix basic test for eventprime --- tests/test-class-plugin-eventprime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-class-plugin-eventprime.php b/tests/test-class-plugin-eventprime.php index 98217fb..a049130 100644 --- a/tests/test-class-plugin-eventprime.php +++ b/tests/test-class-plugin-eventprime.php @@ -80,7 +80,7 @@ class Test_EventPrime extends WP_UnitTestCase { $this->assertEquals( 1, count( $active_event_plugins ) ); // Enable ActivityPub support for the event plugin. - $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); + $this->assertContains( 'em_event', get_option( 'activitypub_support_post_types' ) ); $event_data = array(); $event_data['name'] = 'EventPrime Event title'; -- 2.39.5 From ed773761bd90812a2b13ea8e5fd1b06ee8c7841f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 25 Nov 2024 21:05:13 +0100 Subject: [PATCH 08/10] Add EventPrime to readme --- CHANGELOG.md | 6 ++++++ README.md | 7 +++++++ readme.txt | 1 + 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1f6405..b04de49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +* Integration for EventPrime – Events Calendar, Bookings and Tickets + ## [0.2.1] - 2024-11-16 ### Added diff --git a/README.md b/README.md index 023e5c2..c23c1b6 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ This plugin depends on the [ActivityPub plugin](https://wordpress.org/plugins/ac * [Eventin](https://de.wordpress.org/plugins/wp-event-solution/) * [Modern Events Calendar Lite](https://webnus.net/modern-events-calendar/) * [GatherPress](https://gatherpress.org/) +* [EventPrime – Events Calendar, Bookings and Tickets](https://wordpress.org/plugins/eventprime-event-calendar-management/) ## Configuration ## @@ -100,6 +101,12 @@ We're always interested in your feedback. Feel free to reach out to us via [E-Ma ## Changelog ## +## Unreleased + +### Added + +* Integration for EventPrime – Events Calendar, Bookings and Tickets + ### [0.2.1] 2024-11-16 ### * Initial release on https://wordpress.org/ diff --git a/readme.txt b/readme.txt index 66ef1b9..f200dd6 100644 --- a/readme.txt +++ b/readme.txt @@ -57,6 +57,7 @@ This plugin depends on the [ActivityPub plugin](https://wordpress.org/plugins/ac * [Eventin](https://de.wordpress.org/plugins/wp-event-solution/) * [Modern Events Calendar Lite](https://webnus.net/modern-events-calendar/) * [GatherPress](https://gatherpress.org/) +* [EventPrime – Events Calendar, Bookings and Tickets](https://wordpress.org/plugins/eventprime-event-calendar-management/) == Configuration == -- 2.39.5 From 33bdba1ed073c1f9e1b9f6e04d28b14fef7a2573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 26 Nov 2024 09:17:59 +0100 Subject: [PATCH 09/10] more tests --- tests/test-class-plugin-eventprime.php | 179 ++++++++++++++++++------- 1 file changed, 127 insertions(+), 52 deletions(-) diff --git a/tests/test-class-plugin-eventprime.php b/tests/test-class-plugin-eventprime.php index a049130..c5b5bbc 100644 --- a/tests/test-class-plugin-eventprime.php +++ b/tests/test-class-plugin-eventprime.php @@ -10,46 +10,18 @@ */ class Test_EventPrime extends WP_UnitTestCase { /** - * Mockup events of certain complexity. + * Mockup venues of certain complexity. + * + * @var array */ - public const MOCKUP_VENUS = array( - 'minimal_venue' => array( - 'venue' => 'Minimal Venue', - 'status' => 'publish', - ), - 'complex_venue' => array( - 'venue' => 'Complex Venue', - 'status' => 'publish', - 'show_map' => false, - 'show_map_link' => false, - 'address' => 'Venue address', - 'city' => 'Venue city', - 'country' => 'Venue country', - 'province' => 'Venue province', - 'state' => 'Venue state', - 'stateprovince' => 'Venue stateprovince', - 'zip' => 'Venue zip', - 'phone' => 'Venue phone', - 'website' => 'http://venue.com', - ), - ); + private $mockup_venue = array(); - public const MOCKUP_EVENTS = array( - 'minimal_event' => array( - 'title' => 'My Event', - 'content' => 'Come to my event!', - 'start_date' => '+10 days 15:00:00', - 'duration' => HOUR_IN_SECONDS, - 'status' => 'publish', - ), - 'complex_event' => array( - 'title' => 'My Event', - 'content' => 'Come to my event!', - 'start_date' => '+10 days 15:00:00', - 'duration' => HOUR_IN_SECONDS, - 'status' => 'publish', - ), - ); + /** + * Mockup events for tests. + * + * @var array + */ + protected $mockup_events = array(); /** * Override the setup function, so that tests don't run if the Events Calendar is not active. @@ -67,6 +39,34 @@ class Test_EventPrime extends WP_UnitTestCase { // Delete all posts afterwards. _delete_all_posts(); + + $this->setup_mockup_data(); + } + + /** + * Setup mockup events. + */ + private function setup_mockup_data() { + $this->mockup_events = array( + 'minimal_event' => array( + 'name' => 'EventPrime Event title', + 'description' => 'EventPrime event description', + 'status' => 'Publish', + 'em_event_type' => '', + 'em_venue' => '', + 'em_organizer' => '', + 'em_performer' => '', + 'em_start_date' => strtotime( '+10 days 15:00:00' ), + 'em_end_date' => strtotime( '+10 days 16:00:00' ), + 'em_enable_booking' => 'bookings_off', + 'em_ticket_price' => 0, + ), + ); + + $this->mockup_venue = array( + 'name' => 'Test Venue', + 'address' => 'Fediverse-street 1337, 1234 Fediverse-town', + ); } /** @@ -82,23 +82,10 @@ class Test_EventPrime extends WP_UnitTestCase { // Enable ActivityPub support for the event plugin. $this->assertContains( 'em_event', get_option( 'activitypub_support_post_types' ) ); - $event_data = array(); - $event_data['name'] = 'EventPrime Event title'; - $event_data['description'] = 'EventPrime event description'; - $event_data['status'] = 'Publish'; - $event_data['em_event_type'] = ''; - $event_data['em_venue'] = ''; - $event_data['em_organizer'] = ''; - $event_data['em_performer'] = ''; - $event_data['em_start_date'] = strtotime( '+10 days 15:00:00' ); - $event_data['em_end_date'] = strtotime( '+10 days 16:00:00' ); - $event_data['em_enable_booking'] = 'bookings_off'; - $event_data['em_ticket_price'] = 0; - // Create an EventPrime Event without content. $ep_functions = new Eventprime_Basic_Functions(); - $post_id = $ep_functions->insert_event_post_data( $event_data ); + $post_id = $ep_functions->insert_event_post_data( $this->mockup_events['minimal_event'] ); $wp_object = get_post( $post_id ); @@ -108,4 +95,92 @@ class Test_EventPrime extends WP_UnitTestCase { // Check that we got the right transformer. $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\EventPrime::class, $transformer ); } + + /** + * Test transformation of minimal event. + */ + public function test_transformation_of_minimal_event() { + // Create an EventPrime Event without content. + $ep_functions = new Eventprime_Basic_Functions(); + + $post_id = $ep_functions->insert_event_post_data( $this->mockup_events['minimal_event'] ); + + $wp_object = get_post( $post_id ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'EventPrime Event title', $event_array['name'] ); + $this->assertEquals( 'EventPrime event description', wp_strip_all_tags( $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->assertTrue( $event_array['commentsEnabled'] ); + $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertArrayNotHasKey( 'location', $event_array ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + } + + /** + * Test transformation of minimal event. + */ + public function test_transformation_of_minimal_event_with_venue() { + // Create an EventPrime Event without content. + $ep_functions = new Eventprime_Basic_Functions(); + + $venue_term_id = wp_insert_term( $this->mockup_venue['name'], 'em_venue' )['term_id']; + add_term_meta( $venue_term_id, 'em_address', $this->mockup_venue['address'], true ); + add_term_meta( $venue_term_id, 'em_display_address_on_frontend', true, true ); + + $event_data = $this->mockup_events['minimal_event']; + $event_data['em_venue'] = $venue_term_id; + + $post_id = $ep_functions->insert_event_post_data( $event_data ); + + $wp_object = get_post( $post_id ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'EventPrime Event title', $event_array['name'] ); + $this->assertEquals( 'EventPrime event description', wp_strip_all_tags( $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->assertTrue( $event_array['commentsEnabled'] ); + $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertEquals( $this->mockup_venue['name'], $event_array['location']['name'] ); + $this->assertEquals( $this->mockup_venue['address'], $event_array['location']['address'] ); + + $this->assertEquals( 'MEETING', $event_array['category'] ); + } + + /** + * Test transformation of minimal event with venue which has a hidden address. + */ + public function test_transformation_of_minimal_event_with_venue_with_hidden_address() { + // Create an EventPrime Event without content. + $ep_functions = new Eventprime_Basic_Functions(); + + $venue_term_id = wp_insert_term( $this->mockup_venue['name'], 'em_venue' )['term_id']; + add_term_meta( $venue_term_id, 'em_address', $this->mockup_venue['address'], true ); + add_term_meta( $venue_term_id, 'em_display_address_on_frontend', false, true ); + + $event_data = $this->mockup_events['minimal_event']; + $event_data['em_venue'] = $venue_term_id; + + $post_id = $ep_functions->insert_event_post_data( $event_data ); + + $wp_object = get_post( $post_id ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertArrayNotHasKey( 'address', $event_array['location'] ); + } } -- 2.39.5 From c48fbecf6d2c71ec72e79205349205fba1717d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 26 Nov 2024 09:27:11 +0100 Subject: [PATCH 10/10] phpcs --- tests/test-class-plugin-eventprime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-class-plugin-eventprime.php b/tests/test-class-plugin-eventprime.php index c5b5bbc..354f6df 100644 --- a/tests/test-class-plugin-eventprime.php +++ b/tests/test-class-plugin-eventprime.php @@ -128,7 +128,7 @@ class Test_EventPrime extends WP_UnitTestCase { */ public function test_transformation_of_minimal_event_with_venue() { // Create an EventPrime Event without content. - $ep_functions = new Eventprime_Basic_Functions(); + $ep_functions = new Eventprime_Basic_Functions(); $venue_term_id = wp_insert_term( $this->mockup_venue['name'], 'em_venue' )['term_id']; add_term_meta( $venue_term_id, 'em_address', $this->mockup_venue['address'], true ); @@ -164,7 +164,7 @@ class Test_EventPrime extends WP_UnitTestCase { */ public function test_transformation_of_minimal_event_with_venue_with_hidden_address() { // Create an EventPrime Event without content. - $ep_functions = new Eventprime_Basic_Functions(); + $ep_functions = new Eventprime_Basic_Functions(); $venue_term_id = wp_insert_term( $this->mockup_venue['name'], 'em_venue' )['term_id']; add_term_meta( $venue_term_id, 'em_address', $this->mockup_venue['address'], true ); -- 2.39.5