From 04b0e69af6b92a30a4165dbaa9687a7f2b72f085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:23:07 +0200 Subject: [PATCH] wip draft how to do integration tests for event plugins --- .forgejo/workflows/phpunit.yml | 4 +- {tests => bin}/install-wp-tests.sh | 6 ++- .../transformer/class-the-events-calendar.php | 4 ++ includes/class-setup.php | 10 ++--- tests/bootstrap.php | 13 ++++-- tests/test-class-sample.php | 40 ++++++++++++++++++- 6 files changed, 63 insertions(+), 14 deletions(-) rename {tests => bin}/install-wp-tests.sh (98%) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 3c118eb..dfa957c 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -68,11 +68,11 @@ jobs: - name: Setup Test Environment if: steps.cache-wordpress.outputs.cache-hit != 'true' - run: bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false false false false + run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false false false false - name: Initialize WordPress test database if: steps.cache-wordpress.outputs.cache-hit != 'false' - run: bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false true true true + 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 diff --git a/tests/install-wp-tests.sh b/bin/install-wp-tests.sh similarity index 98% rename from tests/install-wp-tests.sh rename to bin/install-wp-tests.sh index 2bcf328..87f7f46 100755 --- a/tests/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -202,7 +202,9 @@ install_db() { install_wp_plugin() { PLUGIN_NAME=$1 - if [ -d "$WP_CORE_DIR/wp-content/mu-plugins/$PLUGIN_NAME" ]; then + mkdir -p "$WP_CORE_DIR/wp-content/plugins/" + + if [ -d "$WP_CORE_DIR/wp-content/plugins/$PLUGIN_NAME" ]; then return; fi @@ -217,7 +219,7 @@ install_wp_plugin() { fi # Unzip the plugin into the WordPress must-use plugins directory - unzip -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/mu-plugins/" + unzip -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/plugins/" } install_wp_plugins() { diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index e1b42fb..8001613 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -151,6 +151,10 @@ final class The_Events_Calendar extends Event { // We currently only support a single venue. $event_venue = $this->wp_object->venues[0]; + if ( is_null( $event_venue ) ) { + return null; + } + $address = array( 'addressCountry' => $event_venue->country, 'addressLocality' => $event_venue->city, diff --git a/includes/class-setup.php b/includes/class-setup.php index 2de709f..cbe6106 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -59,14 +59,14 @@ class Setup { * @since 1.0.0 */ protected function __construct() { - $this->activitypub_plugin_is_active = is_plugin_active( 'activitypub/activitypub.php' ); + $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ); // BeforeFirstRelease: decide whether we want to do anything at all when ActivityPub plugin is note active. // if ( ! $this->activitypub_plugin_is_active ) { // deactivate_plugins( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ); // return; // }. $this->active_event_plugins = self::detect_active_event_plugins(); - $this->activitypub_plugin_version = get_file_data( WP_PLUGIN_DIR . '/activitypub/activitypub.php', array( 'Version' ) )[0]; + $this->activitypub_plugin_version = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ? constant( 'ACTIVITYPUB_PLUGIN_VERSION' ): '0'; $this->setup_hooks(); } @@ -166,7 +166,7 @@ class Setup { ); // Check if the minimum required version of the ActivityPub plugin is installed. - if ( version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { + if ( ! version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { return; } @@ -256,8 +256,8 @@ class Setup { // If someone installs this plugin, we simply enable ActivityPub support for all currently active event post types. $activitypub_supported_post_types = get_option( 'activitypub_support_post_types', array() ); foreach ( $this->active_event_plugins as $event_plugin ) { - if ( ! in_array( $event_plugin['post_type'], $activitypub_supported_post_types, true ) ) { - $activitypub_supported_post_types[] = $event_plugin['post_type']; + if ( ! in_array( $event_plugin->get_post_type(), $activitypub_supported_post_types, true ) ) { + $activitypub_supported_post_types[] = $event_plugin->get_post_type(); } } update_option( 'activitypub_support_post_types', $activitypub_supported_post_types ); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c8397d0..e32baa7 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -26,15 +26,22 @@ if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { require_once "{$_tests_dir}/includes/functions.php"; /** - * Manually load the plugin being tested. + * Manually load the plugin being tested and its integrations. */ function _manually_load_plugin() { - require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; + $plugin_dir = ABSPATH . '/wp-content/plugins/'; + require_once $plugin_dir . 'activitypub/activitypub.php'; $event_plugin = 'the-events-calendar'; switch ( $event_plugin ) { case 'the-events-calendar': - require ABSPATH . '/wp-content/mu-plugins/the-events-calendar/the-events-calendar.php'; + $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 ); } + require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php index 0adab4f..8f9371c 100644 --- a/tests/test-class-sample.php +++ b/tests/test-class-sample.php @@ -28,9 +28,45 @@ class Test_Sample extends WP_UnitTestCase { // Get instance of our plugin. $aec = \Activitypub_Event_Extensions\Setup::get_instance(); - $this->assertContains( 'the-events-calendar', $aec->get_active_event_plugins() ); + // 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 = $aec->get_active_event_plugins(); + $this->assertEquals( 1, count( $active_event_plugins ) ); + + // Enable ActivityPub support for the event plugin. $aec->activate_activitypub_support_for_active_event_plugins(); - $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' )); + $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); + + $wp_object = tribe_events() + ->set_args( + array( + 'title' => 'My Event', + 'content' => 'Come to my event. Let\'s connect!', + 'start_date' => '+10 days 15:00:00', + 'duration' => HOUR_IN_SECONDS, + 'status' => 'publish', + ) + ) + ->create(); + + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + $event_array = $transformer->to_object()->to_array(); + + $this->assertArrayHasKey( 'type', $event_array ); + $this->assertEquals( 'Event', $event_array['type'] ); + + $this->assertEquals( 'My 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 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); + + $this->assertEquals( ) . 'T16:00:00Z', $event_array['commentsEnabled'] ); + + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); } }