wordpress-activitypub/tests/test-class-activitypub-comment.php
André Menrath 7370e97b5a
Add replies collection (#876)
* typo in phpdoc

* add first draft for adding replies collections to posts and comments

* refactoring

* Fix php CodeSniffer violations

* fix typo in php comment

* add draft for testing replies

* replies: test with own comment

* fix basic test for replies collection

* Restrict 'type' parameter for replies to 'post' or 'comment' in REST API

* some cleanups

* prefer ID over URL

* rename to `reply_id` to make clear that it is not the WordPress comment_id

* modularize retrieving of comment link via comment meta

* fix phpcs

* I think we should be more precise with this

and maybe there are other fallbacks coming

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
2024-09-25 13:24:35 +02:00

306 lines
9.5 KiB
PHP

<?php
class Test_Activitypub_Comment extends WP_UnitTestCase {
public function test_get_source_id_or_url() {
$comment_id = wp_insert_comment(
array(
'comment_type' => 'comment id',
'comment_content' => 'This is a comment id test',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
'source_id' => 'https://example.com/id',
),
)
);
$this->assertEquals( 'https://example.com/id', \Activitypub\Comment::get_source_url( $comment_id ) );
$this->assertEquals( 'https://example.com/id', \Activitypub\Comment::get_source_id( $comment_id ) );
$this->assertEquals( 'https://example.com/id', \Activitypub\Comment::get_source_id( $comment_id, false ) );
$this->assertEquals( null, \Activitypub\Comment::get_source_url( $comment_id, false ) );
$comment_id = wp_insert_comment(
array(
'comment_type' => 'comment url',
'comment_content' => 'This is a comment url test',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
'source_url' => 'https://example.com/url',
),
)
);
$this->assertEquals( 'https://example.com/url', \Activitypub\Comment::get_source_id( $comment_id ) );
$this->assertEquals( 'https://example.com/url', \Activitypub\Comment::get_source_url( $comment_id ) );
$this->assertEquals( 'https://example.com/url', \Activitypub\Comment::get_source_url( $comment_id, false ) );
$this->assertEquals( null, \Activitypub\Comment::get_source_id( $comment_id, false ) );
$comment_id = wp_insert_comment(
array(
'comment_type' => 'comment url and id',
'comment_content' => 'This is a comment url and id test',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
'source_url' => 'https://example.com/url',
'source_id' => 'https://example.com/id',
),
)
);
$this->assertEquals( 'https://example.com/id', \Activitypub\Comment::get_source_id( $comment_id ) );
$this->assertEquals( 'https://example.com/id', \Activitypub\Comment::get_source_id( $comment_id, false ) );
$this->assertEquals( 'https://example.com/url', \Activitypub\Comment::get_source_url( $comment_id ) );
$this->assertEquals( 'https://example.com/url', \Activitypub\Comment::get_source_url( $comment_id, false ) );
}
/**
* @dataProvider ability_to_federate_comment
*/
public function test_check_ability_to_federate_comment( $comment, $expected ) {
$comment_id = wp_insert_comment( $comment );
$comment = get_comment( $comment_id );
$this->assertEquals( $expected['was_sent'], \Activitypub\Comment::was_sent( $comment ) );
$this->assertEquals( $expected['was_received'], \Activitypub\Comment::was_received( $comment ) );
$this->assertEquals( $expected['should_be_federated'], \Activitypub\Comment::should_be_federated( $comment ) );
}
/**
* @dataProvider ability_to_federate_threaded_comment
*/
public function test_check_ability_to_federate_threaded_comment( $parent_comment, $comment, $expected ) {
$parent_comment_id = wp_insert_comment( $parent_comment );
$comment['comment_parent'] = $parent_comment_id;
$comment_id = wp_insert_comment( $comment );
$comment = get_comment( $comment_id );
$this->assertEquals( $expected['was_sent'], \Activitypub\Comment::was_sent( $parent_comment_id ) );
$this->assertEquals( $expected['was_received'], \Activitypub\Comment::was_received( $parent_comment_id ) );
$this->assertEquals( $expected['should_be_federated'], \Activitypub\Comment::should_be_federated( $comment ) );
}
public function test_get_comment_ancestors() {
$comment_id = wp_insert_comment(
array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
)
);
$this->assertEquals( array(), \Activitypub\get_comment_ancestors( $comment_id ) );
$comment_array = get_comment( $comment_id, ARRAY_A );
$parent_comment_id = wp_insert_comment(
array(
'comment_type' => 'parent comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
)
);
$comment_array['comment_parent'] = $parent_comment_id;
wp_update_comment( $comment_array );
$this->assertEquals( array( $parent_comment_id ), \Activitypub\get_comment_ancestors( $comment_id ) );
}
public function ability_to_federate_comment() {
return array(
array(
'comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
),
'expected' => array(
'was_sent' => false,
'was_received' => true,
'should_be_federated' => false,
),
),
array(
'comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => true,
'was_received' => false,
'should_be_federated' => true,
),
),
array(
'comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => false,
'was_received' => false,
'should_be_federated' => false,
),
),
);
}
public function ability_to_federate_threaded_comment() {
return array(
array(
'parent_comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
),
'comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
),
'expected' => array(
'was_sent' => false,
'was_received' => true,
'should_be_federated' => false,
),
),
array(
'parent_comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'protocol' => 'activitypub',
),
),
'comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => false,
'was_received' => true,
'should_be_federated' => true,
),
),
array(
'parent_comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
'comment_meta' => array(
'activitypub_status' => 'federated',
),
),
'comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => true,
'was_received' => false,
'should_be_federated' => true,
),
),
array(
'parent_comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => false,
'was_received' => false,
'should_be_federated' => false,
),
),
array(
'parent_comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => false,
'was_received' => false,
'should_be_federated' => false,
),
),
// this should not be possible but we test it anyway
array(
'parent_comment' => array(
'user_id' => 1,
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'comment' => array(
'comment_type' => 'comment',
'comment_content' => 'This is a comment.',
'comment_author_url' => 'https://example.com',
'comment_author_email' => '',
),
'expected' => array(
'was_sent' => true,
'was_received' => false,
'should_be_federated' => false,
),
),
);
}
}