From f0a4f251ea88252fe384a2a9164edeb07ad13f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 9 Oct 2024 18:18:25 +0200 Subject: [PATCH] add tests for wp event manager --- .forgejo/workflows/phpunit.yml | 7 +- bin/install-wp-tests.sh | 1 + tests/bootstrap.php | 3 + tests/test-class-plugin-wp-event-manager.php | 145 +++++++++++++++++++ 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 tests/test-class-plugin-wp-event-manager.php diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 5bdae8d..b67d8e9 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-4 + key: cache-wordpress-5 - name: Cache Composer id: cache-composer-phpunit @@ -91,5 +91,10 @@ jobs: - name: Run Integration tests for Events Manager run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=events_manager + env: + PHP_VERSION: ${{ matrix.php-version }} + + - name: Run Integration tests for WP Event Manager + run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=wp_event_manager env: PHP_VERSION: ${{ matrix.php-version }} \ No newline at end of file diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 8d495bf..0e0e4ad 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -238,6 +238,7 @@ install_wp_plugins() { install_wp_plugin very-simple-event-list install_wp_plugin gatherpress install_wp_plugin events-manager + install_wp_plugin wp-event-manager } install_wp diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f17b5f9..359543a 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -58,6 +58,9 @@ function _manually_load_plugin() { case 'gatherpress': $plugin_file = 'gatherpress/gatherpress.php'; break; + case 'wp_event_manager': + $plugin_file = 'wp-event-manager/wp-event-manager.php'; + break; } if ( $plugin_file ) { diff --git a/tests/test-class-plugin-wp-event-manager.php b/tests/test-class-plugin-wp-event-manager.php new file mode 100644 index 0000000..62edc5a --- /dev/null +++ b/tests/test-class-plugin-wp-event-manager.php @@ -0,0 +1,145 @@ +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_Bridge\Setup::get_instance()->get_active_event_plugins(); + $this->assertEquals( 1, count( $active_event_plugins ) ); + + // Enable ActivityPub support for the event plugin. + $this->assertContains( 'event_listing', get_option( 'activitypub_support_post_types' ) ); + + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'WP Event Manager TestEvent', + 'post_status' => 'published', + 'post_type' => 'event_listing', + '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_Bridge\Activitypub\Transformer\WP_Event_Manager::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' => 'WP Event Manager TestEvent', + 'post_status' => 'published', + 'post_type' => 'event_listing', + 'post_content' => 'Come to my WP Event Manager 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( 'WP Event Manager TestEvent', $event_array['name'] ); + $this->assertEquals( 'Come to my WP Event Manager event!', wp_strip_all_tags( $event_array['content'] ) ); + $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_online_event() { + // Insert a new Event. + $wp_post_id = wp_insert_post( + array( + 'post_title' => 'WP Event Manager TestEvent', + 'post_status' => 'published', + 'post_type' => 'event_listing', + 'post_content' => 'Come to my WP Event Manager event!', + 'meta_input' => array( + 'event-start-date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 15:00:00' ) ), + 'event-date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 16:00:00' ) ), + '_event_video_url' => 'https://event-federation.eu/meeting-room', + '_event_online' => '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->assertEquals( 'Event', $event_array['type'] ); + $this->assertEquals( 'WP Event Manager TestEvent', $event_array['name'] ); + $this->assertEquals( 'Come to my WP Event Manager event!', 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 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( true, $event_array['isOnline'] ); + $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' => 'Video Link', + 'href' => 'https://event-federation.eu/meeting-room', + 'mediaType' => 'text/html', + ), + $event_array['attachment'] + ); + } +}