From 8d2c8b2d358a3a1bb544ec17bef9f15e5fc9b466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 23 Sep 2024 20:07:36 +0200 Subject: [PATCH 01/17] install event plugins in ci --- .forgejo/workflows/phpunit.yml | 1 + tests/bootstrap.php | 5 ++++ tests/install-wp-tests.sh | 46 ++++++++++++++++++++++++++++++---- tests/test-class-sample.php | 9 +++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 7cd24a9..3c118eb 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - improve_tests pull_request: env: diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7d30eb9..c5bd5d3 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,6 +30,11 @@ require_once "{$_tests_dir}/includes/functions.php"; */ function _manually_load_plugin() { require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; + $event_plugin = getenv( 'WP_EVENT_PLUGIN' ); + switch ( $event_plugin ) { + case 'the-events-calendar': + require ABSPATH . '/wp-content/mu-plugins/the-events-calendar/the-events-calendar.php'; + } } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); diff --git a/tests/install-wp-tests.sh b/tests/install-wp-tests.sh index 286ea40..2bcf328 100755 --- a/tests/install-wp-tests.sh +++ b/tests/install-wp-tests.sh @@ -15,6 +15,16 @@ SKIP_WP_INSTALL=${7-false} SKIP_PLUGINS_INSTALL=${8-false} SKIP_TEST_SUITE_INSTALL=${9-false} +# Initialize the plugin list +PLUGINS="" + +# Parse optional --plugins argument +while [[ "$#" -gt 0 ]]; do + case $1 in + --plugins) PLUGINS="$2"; shift ;; + esac + shift +done TMPDIR=${TMPDIR-/tmp} TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") @@ -189,16 +199,42 @@ install_db() { fi } +install_wp_plugin() { + PLUGIN_NAME=$1 + + if [ -d "$WP_CORE_DIR/wp-content/mu-plugins/$PLUGIN_NAME" ]; then + return; + fi + + # 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" + URL="https://downloads.wordpress.org/plugin/$PLUGIN_FILE" + + # Check if the plugin file already exists + if ! test -f "$TMPDIR/$PLUGIN_FILE"; then + download $URL "$TMPDIR/$PLUGIN_FILE" + fi + + # Unzip the plugin into the WordPress must-use plugins directory + unzip -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/mu-plugins/" +} + install_wp_plugins() { if [ "$SKIP_PLUGINS_INSTALL" = "true" ]; then echo "Skipping WordPress plugin installation." return 0 fi - ACTIVITYPUB_FILE="activitypub.3.2.5.zip" - if ! test -f $TMPDIR/$ACTIVITYPUB_FILE; then - download https://downloads.wordpress.org/plugin/$ACTIVITYPUB_FILE $TMPDIR/$ACTIVITYPUB_FILE - fi - unzip -o $TMPDIR/$ACTIVITYPUB_FILE -d $WP_CORE_DIR/wp-content/plugins/ + # Always install the ActivityPub plugin. + install_wp_plugin activitypub + 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 diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php index 3e9bf11..a0a2e39 100644 --- a/tests/test-class-sample.php +++ b/tests/test-class-sample.php @@ -17,4 +17,13 @@ class Test_Sample extends WP_UnitTestCase { // Replace this with some actual testing code. $this->assertTrue( true ); } + + /** + * Tesd tes + */ + public function test_the_events_calendar() { + // Replace this with some actual testing code. + $class_exists = class_exists( 'Tribe__Events__Main' ); + $this->assertTrue( $class_exists ); + } } -- 2.39.5 From cc3b8e4a91cf35542d304916c718bef79bb32385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 23 Sep 2024 20:17:28 +0200 Subject: [PATCH 02/17] test --- tests/bootstrap.php | 2 +- tests/test-class-sample.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c5bd5d3..c8397d0 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,7 +30,7 @@ require_once "{$_tests_dir}/includes/functions.php"; */ function _manually_load_plugin() { require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; - $event_plugin = getenv( 'WP_EVENT_PLUGIN' ); + $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'; diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php index a0a2e39..0adab4f 100644 --- a/tests/test-class-sample.php +++ b/tests/test-class-sample.php @@ -22,8 +22,15 @@ class Test_Sample extends WP_UnitTestCase { * Tesd tes */ public function test_the_events_calendar() { - // Replace this with some actual testing code. - $class_exists = class_exists( 'Tribe__Events__Main' ); - $this->assertTrue( $class_exists ); + // First check manually that The Events Calendar is loaded. + $class = class_exists( '\Tribe__Events__Main' ); + $this->assertTrue( $class ); + + // Get instance of our plugin. + $aec = \Activitypub_Event_Extensions\Setup::get_instance(); + $this->assertContains( 'the-events-calendar', $aec->get_active_event_plugins() ); + + $aec->activate_activitypub_support_for_active_event_plugins(); + $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' )); } } -- 2.39.5 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 03/17] 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'] ); } } -- 2.39.5 From 515f79fd9c93702b0d705830784b46090736324d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:23:31 +0200 Subject: [PATCH 04/17] enable xdebug on integration test imagae --- Dockerfile | 13 ++++++++++++- docker-compose.yml | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 56e97a7..d2203ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,18 @@ RUN apk update \ RUN docker-php-ext-install mysqli -# Install Composer +# Install Xdebug +RUN apk add --no-cache $PHPIZE_DEPS \ + && apk add --update linux-headers \ + && pecl install xdebug \ + && docker-php-ext-enable xdebug \ + && apk del --purge $PHPIZE_DEPS \ + && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.idekey=VSCODE" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + +# Install Composer RUN EXPECTED_CHECKSUM=$(curl -s https://composer.github.io/installer.sig) \ && curl https://getcomposer.org/installer -o composer-setup.php \ && ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")" \ diff --git a/docker-compose.yml b/docker-compose.yml index 9a4085b..0536ad4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,3 +29,5 @@ services: volumes: - .:/app command: ["composer", "run-script", "test"] + extra_hosts: + - "host.docker.internal:host-gateway" -- 2.39.5 From 4b151f2f4e55bf5db0a41ccfc4c13e262f18d3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:36:02 +0200 Subject: [PATCH 05/17] update wordpress ci cache --- .forgejo/workflows/phpunit.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index dfa957c..26b8bfd 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -22,6 +22,7 @@ jobs: strategy: matrix: php-version: ['8.1', '8.2', '8.3'] + event-plugin: [] name: PHPUnit – PHP ${{ matrix.php-version }} env: extensions: mysql @@ -37,7 +38,7 @@ jobs: path: | ${{ env.WP_CORE_DIR }} ${{ env.WP_TESTS_DIR }} - key: cache-wordpress-1 + key: cache-wordpress-2 - name: Cache Composer id: cache-composer-phpunit -- 2.39.5 From 05779ea0d6cf00c0efbdcc7d5acff63f48144b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:40:03 +0200 Subject: [PATCH 06/17] test --- .forgejo/workflows/phpunit.yml | 2 +- bin/install-wp-tests.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 26b8bfd..f0d590f 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-2 + key: cache-wordpress-3 - name: Cache Composer id: cache-composer-phpunit diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 87f7f46..82ae65f 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -219,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/plugins/" + unzip -q -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/plugins/" } install_wp_plugins() { -- 2.39.5 From 386ed3ead2b563e510b0a1f6846374f43ee418b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:43:22 +0200 Subject: [PATCH 07/17] test --- bin/install-wp-tests.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 82ae65f..ff43d3a 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -192,8 +192,7 @@ install_db() { if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] then echo "Reinstalling will delete the existing test database ($DB_NAME)" - read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB - recreate_db $DELETE_EXISTING_DB + recreate_db yes else create_db fi -- 2.39.5 From e7960707d371f2c81252c801bd5e0bc7399fdb55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:45:29 +0200 Subject: [PATCH 08/17] fix tests --- tests/test-class-sample.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php index 8f9371c..1347d48 100644 --- a/tests/test-class-sample.php +++ b/tests/test-class-sample.php @@ -65,7 +65,7 @@ class Test_Sample extends WP_UnitTestCase { $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['commentsEnabled'] ); $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); } -- 2.39.5 From d1bb939328871f8544ed2c232d265933aeb194db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 10:51:28 +0200 Subject: [PATCH 09/17] test --- composer.json | 2 +- tests/test-class-sample.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2a2a7cc..ba5b52a 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ ], "test": [ "composer install", - "tests/install-wp-tests.sh wordpress-test root wordpress-test test-db latest true", + "bin/install-wp-tests.sh wordpress-test root wordpress-test test-db latest true", "phpunit" ] } diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php index 1347d48..1cdba36 100644 --- a/tests/test-class-sample.php +++ b/tests/test-class-sample.php @@ -39,6 +39,7 @@ class Test_Sample extends WP_UnitTestCase { $aec->activate_activitypub_support_for_active_event_plugins(); $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); + // Create a The Events Calendar Event without content. $wp_object = tribe_events() ->set_args( array( @@ -51,10 +52,16 @@ class Test_Sample extends WP_UnitTestCase { ) ->create(); + // 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\The_Events_Calendar::class, $transformer ); + + // Let the transformer do the work. $event_array = $transformer->to_object()->to_array(); + // Check that the event ActivityStreams representation contains everything as expected. $this->assertArrayHasKey( 'type', $event_array ); $this->assertEquals( 'Event', $event_array['type'] ); -- 2.39.5 From 8bc0a1e76b12a4dcff59dd7706019389f2fca94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 12:26:18 +0200 Subject: [PATCH 10/17] add comment how to debug in test docker-compose file --- docker-compose.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 0536ad4..47d5117 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,6 +4,20 @@ version: '3' # Install docker and docker compose and than just run: # docker compose up +# To live debug in VSCode add this launch configuration to your .vscode/launch.json. +# It assumes that the WordPress root-dir is your workspace root. +# +# { +# "name": "Listen for PHPUnit", +# "type": "php", +# "request": "launch", +# "port": 9003, +# "pathMappings": { +# "/app/": "${workspaceRoot}/wp-content/plugins/activitypub-event-extensions/", +# "/tmp/wordpress/": "${workspaceRoot}/" +# }, +# }, + services: test-db: image: mariadb -- 2.39.5 From 06750e298702d4bb4230c623d4fdfee1e38941df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 12:26:28 +0200 Subject: [PATCH 11/17] improve the-events-calendar --- .../transformer/class-the-events-calendar.php | 49 +++--- tests/test-class-sample.php | 79 ---------- tests/test-class-the-events-calendar.php | 149 ++++++++++++++++++ 3 files changed, 180 insertions(+), 97 deletions(-) delete mode 100644 tests/test-class-sample.php create mode 100644 tests/test-class-the-events-calendar.php diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index 8001613..814df2c 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -145,29 +145,42 @@ final class The_Events_Calendar extends Event { * @return Place|array The place/venue if one is set. */ public function get_location(): Place|null { - if ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) { - return null; - } - // We currently only support a single venue. - $event_venue = $this->wp_object->venues[0]; - - if ( is_null( $event_venue ) ) { + // Get first venue. We currently only support a single venue. + $venue = $this->wp_object->venues->first(); + if ( ! $venue ) { return null; } - $address = array( - 'addressCountry' => $event_venue->country, - 'addressLocality' => $event_venue->city, - 'addressRegion' => $event_venue->province, - 'postalCode' => $event_venue->zip, - 'streetAddress' => $event_venue->address, - 'type' => 'PostalAddress', - ); + // Set the address. + $address = array(); + + if ( ! empty( $venue->country ) ) { + $address['addressCountry'] = $venue->country; + } + + if ( ! empty( $venue->city ) ) { + $address['addressLocality'] = $venue->city; + } + + if ( ! empty( $venue->province ) ) { + $address['addressRegion'] = $venue->province; + } + + if ( ! empty( $venue->zip ) ) { + $address['postalCode'] = $venue->zip; + } + + if ( ! empty( $venue->address ) ) { + $address['streetAddress'] = $venue->address; + } + $address['type'] = 'PostalAddress'; $location = new Place(); - $location->set_address( $address ); - $location->set_id( $event_venue->permalink ); - $location->set_name( $event_venue->post_name ); + if ( count( $address ) > 1 ) { + $location->set_address( $address ); + } + $location->set_id( $venue->permalink ); + $location->set_name( $venue->post_title ); return $location; } diff --git a/tests/test-class-sample.php b/tests/test-class-sample.php deleted file mode 100644 index 1cdba36..0000000 --- a/tests/test-class-sample.php +++ /dev/null @@ -1,79 +0,0 @@ -assertTrue( true ); - } - - /** - * Tesd tes - */ - public function test_the_events_calendar() { - // First check manually that The Events Calendar is loaded. - $class = class_exists( '\Tribe__Events__Main' ); - $this->assertTrue( $class ); - - // Get instance of our plugin. - $aec = \Activitypub_Event_Extensions\Setup::get_instance(); - - // 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' ) ); - - // Create a The Events Calendar Event without content. - $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(); - - // 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\The_Events_Calendar::class, $transformer ); - - // Let the transformer do the work. - $event_array = $transformer->to_object()->to_array(); - - // Check that the event ActivityStreams representation contains everything as expected. - $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( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['commentsEnabled'] ); - - $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); - } -} diff --git a/tests/test-class-the-events-calendar.php b/tests/test-class-the-events-calendar.php new file mode 100644 index 0000000..ee3ae71 --- /dev/null +++ b/tests/test-class-the-events-calendar.php @@ -0,0 +1,149 @@ + 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. Let\'s connect!', + 'start_date' => '+10 days 15:00:00', + 'duration' => HOUR_IN_SECONDS, + 'status' => 'publish', + ), + 'complex_event' => 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', + ), + ); + + /** + * 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( '\Tribe__Events__Main' ) ) { + self::markTestSkipped( 'The Events Calendar plugin is not active.' ); + } + + // 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(); + + } + + /** + * 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( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); + + // Create a The Events Calendar Event without content. + $wp_object = tribe_events() + ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) + ->create(); + + // 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\The_Events_Calendar::class, $transformer ); + } + + /** + * Test transformation of minimal event without venue. + */ + public function test_transform_of_minimal_event_without_venue() { + // Create a The Events Calendar Event. + $wp_object = tribe_events() + ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + // Let the transformer do the work. + $event_array = $transformer->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $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->assertTrue( $event_array['commentsEnabled'] ); + $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); + $this->assertEquals( 'free', $event_array['joinMode'] ); + $this->assertArrayNotHasKey( 'location', $event_array ); + } + + /** + * Test transformation of minimal event with minimal venue. + */ + public function test_transform_of_minimal_event_with_venue() { + // Create Venue. + $venue = tribe_venues()->set_args( self::MOCKUP_VENUS['minimal_venue'] )->create(); + // Create a The Events Calendar Event. + $wp_object = tribe_events() + ->set_args( self::MOCKUP_EVENTS['complex_event'] ) + ->set( 'venue', $venue->ID ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + // Let the transformer do the work. + $event_array = $transformer->to_object()->to_array(); + + // Check that the event ActivityStreams representation contains everything as expected. + $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( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['commentsEnabled'] ); + $this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] ); + $this->assertArrayHasKey( 'location', $event_array ); + $this->assertEquals( 'Place', $event_array['location']['type'] ); + $this->assertEquals( self::MOCKUP_VENUS['minimal_venue']['venue'], $event_array['location']['name'] ); + } +} -- 2.39.5 From 0595ce4fd78c4233e8f9d328de10546b42fa0fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 13:46:58 +0200 Subject: [PATCH 12/17] add tests for the events calendar --- ...test-class-plugin-the-events-calendar.php} | 72 ++++++++++++++++--- 1 file changed, 62 insertions(+), 10 deletions(-) rename tests/{test-class-the-events-calendar.php => test-class-plugin-the-events-calendar.php} (67%) diff --git a/tests/test-class-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php similarity index 67% rename from tests/test-class-the-events-calendar.php rename to tests/test-class-plugin-the-events-calendar.php index ee3ae71..44df750 100644 --- a/tests/test-class-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -9,6 +9,9 @@ * Sample test case. */ class Test_The_Events_Calendar extends WP_UnitTestCase { + /** + * Mockup events of certain complexity. + */ public const MOCKUP_VENUS = array( 'minimal_venue' => array( 'venue' => 'Minimal Venue', @@ -48,6 +51,21 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { ), ); + public const MOCKUP_CATEGORIES = array( + 'concert' => array( + 'cat_name' => 'concert', + 'category_description' => 'Mostly live concerts', + 'category_nicename' => 'Concert', + 'taxonomy' => 'tribe_events_cat', + ), + 'theatre' => array( + 'cat_name' => 'theatre', + 'category_description' => 'Theatre shows', + 'category_nicename' => 'Theatre', + 'taxonomy' => 'tribe_events_cat', + ), + ); + /** * Override the setup function, so that tests don't run if the Events Calendar is not active. */ @@ -62,8 +80,8 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { $aec = \Activitypub_Event_Extensions\Setup::get_instance(); $aec->activate_activitypub_support_for_active_event_plugins(); + // Delete all posts afterwards. _delete_all_posts(); - } /** @@ -77,7 +95,7 @@ class Test_The_Events_Calendar 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( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); // Create a The Events Calendar Event without content. $wp_object = tribe_events() @@ -100,10 +118,8 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) ->create(); - // Call the transformer Factory. - $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); - // Let the transformer do the work. - $event_array = $transformer->to_object()->to_array(); + // Call the transformer. + $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'] ); @@ -115,6 +131,44 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { $this->assertEquals( 'allow_all', $event_array['repliesModerationOption'] ); $this->assertEquals( 'free', $event_array['joinMode'] ); $this->assertArrayNotHasKey( 'location', $event_array ); + $this->assertEquals( 'MEETING', $event_array['category'] ); + } + + /** + * Test transformation of event with mapped category. + */ + public function test_transform_event_with_mapped_categories() { + // Create category. + $category_id_music = wp_insert_category( self::MOCKUP_CATEGORIES['concert'] ); + $category_id_theatre = wp_insert_category( self::MOCKUP_CATEGORIES['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 The Events Calendar event with the music category. + $wp_object = tribe_events() + ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) + ->set( 'category', array( $category_id_music ) ) + ->create(); + // Call the transformer. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); + // See if the default category mapping is applied. + $this->assertEquals( 'MUSIC', $event_array['category'] ); + + // Create a The Events Calendar event with the theatre category. + $wp_object = tribe_events() + ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) + ->set( 'category', array( $category_id_theatre ) ) + ->create(); + + // Call the transformer. + $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); + + // See if the default category mapping is applied. + $this->assertEquals( 'THEATRE', $event_array['category'] ); } /** @@ -129,10 +183,8 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { ->set( 'venue', $venue->ID ) ->create(); - // Call the transformer Factory. - $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); - // Let the transformer do the work. - $event_array = $transformer->to_object()->to_array(); + // Call the transformer. + $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'] ); -- 2.39.5 From da9e71008d4733556a23cfe324f220be2d8687a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 14:28:06 +0200 Subject: [PATCH 13/17] fix edge cases for plugin setup --- includes/class-setup.php | 4 ++-- tests/test-class-plugin-the-events-calendar.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/class-setup.php b/includes/class-setup.php index cbe6106..c3f9a79 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -59,7 +59,7 @@ class Setup { * @since 1.0.0 */ protected function __construct() { - $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ); + $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) || is_plugin_active( 'activitypub/activitypub.php' ); // 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 ); @@ -206,7 +206,7 @@ class Setup { // The ActivityPub plugin is not active. add_action( 'admin_notices', array( 'Activitypub_Event_Extensions\Admin\General_Admin_Notices', 'activitypub_plugin_not_enabled' ), 10, 1 ); } - 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 ) ) { // The ActivityPub plugin is too old. add_action( 'admin_notices', array( 'Activitypub_Event_Extensions\Admin\General_Admin_Notices', 'activitypub_plugin_version_too_old' ), 10, 1 ); } diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 44df750..406df03 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -164,6 +164,10 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { ->set( 'category', array( $category_id_theatre ) ) ->create(); + $test1 = wp_set_post_categories( $wp_object->ID, $category_id_theatre ); + $terms = get_the_terms( $wp_object->ID, 'tribe_events_cat' ); + get_the_terms( $post_id, 'tribe_events_cat' ); + // Call the transformer. $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); -- 2.39.5 From 98dc3c3839e3858ba8da17c44cdd8bfe6acbf5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 14:31:09 +0200 Subject: [PATCH 14/17] fix function calls that are static --- includes/class-setup.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-setup.php b/includes/class-setup.php index c3f9a79..8f8f773 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -234,9 +234,9 @@ class Setup { // Get the transformer for a specific event plugins event-post type. foreach ( $this->active_event_plugins as $event_plugin ) { if ( $wp_object->post_type === $event_plugin->get_post_type() ) { - $transformer_class = $event_plugin->get_activitypub_event_transformer_class(); + $transformer_class = $event_plugin::get_activitypub_event_transformer_class(); if ( class_exists( $transformer_class ) ) { - return new $transformer_class( $wp_object, $event_plugin->get_event_category_taxonomy() ); + return new $transformer_class( $wp_object, $event_plugin::get_event_category_taxonomy() ); } } } -- 2.39.5 From f04f0295b93cad5475ee03fdfb8144bb9fbddc09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 15:35:32 +0200 Subject: [PATCH 15/17] fix tests --- .../transformer/class-the-events-calendar.php | 12 ++++++- .../test-class-plugin-the-events-calendar.php | 35 ++++--------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index 814df2c..c576754 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -145,8 +145,18 @@ final class The_Events_Calendar extends Event { * @return Place|array The place/venue if one is set. */ public function get_location(): Place|null { + // Get short handle for the venues. + $venues = $this->wp_object->venues; + // Get first venue. We currently only support a single venue. - $venue = $this->wp_object->venues->first(); + if ( $venues instanceof \Tribe\Events\Collections\Lazy_Post_Collection ){ + $venue = $venues->first(); + } elseif ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) { + return null; + } else { + $venue = $venues[0]; + } + if ( ! $venue ) { return null; } diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 406df03..4614746 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -51,21 +51,6 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { ), ); - public const MOCKUP_CATEGORIES = array( - 'concert' => array( - 'cat_name' => 'concert', - 'category_description' => 'Mostly live concerts', - 'category_nicename' => 'Concert', - 'taxonomy' => 'tribe_events_cat', - ), - 'theatre' => array( - 'cat_name' => 'theatre', - 'category_description' => 'Theatre shows', - 'category_nicename' => 'Theatre', - 'taxonomy' => 'tribe_events_cat', - ), - ); - /** * Override the setup function, so that tests don't run if the Events Calendar is not active. */ @@ -139,8 +124,8 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { */ public function test_transform_event_with_mapped_categories() { // Create category. - $category_id_music = wp_insert_category( self::MOCKUP_CATEGORIES['concert'] ); - $category_id_theatre = wp_insert_category( self::MOCKUP_CATEGORIES['theatre'] ); + $category_id_music = wp_insert_term( 'Music', Tribe__Events__Main::TAXONOMY, array( 'slug' => 'music' ) ); + $category_id_theatre = wp_insert_term( 'Theatre', Tribe__Events__Main::TAXONOMY, array( 'slug' => 'theatre' ) ); // Set default mapping for event categories. update_option( 'activitypub_event_extensions_default_event_category', 'MUSIC' ); @@ -151,26 +136,18 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { // Create a The Events Calendar event with the music category. $wp_object = tribe_events() ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) - ->set( 'category', array( $category_id_music ) ) ->create(); + // Set the post term music to the event. + wp_set_post_terms( $wp_object->ID, $category_id_music['term_id'], Tribe__Events__Main::TAXONOMY ); // Call the transformer. $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); // See if the default category mapping is applied. $this->assertEquals( 'MUSIC', $event_array['category'] ); - // Create a The Events Calendar event with the theatre category. - $wp_object = tribe_events() - ->set_args( self::MOCKUP_EVENTS['minimal_event'] ) - ->set( 'category', array( $category_id_theatre ) ) - ->create(); - - $test1 = wp_set_post_categories( $wp_object->ID, $category_id_theatre ); - $terms = get_the_terms( $wp_object->ID, 'tribe_events_cat' ); - get_the_terms( $post_id, 'tribe_events_cat' ); - + // Set the post term theatre to the event. + wp_set_post_terms( $wp_object->ID, $category_id_theatre['term_id'], Tribe__Events__Main::TAXONOMY ); // Call the transformer. $event_array = \Activitypub\Transformer\Factory::get_transformer( $wp_object )->to_object()->to_array(); - // See if the default category mapping is applied. $this->assertEquals( 'THEATRE', $event_array['category'] ); } -- 2.39.5 From 4febff50ef6c5a3e93687e276fb7808d0affd2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 15:39:09 +0200 Subject: [PATCH 16/17] fix ci workflow matrix: remove currently unused event_plugin --- .forgejo/workflows/phpunit.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index f0d590f..23321d9 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -22,7 +22,6 @@ jobs: strategy: matrix: php-version: ['8.1', '8.2', '8.3'] - event-plugin: [] name: PHPUnit – PHP ${{ matrix.php-version }} env: extensions: mysql -- 2.39.5 From 8fecbe792f5be91cb38d11b1712914b85fdcedce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 24 Sep 2024 16:10:05 +0200 Subject: [PATCH 17/17] fix linter issues --- .../transformer/class-the-events-calendar.php | 2 +- includes/class-setup.php | 18 ++++++++++++++++-- .../test-class-plugin-the-events-calendar.php | 4 ++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index c576754..2c590b8 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -149,7 +149,7 @@ final class The_Events_Calendar extends Event { $venues = $this->wp_object->venues; // Get first venue. We currently only support a single venue. - if ( $venues instanceof \Tribe\Events\Collections\Lazy_Post_Collection ){ + if ( $venues instanceof \Tribe\Events\Collections\Lazy_Post_Collection ) { $venue = $venues->first(); } elseif ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) { return null; diff --git a/includes/class-setup.php b/includes/class-setup.php index 8f8f773..9e4a1e6 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -59,14 +59,15 @@ class Setup { * @since 1.0.0 */ protected function __construct() { - $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) || is_plugin_active( 'activitypub/activitypub.php' ); + $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) || + is_plugin_active( 'activitypub/activitypub.php' ); // 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 = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ? constant( 'ACTIVITYPUB_PLUGIN_VERSION' ): '0'; + $this->activitypub_plugin_version = self::get_activitypub_plugin_version(); $this->setup_hooks(); } @@ -95,6 +96,19 @@ class Setup { return self::$instance; } + /** + * LooksUp the current version of the ActivityPub. + * + * @return string The semantic Version. + */ + private static function get_activitypub_plugin_version(): string { + if ( defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ) { + return constant( 'ACTIVITYPUB_PLUGIN_VERSION' ); + } + $version = get_file_data( WP_PLUGIN_DIR . '/activitypub/activitypub.php', array( 'Version' ) )[0]; + return $version ?? '0.0.0'; + } + /** * Getter function for the active event plugins. * diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 4614746..c2c9eb6 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -14,8 +14,8 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { */ public const MOCKUP_VENUS = array( 'minimal_venue' => array( - 'venue' => 'Minimal Venue', - 'status' => 'publish', + 'venue' => 'Minimal Venue', + 'status' => 'publish', ), 'complex_venue' => array( 'venue' => 'Complex Venue', -- 2.39.5