add test templates for event-manager and gatherpress
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 41s

This commit is contained in:
André Menrath 2024-09-24 20:31:48 +02:00
parent 5f6f02cc81
commit 2bcd4c5039
8 changed files with 233 additions and 59 deletions

View file

@ -83,3 +83,13 @@ jobs:
run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=vs_event_list run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=vs_event_list
env: env:
PHP_VERSION: ${{ matrix.php-version }} PHP_VERSION: ${{ matrix.php-version }}
- name: Run Integration tests for GatherPress
run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=gatherpress
env:
PHP_VERSION: ${{ matrix.php-version }}
- name: Run Integration tests for Events Manager
run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit --filter=events_manager
env:
PHP_VERSION: ${{ matrix.php-version }}

View file

@ -52,9 +52,13 @@
"@test-gatherpress", "@test-gatherpress",
"@test-events-manager" "@test-events-manager"
], ],
"test-vs-event_list": "phpunit --filter=vs_event_list", "test-debug": [
"@prepare-test",
"@test-events-manager"
],
"test-vs-event-list": "phpunit --filter=vs_event_list",
"test-the-events-calendar": "phpunit --filter=the_events_calendar", "test-the-events-calendar": "phpunit --filter=the_events_calendar",
"test-gatherpress": "phpunit --filter=gatherpress", "test-gatherpress": "phpunit --filter=gatherpress",
"test-events-manager": "phpunit --filter=events-manager" "test-events-manager": "phpunit --filter=events_manager"
} }
} }

View file

@ -42,6 +42,6 @@ services:
- test-db - test-db
volumes: volumes:
- .:/app - .:/app
command: ["composer", "run-script", "test"] command: ["composer", "run-script", "test-debug"]
extra_hosts: extra_hosts:
- "host.docker.internal:host-gateway" - "host.docker.internal:host-gateway"

View file

