|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Interaction REST API endpoint test file. |
| 4 | + * |
| 5 | + * @package Activitypub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests\Rest; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests for Interaction REST API endpoint. |
| 12 | + * |
| 13 | + * @coversDefaultClass \Activitypub\Rest\Interaction_Controller |
| 14 | + */ |
| 15 | +class Test_Interaction_Controller extends \Activitypub\Tests\Test_REST_Controller_Testcase { |
| 16 | + |
| 17 | + /** |
| 18 | + * Tear down. |
| 19 | + */ |
| 20 | + public function tear_down() { |
| 21 | + \remove_all_filters( 'activitypub_interactions_follow_url' ); |
| 22 | + \remove_all_filters( 'activitypub_interactions_reply_url' ); |
| 23 | + |
| 24 | + parent::tear_down(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test route registration. |
| 29 | + * |
| 30 | + * @covers ::register_routes |
| 31 | + */ |
| 32 | + public function test_register_routes() { |
| 33 | + $routes = rest_get_server()->get_routes(); |
| 34 | + $this->assertArrayHasKey( '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions', $routes ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test get_item with invalid URI. |
| 39 | + * |
| 40 | + * @covers ::get_item |
| 41 | + */ |
| 42 | + public function test_get_item_invalid_uri() { |
| 43 | + $this->expectException( \WPDieException::class ); |
| 44 | + |
| 45 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 46 | + $request->set_param( 'uri', 'invalid-uri' ); |
| 47 | + $response = rest_get_server()->dispatch( $request ); |
| 48 | + |
| 49 | + $this->assertEquals( 400, $response->get_status() ); |
| 50 | + $data = $response->get_data(); |
| 51 | + $this->assertEquals( 'activitypub_invalid_object', $data['code'] ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test get_item with Note object type. |
| 56 | + * |
| 57 | + * @covers ::get_item |
| 58 | + */ |
| 59 | + public function test_get_item() { |
| 60 | + \add_filter( |
| 61 | + 'pre_http_request', |
| 62 | + function () { |
| 63 | + return array( |
| 64 | + 'response' => array( 'code' => 200 ), |
| 65 | + 'body' => wp_json_encode( |
| 66 | + array( |
| 67 | + 'type' => 'Note', |
| 68 | + 'url' => 'https://example.org/note', |
| 69 | + ) |
| 70 | + ), |
| 71 | + ); |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 76 | + $request->set_param( 'uri', 'https://example.org/note' ); |
| 77 | + $response = rest_get_server()->dispatch( $request ); |
| 78 | + |
| 79 | + $this->assertEquals( 302, $response->get_status() ); |
| 80 | + $this->assertArrayHasKey( 'Location', $response->get_headers() ); |
| 81 | + $this->assertStringContainsString( 'post-new.php?in_reply_to=', $response->get_headers()['Location'] ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test get_item with custom follow URL filter. |
| 86 | + * |
| 87 | + * @covers ::get_item |
| 88 | + */ |
| 89 | + public function test_get_item_custom_follow_url() { |
| 90 | + \add_filter( |
| 91 | + 'pre_http_request', |
| 92 | + function () { |
| 93 | + return array( |
| 94 | + 'response' => array( 'code' => 200 ), |
| 95 | + 'body' => wp_json_encode( |
| 96 | + array( |
| 97 | + 'type' => 'Person', |
| 98 | + 'url' => 'https://example.org/person', |
| 99 | + ) |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + \add_filter( 'activitypub_interactions_follow_url', array( $this, 'follow_or_reply_url' ) ); |
| 106 | + |
| 107 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 108 | + $request->set_param( 'uri', 'https://example.org/person' ); |
| 109 | + $response = rest_get_server()->dispatch( $request ); |
| 110 | + |
| 111 | + $this->assertEquals( 302, $response->get_status() ); |
| 112 | + $this->assertArrayHasKey( 'Location', $response->get_headers() ); |
| 113 | + $this->assertEquals( 'https://custom-follow-or-reply-url.com', $response->get_headers()['Location'] ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test get_item with custom reply URL filter. |
| 118 | + * |
| 119 | + * @covers ::get_item |
| 120 | + */ |
| 121 | + public function test_get_item_custom_reply_url() { |
| 122 | + \add_filter( |
| 123 | + 'pre_http_request', |
| 124 | + function () { |
| 125 | + return array( |
| 126 | + 'response' => array( 'code' => 200 ), |
| 127 | + 'body' => wp_json_encode( |
| 128 | + array( |
| 129 | + 'type' => 'Note', |
| 130 | + 'url' => 'https://example.org/note', |
| 131 | + ) |
| 132 | + ), |
| 133 | + ); |
| 134 | + } |
| 135 | + ); |
| 136 | + |
| 137 | + \add_filter( 'activitypub_interactions_reply_url', array( $this, 'follow_or_reply_url' ) ); |
| 138 | + |
| 139 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 140 | + $request->set_param( 'uri', 'https://example.org/note' ); |
| 141 | + $response = rest_get_server()->dispatch( $request ); |
| 142 | + |
| 143 | + $this->assertEquals( 302, $response->get_status() ); |
| 144 | + $this->assertArrayHasKey( 'Location', $response->get_headers() ); |
| 145 | + $this->assertEquals( 'https://custom-follow-or-reply-url.com', $response->get_headers()['Location'] ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test get_item with WP_Error response from get_remote_object. |
| 150 | + * |
| 151 | + * @covers ::get_item |
| 152 | + */ |
| 153 | + public function test_get_item_wp_error() { |
| 154 | + $this->expectException( \WPDieException::class ); |
| 155 | + |
| 156 | + \add_filter( |
| 157 | + 'pre_http_request', |
| 158 | + function () { |
| 159 | + return new \WP_Error( 'http_request_failed', 'Connection failed.' ); |
| 160 | + } |
| 161 | + ); |
| 162 | + |
| 163 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 164 | + $request->set_param( 'uri', 'https://example.org/person' ); |
| 165 | + $response = rest_get_server()->dispatch( $request ); |
| 166 | + |
| 167 | + $this->assertEquals( 400, $response->get_status() ); |
| 168 | + $data = $response->get_data(); |
| 169 | + $this->assertEquals( 'activitypub_invalid_object', $data['code'] ); |
| 170 | + $this->assertEquals( 'The URL is not supported!', $data['message'] ); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Test get_item with invalid object without type. |
| 175 | + * |
| 176 | + * @covers ::get_item |
| 177 | + */ |
| 178 | + public function test_get_item_invalid_object() { |
| 179 | + $this->expectException( \WPDieException::class ); |
| 180 | + |
| 181 | + \add_filter( |
| 182 | + 'pre_http_request', |
| 183 | + function () { |
| 184 | + return array( |
| 185 | + 'response' => array( 'code' => 200 ), |
| 186 | + 'body' => wp_json_encode( |
| 187 | + array( |
| 188 | + 'url' => 'https://example.org/invalid', |
| 189 | + ) |
| 190 | + ), |
| 191 | + ); |
| 192 | + } |
| 193 | + ); |
| 194 | + |
| 195 | + $request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/interactions' ); |
| 196 | + $request->set_param( 'uri', 'https://example.org/invalid' ); |
| 197 | + $response = rest_get_server()->dispatch( $request ); |
| 198 | + |
| 199 | + $this->assertEquals( 400, $response->get_status() ); |
| 200 | + $data = $response->get_data(); |
| 201 | + $this->assertEquals( 'activitypub_invalid_object', $data['code'] ); |
| 202 | + $this->assertEquals( 'The URL is not supported!', $data['message'] ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Test get_item_schema method. |
| 207 | + * |
| 208 | + * @doesNotPerformAssertions |
| 209 | + */ |
| 210 | + public function test_get_item_schema() { |
| 211 | + // Controller does not implement get_item_schema(). |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns a valid follow URL. |
| 216 | + */ |
| 217 | + public function follow_or_reply_url() { |
| 218 | + return 'https://custom-follow-or-reply-url.com'; |
| 219 | + } |
| 220 | +} |
0 commit comments