From aa884055d6351191aa6c651f38990f193156bf30 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 20 Jan 2025 17:40:39 +0100 Subject: [PATCH] fix unittests --- includes/class-comment.php | 2 +- tests/includes/class-test-comment.php | 12 ++++++++++ .../transformer/class-test-factory.php | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/includes/class-comment.php b/includes/class-comment.php index 3c42d02be..b0ad16bc2 100644 --- a/includes/class-comment.php +++ b/includes/class-comment.php @@ -229,7 +229,7 @@ public static function should_be_federated( $comment ) { return false; } - if ( is_single_user() && \user_can( $user_id, 'publish_posts' ) ) { + if ( is_single_user() && \user_can( $user_id, 'activitypub' ) ) { // On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user. $user_id = Actors::BLOG_USER_ID; } diff --git a/tests/includes/class-test-comment.php b/tests/includes/class-test-comment.php index 1ef7b33c5..145034963 100644 --- a/tests/includes/class-test-comment.php +++ b/tests/includes/class-test-comment.php @@ -293,6 +293,9 @@ public function ability_to_federate_comment() { 'comment_content' => 'This is a sent comment.', 'comment_author_url' => 'https://example.com', 'comment_author_email' => '', + 'comment_meta' => array( + 'activitypub_status' => 'pending', + ), ), 'expected' => array( 'was_sent' => true, @@ -362,6 +365,9 @@ public function ability_to_federate_threaded_comment() { 'comment_content' => 'This is another comment.', 'comment_author_url' => 'https://example.com', 'comment_author_email' => '', + 'comment_meta' => array( + 'activitypub_status' => 'pending', + ), ), 'expected' => array( 'was_sent' => false, @@ -386,6 +392,9 @@ public function ability_to_federate_threaded_comment() { 'comment_content' => 'This is yet another comment.', 'comment_author_url' => 'https://example.com', 'comment_author_email' => '', + 'comment_meta' => array( + 'activitypub_status' => 'pending', + ), ), 'expected' => array( 'was_sent' => true, @@ -440,6 +449,9 @@ public function ability_to_federate_threaded_comment() { 'comment_content' => 'This is a parent comment that should not be possible.', 'comment_author_url' => 'https://example.com', 'comment_author_email' => '', + 'comment_meta' => array( + 'activitypub_status' => 'federated', + ), ), 'comment' => array( 'comment_type' => 'comment', diff --git a/tests/includes/transformer/class-test-factory.php b/tests/includes/transformer/class-test-factory.php index 9d8e38b54..59fff9c39 100644 --- a/tests/includes/transformer/class-test-factory.php +++ b/tests/includes/transformer/class-test-factory.php @@ -42,6 +42,13 @@ class Test_Factory extends WP_UnitTestCase { */ protected static $comment_id; + /** + * Test user ID. + * + * @var int + */ + protected static $user_id; + /** * Create fake data before tests run. * @@ -58,10 +65,20 @@ public static function wpSetUpBeforeClass( $factory ) { ) ); + self::$user_id = $factory->user->create( + array( + 'role' => 'administrator', + ) + ); + // Create test comment. self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id, + 'user_id' => self::$user_id, + 'comment_meta' => array( + 'activitypub_status' => 'pending', + ), ) ); } @@ -73,6 +90,7 @@ public static function wpTearDownAfterClass() { wp_delete_post( self::$post_id, true ); wp_delete_post( self::$attachment_id, true ); wp_delete_comment( self::$comment_id, true ); + wp_delete_user( self::$user_id, true ); } /** @@ -104,10 +122,16 @@ public function test_get_transformer_post() { * @covers ::get_transformer */ public function test_get_transformer_attachment() { + // Allow attachment to be federated. + \add_post_type_support( 'attachment', 'activitypub' ); + $attachment = get_post( self::$attachment_id ); $transformer = Factory::get_transformer( $attachment ); $this->assertInstanceOf( Attachment::class, $transformer ); + + // Remove support for attachment. + \remove_post_type_support( 'attachment', 'activitypub' ); } /**