wordpress-activitypub-event.../tests/test-class-reminder.php
André Menrath 4263233e10
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 40s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m7s
fix phpcs
2024-10-11 19:27:12 +02:00

183 lines
6.3 KiB
PHP

<?php
/**
* Test for Reminder class.
*
* @package ActivityPub_Event_Bridge
*/
/**
* Test class for testing the scheduling of reminder Activities.
*/
class Test_Reminder extends WP_UnitTestCase {
/**
* Mockup events of certain complexity.
*/
public const MOCKUP_VENUE = array(
'venue' => 'Minimal Venue',
'status' => 'publish',
);
public const MOCKUP_EVENT = array(
'title' => 'My Event',
'content' => 'Come to my event!',
'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 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_Bridge\Setup::get_instance();
$aec->activate_activitypub_support_for_active_event_plugins();
// Delete all posts afterwards.
_delete_all_posts();
}
/**
* Test that with the default reminder setting (time-gap is zero) no reminder event is scheduled.
*/
public function test_event_reminder_not_being_scheduled_by_default() {
// Create a The Events Calendar Event.
$wp_object = tribe_events()
->set_args( self::MOCKUP_EVENT )
->create();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertEquals( false, $scheduled_event );
}
/**
* Test that with a side-wide option the reminder is scheduled.
*/
public function test_event_reminder_scheduled_with_site_wide_option() {
\update_option( 'activitypub_event_bridge_reminder_time_gap', DAY_IN_SECONDS );
// Create a The Events Calendar Event.
$wp_object = tribe_events()
->set_args( self::MOCKUP_EVENT )
->create();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertNotEquals( false, $scheduled_event );
$this->assertEquals( strtotime( self::MOCKUP_EVENT['start_date'] ) - DAY_IN_SECONDS, $scheduled_event->timestamp );
$this->assertEquals( false, $scheduled_event->schedule );
$this->assertEquals( 'activitypub_event_bridge_send_event_reminder', $scheduled_event->hook );
}
/**
* Test that a specific event can override the side-wide reminder default.
*/
public function test_event_reminder_scheduled_with_per_event_override() {
\update_option( 'activitypub_event_bridge_reminder_time_gap', DAY_IN_SECONDS );
// Create a The Events Calendar Event.
$wp_object = tribe_events()
->set_args(
array_merge(
self::MOCKUP_EVENT,
array( 'activitypub_event_bridge_reminder_time_gap' => DAY_IN_SECONDS * 3 ),
)
)
->create();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertNotEquals( false, $scheduled_event );
$this->assertEquals( strtotime( self::MOCKUP_EVENT['start_date'] ) - DAY_IN_SECONDS * 3, $scheduled_event->timestamp );
$this->assertEquals( false, $scheduled_event->schedule );
$this->assertEquals( 'activitypub_event_bridge_send_event_reminder', $scheduled_event->hook );
// Now update the option once more to see if the schedule got updated too.
$post_id = array_key_first(
tribe_events( $wp_object->ID )
->set_args(
array( 'activitypub_event_bridge_reminder_time_gap' => HOUR_IN_SECONDS ),
)
->save()
);
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $post_id ) );
$this->assertNotEquals( false, $scheduled_event );
$this->assertEquals( strtotime( self::MOCKUP_EVENT['start_date'] ) - HOUR_IN_SECONDS, $scheduled_event->timestamp );
}
/**
* Test that the scheduled reminder is removed when the event is deleted.
*/
public function test_event_reminder_deleted_event() {
\update_option( 'activitypub_event_bridge_reminder_time_gap', DAY_IN_SECONDS );
// Create a The Events Calendar Event.
$wp_object = tribe_events()
->set_args(
array_merge(
self::MOCKUP_EVENT,
array( 'activitypub_event_bridge_reminder_time_gap' => DAY_IN_SECONDS * 3 ),
)
)
->create();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertNotEquals( false, $scheduled_event );
$this->assertEquals( strtotime( self::MOCKUP_EVENT['start_date'] ) - DAY_IN_SECONDS * 3, $scheduled_event->timestamp );
$this->assertEquals( false, $scheduled_event->schedule );
$this->assertEquals( 'activitypub_event_bridge_send_event_reminder', $scheduled_event->hook );
// Now delete the event.
tribe_events( $wp_object->ID )->delete();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertEquals( false, $scheduled_event );
}
/**
* Test that the scheduled reminder is removed when the event is moved to trash.
*/
public function test_event_reminder_event_moved_to_trash() {
\update_option( 'activitypub_event_bridge_reminder_time_gap', DAY_IN_SECONDS );
// Create a The Events Calendar Event.
$wp_object = tribe_events()
->set_args(
array_merge(
self::MOCKUP_EVENT,
array( 'activitypub_event_bridge_reminder_time_gap' => DAY_IN_SECONDS * 3 ),
)
)
->create();
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertNotEquals( false, $scheduled_event );
$this->assertEquals( strtotime( self::MOCKUP_EVENT['start_date'] ) - DAY_IN_SECONDS * 3, $scheduled_event->timestamp );
$this->assertEquals( false, $scheduled_event->schedule );
$this->assertEquals( 'activitypub_event_bridge_send_event_reminder', $scheduled_event->hook );
// Now move the event to the trash.
$post_id = array_key_first(
tribe_events( $wp_object->ID )
->set_args(
array( 'post_status' => 'trash' ),
)
->save()
);
$scheduled_event = \wp_get_scheduled_event( 'activitypub_event_bridge_send_event_reminder', array( $wp_object->ID ) );
$this->assertEquals( false, $scheduled_event );
}
}