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