-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add webfinger as comment author email if available (#1374)
* Add webfinger as comment author email if available * Add upgrade routine * Add unit tests * Changelog * add doc to reverse-discovery spec --------- Co-authored-by: Matthias Pfefferle <[email protected]>
- Loading branch information
Showing
7 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,4 +597,99 @@ public function test_create_comment_outbox_items_batching() { | |
$result = Migration::create_comment_outbox_items( 1, 1000 ); | ||
$this->assertNull( $result ); | ||
} | ||
|
||
/** | ||
* Test update_comment_author_emails updates emails with webfinger addresses. | ||
* | ||
* @covers ::update_comment_author_emails | ||
*/ | ||
public function test_update_comment_author_emails() { | ||
$author_url = 'https://example.com/users/test'; | ||
$comment_id = self::factory()->comment->create( | ||
array( | ||
'comment_post_ID' => self::$fixtures['posts'][0], | ||
'comment_author' => 'Test User', | ||
'comment_author_url' => $author_url, | ||
'comment_author_email' => '', | ||
'comment_type' => 'comment', | ||
'comment_meta' => array( 'protocol' => 'activitypub' ), | ||
) | ||
); | ||
|
||
// Mock the HTTP request. | ||
\add_filter( 'pre_http_request', array( $this, 'mock_webfinger' ) ); | ||
|
||
$result = Migration::update_comment_author_emails( 50, 0 ); | ||
|
||
$this->assertNull( $result ); | ||
|
||
$updated_comment = \get_comment( $comment_id ); | ||
$this->assertEquals( '[email protected]', $updated_comment->comment_author_email ); | ||
|
||
// Clean up. | ||
\remove_filter( 'pre_http_request', array( $this, 'mock_webfinger' ) ); | ||
\wp_delete_comment( $comment_id, true ); | ||
} | ||
|
||
/** | ||
* Test update_comment_author_emails handles batching correctly. | ||
* | ||
* @covers ::update_comment_author_emails | ||
*/ | ||
public function test_update_comment_author_emails_batching() { | ||
// Create multiple comments. | ||
$comment_ids = array(); | ||
for ( $i = 0; $i < 3; $i++ ) { | ||
$comment_ids[] = self::factory()->comment->create( | ||
array( | ||
'comment_post_ID' => self::$fixtures['posts'][0], | ||
'comment_author' => "Test User $i", | ||
'comment_author_url' => "https://example.com/users/test$i", | ||
'comment_author_email' => '', | ||
'comment_content' => "Test comment $i", | ||
'comment_type' => 'comment', | ||
'comment_meta' => array( 'protocol' => 'activitypub' ), | ||
) | ||
); | ||
} | ||
|
||
// Mock the HTTP request. | ||
\add_filter( 'pre_http_request', array( $this, 'mock_webfinger' ) ); | ||
|
||
// Process first batch of 2 comments. | ||
$result = Migration::update_comment_author_emails( 2, 0 ); | ||
$this->assertEqualSets( | ||
array( | ||
'batch_size' => 2, | ||
'offset' => 2, | ||
), | ||
$result | ||
); | ||
|
||
// Process second batch with remaining comment. | ||
$result = Migration::update_comment_author_emails( 2, 2 ); | ||
$this->assertNull( $result ); | ||
|
||
// Verify all comments were updated. | ||
foreach ( $comment_ids as $comment_id ) { | ||
$comment = \get_comment( $comment_id ); | ||
$this->assertEquals( '[email protected]', $comment->comment_author_email ); | ||
|
||
wp_delete_comment( $comment_id, true ); | ||
} | ||
|
||
\remove_filter( 'pre_http_request', array( $this, 'mock_webfinger' ) ); | ||
} | ||
|
||
/** | ||
* Mock webfinger response. | ||
* | ||
* @return array | ||
*/ | ||
public function mock_webfinger() { | ||
return array( | ||
'body' => wp_json_encode( array( 'subject' => 'acct:[email protected]' ) ), | ||
'response' => array( 'code' => 200 ), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -471,4 +471,35 @@ function () { | |
|
||
remove_all_filters( 'pre_get_remote_metadata_by_actor' ); | ||
} | ||
|
||
/** | ||
* Test activity_to_comment sets webfinger as comment author email. | ||
* | ||
* @covers ::activity_to_comment | ||
*/ | ||
public function test_activity_to_comment_sets_webfinger_email() { | ||
$actor_url = 'https://example.com/users/tester'; | ||
$activity = array( | ||
'type' => 'Create', | ||
'actor' => $actor_url, | ||
'object' => array( | ||
'content' => 'Test comment content', | ||
'id' => 'https://example.com/activities/1', | ||
), | ||
); | ||
|
||
$filter = function () { | ||
return array( | ||
'body' => wp_json_encode( array( 'subject' => 'acct:[email protected]' ) ), | ||
'response' => array( 'code' => 200 ), | ||
); | ||
}; | ||
\add_filter( 'pre_http_request', $filter ); | ||
|
||
$comment_data = Interactions::activity_to_comment( $activity ); | ||
|
||
$this->assertEquals( '[email protected]', $comment_data['comment_author_email'] ); | ||
|
||
\remove_filter( 'pre_http_request', $filter ); | ||
} | ||
} |