|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test file for Comment transformer. |
| 4 | + * |
| 5 | + * @package ActivityPub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests\Transformer; |
| 9 | + |
| 10 | +use Activitypub\Transformer\Comment; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test class for Comment Transformer. |
| 14 | + * |
| 15 | + * @coversDefaultClass \Activitypub\Transformer\Comment |
| 16 | + */ |
| 17 | +class Test_Comment extends \WP_UnitTestCase { |
| 18 | + /** |
| 19 | + * Test post ID. |
| 20 | + * |
| 21 | + * @var int |
| 22 | + */ |
| 23 | + protected static $post_id; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create fake data before tests run. |
| 27 | + * |
| 28 | + * @param \WP_UnitTest_Factory $factory Helper that creates fake data. |
| 29 | + */ |
| 30 | + public static function wpSetUpBeforeClass( $factory ) { |
| 31 | + self::$post_id = $factory->post->create(); |
| 32 | + |
| 33 | + // Mock the WebFinger wp_safe_remote_get. |
| 34 | + add_filter( 'pre_http_request', array( self::class, 'pre_http_request' ), 10, 3 ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Clean up after tests. |
| 39 | + */ |
| 40 | + public static function wpTearDownAfterClass() { |
| 41 | + wp_delete_post( self::$post_id, true ); |
| 42 | + remove_filter( 'pre_http_request', array( self::class, 'pre_http_request' ) ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test content generation with reply context. |
| 47 | + * |
| 48 | + * @covers ::to_object |
| 49 | + */ |
| 50 | + public function test_content_with_reply_context() { |
| 51 | + // Create a parent ActivityPub comment. |
| 52 | + $parent_comment_id = self::factory()->comment->create( |
| 53 | + array( |
| 54 | + 'comment_post_ID' => self::$post_id, |
| 55 | + 'comment_author_url' => 'https://remote.example/@author', |
| 56 | + 'comment_meta' => array( |
| 57 | + 'protocol' => 'activitypub', |
| 58 | + ), |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + // Create a reply comment. |
| 63 | + $reply_comment_id = self::factory()->comment->create( |
| 64 | + array( |
| 65 | + 'comment_post_ID' => self::$post_id, |
| 66 | + 'comment_parent' => $parent_comment_id, |
| 67 | + 'comment_author_url' => 'https://example.net/@remote', |
| 68 | + 'comment_meta' => array( |
| 69 | + 'protocol' => 'activitypub', |
| 70 | + ), |
| 71 | + ) |
| 72 | + ); |
| 73 | + |
| 74 | + // Create a reply comment. |
| 75 | + $test_comment_id = self::factory()->comment->create( |
| 76 | + array( |
| 77 | + 'comment_post_ID' => self::$post_id, |
| 78 | + 'comment_parent' => $reply_comment_id, |
| 79 | + 'comment_author_url' => 'https://example.com/@test', |
| 80 | + ) |
| 81 | + ); |
| 82 | + |
| 83 | + // Transform comment to ActivityPub object. |
| 84 | + $comment = get_comment( $test_comment_id ); |
| 85 | + $transformer = new Comment( $comment ); |
| 86 | + $object = $transformer->to_object(); |
| 87 | + |
| 88 | + // Get the content. |
| 89 | + $content = $object->get_content(); |
| 90 | + |
| 91 | + // Test that reply context is added. |
| 92 | + $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 ); |
| 93 | + |
| 94 | + // Clean up. |
| 95 | + wp_delete_comment( $reply_comment_id, true ); |
| 96 | + wp_delete_comment( $parent_comment_id, true ); |
| 97 | + wp_delete_comment( $test_comment_id, true ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test content generation with reply context. |
| 102 | + * |
| 103 | + * @param mixed $data The response data. |
| 104 | + * @param array $parsed_args The request arguments. |
| 105 | + * @param string $url The request URL. |
| 106 | + * @return mixed The response data. |
| 107 | + */ |
| 108 | + public static function pre_http_request( $data, $parsed_args, $url ) { |
| 109 | + if ( str_starts_with( $url, 'https://remote.example' ) ) { |
| 110 | + return self::dummy_response( |
| 111 | + wp_json_encode( |
| 112 | + array( |
| 113 | + 'subject' => 'acct:[email protected]', |
| 114 | + 'links' => array( |
| 115 | + 'self' => array( 'href' => 'https://remote.example/@author' ), |
| 116 | + ), |
| 117 | + ) |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + if ( str_starts_with( $url, 'https://example.net/' ) ) { |
| 123 | + return self::dummy_response( |
| 124 | + wp_json_encode( |
| 125 | + array( |
| 126 | + 'subject' => 'https://example.net/@remote', |
| 127 | + 'aliases' => array( |
| 128 | + |
| 129 | + ), |
| 130 | + 'links' => array( |
| 131 | + 'self' => array( 'href' => 'https://example.net/@remote' ), |
| 132 | + ), |
| 133 | + ) |
| 134 | + ) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + return $data; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Create a dummy response. |
| 143 | + * |
| 144 | + * @param string $body The body of the response. |
| 145 | + * |
| 146 | + * @return array The dummy response. |
| 147 | + */ |
| 148 | + private static function dummy_response( $body ) { |
| 149 | + return array( |
| 150 | + 'headers' => array(), |
| 151 | + 'body' => $body, |
| 152 | + 'response' => array( 'code' => 200 ), |
| 153 | + 'cookies' => array(), |
| 154 | + 'filename' => null, |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments