-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use factories and make callback removable
- Loading branch information
Showing
1 changed file
with
61 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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:[email protected]', | ||
'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:[email protected]', | ||
), | ||
'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' ) ); | ||
} | ||
|
||
/** | ||
|
@@ -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', | ||
) | ||
); | ||
|
||
|
@@ -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">@[email protected]</a> <a class="u-mention mention" href="https://remote.example/@author">@[email protected]</a></p><p>Reply comment</p>', $content ); | ||
$this->assertEquals( '<p><a class="u-mention mention" href="https://example.net/@remote">@[email protected]</a> <a class="u-mention mention" href="https://remote.example/@author">@[email protected]</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:[email protected]', | ||
'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:[email protected]', | ||
), | ||
'links' => array( | ||
'self' => array( 'href' => 'https://example.net/@remote' ), | ||
), | ||
) | ||
) | ||
); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
|