|
12 | 12 | */ |
13 | 13 | class Test_Functions_Post extends \WP_UnitTestCase { |
14 | 14 |
|
| 15 | + /** |
| 16 | + * Test is_post_federated function. |
| 17 | + * |
| 18 | + * A post counts as federated only when its federation state is "federated" |
| 19 | + * AND it currently still meets the public-queryability criteria (post type |
| 20 | + * enabled, public status, allowed visibility, no password). |
| 21 | + * |
| 22 | + * @covers \Activitypub\is_post_federated |
| 23 | + */ |
| 24 | + public function test_is_post_federated() { |
| 25 | + // Federated and still publicly queryable. |
| 26 | + $federated_post_id = self::factory()->post->create(); |
| 27 | + \update_post_meta( $federated_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 28 | + $this->assertTrue( \Activitypub\is_post_federated( $federated_post_id ), 'A federated, publicly queryable post is federated.' ); |
| 29 | + |
| 30 | + // Never federated (no status), even though it is publicly queryable. |
| 31 | + $never_post_id = self::factory()->post->create(); |
| 32 | + $this->assertFalse( \Activitypub\is_post_federated( $never_post_id ), 'A post that was never federated is not federated.' ); |
| 33 | + |
| 34 | + // Federated but soft-deleted (status no longer "federated"). |
| 35 | + $deleted_post_id = self::factory()->post->create(); |
| 36 | + \update_post_meta( $deleted_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_DELETED ); |
| 37 | + $this->assertFalse( \Activitypub\is_post_federated( $deleted_post_id ), 'A soft-deleted post is not federated.' ); |
| 38 | + |
| 39 | + // Federated status, but visibility later switched to local — must not count as federated. |
| 40 | + $local_post_id = self::factory()->post->create(); |
| 41 | + \update_post_meta( $local_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 42 | + \update_post_meta( $local_post_id, 'activitypub_content_visibility', ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL ); |
| 43 | + $this->assertFalse( \Activitypub\is_post_federated( $local_post_id ), 'A federated post switched to local visibility is no longer federated.' ); |
| 44 | + |
| 45 | + // Federated status, but post moved to a private status — must not count as federated. |
| 46 | + $private_post_id = self::factory()->post->create( array( 'post_status' => 'private' ) ); |
| 47 | + \update_post_meta( $private_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 48 | + $this->assertFalse( \Activitypub\is_post_federated( $private_post_id ), 'A federated post moved to private status is no longer federated.' ); |
| 49 | + |
| 50 | + // Federated status, but password-protected — must not count as federated. |
| 51 | + $password_post_id = self::factory()->post->create( array( 'post_password' => 'secret' ) ); |
| 52 | + \update_post_meta( $password_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 53 | + $this->assertFalse( \Activitypub\is_post_federated( $password_post_id ), 'A federated but password-protected post is not federated.' ); |
| 54 | + |
| 55 | + // Federated status, but post type no longer supports ActivityPub. |
| 56 | + \register_post_type( 'unsupported', array() ); |
| 57 | + $unsupported_post_id = self::factory()->post->create( array( 'post_type' => 'unsupported' ) ); |
| 58 | + \update_post_meta( $unsupported_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 59 | + $this->assertFalse( \Activitypub\is_post_federated( $unsupported_post_id ), 'A federated post on an unsupported post type is not federated.' ); |
| 60 | + \unregister_post_type( 'unsupported' ); |
| 61 | + |
| 62 | + // An integration can veto via the public-queryability filter. |
| 63 | + $filtered_post_id = self::factory()->post->create(); |
| 64 | + \update_post_meta( $filtered_post_id, 'activitypub_status', ACTIVITYPUB_OBJECT_STATE_FEDERATED ); |
| 65 | + $this->assertTrue( \Activitypub\is_post_federated( $filtered_post_id ), 'Federated post is federated before filtering.' ); |
| 66 | + \add_filter( 'activitypub_is_post_publicly_queryable', '__return_false' ); |
| 67 | + try { |
| 68 | + $this->assertFalse( \Activitypub\is_post_federated( $filtered_post_id ), 'An integration filtering the post non-queryable makes it not federated.' ); |
| 69 | + } finally { |
| 70 | + \remove_filter( 'activitypub_is_post_publicly_queryable', '__return_false' ); |
| 71 | + } |
| 72 | + |
| 73 | + // Invalid input. |
| 74 | + $this->assertFalse( \Activitypub\is_post_federated( 0 ), 'An empty post is not federated.' ); |
| 75 | + } |
| 76 | + |
15 | 77 | /** |
16 | 78 | * Test is_post_disabled function. |
17 | 79 | * |
|
0 commit comments