@@ -427,4 +427,70 @@ function ( $event ) use ( &$scheduled_events ) {
427427 wp_delete_post ( $ announce_outbox_id , true );
428428 remove_all_filters ( 'schedule_event ' );
429429 }
430+
431+ /**
432+ * Test cleanup_remote_actors method.
433+ *
434+ * @covers ::cleanup_remote_actors
435+ */
436+ public function test_cleanup_remote_actors () {
437+ // Mock actor metadata.
438+ \add_filter (
439+ 'activitypub_pre_http_get_remote_object ' ,
440+ function () {
441+ return array (
442+ 'name ' => 'Test User ' ,
443+ 'preferredUsername ' => 'test ' ,
444+ 'id ' => 'https://example.com/users/test ' ,
445+ 'url ' => 'https://example.com/@test ' ,
446+ 'inbox ' => 'https://example.com/users/test/inbox ' ,
447+ );
448+ }
449+ );
450+
451+ $ actor = Actors::fetch_remote_by_uri ( 'https://example.com/users/test ' );
452+
453+ for ( $ i = 0 ; $ i < 6 ; $ i ++ ) {
454+ Actors::add_error ( $ actor ->ID , 'Failed to fetch or parse metadata ' . $ i );
455+ }
456+
457+ // Track scheduled events.
458+ $ scheduled_events = array ();
459+ \add_filter (
460+ 'schedule_event ' ,
461+ function ( $ event ) use ( &$ scheduled_events ) {
462+ if ( 'activitypub_delete_actor_interactions ' === $ event ->hook ) {
463+ $ scheduled_events [] = array (
464+ 'hook ' => $ event ->hook ,
465+ 'args ' => $ event ->args ,
466+ 'time ' => $ event ->timestamp ,
467+ );
468+ }
469+ return $ event ;
470+ }
471+ );
472+ \add_filter (
473+ 'pre_get_remote_metadata_by_actor ' ,
474+ function () {
475+ return new \WP_Error ( 'no_actor ' , 'No actor found ' );
476+ }
477+ );
478+
479+ // Run the cleanup function.
480+ Scheduler::cleanup_remote_actors ();
481+
482+ // Verify that the event was scheduled with the actor URL as parameter.
483+ $ this ->assertCount ( 1 , $ scheduled_events , 'Should schedule 1 event ' );
484+ $ this ->assertEquals ( 'activitypub_delete_actor_interactions ' , $ scheduled_events [0 ]['hook ' ], 'Should schedule the correct hook ' );
485+ $ this ->assertCount ( 1 , $ scheduled_events [0 ]['args ' ], 'Should have 1 argument ' );
486+ $ this ->assertEquals ( 'https://example.com/users/test ' , $ scheduled_events [0 ]['args ' ][0 ], 'Should pass actor URL as parameter ' );
487+
488+ // Verify the actor was deleted.
489+ $ this ->assertNull ( \get_post ( $ actor ->ID ), 'Actor should be deleted ' );
490+
491+ // Clean up.
492+ \remove_all_filters ( 'activitypub_pre_http_get_remote_object ' );
493+ \remove_all_filters ( 'pre_get_remote_metadata_by_actor ' );
494+ \remove_all_filters ( 'schedule_event ' );
495+ }
430496}
0 commit comments