@ -50,6 +50,13 @@ class Event extends Post {
return 'Event'; return 'Event';
} }
/**
* Get a sane default for whether comments are enabled.
*/
protected function get_comments_enabled() {
'open';
}
/** /**
* Returns the title of the event. * Returns the title of the event.
* *

View file

@ -49,29 +49,18 @@ final class GatherPress extends Event {
protected $gp_venue; protected $gp_venue;
/** /**
* Get transformer name. * Extend the constructor, to also set the GatherPress objects.
* *
* Retrieve the transformers name. * This is a special class object form The Events Calendar which
* has a lot of useful functions, we make use of our getter functions.
* *
* @since 1.0.0 * @param WP_Post $wp_object The WordPress object.
* @access public * @param string $wp_taxonomy The taxonomy slug of the event post type.
* @return string Widget name.
*/ */
public function get_transformer_name() { public function __construct( $wp_object, $wp_taxonomy ) {
return 'gatherpress/gp-event'; parent::__construct( $wp_object, $wp_taxonomy );
} $this->gp_event = new GatherPress_Event( $this->wp_object->ID );
$this->gp_venue = $this->gp_event->get_venue_information();
/**
* Get transformer title.
*
* Retrieve the transformers label.
*
* @since 1.0.0
* @access public
* @return string Widget title.
*/
public function get_transformer_label() {
return 'GatherPress Event';
} }
/** /**
@ -90,15 +79,20 @@ final class GatherPress extends Event {
/** /**
* Get the event location. * Get the event location.
* *
* @return array The Place. * @return Place|null The place objector null if not place set.
*/ */
public function get_location() { public function get_location(): Place|null {
$address = $this->gp_venue['full_address']; $address = $this->gp_venue['full_address'];
if ( $address ) {
$place = new Place(); $place = new Place();
$place->set_type( 'Place' ); $place->set_type( 'Place' );
$place->set_name( $address ); $place->set_name( $address );
$place->set_address( $address ); $place->set_address( $address );
return $place; return $place;
} else {
return null;
}
} }
/** /**
@ -134,7 +128,7 @@ final class GatherPress extends Event {
/** /**
* Overrides/extends the get_attachments function to also add the event Link. * Overrides/extends the get_attachments function to also add the event Link.
*/ */
protected function get_attachment() { protected function get_attachment(): array {
$attachments = parent::get_attachment(); $attachments = parent::get_attachment();
if ( count( $attachments ) ) { if ( count( $attachments ) ) {
$attachments[0]['type'] = 'Document'; $attachments[0]['type'] = 'Document';
@ -154,7 +148,7 @@ final class GatherPress extends Event {
* *
* @return string The User-URL. * @return string The User-URL.
*/ */
protected function get_attributed_to() { protected function get_attributed_to(): string {
$user = new Blog(); $user = new Blog();
return $user->get_url(); return $user->get_url();
} }
@ -167,7 +161,7 @@ final class GatherPress extends Event {
* *
* @return string $summary The custom event summary. * @return string $summary The custom event summary.
*/ */
public function get_summary() { public function get_summary(): string {
if ( $this->wp_object->excerpt ) { if ( $this->wp_object->excerpt ) {
$excerpt = $this->wp_object->post_excerpt; $excerpt = $this->wp_object->post_excerpt;
} elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { } elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) {
@ -184,38 +178,31 @@ final class GatherPress extends Event {
return $summary; return $summary;
} }
/**
* Get the content.
*/
public function get_content(): string {
return $this->wp_object->post_content;
}
/**
* Determine whether the event is online.
*
* @return bool
*/
public function get_is_online(): bool {
return $this->gp_event->maybe_get_online_event_link() ? true : false;
}
/** /**
* Transform the WordPress Object into an ActivityPub Object. * Transform the WordPress Object into an ActivityPub Object.
* *
* @return Activitypub\Activity\Event * @return Activitypub\Activity\Event
*/ */
public function to_object() { public function to_object() {
$this->ap_object = new Event_Object(); $activitypub_object = parent::to_object();
$this->gp_event = new GatherPress_Event( $this->wp_object->ID );
$this->gp_venue = $this->gp_event->get_venue_information();
$this->ap_object = parent::to_object(); return $activitypub_object;
$this->ap_object->set_comments_enabled( 'open' === $this->wp_object->comment_status ? true : false );
$this->ap_object->set_external_participation_url( $this->get_url() );
$online_event_link = $this->gp_event->maybe_get_online_event_link();
if ( $online_event_link ) {
$this->ap_object->set_is_online( true );
} else {
$this->ap_object->set_is_online( false );
}
$this->ap_object->set_status( 'CONFIRMED' );
$this->ap_object->set_name( get_the_title( $this->wp_object->ID ) );
$this->ap_object->set_actor( get_rest_url_by_path( 'application' ) );
$this->ap_object->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );
$this->ap_object->set_location();
return $this->ap_object;
} }
} }

View file

@ -0,0 +1,63 @@
<?php
/**
* Class SampleTest
*
* @package Activitypub_Event_Extensions
*/
/**
* Sample test case.
*/
class Test_Events_Manager extends WP_UnitTestCase {
/**
* 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( 'EM_Events' ) ) {
self::markTestSkipped( 'VS Event List plugin is not active.' );
}
// 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();
// Delete all posts afterwards.
_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( EM_POST_TYPE_EVENT, get_option( 'activitypub_support_post_types' ) );
// Insert a new Event.
$wp_post_id = wp_insert_post(
array(
'post_title' => 'Events Manager Test event',
'post_status' => 'published',
'post_type' => EM_POST_TYPE_EVENT,
'meta_input' => array(
'event_start_time' => strtotime( '+10 days 15:00:00' ),
),
)
);
$wp_object = get_post( $wp_post_id );
// 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\Events_Manager::class, $transformer );
}
}

View file

@ -0,0 +1,103 @@
<?php
/**
* Class SampleTest
*
* @package Activitypub_Event_Extensions
*/
/**
* Sample test case.
*/
class Test_GatherPress extends WP_UnitTestCase {
/**
* 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 ( ! defined( 'GATHERPRESS_CORE_FILE' ) ) {
self::markTestSkipped( 'GatherPress plugin is not active.' );
}
// Mock the plugin activation.
GatherPress\Core\Setup::get_instance()->activate_gatherpress_plugin( false );
// 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 afterwards.
_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( 'gatherpress_event', get_option( 'activitypub_support_post_types' ) );
// Mock GatherPress Event.
$post_id = wp_insert_post(
array(
'post_title' => 'Unit Test Event',
'post_type' => 'gatherpress_event',
'post_content' => 'Unit Test description.',
)
);
$event = new \GatherPress\Core\Event( $post_id );
$params = array(
'datetime_start' => '+10 days 15:00:00',
'datetime_end' => '+10 days 16:00:00',
'timezone' => 'America/New_York',
);
$event->save_datetimes( $params );
// Call the transformer Factory.
$transformer = \Activitypub\Transformer\Factory::get_transformer( $event->event );
// Check that we got the right transformer.
$this->assertInstanceOf( \Activitypub_Event_Extensions\Activitypub\Transformer\GatherPress::class, $transformer );
}
/**
* Test transformation to ActivityPUb for basic event.
*/
public function test_transform_of_basic_event() {
// Mock GatherPress Event.
$post_id = wp_insert_post(
array(
'post_title' => 'Unit Test Event',
'post_type' => 'gatherpress_event',
'post_content' => 'Unit Test description.',
'post_status' => 'published',
)
);
$event = new \GatherPress\Core\Event( $post_id );
$params = array(
'datetime_start' => '+10 days 15:00:00',
'datetime_end' => '+10 days 16:00:00',
'timezone' => 'America/New_York',
);
$event->save_datetimes( $params );
// Call the transformer Factory.
$event_array = \Activitypub\Transformer\Factory::get_transformer( $event->event )->to_object()->to_array();
// Check that the event ActivityStreams representation contains everything as expected.
$this->assertEquals( 'Event', $event_array['type'] );
$this->assertEquals( 'Unit Test Event', $event_array['name'] );
$this->assertEquals( 'Unit Test description.', $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( 'external', $event_array['joinMode'] );
$this->assertArrayNotHasKey( 'location', $event_array );
}
}