wip draft how to do integration tests for event plugins

This commit is contained in:
André Menrath 2024-09-24 10:23:07 +02:00
parent cc3b8e4a91
commit 04b0e69af6
6 changed files with 63 additions and 14 deletions

View file

@ -68,11 +68,11 @@ jobs:
- name: Setup Test Environment - name: Setup Test Environment
if: steps.cache-wordpress.outputs.cache-hit != 'true' if: steps.cache-wordpress.outputs.cache-hit != 'true'
run: bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false false false false run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false false false false
- name: Initialize WordPress test database - name: Initialize WordPress test database
if: steps.cache-wordpress.outputs.cache-hit != 'false' if: steps.cache-wordpress.outputs.cache-hit != 'false'
run: bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false true true true run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false true true true
- name: Run PHPUnit - name: Run PHPUnit
run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit run: cd /workspace/Event-Federation/wordpress-activitypub-event-extensions/ && ./vendor/bin/phpunit

View file

@ -202,7 +202,9 @@ install_db() {
install_wp_plugin() { install_wp_plugin() {
PLUGIN_NAME=$1 PLUGIN_NAME=$1
if [ -d "$WP_CORE_DIR/wp-content/mu-plugins/$PLUGIN_NAME" ]; then mkdir -p "$WP_CORE_DIR/wp-content/plugins/"
if [ -d "$WP_CORE_DIR/wp-content/plugins/$PLUGIN_NAME" ]; then
return; return;
fi fi
@ -217,7 +219,7 @@ install_wp_plugin() {
fi fi
# Unzip the plugin into the WordPress must-use plugins directory # Unzip the plugin into the WordPress must-use plugins directory
unzip -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/mu-plugins/" unzip -o "$TMPDIR/$PLUGIN_FILE" -d "$WP_CORE_DIR/wp-content/plugins/"
} }
install_wp_plugins() { install_wp_plugins() {

View file

@ -151,6 +151,10 @@ final class The_Events_Calendar extends Event {
// We currently only support a single venue. // We currently only support a single venue.
$event_venue = $this->wp_object->venues[0]; $event_venue = $this->wp_object->venues[0];
if ( is_null( $event_venue ) ) {
return null;
}
$address = array( $address = array(
'addressCountry' => $event_venue->country, 'addressCountry' => $event_venue->country,
'addressLocality' => $event_venue->city, 'addressLocality' => $event_venue->city,

View file

@ -59,14 +59,14 @@ class Setup {
* @since 1.0.0 * @since 1.0.0
*/ */
protected function __construct() { protected function __construct() {
$this->activitypub_plugin_is_active = is_plugin_active( 'activitypub/activitypub.php' ); $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' );
// BeforeFirstRelease: decide whether we want to do anything at all when ActivityPub plugin is note active. // BeforeFirstRelease: decide whether we want to do anything at all when ActivityPub plugin is note active.
// if ( ! $this->activitypub_plugin_is_active ) { // if ( ! $this->activitypub_plugin_is_active ) {
// deactivate_plugins( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ); // deactivate_plugins( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE );
// return; // return;
// }. // }.
$this->active_event_plugins = self::detect_active_event_plugins(); $this->active_event_plugins = self::detect_active_event_plugins();
$this->activitypub_plugin_version = get_file_data( WP_PLUGIN_DIR . '/activitypub/activitypub.php', array( 'Version' ) )[0]; $this->activitypub_plugin_version = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ? constant( 'ACTIVITYPUB_PLUGIN_VERSION' ): '0';
$this->setup_hooks(); $this->setup_hooks();
} }
@ -166,7 +166,7 @@ class Setup {
); );
// Check if the minimum required version of the ActivityPub plugin is installed. // Check if the minimum required version of the ActivityPub plugin is installed.
if ( version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { if ( ! version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) {
return; return;
} }
@ -256,8 +256,8 @@ class Setup {
// If someone installs this plugin, we simply enable ActivityPub support for all currently active event post types. // If someone installs this plugin, we simply enable ActivityPub support for all currently active event post types.
$activitypub_supported_post_types = get_option( 'activitypub_support_post_types', array() ); $activitypub_supported_post_types = get_option( 'activitypub_support_post_types', array() );
foreach ( $this->active_event_plugins as $event_plugin ) { foreach ( $this->active_event_plugins as $event_plugin ) {
if ( ! in_array( $event_plugin['post_type'], $activitypub_supported_post_types, true ) ) { if ( ! in_array( $event_plugin->get_post_type(), $activitypub_supported_post_types, true ) ) {
$activitypub_supported_post_types[] = $event_plugin['post_type']; $activitypub_supported_post_types[] = $event_plugin->get_post_type();
} }
} }
update_option( 'activitypub_support_post_types', $activitypub_supported_post_types ); update_option( 'activitypub_support_post_types', $activitypub_supported_post_types );

View file

@ -26,15 +26,22 @@ if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
require_once "{$_tests_dir}/includes/functions.php"; require_once "{$_tests_dir}/includes/functions.php";
/** /**
* Manually load the plugin being tested. * Manually load the plugin being tested and its integrations.
*/ */
function _manually_load_plugin() { function _manually_load_plugin() {
require dirname( __DIR__ ) . '/activitypub-event-extensions.php'; $plugin_dir = ABSPATH . '/wp-content/plugins/';
require_once $plugin_dir . 'activitypub/activitypub.php';
$event_plugin = 'the-events-calendar'; $event_plugin = 'the-events-calendar';
switch ( $event_plugin ) { switch ( $event_plugin ) {
case 'the-events-calendar': case 'the-events-calendar':
require ABSPATH . '/wp-content/mu-plugins/the-events-calendar/the-events-calendar.php'; $plugin_file = 'the-events-calendar/the-events-calendar.php';
require_once $plugin_dir . $plugin_file;
$current = get_option( 'active_plugins', array() );
$current[] = $plugin_file;
sort( $current );
update_option( 'active_plugins', $current );
} }
require dirname( __DIR__ ) . '/activitypub-event-extensions.php';
} }
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

View file

@ -28,9 +28,45 @@ class Test_Sample extends WP_UnitTestCase {
// Get instance of our plugin. // Get instance of our plugin.
$aec = \Activitypub_Event_Extensions\Setup::get_instance(); $aec = \Activitypub_Event_Extensions\Setup::get_instance();
$this->assertContains( 'the-events-calendar', $aec->get_active_event_plugins() );
// 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(); $aec->activate_activitypub_support_for_active_event_plugins();
$this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) ); $this->assertContains( 'tribe_events', get_option( 'activitypub_support_post_types' ) );
$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();
$transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object );
$event_array = $transformer->to_object()->to_array();
$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( ) . 'T16:00:00Z', $event_array['commentsEnabled'] );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 16:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] );
} }
} }