Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions disqus/rest-api/class-disqus-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ private function validate_disqus_post_data( $post ) {
*/
private function comment_data_from_post( $post ) {
$thread = array_key_exists( 'threadData', $post ) ? $post['threadData'] : $post['thread'];
$author = $post['author'];
$author = isset( $post['author'] ) ? $post['author'] : null;

$wp_post_id = null;

Expand Down Expand Up @@ -782,12 +782,14 @@ private function comment_data_from_post( $post ) {
// Email is a special permission for Disqus API applications and won't be present
// if the user has not set the permission for their API application.
$author_email = null;
if ( isset( $author['email'] ) ) {
$author_email = $author['email'];
} elseif ( $author['isAnonymous'] ) {
$author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com';
} else {
$author_email = 'user-' . $author['id'] . '@disqus.com';
if ( $author ) {
if ( isset( $author['email'] ) ) {
$author_email = $author['email'];
} elseif ( isset( $author['isAnonymous'] ) && $author['isAnonymous'] ) {
$author_email = 'anonymized-' . md5( $author['name'] ) . '@disqus.com';
} elseif ( isset( $author['id'] ) ) {
$author_email = 'user-' . $author['id'] . '@disqus.com';
}
}

// Translate the comment approval state.
Expand All @@ -804,10 +806,10 @@ private function comment_data_from_post( $post ) {

return array(
'comment_post_ID' => (int) $wp_post_id,
'comment_author' => $author['name'],
'comment_author' => $author && isset( $author['name'] ) ? $author['name'] : 'Anonymous',
'comment_author_email' => $author_email,
'comment_author_IP' => $post['ipAddress'],
'comment_author_url' => isset( $author['url'] ) ? $author['url'] : '',
'comment_author_url' => $author && isset( $author['url'] ) ? $author['url'] : '',
'comment_content' => $post['raw_message'],
'comment_date' => $post['createdAt'],
'comment_date_gmt' => $post['createdAt'],
Expand Down
93 changes: 93 additions & 0 deletions tests/test-rest-api-comment-data-from-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/*
* Tests for Disqus_Rest_Api::comment_data_from_post author email logic.
*/

class Test_Disqus_Rest_Api_Comment_Data_From_Post extends WP_UnitTestCase {
protected $disqus_rest_api;

public function set_up() {
parent::set_up();
$this->disqus_rest_api = new Disqus_Rest_Api( null, null );
}

private function call_comment_data_from_post( $post ) {
$class = new ReflectionClass( 'Disqus_Rest_Api' );
$method = $class->getMethod( 'comment_data_from_post' );
$method->setAccessible( true );
return $method->invokeArgs( $this->disqus_rest_api, array( $post ) );
}

public function test_email_from_author_email() {
$post = [
'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ],
'author' => [ 'email' => 'foo@example.com', 'name' => 'Foo' ],
'parent' => null,
'ipAddress' => '127.0.0.1',
'raw_message' => 'Test',
'createdAt' => '2020-01-01 00:00:00',
'isApproved' => true,
'isDeleted' => false,
'isSpam' => false,
'id' => 123,
'forum' => get_option( 'disqus_forum_url' ),
];
$data = $this->call_comment_data_from_post( $post );
$this->assertEquals( 'foo@example.com', $data['comment_author_email'] );
}

public function test_email_from_anonymous_author() {
$post = [
'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ],
'author' => [ 'isAnonymous' => true, 'name' => 'Anon' ],
'parent' => null,
'ipAddress' => '127.0.0.1',
'raw_message' => 'Test',
'createdAt' => '2020-01-01 00:00:00',
'isApproved' => true,
'isDeleted' => false,
'isSpam' => false,
'id' => 123,
'forum' => get_option( 'disqus_forum_url' ),
];
$data = $this->call_comment_data_from_post( $post );
$this->assertStringStartsWith( 'anonymized-', $data['comment_author_email'] );
$this->assertStringEndsWith( '@disqus.com', $data['comment_author_email'] );
}

public function test_email_from_author_id() {
$post = [
'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ],
'author' => [ 'id' => 42 ],
'parent' => null,
'ipAddress' => '127.0.0.1',
'raw_message' => 'Test',
'createdAt' => '2020-01-01 00:00:00',
'isApproved' => true,
'isDeleted' => false,
'isSpam' => false,
'id' => 123,
'forum' => get_option( 'disqus_forum_url' ),
];
$data = $this->call_comment_data_from_post( $post );
$this->assertEquals( 'user-42@disqus.com', $data['comment_author_email'] );
}

public function test_email_when_no_author() {
$post = [
'thread' => [ 'id' => 1, 'identifiers' => [ '1' ] ],
'author' => null,
'parent' => null,
'ipAddress' => '127.0.0.1',
'raw_message' => 'Test',
'createdAt' => '2020-01-01 00:00:00',
'isApproved' => true,
'isDeleted' => false,
'isSpam' => false,
'id' => 123,
'forum' => get_option( 'disqus_forum_url' ),
];
$data = $this->call_comment_data_from_post( $post );
$this->assertNull( $data['comment_author_email'] );
}
}