From 7fa58cf26c38b4052b502598b8d2a2fdaf4793cf Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:23:23 +0800 Subject: [PATCH] add first unit tests for class inbox --- includes/rest/class-inbox.php | 6 ++- tests/test-class-inbox.php | 69 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/test-class-inbox.php diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 38d1d50..8dc8ae4 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -431,7 +431,11 @@ class Inbox { * * @return array Comment data suitable for creating a comment. */ - private static function convert_object_to_comment_data( $object ) { + public static function convert_object_to_comment_data( $object ) { + if ( ! isset( $object['object'] ) ) { + return false; + } + // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email diff --git a/tests/test-class-inbox.php b/tests/test-class-inbox.php new file mode 100644 index 0000000..4b13e1d --- /dev/null +++ b/tests/test-class-inbox.php @@ -0,0 +1,69 @@ +user_url = $authordata->user_url; + + $post = \wp_insert_post( + array( + 'post_author' => 1, + 'post_content' => 'test', + ) + ); + $this->post_permalink = \get_permalink( $post ); + + \add_filter( 'pre_get_remote_metadata_by_actor', array( '\Test_Inbox', 'get_remote_metadata_by_actor' ), 10, 2); + } + + public function get_remote_metadata_by_actor( $value, $actor ) { + return array( + "name" => "Example User", + "icon" => array( + "url" => "https://example.com/icon", + ), + ); + } + + public function test_convert_object_to_comment_data_basic() { + $inbox = new \Activitypub\Rest\Inbox(); + $object = array( + "actor" => $this->user_url, + "to" => [ $this->user_url ], + "cc" => [ "https://www.w3.org/ns/activitystreams#Public" ], + "object" => array( + "id" => "123", + "url" => "https://example.com/example", + "inReplyTo" => $this->post_permalink, + "content" => "example", + ), + ); + $converted = $inbox->convert_object_to_comment_data($object); + + $this->assertGreaterThan(1, $converted["comment_post_ID"]); + $this->assertEquals($converted["comment_author"], "Example User"); + $this->assertEquals($converted["comment_author_url"], "http://example.org"); + $this->assertEquals($converted["comment_content"], "example"); + $this->assertEquals($converted["comment_type"], ""); + $this->assertEquals($converted["comment_author_email"], ""); + $this->assertEquals($converted["comment_parent"], 0); + $this->assertArrayHasKey("comment_meta", $converted); + $this->assertEquals($converted["comment_meta"]["source_id"], "http://123"); + $this->assertEquals($converted["comment_meta"]["source_url"], "https://example.com/example"); + $this->assertEquals($converted["comment_meta"]["avatar_url"], "https://example.com/icon"); + $this->assertEquals($converted["comment_meta"]["protocol"], "activitypub"); + } + + public function test_convert_object_to_comment_data_non_public_rejected() { + $inbox = new \Activitypub\Rest\Inbox(); + $object = array( + "to" => ["https://example.com/profile/test"], + "cc" => [], + ); + $converted = $inbox->convert_object_to_comment_data($object); + $this->assertFalse($converted); + } +}