|
7 | 7 |
|
8 | 8 | namespace Activitypub\Tests; |
9 | 9 |
|
10 | | -use Activitypub\Activity\Activity; |
11 | 10 | use Activitypub\Scheduler; |
12 | | -use Activitypub\Collection\Outbox; |
| 11 | +use Activitypub\Activity\Activity; |
13 | 12 | use Activitypub\Activity\Base_Object; |
14 | | -use WP_UnitTestCase; |
| 13 | +use Activitypub\Collection\Outbox; |
| 14 | +use Activitypub\Collection\Actors; |
| 15 | + |
15 | 16 | use function Activitypub\add_to_outbox; |
16 | 17 |
|
17 | 18 | /** |
18 | 19 | * Test class for Scheduler. |
19 | 20 | * |
20 | 21 | * @coversDefaultClass \Activitypub\Scheduler |
21 | 22 | */ |
22 | | -class Test_Scheduler extends WP_UnitTestCase { |
| 23 | +class Test_Scheduler extends \WP_UnitTestCase { |
23 | 24 | /** |
24 | 25 | * Test user ID. |
25 | 26 | * |
@@ -285,4 +286,68 @@ public function test_async_batch_with_invalid_callback() { |
285 | 286 | // Run async_batch with invalid callback. |
286 | 287 | Scheduler::async_batch( array( $mock_class, 'callback' ) ); |
287 | 288 | } |
| 289 | + |
| 290 | + /** |
| 291 | + * Test schedule_announce_activity method. |
| 292 | + * |
| 293 | + * @covers ::schedule_announce_activity |
| 294 | + */ |
| 295 | + public function test_schedule_announce_activity() { |
| 296 | + // Set the actor mode to both blog and user mode. |
| 297 | + \update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE ); |
| 298 | + |
| 299 | + $activity = new Activity(); |
| 300 | + $activity->set_type( 'Create' ); |
| 301 | + $activity->set_id( 'https://example.com/test-id' ); |
| 302 | + |
| 303 | + // Create a Note object for the activity. |
| 304 | + $note = new Base_Object(); |
| 305 | + $note->set_type( 'Note' ); |
| 306 | + $note->set_content( 'Test content' ); |
| 307 | + $note->set_id( 'https://example.com/note/1' ); |
| 308 | + $activity->set_object( $note ); |
| 309 | + |
| 310 | + $outbox_activity_id = Outbox::add( $activity, self::$user_id ); |
| 311 | + |
| 312 | + $scheduled_events = array(); |
| 313 | + add_filter( |
| 314 | + 'schedule_event', |
| 315 | + function ( $event ) use ( &$scheduled_events ) { |
| 316 | + if ( 'activitypub_process_outbox' === $event->hook ) { |
| 317 | + $scheduled_events[] = $event->args[0]; |
| 318 | + } |
| 319 | + return $event; |
| 320 | + } |
| 321 | + ); |
| 322 | + |
| 323 | + Scheduler::schedule_announce_activity( $outbox_activity_id, $activity, self::$user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ); |
| 324 | + |
| 325 | + // Get the most recent outbox item for the blog actor. |
| 326 | + $announce_outbox_items = get_posts( |
| 327 | + array( |
| 328 | + 'post_type' => Outbox::POST_TYPE, |
| 329 | + 'post_author' => Actors::BLOG_USER_ID, |
| 330 | + 'post_status' => 'pending', |
| 331 | + 'orderby' => 'ID', |
| 332 | + 'order' => 'DESC', |
| 333 | + 'posts_per_page' => 1, |
| 334 | + ) |
| 335 | + ); |
| 336 | + |
| 337 | + $this->assertNotEmpty( $announce_outbox_items, 'No announce outbox items found' ); |
| 338 | + $announce_outbox_id = $announce_outbox_items[0]->ID; |
| 339 | + |
| 340 | + $this->assertCount( 1, $scheduled_events, 'Should schedule 1 event' ); |
| 341 | + $this->assertContains( $announce_outbox_id, $scheduled_events, 'Should schedule the announce outbox activity' ); |
| 342 | + |
| 343 | + // Check for Announce activity in the outbox. |
| 344 | + $announce_post = get_post( $announce_outbox_id ); |
| 345 | + $announce_activity = json_decode( $announce_post->post_content, true ); |
| 346 | + $this->assertEquals( 'Announce', $announce_activity['type'] ); |
| 347 | + |
| 348 | + // Clean up. |
| 349 | + wp_delete_post( $outbox_activity_id, true ); |
| 350 | + wp_delete_post( $announce_outbox_id, true ); |
| 351 | + remove_all_filters( 'schedule_event' ); |
| 352 | + } |
288 | 353 | } |
0 commit comments