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] 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'] ); + } }