add basic event transformation test for Events Manager
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 36s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 57s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 58s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 58s

This commit is contained in:
André Menrath 2024-09-25 09:37:50 +02:00
parent 253d3e9010
commit ef3389e64d
3 changed files with 109 additions and 2 deletions

View file

@ -94,16 +94,21 @@ final class Events_Manager extends Event_Transformer {
return null; return null;
} }
$location = new Place();
$em_location = $this->em_event->get_location(); $em_location = $this->em_event->get_location();
if ( '' === $em_location->location_id ) {
return null;
}
$location = new Place();
$location->set_name( $em_location->location_name ); $location->set_name( $em_location->location_name );
$address = array( $address = array(
'type' => 'PostalAddress', 'type' => 'PostalAddress',
'addressCountry' => $em_location->location_country, 'addressCountry' => $em_location->location_country,
'addressLocality' => $em_location->location_town, '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, 'name' => $em_location->location_name,
); );
if ( $em_location->location_state ) { if ( $em_location->location_state ) {

View file

@ -69,6 +69,14 @@ function _manually_load_plugin() {
update_option( 'active_plugins', $current ); 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. // At last manually load our WordPress plugin.
require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; require dirname( __DIR__ ) . '/activitypub-event-extensions.php';
} }

View file

@ -19,6 +19,9 @@ class Test_Events_Manager extends WP_UnitTestCase {
self::markTestSkipped( 'VS Event List plugin is not active.' ); 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. // Make sure that ActivityPub support is enabled for Events Manager.
$aec = \Activitypub_Event_Extensions\Setup::get_instance(); $aec = \Activitypub_Event_Extensions\Setup::get_instance();
$aec->activate_activitypub_support_for_active_event_plugins(); $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. // Check that we got the right transformer.
$this->assertInstanceOf( \Activitypub_Event_Extensions\Activitypub\Transformer\Events_Manager::class, $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'] );
}
} }