@@ -579,4 +579,148 @@ public function test_add_activity_with_duplicate_recipients() {
579579 $ this ->assertContains ( 2 , $ recipients );
580580 $ this ->assertContains ( 3 , $ recipients );
581581 }
582+
583+ /**
584+ * Test add_recipients function.
585+ *
586+ * @covers ::add_recipients
587+ */
588+ public function test_add_recipients () {
589+ $ activity = new Activity ();
590+ $ activity ->set_id ( 'https://remote.example.com/activities/add-recipients ' );
591+ $ activity ->set_type ( 'Create ' );
592+ $ activity ->set_actor ( 'https://remote.example.com/users/testuser ' );
593+
594+ $ object = new Base_Object ();
595+ $ object ->set_id ( 'https://remote.example.com/objects/add-recipients ' );
596+ $ object ->set_type ( 'Note ' );
597+ $ activity ->set_object ( $ object );
598+
599+ $ inbox_id = Inbox::add ( $ activity , 1 );
600+
601+ // Add multiple recipients at once.
602+ Inbox::add_recipients ( $ inbox_id , array ( 2 , 3 , 4 ) );
603+
604+ // Verify all recipients were added.
605+ $ recipients = Inbox::get_recipients ( $ inbox_id );
606+ $ this ->assertCount ( 4 , $ recipients );
607+ $ this ->assertContains ( 1 , $ recipients );
608+ $ this ->assertContains ( 2 , $ recipients );
609+ $ this ->assertContains ( 3 , $ recipients );
610+ $ this ->assertContains ( 4 , $ recipients );
611+ }
612+
613+ /**
614+ * Test deduplicate function with no duplicates.
615+ *
616+ * @covers ::deduplicate
617+ */
618+ public function test_deduplicate_no_duplicates () {
619+ $ activity = new Activity ();
620+ $ activity ->set_id ( 'https://remote.example.com/activities/single-item ' );
621+ $ activity ->set_type ( 'Create ' );
622+ $ activity ->set_actor ( 'https://remote.example.com/users/testuser ' );
623+
624+ $ object = new Base_Object ();
625+ $ object ->set_id ( 'https://remote.example.com/objects/single-item ' );
626+ $ object ->set_type ( 'Note ' );
627+ $ activity ->set_object ( $ object );
628+
629+ $ inbox_id = Inbox::add ( $ activity , 1 );
630+
631+ // Deduplicate should return the same post.
632+ $ result = Inbox::deduplicate ( 'https://remote.example.com/activities/single-item ' );
633+ $ this ->assertInstanceOf ( 'WP_Post ' , $ result );
634+ $ this ->assertEquals ( $ inbox_id , $ result ->ID );
635+ }
636+
637+ /**
638+ * Test deduplicate function with duplicates.
639+ *
640+ * @covers ::deduplicate
641+ */
642+ public function test_deduplicate_with_duplicates () {
643+ $ activity = new Activity ();
644+ $ activity ->set_id ( 'https://remote.example.com/activities/duplicate-guid ' );
645+ $ activity ->set_type ( 'Create ' );
646+ $ activity ->set_actor ( 'https://remote.example.com/users/testuser ' );
647+
648+ $ object = new Base_Object ();
649+ $ object ->set_id ( 'https://remote.example.com/objects/duplicate-guid ' );
650+ $ object ->set_type ( 'Note ' );
651+ $ activity ->set_object ( $ object );
652+
653+ // Manually create duplicate inbox posts with same GUID.
654+ $ inbox_id_1 = \wp_insert_post (
655+ array (
656+ 'post_type ' => Inbox::POST_TYPE ,
657+ 'post_status ' => 'publish ' ,
658+ 'post_content ' => \wp_json_encode ( $ activity ->to_array () ),
659+ 'guid ' => 'https://remote.example.com/activities/duplicate-guid ' ,
660+ )
661+ );
662+ \add_post_meta ( $ inbox_id_1 , '_activitypub_user_id ' , 1 );
663+ \add_post_meta ( $ inbox_id_1 , '_activitypub_user_id ' , 2 );
664+
665+ $ inbox_id_2 = \wp_insert_post (
666+ array (
667+ 'post_type ' => Inbox::POST_TYPE ,
668+ 'post_status ' => 'publish ' ,
669+ 'post_content ' => \wp_json_encode ( $ activity ->to_array () ),
670+ 'guid ' => 'https://remote.example.com/activities/duplicate-guid ' ,
671+ )
672+ );
673+ \add_post_meta ( $ inbox_id_2 , '_activitypub_user_id ' , 3 );
674+ \add_post_meta ( $ inbox_id_2 , '_activitypub_user_id ' , 4 );
675+
676+ $ inbox_id_3 = \wp_insert_post (
677+ array (
678+ 'post_type ' => Inbox::POST_TYPE ,
679+ 'post_status ' => 'publish ' ,
680+ 'post_content ' => \wp_json_encode ( $ activity ->to_array () ),
681+ 'guid ' => 'https://remote.example.com/activities/duplicate-guid ' ,
682+ )
683+ );
684+ \add_post_meta ( $ inbox_id_3 , '_activitypub_user_id ' , 5 );
685+
686+ // Run deduplication.
687+ $ result = Inbox::deduplicate ( 'https://remote.example.com/activities/duplicate-guid ' );
688+
689+ // Should return the first post.
690+ $ this ->assertInstanceOf ( 'WP_Post ' , $ result );
691+ $ this ->assertEquals ( $ inbox_id_1 , $ result ->ID );
692+
693+ // Verify all recipients were merged.
694+ $ recipients = Inbox::get_recipients ( $ inbox_id_1 );
695+ $ this ->assertCount ( 5 , $ recipients );
696+ $ this ->assertContains ( 1 , $ recipients );
697+ $ this ->assertContains ( 2 , $ recipients );
698+ $ this ->assertContains ( 3 , $ recipients );
699+ $ this ->assertContains ( 4 , $ recipients );
700+ $ this ->assertContains ( 5 , $ recipients );
701+
702+ // Verify duplicates were deleted.
703+ $ this ->assertNull ( \get_post ( $ inbox_id_2 ) );
704+ $ this ->assertNull ( \get_post ( $ inbox_id_3 ) );
705+
706+ // Verify only one post exists with this GUID.
707+ $ posts = \get_posts (
708+ array (
709+ 'post_type ' => Inbox::POST_TYPE ,
710+ 'guid ' => 'https://remote.example.com/activities/duplicate-guid ' ,
711+ 'posts_per_page ' => -1 ,
712+ )
713+ );
714+ $ this ->assertCount ( 1 , $ posts );
715+ }
716+
717+ /**
718+ * Test deduplicate function with non-existent GUID.
719+ *
720+ * @covers ::deduplicate
721+ */
722+ public function test_deduplicate_non_existent () {
723+ $ result = Inbox::deduplicate ( 'https://remote.example.com/activities/non-existent ' );
724+ $ this ->assertFalse ( $ result );
725+ }
582726}
0 commit comments