@@ -597,4 +597,99 @@ public function test_create_comment_outbox_items_batching() {
597597 $ result = Migration::create_comment_outbox_items ( 1 , 1000 );
598598 $ this ->assertNull ( $ result );
599599 }
600+
601+ /**
602+ * Test update_comment_author_emails updates emails with webfinger addresses.
603+ *
604+ * @covers ::update_comment_author_emails
605+ */
606+ public function test_update_comment_author_emails () {
607+ $ author_url = 'https://example.com/users/test ' ;
608+ $ comment_id = self ::factory ()->comment ->create (
609+ array (
610+ 'comment_post_ID ' => self ::$ fixtures ['posts ' ][0 ],
611+ 'comment_author ' => 'Test User ' ,
612+ 'comment_author_url ' => $ author_url ,
613+ 'comment_author_email ' => '' ,
614+ 'comment_type ' => 'comment ' ,
615+ 'comment_meta ' => array ( 'protocol ' => 'activitypub ' ),
616+ )
617+ );
618+
619+ // Mock the HTTP request.
620+ \add_filter ( 'pre_http_request ' , array ( $ this , 'mock_webfinger ' ) );
621+
622+ $ result = Migration::update_comment_author_emails ( 50 , 0 );
623+
624+ $ this ->assertNull ( $ result );
625+
626+ $ updated_comment = \get_comment ( $ comment_id );
627+ $ this ->
assertEquals (
'[email protected] ' ,
$ updated_comment->
comment_author_email );
628+
629+ // Clean up.
630+ \remove_filter ( 'pre_http_request ' , array ( $ this , 'mock_webfinger ' ) );
631+ \wp_delete_comment ( $ comment_id , true );
632+ }
633+
634+ /**
635+ * Test update_comment_author_emails handles batching correctly.
636+ *
637+ * @covers ::update_comment_author_emails
638+ */
639+ public function test_update_comment_author_emails_batching () {
640+ // Create multiple comments.
641+ $ comment_ids = array ();
642+ for ( $ i = 0 ; $ i < 3 ; $ i ++ ) {
643+ $ comment_ids [] = self ::factory ()->comment ->create (
644+ array (
645+ 'comment_post_ID ' => self ::$ fixtures ['posts ' ][0 ],
646+ 'comment_author ' => "Test User $ i " ,
647+ 'comment_author_url ' => "https://example.com/users/test $ i " ,
648+ 'comment_author_email ' => '' ,
649+ 'comment_content ' => "Test comment $ i " ,
650+ 'comment_type ' => 'comment ' ,
651+ 'comment_meta ' => array ( 'protocol ' => 'activitypub ' ),
652+ )
653+ );
654+ }
655+
656+ // Mock the HTTP request.
657+ \add_filter ( 'pre_http_request ' , array ( $ this , 'mock_webfinger ' ) );
658+
659+ // Process first batch of 2 comments.
660+ $ result = Migration::update_comment_author_emails ( 2 , 0 );
661+ $ this ->assertEqualSets (
662+ array (
663+ 'batch_size ' => 2 ,
664+ 'offset ' => 2 ,
665+ ),
666+ $ result
667+ );
668+
669+ // Process second batch with remaining comment.
670+ $ result = Migration::update_comment_author_emails ( 2 , 2 );
671+ $ this ->assertNull ( $ result );
672+
673+ // Verify all comments were updated.
674+ foreach ( $ comment_ids as $ comment_id ) {
675+ $ comment = \get_comment ( $ comment_id );
676+ $ this ->
assertEquals (
'[email protected] ' ,
$ comment->
comment_author_email );
677+
678+ wp_delete_comment ( $ comment_id , true );
679+ }
680+
681+ \remove_filter ( 'pre_http_request ' , array ( $ this , 'mock_webfinger ' ) );
682+ }
683+
684+ /**
685+ * Mock webfinger response.
686+ *
687+ * @return array
688+ */
689+ public function mock_webfinger () {
690+ return array (
691+ 'body ' =>
wp_json_encode (
array (
'subject ' =>
'acct:[email protected] ' ) ),
692+ 'response ' => array ( 'code ' => 200 ),
693+ );
694+ }
600695}
0 commit comments