Skip to content

Commit

Permalink
Use factories and make callback removable
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Dec 19, 2024
1 parent ea2260f commit 1215118
Showing 1 changed file with 61 additions and 71 deletions.
132 changes: 61 additions & 71 deletions tests/includes/transformer/class-test-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
namespace Activitypub\Tests\Transformer;

use Activitypub\Transformer\Comment;
use WP_UnitTestCase;

/**
* Test class for Comment Transformer.
*
* @coversDefaultClass \Activitypub\Transformer\Comment
*/
class Test_Comment extends WP_UnitTestCase {
class Test_Comment extends \WP_UnitTestCase {
/**
* Test post ID.
*
Expand All @@ -26,60 +25,21 @@ class Test_Comment extends WP_UnitTestCase {
/**
* Create fake data before tests run.
*
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
* @param \WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create(
array(
'post_title' => 'Test Post',
'post_content' => 'Test Content',
'post_status' => 'publish',
)
);
self::$post_id = $factory->post->create();

// Mock the WebFinger wp_safe_remote_get.
add_filter(
'pre_http_request',
function ( $data, $parsed_args, $url ) {
if ( str_starts_with( $url, 'https://remote.example' ) ) {
return self::dummy_response(
wp_json_encode(
array(
'subject' => 'acct:author@remote.example',
'links' => array(
'self' => array( 'href' => 'https://remote.example/@author' ),
),
)
)
);
}
if ( str_starts_with( $url, 'https://example.net/' ) ) {
return self::dummy_response(
wp_json_encode(
array(
'subject' => 'https://example.net/@remote',
'aliases' => array(
'acct:remote@example.net',
),
'links' => array(
'self' => array( 'href' => 'https://example.net/@remote' ),
),
)
)
);
}
return $data;
},
10,
3
);
add_filter( 'pre_http_request', array( self::class, 'pre_http_request' ), 10, 3 );
}

/**
* Clean up after tests.
*/
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post_id, true );
remove_filter( 'pre_http_request', array( self::class, 'pre_http_request' ) );
}

/**
Expand All @@ -89,46 +49,34 @@ public static function wpTearDownAfterClass() {
*/
public function test_content_with_reply_context() {
// Create a parent ActivityPub comment.
$parent_comment_id = wp_insert_comment(
$parent_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_content' => 'Parent comment',
'comment_type' => 'comment',
'comment_author' => 'Remote Author',
'comment_author_url' => 'https://remote.example/@author',
'comment_author_email' => '',
'comment_meta' => array(
'comment_post_ID' => self::$post_id,
'comment_author_url' => 'https://remote.example/@author',
'comment_meta' => array(
'protocol' => 'activitypub',
),
)
);

// Create a reply comment.
$reply_comment_id = wp_insert_comment(
$reply_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_parent' => $parent_comment_id,
'comment_content' => 'Reply comment',
'comment_type' => 'comment',
'comment_author' => 'Local Author',
'comment_author_url' => 'https://example.net/@remote',
'comment_author_email' => '',
'comment_meta' => array(
'comment_post_ID' => self::$post_id,
'comment_parent' => $parent_comment_id,
'comment_author_url' => 'https://example.net/@remote',
'comment_meta' => array(
'protocol' => 'activitypub',
),
)
);

// Create a reply comment.
$test_comment_id = wp_insert_comment(
$test_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'comment_parent' => $reply_comment_id,
'comment_content' => 'Reply comment',
'comment_type' => 'comment',
'comment_author' => 'Local Author',
'comment_author_url' => 'https://example.com/@test',
'comment_author_email' => '',
'comment_post_ID' => self::$post_id,
'comment_parent' => $reply_comment_id,
'comment_author_url' => 'https://example.com/@test',
)
);

Expand All @@ -141,11 +89,53 @@ public function test_content_with_reply_context() {
$content = $object->get_content();

// Test that reply context is added.
$this->assertEquals( '<p><a class="u-mention mention" href="https://example.net/@remote">@remote@example.net</a> <a class="u-mention mention" href="https://remote.example/@author">@author@remote.example</a></p><p>Reply comment</p>', $content );
$this->assertEquals( '<p><a class="u-mention mention" href="https://example.net/@remote">@remote@example.net</a> <a class="u-mention mention" href="https://remote.example/@author">@author@remote.example</a></p><p>This is a comment</p>', $content );

// Clean up.
wp_delete_comment( $reply_comment_id, true );
wp_delete_comment( $parent_comment_id, true );
wp_delete_comment( $test_comment_id, true );
}

/**
* Test content generation with reply context.
*
* @param mixed $data The response data.
* @param array $parsed_args The request arguments.
* @param string $url The request URL.
* @return mixed The response data.
*/
public static function pre_http_request( $data, $parsed_args, $url ) {
if ( str_starts_with( $url, 'https://remote.example' ) ) {
return self::dummy_response(
wp_json_encode(
array(
'subject' => 'acct:author@remote.example',
'links' => array(
'self' => array( 'href' => 'https://remote.example/@author' ),
),
)
)
);
}

if ( str_starts_with( $url, 'https://example.net/' ) ) {
return self::dummy_response(
wp_json_encode(
array(
'subject' => 'https://example.net/@remote',
'aliases' => array(
'acct:remote@example.net',
),
'links' => array(
'self' => array( 'href' => 'https://example.net/@remote' ),
),
)
)
);
}

return $data;
}

/**
Expand Down

0 comments on commit 1215118

Please sign in to comment.