From dc24f292be99ae8be93b81f4a00e9b5f13bea53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Thu, 21 Nov 2024 16:24:47 +0100 Subject: [PATCH] add tests --- .forgejo/workflows/phpunit.yml | 6 + composer.json | 3 +- .../activitypub/transformer/class-event.php | 15 +- tests/bootstrap.php | 28 ++- ...ss-activitypub-event-bridge-shortcodes.php | 193 ++++++++++++++++++ 5 files changed, 231 insertions(+), 14 deletions(-) create mode 100644 tests/test-class-activitypub-event-bridge-shortcodes.php diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index f8e08ac..5fc523b 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -75,6 +75,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 ${{ matrix.wordpress-version }} false true true true + + - name: Run General Tests + run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=activitypub_event_bridge + env: + PHP_VERSION: ${{ matrix.php-version }} + - name: Run Integration tests for The Events Calendar run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=the_events_calendar env: diff --git a/composer.json b/composer.json index 4da8b48..6e1aff4 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,7 @@ ], "test-debug": [ "@prepare-test", - "@test-gatherpress" + "@test-activitypub-event-bridge-shortcodes" ], "test-vs-event-list": "phpunit --filter=vs_event_list", "test-the-events-calendar": "phpunit --filter=the_events_calendar", @@ -67,6 +67,7 @@ "test-wp-event-manager": "phpunit --filter=wp_event_manager", "test-eventin": "phpunit --filter=eventin", "test-modern-events-calendar-lite": "phpunit --filter=modern_events_calendar_lite", + "test-activitypub-event-bridge-shortcodes": "phpunit --filter=activitypub_event_bridge_shortcodes", "test-all": "phpunit" } } diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index ffdac82..b0775f6 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -245,13 +245,13 @@ abstract class Event extends Post { $args = shortcode_atts( array( 'icon' => 'true', - 'title' => 'true', + 'label' => 'true', ), $atts ); $args['icon'] = filter_var( $args['icon'], FILTER_VALIDATE_BOOLEAN ); - $args['title'] = filter_var( $args['title'], FILTER_VALIDATE_BOOLEAN ); + $args['label'] = filter_var( $args['label'], FILTER_VALIDATE_BOOLEAN ); $output = array(); @@ -259,7 +259,7 @@ abstract class Event extends Post { $output[] = $icon; } - if ( $args['title'] ) { + if ( $args['label'] ) { $output[] = $label . ':'; } @@ -278,7 +278,7 @@ abstract class Event extends Post { $args = shortcode_atts( array( 'icon' => 'true', - 'title' => 'true', + 'label' => 'true', 'country' => 'true', 'zip' => 'true', 'city' => 'true', @@ -306,7 +306,7 @@ abstract class Event extends Post { if ( $args['icon'] ) { $output[] = '📍'; } - if ( $args['title'] ) { + if ( $args['label'] ) { $output[] = esc_html__( 'Location', 'activitypub-event-bridge' ) . ':'; } @@ -477,7 +477,10 @@ abstract class Event extends Post { $start_time = $this->get_start_time(); $end_time = $this->get_end_time(); $address = $this->format_address( $this->get_location() ); - $time_atts = array( 'icon' => true, 'label' => true); + $time_atts = array( + 'icon' => true, + 'label' => true, + ); $formatted_items = array(); if ( ! empty( $category ) ) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4262f2b..4c8b6f3 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -25,6 +25,21 @@ if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { // Give access to tests_add_filter() function. require_once "{$_tests_dir}/includes/functions.php"; +/** + * Function to manually load an event plugin. + * + * @param string $plugin_file The main plugin file of the event plugin. + */ +function _manually_load_event_plugin( $plugin_file ) { + $plugin_dir = ABSPATH . '/wp-content/plugins/'; + require_once $plugin_dir . $plugin_file; + update_option( 'purchase_history_table_structure_migration_done', true ); + $current = get_option( 'active_plugins', array() ); + $current[] = $plugin_file; + sort( $current ); + update_option( 'active_plugins', $current ); +} + /** * Manually load the plugin being tested and its integrations. */ @@ -74,13 +89,12 @@ function _manually_load_plugin() { } if ( $plugin_file ) { - // Manually load the event plugin. - require_once $plugin_dir . $plugin_file; - update_option( 'purchase_history_table_structure_migration_done', true ); - $current = get_option( 'active_plugins', array() ); - $current[] = $plugin_file; - sort( $current ); - update_option( 'active_plugins', $current ); + _manually_load_event_plugin( $plugin_file ); + } else { + // For all other tests we mainly use the Events Calendar as a reference. + _manually_load_event_plugin( 'the-events-calendar/the-events-calendar.php' ); + _manually_load_event_plugin( 'very-simple-event-list/vsel.php' ); + } // Hot fix that allows using Events Manager within unit tests, because the em_init() is later not run as admin. diff --git a/tests/test-class-activitypub-event-bridge-shortcodes.php b/tests/test-class-activitypub-event-bridge-shortcodes.php new file mode 100644 index 0000000..0b7e1ba --- /dev/null +++ b/tests/test-class-activitypub-event-bridge-shortcodes.php @@ -0,0 +1,193 @@ +activate_activitypub_support_for_active_event_plugins(); + + // Delete all posts afterwards. + _delete_all_posts(); + } + + /** + * Test the shortcode for rendering the events start time. + */ + public function test_start_time() { + // Create a The Events Calendar Event without content. + $wp_object = tribe_events() + ->set_args( Test_The_Events_Calendar::MOCKUP_EVENTS['minimal_event'] ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + if ( ! $transformer instanceof \ActivityPub_Event_Bridge\Activitypub\Transformer\Event ) { + return; + } + + $transformer->register_shortcodes(); + + $summary = '[ap_start_time]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '🗓️ Start: December 1, 2024 3:00 pm', $summary ); + + $summary = '[ap_start_time icon="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'Start: December 1, 2024 3:00 pm', $summary ); + + $summary = '[ap_start_time icon="false" label="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'December 1, 2024 3:00 pm', $summary ); + + $transformer->unregister_shortcodes(); + } + + /** + * Test the shortcode for rendering the events end time. + */ + public function test_end_time() { + // Create a The Events Calendar Event without content. + $wp_object = tribe_events() + ->set_args( Test_The_Events_Calendar::MOCKUP_EVENTS['minimal_event'] ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + if ( ! $transformer instanceof \ActivityPub_Event_Bridge\Activitypub\Transformer\Event ) { + return; + } + + $transformer->register_shortcodes(); + + $summary = '[ap_end_time]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '⏳ End: December 1, 2024 4:00 pm', $summary ); + + $summary = '[ap_end_time icon="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'End: December 1, 2024 4:00 pm', $summary ); + + $summary = '[ap_end_time icon="false" label="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'December 1, 2024 4:00 pm', $summary ); + + $transformer->unregister_shortcodes(); + } + + /** + * Test the shortcode for rendering the events location when no location is set. + */ + public function test_location_when_no_location_is_set() { + // Create a The Events Calendar Event without content. + $wp_object = tribe_events() + ->set_args( Test_The_Events_Calendar::MOCKUP_EVENTS['minimal_event'] ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + if ( ! $transformer instanceof \ActivityPub_Event_Bridge\Activitypub\Transformer\Event ) { + return; + } + + $transformer->register_shortcodes(); + + $summary = '[ap_location]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '', $summary ); + + $transformer->unregister_shortcodes(); + } + + /** + * Test the shortcode for rendering the events location when location is set. + */ + public function test_location_when_location_is_set() { + // Create Venue. + $venue = tribe_venues()->set_args( Test_The_Events_Calendar::MOCKUP_VENUS['minimal_venue'] )->create(); + // Create a The Events Calendar Event. + $wp_object = tribe_events() + ->set_args( Test_The_Events_Calendar::MOCKUP_EVENTS['complex_event'] ) + ->set( 'venue', $venue->ID ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + if ( ! $transformer instanceof \ActivityPub_Event_Bridge\Activitypub\Transformer\Event ) { + return; + } + + $transformer->register_shortcodes(); + + $summary = '[ap_location]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '📍 Location: Minimal Venue', $summary ); + + $summary = '[ap_location icon="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'Location: Minimal Venue', $summary ); + + $summary = '[ap_location icon="false" label="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( 'Minimal Venue', $summary ); + + $transformer->unregister_shortcodes(); + } + + /** + * Test the shortcode for rendering the events location when location with detailed address is set. + */ + public function test_location_when_detailed_location_is_set() { + // Create Venue. + $venue = tribe_venues()->set_args( Test_The_Events_Calendar::MOCKUP_VENUS['complex_venue'] )->create(); + // Create a The Events Calendar Event. + $wp_object = tribe_events() + ->set_args( Test_The_Events_Calendar::MOCKUP_EVENTS['complex_event'] ) + ->set( 'venue', $venue->ID ) + ->create(); + + // Call the transformer Factory. + $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); + + if ( ! $transformer instanceof \ActivityPub_Event_Bridge\Activitypub\Transformer\Event ) { + return; + } + + $transformer->register_shortcodes(); + + $summary = '[ap_location]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '📍 Location: Complex Venue, Venue address, Venue zip, Venue city, Venue country', $summary ); + + $summary = '[ap_location country="false"]'; + $summary = do_shortcode( $summary ); + $this->assertEquals( '📍 Location: Complex Venue, Venue address, Venue zip, Venue city', $summary ); + + $transformer->unregister_shortcodes(); + } +}