|
9 | 9 |
|
10 | 10 | use Activitypub\Activity\Activity; |
11 | 11 | use Activitypub\Handler\Announce; |
| 12 | +use Activitypub\Model\Blog; |
12 | 13 |
|
13 | 14 | /** |
14 | 15 | * Test class for Activitypub Announce Handler. |
@@ -104,13 +105,13 @@ public static function create_test_object() { |
104 | 105 | * @covers ::handle_announce |
105 | 106 | */ |
106 | 107 | public function test_handle_announce() { |
107 | | - $user_url = \get_userdata( $this->user_id )->user_url; |
| 108 | + $external_actor = 'https://example.com/users/testuser'; |
108 | 109 |
|
109 | 110 | $object = array( |
110 | | - 'actor' => $user_url, |
| 111 | + 'actor' => $external_actor, |
111 | 112 | 'type' => 'Announce', |
112 | 113 | 'id' => 'https://example.com/id/' . microtime( true ), |
113 | | - 'to' => array( $user_url ), |
| 114 | + 'to' => array( $external_actor ), |
114 | 115 | 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
115 | 116 | 'object' => $this->post_permalink, |
116 | 117 | ); |
@@ -156,13 +157,13 @@ public function test_handle_announces( $announce, $recursion, $message ) { |
156 | 157 | * @covers ::maybe_save_announce |
157 | 158 | */ |
158 | 159 | public function test_maybe_save_announce() { |
159 | | - $user_url = \get_userdata( $this->user_id )->user_url; |
| 160 | + $external_actor = 'https://example.com/users/testuser'; |
160 | 161 |
|
161 | 162 | $activity = array( |
162 | | - 'actor' => $user_url, |
| 163 | + 'actor' => $external_actor, |
163 | 164 | 'type' => 'Announce', |
164 | 165 | 'id' => 'https://example.com/id/' . microtime( true ), |
165 | | - 'to' => array( $user_url ), |
| 166 | + 'to' => array( $external_actor ), |
166 | 167 | 'object' => $this->post_permalink, |
167 | 168 | ); |
168 | 169 |
|
@@ -221,4 +222,131 @@ public static function data_handle_announces() { |
221 | 222 | ), |
222 | 223 | ); |
223 | 224 | } |
| 225 | + |
| 226 | + /** |
| 227 | + * Test that announces from the blog actor are ignored. |
| 228 | + * |
| 229 | + * @covers ::handle_announce |
| 230 | + */ |
| 231 | + public function test_ignore_blog_actor_announce() { |
| 232 | + $blog = new Blog(); |
| 233 | + $blog_url = $blog->get_id(); |
| 234 | + |
| 235 | + $object = array( |
| 236 | + 'actor' => $blog_url, |
| 237 | + 'type' => 'Announce', |
| 238 | + 'id' => 'https://example.com/id/' . microtime( true ), |
| 239 | + 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 240 | + 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 241 | + 'object' => $this->post_permalink, |
| 242 | + ); |
| 243 | + |
| 244 | + // Set up mock action to track whether the announce is handled (should be ignored). |
| 245 | + $handled_action = new \MockAction(); |
| 246 | + \add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 247 | + |
| 248 | + // Call with blog actor as sender - should be ignored. |
| 249 | + Announce::handle_announce( $object, $this->user_id ); |
| 250 | + |
| 251 | + // Verify the announce was NOT handled. |
| 252 | + $this->assertEquals( 0, $handled_action->get_call_count() ); |
| 253 | + |
| 254 | + // Verify no comment was created. |
| 255 | + $args = array( |
| 256 | + 'type' => 'repost', |
| 257 | + 'post_id' => $this->post_id, |
| 258 | + ); |
| 259 | + |
| 260 | + $query = new \WP_Comment_Query( $args ); |
| 261 | + $result = $query->comments; |
| 262 | + |
| 263 | + $this->assertEmpty( $result ); |
| 264 | + |
| 265 | + \remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Test that announces from external actors are not ignored. |
| 270 | + * |
| 271 | + * @covers ::handle_announce |
| 272 | + */ |
| 273 | + public function test_external_actor_announce_not_ignored() { |
| 274 | + $external_actor = 'https://external.example.com/users/someone'; |
| 275 | + |
| 276 | + $object = array( |
| 277 | + 'actor' => $external_actor, |
| 278 | + 'type' => 'Announce', |
| 279 | + 'id' => 'https://external.example.com/id/' . microtime( true ), |
| 280 | + 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 281 | + 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 282 | + 'object' => $this->post_permalink, |
| 283 | + ); |
| 284 | + |
| 285 | + // Set up mock action to verify the announce is handled. |
| 286 | + $handled_action = new \MockAction(); |
| 287 | + \add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 288 | + |
| 289 | + // Call with external actor - should be processed. |
| 290 | + Announce::handle_announce( $object, $this->user_id ); |
| 291 | + |
| 292 | + // Verify the announce WAS handled. |
| 293 | + $this->assertEquals( 1, $handled_action->get_call_count() ); |
| 294 | + |
| 295 | + // Verify comment was created. |
| 296 | + $args = array( |
| 297 | + 'type' => 'repost', |
| 298 | + 'post_id' => $this->post_id, |
| 299 | + ); |
| 300 | + |
| 301 | + $query = new \WP_Comment_Query( $args ); |
| 302 | + $result = $query->comments; |
| 303 | + |
| 304 | + $this->assertNotEmpty( $result ); |
| 305 | + $this->assertInstanceOf( 'WP_Comment', $result[0] ); |
| 306 | + |
| 307 | + \remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * Test that announces from same domain but different actor are not ignored. |
| 312 | + * |
| 313 | + * @covers ::handle_announce |
| 314 | + */ |
| 315 | + public function test_same_domain_different_actor_not_ignored() { |
| 316 | + // Get a regular user actor URL (not the blog actor). |
| 317 | + $user_url = \get_author_posts_url( $this->user_id ); |
| 318 | + |
| 319 | + $object = array( |
| 320 | + 'actor' => $user_url, |
| 321 | + 'type' => 'Announce', |
| 322 | + 'id' => \home_url( '/activity/' . microtime( true ) ), |
| 323 | + 'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 324 | + 'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ), |
| 325 | + 'object' => $this->post_permalink, |
| 326 | + ); |
| 327 | + |
| 328 | + // Set up mock action to verify the announce is handled. |
| 329 | + $handled_action = new \MockAction(); |
| 330 | + \add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 331 | + |
| 332 | + // Call with same domain but user actor - should be processed. |
| 333 | + Announce::handle_announce( $object, $this->user_id ); |
| 334 | + |
| 335 | + // Verify the announce WAS handled. |
| 336 | + $this->assertEquals( 1, $handled_action->get_call_count() ); |
| 337 | + |
| 338 | + // Verify comment was created. |
| 339 | + $args = array( |
| 340 | + 'type' => 'repost', |
| 341 | + 'post_id' => $this->post_id, |
| 342 | + ); |
| 343 | + |
| 344 | + $query = new \WP_Comment_Query( $args ); |
| 345 | + $result = $query->comments; |
| 346 | + |
| 347 | + $this->assertNotEmpty( $result ); |
| 348 | + $this->assertInstanceOf( 'WP_Comment', $result[0] ); |
| 349 | + |
| 350 | + \remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) ); |
| 351 | + } |
224 | 352 | } |
0 commit comments