From 5dfd6806eb7355a72c25ca2acb68c6baa38c7c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 17:37:44 +0200 Subject: [PATCH 01/13] add test class for vs event llist --- tests/test-class-plugin-vs-event-list.php | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test-class-plugin-vs-event-list.php diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list.php new file mode 100644 index 0000000..df669f2 --- /dev/null +++ b/tests/test-class-plugin-vs-event-list.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( 'event', get_option( 'activitypub_support_post_types' ) ); + + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + '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\VS_Event_List::class, $transformer ); + } +} -- 2.39.5 From 5f6f02cc8141a2bc7411529d99c07c04c52a97d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 19:05:21 +0200 Subject: [PATCH 02/13] add multiple phpunit runs with filters for each event plugin integration --- .forgejo/workflows/phpunit.yml | 11 +++-- bin/install-wp-tests.sh | 20 ++++---- composer.json | 18 +++++-- docker-compose.yml | 6 +-- tests/bootstrap.php | 47 +++++++++++++++---- .../test-class-plugin-the-events-calendar.php | 2 +- 6 files changed, 75 insertions(+), 29 deletions(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 23321d9..f4a03dd 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -37,7 +37,7 @@ jobs: path: | ${{ env.WP_CORE_DIR }} ${{ env.WP_TESTS_DIR }} - key: cache-wordpress-3 + key: cache-wordpress-4 - name: Cache Composer id: cache-composer-phpunit @@ -74,7 +74,12 @@ jobs: if: steps.cache-wordpress.outputs.cache-hit != 'false' run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false true true true - - name: Run PHPUnit - run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit + - name: Run Integration tests for The Events Calendar + run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=the_events_calendar + env: + PHP_VERSION: ${{ matrix.php-version }} + + - name: Run Integration tests for VS Event List + run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=vs_event_list env: PHP_VERSION: ${{ matrix.php-version }} diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index ff43d3a..8d495bf 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -209,7 +209,12 @@ install_wp_plugin() { # Get the latest tag. LATEST_TAG=$(svn log https://plugins.svn.wordpress.org/$PLUGIN_NAME/tags --limit 1 | awk 'NR == 4 { print $4 }') - PLUGIN_FILE="$PLUGIN_NAME.$LATEST_TAG.zip" + if [ -n "$LATEST_TAG" ]; then + PLUGIN_FILE="$PLUGIN_NAME.$LATEST_TAG.zip" + else + PLUGIN_FILE="$PLUGIN_NAME.zip" + fi + URL="https://downloads.wordpress.org/plugin/$PLUGIN_FILE" # Check if the plugin file already exists @@ -226,16 +231,13 @@ install_wp_plugins() { echo "Skipping WordPress plugin installation." return 0 fi - # Always install the ActivityPub plugin. + # Install the one and only ActivityPub plugin (greetings @pfefferle). install_wp_plugin activitypub + # Install (not-activate) all supported event plugins. install_wp_plugin the-events-calendar - # Install additional plugins. - # if [[ -n "$PLUGINS" ]]; then - # IFS=',' read -ra PLUGIN_ARRAY <<< "$PLUGINS" - # for plugin in "${PLUGIN_ARRAY[@]}"; do - # install_wp_plugin "$plugin" - # done - # fi + install_wp_plugin very-simple-event-list + install_wp_plugin gatherpress + install_wp_plugin events-manager } install_wp diff --git a/composer.json b/composer.json index ba5b52a..d4048d0 100644 --- a/composer.json +++ b/composer.json @@ -41,10 +41,20 @@ "lint:fix": [ "vendor/bin/phpcbf" ], - "test": [ + "prepare-test": [ "composer install", - "bin/install-wp-tests.sh wordpress-test root wordpress-test test-db latest true", - "phpunit" - ] + "bin/install-wp-tests.sh wordpress-test root wordpress-test test-db latest true" + ], + "test": [ + "@prepare-test", + "@test-vs-event-list", + "@test-the-events-calendar", + "@test-gatherpress", + "@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" } } diff --git a/docker-compose.yml b/docker-compose.yml index 47d5117..a1d0658 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,10 +26,10 @@ services: MARIADB_ROOT_PASSWORD: wordpress-test healthcheck: test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] - start_period: 5s - interval: 4s + start_period: 2s + interval: 1s timeout: 5s - retries: 5 + retries: 10 test-php: build: diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e32baa7..c586a84 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,17 +30,46 @@ require_once "{$_tests_dir}/includes/functions.php"; */ function _manually_load_plugin() { $plugin_dir = ABSPATH . '/wp-content/plugins/'; + + // Always manually load the ActivityPub plugin. require_once $plugin_dir . 'activitypub/activitypub.php'; - $event_plugin = 'the-events-calendar'; - switch ( $event_plugin ) { - case 'the-events-calendar': - $plugin_file = 'the-events-calendar/the-events-calendar.php'; - require_once $plugin_dir . $plugin_file; - $current = get_option( 'active_plugins', array() ); - $current[] = $plugin_file; - sort( $current ); - update_option( 'active_plugins', $current ); + + // Capture the --filter argument. + $activitypub_event_extension_integration_filter = null; + foreach ( $_SERVER['argv'] as $arg ) { + if ( strpos( $arg, '--filter=' ) === 0 ) { + $activitypub_event_extension_integration_filter = substr( $arg, strlen( '--filter=' ) ); + break; + } } + + $plugin_file = null; + // See if we want to run integration tests for a specific event-plugin. + switch ( $activitypub_event_extension_integration_filter ) { + case 'the_events_calendar': + $plugin_file = 'the-events-calendar/the-events-calendar.php'; + break; + case 'vs_event_list': + $plugin_file = 'very-simple-event-list/vsel.php'; + break; + case 'events_manager': + $plugin_file = 'events-manager/events-manager.php'; + break; + case 'gatherpress': + $plugin_file = 'gatherpress/gatherpress.php'; + break; + } + + if ( $plugin_file ) { + // Manually load the event plugin. + require_once $plugin_dir . $plugin_file; + $current = get_option( 'active_plugins', array() ); + $current[] = $plugin_file; + sort( $current ); + update_option( 'active_plugins', $current ); + } + + // At last manually load our WordPress plugin. require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; } diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 3f161f7..424a420 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -72,7 +72,7 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { /** * Test that the right transformer gets applied. */ - public function test_transformer_class() { + 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. -- 2.39.5 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 03/13] 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 -- 2.39.5 From 253d3e90101fb8a9e906ac45f464b3edaf36bc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 20:34:19 +0200 Subject: [PATCH 04/13] lint files --- .forgejo/workflows/phpunit.yml | 4 ++-- includes/activitypub/transformer/class-gatherpress.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index e8810ce..a576ee2 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -84,12 +84,12 @@ jobs: env: PHP_VERSION: ${{ matrix.php-version }} - - name: Run Integration tests for GatherPress + - 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 + - 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/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index 2d700d6..ab176c4 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -92,7 +92,6 @@ final class GatherPress extends Event { } else { return null; } - } /** -- 2.39.5 From ef3389e64dca94aff4396e75516f6440bd56f526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 09:37:50 +0200 Subject: [PATCH 05/13] add basic event transformation test for Events Manager --- .../transformer/class-events-manager.php | 9 +- tests/bootstrap.php | 8 ++ tests/test-class-plugin-events-manger.php | 94 +++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 487181c..f4c8a01 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -94,16 +94,21 @@ final class Events_Manager extends Event_Transformer { return null; } - $location = new Place(); $em_location = $this->em_event->get_location(); + if ( '' === $em_location->location_id ) { + return null; + } + + $location = new Place(); $location->set_name( $em_location->location_name ); $address = array( 'type' => 'PostalAddress', 'addressCountry' => $em_location->location_country, 'addressLocality' => $em_location->location_town, - 'streetAddress' => $em_location->location_address, + 'postalAddress' => $em_location->location_address, + 'postalCode' => $em_location->location_postcode, 'name' => $em_location->location_name, ); if ( $em_location->location_state ) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c586a84..de49112 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -69,6 +69,14 @@ function _manually_load_plugin() { update_option( 'active_plugins', $current ); } + // Hot fix that allows using Events Manager within unit tests, because the em_init() is later not run as admin. + if ( 'events_manager' === $activitypub_event_extension_integration_filter ) { + require_once $plugin_dir . 'events-manager/em-install.php'; + em_create_events_table(); + em_create_events_meta_table(); + em_create_locations_table(); + } + // At last manually load our WordPress plugin. require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; } diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php index 2037866..7ee8195 100644 --- a/tests/test-class-plugin-events-manger.php +++ b/tests/test-class-plugin-events-manger.php @@ -19,6 +19,9 @@ class Test_Events_Manager extends WP_UnitTestCase { self::markTestSkipped( 'VS Event List plugin is not active.' ); } + // For tests allow every user to create new events. + update_option( 'dbem_events_anonymous_submissions', true ); + // Make sure that ActivityPub support is enabled for Events Manager. $aec = \Activitypub_Event_Extensions\Setup::get_instance(); $aec->activate_activitypub_support_for_active_event_plugins(); @@ -60,4 +63,95 @@ class Test_Events_Manager extends WP_UnitTestCase { // Check that we got the right transformer. $this->assertInstanceOf( \Activitypub_Event_Extensions\Activitypub\Transformer\Events_Manager::class, $transformer ); } + + /** + * Test the transformation of a minimal event. + */ + public function test_transform_of_minimal_event() { + // Create mockup event. + $event = new EM_Event(); + $event->event_name = 'Events Manager Test event'; + $event->post_content = 'Event description'; + $event->event_start_date = gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ); + $event->event_end_date = gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ); + $event->event_start_time = '15:00:00'; + $event->event_end_time = '16:00:00'; + $event->start = strtotime( $event->event_start_date . ' ' . $event->event_start_time ); + $event->end = strtotime( $event->event_end_date . ' ' . $event->event_end_time ); + $event->force_status = 'published'; + $event->event_rsvp = false; + // Save the Event. + $event->save(); + // Insert a new Event. + + $post = get_post( $event->post_id ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $post )->to_object()->to_array(); + + // Check that we got the right transformer. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'Events Manager Test event', $event_array['name'] ); + $this->assertEquals( '', $event_array['content'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); + // $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 the transformation of a minimal event. + */ + public function test_transform_of_event_with_location() { + // Create a mockup location. + $location = new EM_Location(); + $location->location_name = 'Test location'; + $location->location_address = 'Test Address'; + $location->location_town = 'Test Town'; + $location->location_state = 'Test state'; + $location->location_postcode = '1337'; + $location->location_region = 'Test region'; + $location->location_country = 'AT'; // Must be a two char country code. + $location->save(); + // Create mockup event. + $event = new EM_Event(); + $event->event_name = 'Events Manager Test event'; + $event->post_content = 'Event description'; + $event->location_id = $location->location_id; + $event->event_start_date = gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ); + $event->event_end_date = gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ); + $event->event_start_time = '15:00:00'; + $event->event_end_time = '16:00:00'; + $event->start = strtotime( $event->event_start_date . ' ' . $event->event_start_time ); + $event->end = strtotime( $event->event_end_date . ' ' . $event->event_end_time ); + $event->force_status = 'published'; + $event->event_rsvp = false; + // Save the Event. + $event->save(); + // Insert a new Event. + + $post = get_post( $event->post_id ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $post )->to_object()->to_array(); + + // Check that we got the right transformer. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'Events Manager Test event', $event_array['name'] ); + $this->assertEquals( '', $event_array['content'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); + // $this->assertTrue( $event_array['commentsEnabled'] ); + // $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + $this->assertArrayHasKey( 'location', $event_array ); + $this->assertEquals( 'Test location', $event_array['location']['name'] ); + $this->assertEquals( 'Test Address', $event_array['location']['address']['postalAddress'] ); + $this->assertEquals( 'Test Town', $event_array['location']['address']['addressLocality'] ); + $this->assertEquals( 'Test state', $event_array['location']['address']['addressRegion'] ); + $this->assertEquals( '1337', $event_array['location']['address']['postalCode'] ); + $this->assertEquals( 'AT', $event_array['location']['address']['addressCountry'] ); + } } -- 2.39.5 From 356a96adb63ee191ad7b2f66b28bcdf68a7b0389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 09:43:13 +0200 Subject: [PATCH 06/13] Add default function for whether comments are allowed --- includes/activitypub/transformer/class-event.php | 2 +- tests/test-class-plugin-events-manger.php | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index 994f119..f796e14 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -54,7 +54,7 @@ class Event extends Post { * Get a sane default for whether comments are enabled. */ protected function get_comments_enabled() { - 'open'; + return comments_open( $this->wp_object ); } /** diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php index 7ee8195..9384664 100644 --- a/tests/test-class-plugin-events-manger.php +++ b/tests/test-class-plugin-events-manger.php @@ -94,8 +94,8 @@ class Test_Events_Manager extends WP_UnitTestCase { $this->assertEquals( 'Events Manager Test event', $event_array['name'] ); $this->assertEquals( '', $event_array['content'] ); $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); - // $this->assertTrue( $event_array['commentsEnabled'] ); - // $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); + $this->assertEquals( comments_open( $event->post_id ), $event_array['commentsEnabled'] ); + $this->assertEquals( comments_open( $event->post_id )? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); $this->assertEquals( 'external', $event_array['joinMode'] ); $this->assertArrayNotHasKey( 'location', $event_array ); $this->assertEquals( 'MEETING', $event_array['category'] ); @@ -142,8 +142,6 @@ class Test_Events_Manager extends WP_UnitTestCase { $this->assertEquals( 'Events Manager Test event', $event_array['name'] ); $this->assertEquals( '', $event_array['content'] ); $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); - // $this->assertTrue( $event_array['commentsEnabled'] ); - // $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); $this->assertEquals( 'external', $event_array['joinMode'] ); $this->assertEquals( 'MEETING', $event_array['category'] ); $this->assertArrayHasKey( 'location', $event_array ); -- 2.39.5 From 1873b940f83f1fd1cfd836069ccbf30cb5186a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 09:44:32 +0200 Subject: [PATCH 07/13] phpcs --- includes/activitypub/transformer/class-events-manager.php | 2 +- tests/test-class-plugin-events-manger.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index f4c8a01..4b1b198 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -108,7 +108,7 @@ final class Events_Manager extends Event_Transformer { 'addressCountry' => $em_location->location_country, 'addressLocality' => $em_location->location_town, 'postalAddress' => $em_location->location_address, - 'postalCode' => $em_location->location_postcode, + 'postalCode' => $em_location->location_postcode, 'name' => $em_location->location_name, ); if ( $em_location->location_state ) { diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php index 9384664..2425455 100644 --- a/tests/test-class-plugin-events-manger.php +++ b/tests/test-class-plugin-events-manger.php @@ -95,7 +95,7 @@ class Test_Events_Manager extends WP_UnitTestCase { $this->assertEquals( '', $event_array['content'] ); $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); $this->assertEquals( comments_open( $event->post_id ), $event_array['commentsEnabled'] ); - $this->assertEquals( comments_open( $event->post_id )? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); + $this->assertEquals( comments_open( $event->post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); $this->assertEquals( 'external', $event_array['joinMode'] ); $this->assertArrayNotHasKey( 'location', $event_array ); $this->assertEquals( 'MEETING', $event_array['category'] ); -- 2.39.5 From c7e5acdccb533496e66d0beac795e593cb5a1167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 10:10:15 +0200 Subject: [PATCH 08/13] add more tests for Events Manager --- tests/test-class-plugin-events-manger.php | 65 ++++++++++++++----- .../test-class-plugin-the-events-calendar.php | 2 +- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php index 2425455..cf4bc46 100644 --- a/tests/test-class-plugin-events-manger.php +++ b/tests/test-class-plugin-events-manger.php @@ -73,21 +73,14 @@ class Test_Events_Manager extends WP_UnitTestCase { $event->event_name = 'Events Manager Test event'; $event->post_content = 'Event description'; $event->event_start_date = gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ); - $event->event_end_date = gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ); $event->event_start_time = '15:00:00'; - $event->event_end_time = '16:00:00'; $event->start = strtotime( $event->event_start_date . ' ' . $event->event_start_time ); - $event->end = strtotime( $event->event_end_date . ' ' . $event->event_end_time ); $event->force_status = 'published'; $event->event_rsvp = false; - // Save the Event. - $event->save(); - // Insert a new Event. - - $post = get_post( $event->post_id ); + $this->assertTrue( $event->save() ); // Call the transformer Factory. - $event_array = \Activitypub\Transformer\Factory::get_transformer( $post )->to_object()->to_array(); + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $event->post_id ) )->to_object()->to_array(); // Check that we got the right transformer. $this->assertEquals( 'Event', $event_array['type'] ); @@ -98,13 +91,14 @@ class Test_Events_Manager extends WP_UnitTestCase { $this->assertEquals( comments_open( $event->post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); $this->assertEquals( 'external', $event_array['joinMode'] ); $this->assertArrayNotHasKey( 'location', $event_array ); + $this->assertArrayNotHasKey( 'endTime', $event_array ); $this->assertEquals( 'MEETING', $event_array['category'] ); } /** - * Test the transformation of a minimal event. + * Test the transformation of a event with full location. */ - public function test_transform_of_event_with_location() { + public function test_transform_of__full_event_with_location() { // Create a mockup location. $location = new EM_Location(); $location->location_name = 'Test location'; @@ -114,7 +108,8 @@ class Test_Events_Manager extends WP_UnitTestCase { $location->location_postcode = '1337'; $location->location_region = 'Test region'; $location->location_country = 'AT'; // Must be a two char country code. - $location->save(); + $this->assertTrue( $location->save() ); + // Create mockup event. $event = new EM_Event(); $event->event_name = 'Events Manager Test event'; @@ -128,14 +123,10 @@ class Test_Events_Manager extends WP_UnitTestCase { $event->end = strtotime( $event->event_end_date . ' ' . $event->event_end_time ); $event->force_status = 'published'; $event->event_rsvp = false; - // Save the Event. - $event->save(); - // Insert a new Event. - - $post = get_post( $event->post_id ); + $this->assertTrue( $event->save() ); // Call the transformer Factory. - $event_array = \Activitypub\Transformer\Factory::get_transformer( $post )->to_object()->to_array(); + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $event->post_id ) )->to_object()->to_array(); // Check that we got the right transformer. $this->assertEquals( 'Event', $event_array['type'] ); @@ -152,4 +143,42 @@ class Test_Events_Manager extends WP_UnitTestCase { $this->assertEquals( '1337', $event_array['location']['address']['postalCode'] ); $this->assertEquals( 'AT', $event_array['location']['address']['addressCountry'] ); } + + /** + * Test the transformation of a minimal event. + */ + public function test_transform_of_event_with_name_only_location() { + // Create a mockup location. + $location = new EM_Location(); + $location->location_name = 'Name only location'; + $this->assertTrue( $location->save() ); + + // Create mockup event. + $event = new EM_Event(); + $event->event_name = 'Events Manager Test event'; + $event->post_content = 'Event description'; + $event->location_id = $location->location_id; + $event->event_start_date = gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ); + $event->event_end_date = gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ); + $event->event_start_time = '15:00:00'; + $event->event_end_time = '16:00:00'; + $event->start = strtotime( $event->event_start_date . ' ' . $event->event_start_time ); + $event->end = strtotime( $event->event_end_date . ' ' . $event->event_end_time ); + $event->force_status = 'published'; + $event->event_rsvp = false; + $this->assertTrue( $event->save() ); + + // Call the transformer Factory. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $event->post_id ) )->to_object()->to_array(); + + // Check that we got the right transformer. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'Events Manager Test event', $event_array['name'] ); + $this->assertEquals( '', $event_array['content'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + $this->assertArrayHasKey( 'location', $event_array ); + $this->assertEquals( 'Name only location', $event_array['location']['name'] ); + } } diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 424a420..c4f5f6d 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -180,7 +180,7 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { } /** - * Test transformation of minimal event with minimal venue. + * Test transformation of minimal event with fully filled venue. */ public function test_transform_of_minimal_event_with_address_venue() { // Create Venue. -- 2.39.5 From cadb01a4884969e5b2a6e9af8cc4be370e193066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 10:57:36 +0200 Subject: [PATCH 09/13] improve transformer for vs event list --- composer.json | 2 +- .../activitypub/transformer/class-event.php | 18 +- .../transformer/class-vs-event-list.php | 53 +++--- .../test-class-plugin-vs-event-list copy.php | 63 ------- tests/test-class-plugin-vs-event-list.php | 166 ++++++++++++++++++ 5 files changed, 201 insertions(+), 101 deletions(-) delete mode 100644 tests/test-class-plugin-vs-event-list copy.php create mode 100644 tests/test-class-plugin-vs-event-list.php diff --git a/composer.json b/composer.json index ee97807..d091876 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,7 @@ ], "test-debug": [ "@prepare-test", - "@test-events-manager" + "@test-vs-event-list" ], "test-vs-event-list": "phpunit --filter=vs_event_list", "test-the-events-calendar": "phpunit --filter=the_events_calendar", diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index f796e14..4dc96e1 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -35,7 +35,7 @@ class Event extends Post { * * @return string The User-URL. */ - protected function get_actor() { + protected function get_actor(): ?string { return $this->get_attributed_to(); } @@ -46,14 +46,14 @@ class Event extends Post { * * @return string The Event Object-Type. */ - protected function get_type() { + protected function get_type(): string { return 'Event'; } /** * Get a sane default for whether comments are enabled. */ - protected function get_comments_enabled() { + protected function get_comments_enabled(): ?bool { return comments_open( $this->wp_object ); } @@ -64,7 +64,7 @@ class Event extends Post { * * @return string The name. */ - protected function get_name() { + protected function get_name(): string { return $this->wp_object->post_title; } @@ -86,7 +86,7 @@ class Event extends Post { * * @return string */ - public function get_join_mode() { + public function get_join_mode(): ?string { return 'external'; } @@ -96,16 +96,16 @@ class Event extends Post { * Currently we don't handle joins, we always mark events as external. * We just link back to the events HTML representation on our WordPress site. * - * @return string|null The external participation URL. + * @return ?string The external participation URL. */ - public function get_external_participation_url(): string|null { + public function get_external_participation_url(): ?string { return 'external' === $this->get_join_mode() ? $this->get_url() : null; } /** * Set the event category, via the mapping setting. */ - public function get_category() { + public function get_category(): ?string { $current_category_mapping = \get_option( 'activitypub_event_extensions_event_category_mappings', array() ); $terms = \get_the_terms( $this->wp_object, $this->wp_taxonomy ); @@ -123,7 +123,7 @@ class Event extends Post { * * @return Event_Object */ - public function to_object() { + public function to_object(): Event_Object { $activitypub_object = new Event_Object(); $activitypub_object = $this->transform_object_properties( $activitypub_object ); diff --git a/includes/activitypub/transformer/class-vs-event-list.php b/includes/activitypub/transformer/class-vs-event-list.php index 2f0cd56..f6f9b61 100644 --- a/includes/activitypub/transformer/class-vs-event-list.php +++ b/includes/activitypub/transformer/class-vs-event-list.php @@ -27,7 +27,6 @@ if ( ! defined( 'ABSPATH' ) ) { * @since 1.0.0 */ final class VS_Event_List extends Event_Transformer { - /** * The target transformer ActivityPub Event object. * @@ -35,19 +34,6 @@ final class VS_Event_List extends Event_Transformer { */ protected $ap_object; - /** - * Get transformer name. - * - * Retrieve the transformers name. - * - * @since 1.0.0 - * @access public - * @return string Widget name. - */ - public function get_transformer_name(): string { - return 'activitypub-event-transformers/vs-event'; - } - /** * Returns the ActivityStreams 2.0 Object-Type for an Event. * @@ -64,21 +50,28 @@ final class VS_Event_List extends Event_Transformer { * * @return Place The Place. */ - public function get_location(): Place { + public function get_location(): ?Place { $address = get_post_meta( $this->wp_object->ID, 'event-location', true ); - $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; + } } /** * Get the end time from the events metadata. */ - protected function get_end_time(): string { + protected function get_end_time(): ?string { + if ( 'yes' === get_post_meta( $this->wp_object->ID, 'event-hide-end-time', true ) ) { + return null; + } $end_time = get_post_meta( $this->wp_object->ID, 'event-date', true ); - return \gmdate( 'Y-m-d\TH:i:s\Z', $end_time ); + return $end_time ? \gmdate( 'Y-m-d\TH:i:s\Z', $end_time ) : null; } /** @@ -86,28 +79,32 @@ final class VS_Event_List extends Event_Transformer { */ protected function get_start_time(): string { $start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true ); - return \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ); + return $start_time ? \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ) : null; } /** * Get the event link from the events metadata. + * + * @return ?array Associated array of an ActivityStreams Link object with the events URL. */ - private function get_event_link(): array { - $event_link = get_post_meta( $this->wp_object->ID, 'event-link', true ); + private function get_event_link(): ?array { + $event_link = get_post_meta( $this->wp_object->ID, 'event-link', true ); + $event_link_label = get_post_meta( $this->wp_object->ID, 'event-link-label', true ) ?? 'Event Link'; if ( $event_link ) { return array( 'type' => 'Link', - 'name' => 'Website', + 'name' => $event_link_label, 'href' => \esc_url( $event_link ), 'mediaType' => 'text/html', ); } + return null; } /** * 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'; @@ -128,7 +125,7 @@ final class VS_Event_List extends Event_Transformer { * * @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 ) ) { diff --git a/tests/test-class-plugin-vs-event-list copy.php b/tests/test-class-plugin-vs-event-list copy.php deleted file mode 100644 index df669f2..0000000 --- a/tests/test-class-plugin-vs-event-list copy.php +++ /dev/null @@ -1,63 +0,0 @@ -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( 'event', get_option( 'activitypub_support_post_types' ) ); - - // Insert a new Event. - $wp_post_id = wp_insert_post( - array( - 'post_title' => 'VSEL Test Event', - 'post_status' => 'published', - '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\VS_Event_List::class, $transformer ); - } -} diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list.php new file mode 100644 index 0000000..7321887 --- /dev/null +++ b/tests/test-class-plugin-vs-event-list.php @@ -0,0 +1,166 @@ +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( 'event', get_option( 'activitypub_support_post_types' ) ); + + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + 'post_type' => 'event', + 'meta_input' => array( + 'event-start-date' => 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\VS_Event_List::class, $transformer ); + } + + /** + * Test the transformation to ActivityStreams of minimal event. + */ + public function test_transform_of_minimal_event() { + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + 'post_type' => 'event', + 'meta_input' => array( + 'event-start-date' => strtotime( '+10 days 15:00:00' ), + ), + ) + ); + + // Transform the event to ActivityStreams. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'VSEL Test Event', $event_array['name'] ); + $this->assertEquals( '', $event_array['content'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] ); + $this->assertArrayNotHasKey( 'endTime', $event_array ); + $this->assertEquals( comments_open( $wp_post_id ), $event_array['commentsEnabled'] ); + $this->assertEquals( comments_open( $wp_post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertEquals( esc_url( get_permalink( $wp_post_id ) ), $event_array['externalParticipationUrl'] ); + $this->assertArrayNotHasKey( 'location', $event_array ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + } + + /** + * Test the transformation to ActivityStreams of minimal event. + */ + public function test_transform_of_full_event() { + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + 'post_type' => 'event', + 'meta_input' => array( + 'event-start-date' => strtotime( '+10 days 15:00:00' ), + 'event-date' => strtotime( '+10 days 16:00:00' ), + 'event-link' => 'https://event-federation.eu/vsel-test-event', + 'event-link-label' => 'Website', + ), + ) + ); + + // Transform the event to ActivityStreams. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'VSEL Test Event', $event_array['name'] ); + $this->assertEquals( '', $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 15:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); + $this->assertEquals( comments_open( $wp_post_id ), $event_array['commentsEnabled'] ); + $this->assertEquals( comments_open( $wp_post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'external', $event_array['joinMode'] ); + $this->assertEquals( esc_url( get_permalink( $wp_post_id ) ), $event_array['externalParticipationUrl'] ); + $this->assertArrayNotHasKey( 'location', $event_array ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + $this->assertContains( + array( + 'type' => 'Link', + 'name' => 'Website', + 'href' => 'https://event-federation.eu/vsel-test-event', + 'mediaType' => 'text/html', + ), + $event_array['attachment'] + ); + } + + /** + * Test the transformation to ActivityStreams of event with hidden end time. + */ + public function test_transform_of_event_with_hidden_end_time() { + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + 'post_type' => 'event', + 'meta_input' => array( + 'event-start-date' => strtotime( '+10 days 15:00:00' ), + 'event-date' => strtotime( '+10 days 16:00:00' ), + 'event-hide-end-time' => 'yes', + ), + ) + ); + + // Transform the event to ActivityStreams. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $this->assertArrayNotHasKey( 'endTime', $event_array ); + } +} -- 2.39.5 From 8391b1dbc2c992ce1daa6b2623e0b50db755a1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 11:03:05 +0200 Subject: [PATCH 10/13] fix return type of to_object of final event transformers --- includes/activitypub/transformer/class-events-manager.php | 2 +- includes/activitypub/transformer/class-gatherpress.php | 2 +- includes/activitypub/transformer/class-the-events-calendar.php | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 4b1b198..5fed600 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -264,7 +264,7 @@ final class Events_Manager extends Event_Transformer { * * @return Activitypub\Activity\Event */ - public function to_object() { + public function to_object(): Event { $this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' ); $activitypub_object = new Event(); diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index ab176c4..1bd9081 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -199,7 +199,7 @@ final class GatherPress extends Event { * * @return Activitypub\Activity\Event */ - public function to_object() { + public function to_object(): Event_Object { $activitypub_object = parent::to_object(); return $activitypub_object; diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index 6881271..7707ca8 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -14,6 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) { use Activitypub_Event_Extensions\Activitypub\Transformer\Event; use Activitypub\Activity\Extended_Object\Place; +use Activitypub\Activity\Extended_Object\Event as Event_Object; use WP_Error; use WP_Post; @@ -194,7 +195,7 @@ final class The_Events_Calendar extends Event { * * @return Event_Object */ - public function to_object() { + public function to_object(): Event_Object { $activitypub_object = parent::to_object(); return $activitypub_object; -- 2.39.5 From 61eaa01fc6c7a77d0811520b8c979b81ec941dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 11:09:12 +0200 Subject: [PATCH 11/13] fix return type of get_name for Events Manager transformer --- includes/activitypub/transformer/class-events-manager.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 5fed600..3e95fe0 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -254,8 +254,10 @@ final class Events_Manager extends Event_Transformer { /** * Get the events title/name. + * + * @return string */ - protected function get_name() { + protected function get_name(): string { return $this->em_event->event_name; } -- 2.39.5 From b361295577172290c46677b4dd4697acd0536cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 11:13:45 +0200 Subject: [PATCH 12/13] add test for mapped event categories for VS Event List --- tests/test-class-plugin-vs-event-list.php | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list.php index 7321887..6145691 100644 --- a/tests/test-class-plugin-vs-event-list.php +++ b/tests/test-class-plugin-vs-event-list.php @@ -163,4 +163,49 @@ class Test_VS_Event_List extends WP_UnitTestCase { // Check that the event ActivityStreams representation contains everything as expected. $this->assertArrayNotHasKey( 'endTime', $event_array ); } + + /** + * Test transformation of event with mapped category. + */ + public function test_transform_event_with_mapped_categories() { + // Create category. + $category_id_music = wp_insert_term( 'Music', 'event_cat', array( 'slug' => 'music' ) ); + $category_id_theatre = wp_insert_term( 'Theatre', 'event_cat', array( 'slug' => 'theatre' ) ); + + // Set default mapping for event categories. + update_option( 'activitypub_event_extensions_default_event_category', 'MUSIC' ); + + // Set an override for the category with the slug theatre. + update_option( 'activitypub_event_extensions_event_category_mappings', array( 'theatre' => 'THEATRE' ) ); + + // Create a VS Event List event with the music category. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'VSEL Test Event', + 'post_status' => 'published', + 'post_type' => 'event', + 'meta_input' => array( + 'event-start-date' => strtotime( '+10 days 15:00:00' ), + 'event-date' => strtotime( '+10 days 16:00:00' ), + 'event-hide-end-time' => 'yes', + ), + ) + ); + wp_set_post_terms( $wp_post_id, $category_id_music['term_id'], 'event_cat' ); + + // Call the transformer. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + + // See if the default category mapping is applied. + $this->assertEquals( 'MUSIC', $event_array['category'] ); + + // Change the event category to theatre. + wp_set_post_terms( $wp_post_id, $category_id_theatre['term_id'], 'event_cat', false ); + + // Call the transformer. + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + + // See if the default category mapping is applied. + $this->assertEquals( 'THEATRE', $event_array['category'] ); + } } -- 2.39.5 From b8bf5a28b32d7fb8d5863676e906647ba068d498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 25 Sep 2024 11:16:41 +0200 Subject: [PATCH 13/13] phpcs --- tests/test-class-plugin-vs-event-list.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list.php index 6145691..98d5830 100644 --- a/tests/test-class-plugin-vs-event-list.php +++ b/tests/test-class-plugin-vs-event-list.php @@ -194,7 +194,7 @@ class Test_VS_Event_List extends WP_UnitTestCase { wp_set_post_terms( $wp_post_id, $category_id_music['term_id'], 'event_cat' ); // Call the transformer. - $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); + $event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array(); // See if the default category mapping is applied. $this->assertEquals( 'MUSIC', $event_array['category'] ); -- 2.39.5