|
18 | 18 | */ |
19 | 19 | class Test_Reaction_Sync extends WP_UnitTestCase { |
20 | 20 |
|
| 21 | + /** |
| 22 | + * Reset post-type support state leaked by the resolver tests. |
| 23 | + */ |
| 24 | + public function tear_down(): void { |
| 25 | + \delete_option( 'atmosphere_support_post_types' ); |
| 26 | + |
| 27 | + if ( \post_type_exists( 'atmos_hidden_cpt' ) ) { |
| 28 | + \unregister_post_type( 'atmos_hidden_cpt' ); |
| 29 | + } |
| 30 | + |
| 31 | + parent::tear_down(); |
| 32 | + } |
| 33 | + |
21 | 34 | /** |
22 | 35 | * Test that find_post_by_bsky_uri returns the correct post. |
23 | 36 | */ |
@@ -75,6 +88,146 @@ public function test_find_post_by_bsky_uri_not_found() { |
75 | 88 | $this->assertFalse( $method->invoke( null, 'at://did:plc:unknown/app.bsky.feed.post/xyz' ) ); |
76 | 89 | } |
77 | 90 |
|
| 91 | + /** |
| 92 | + * A reaction targeting a supported non-`post` post type must resolve. |
| 93 | + * |
| 94 | + * Regression: `find_post_by_bsky_uri()` called `get_posts()` without a |
| 95 | + * `post_type`, so WordPress defaulted the query to `post_type => 'post'`. |
| 96 | + * Every like/repost/reply on a supported page or custom post type then |
| 97 | + * resolved to nothing and was dropped silently — no comment row, no |
| 98 | + * error, no log. Fails before the resolver is scoped to the federated |
| 99 | + * post types. |
| 100 | + */ |
| 101 | + public function test_find_post_by_bsky_uri_resolves_supported_non_default_post_type() { |
| 102 | + \update_option( 'atmosphere_support_post_types', array( 'post', 'page' ) ); |
| 103 | + |
| 104 | + $post_id = self::factory()->post->create( |
| 105 | + array( |
| 106 | + 'post_type' => 'page', |
| 107 | + 'post_status' => 'publish', |
| 108 | + ) |
| 109 | + ); |
| 110 | + $uri = 'at://did:plc:test123/app.bsky.feed.post/page123'; |
| 111 | + |
| 112 | + \update_post_meta( $post_id, BskyPost::META_URI, $uri ); |
| 113 | + |
| 114 | + $method = new \ReflectionMethod( Reaction_Sync::class, 'find_post_by_bsky_uri' ); |
| 115 | + |
| 116 | + $this->assertSame( $post_id, $method->invoke( null, $uri ) ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * A supported CPT registered with `exclude_from_search` must resolve too. |
| 121 | + * |
| 122 | + * This is why the resolver scopes to `get_supported_post_types()` rather |
| 123 | + * than the `post_type => 'any'` shortcut: `'any'` omits post types flagged |
| 124 | + * `exclude_from_search`, so a naive `'any'` fix would still drop reactions |
| 125 | + * on such a type. Fails both before the fix and under an `'any'`-based fix. |
| 126 | + */ |
| 127 | + public function test_find_post_by_bsky_uri_resolves_supported_exclude_from_search_cpt() { |
| 128 | + \register_post_type( |
| 129 | + 'atmos_hidden_cpt', |
| 130 | + array( |
| 131 | + 'public' => true, |
| 132 | + 'exclude_from_search' => true, |
| 133 | + ) |
| 134 | + ); |
| 135 | + \update_option( 'atmosphere_support_post_types', array( 'post', 'atmos_hidden_cpt' ) ); |
| 136 | + |
| 137 | + $post_id = self::factory()->post->create( |
| 138 | + array( |
| 139 | + 'post_type' => 'atmos_hidden_cpt', |
| 140 | + 'post_status' => 'publish', |
| 141 | + ) |
| 142 | + ); |
| 143 | + $uri = 'at://did:plc:test123/app.bsky.feed.post/hidden123'; |
| 144 | + |
| 145 | + \update_post_meta( $post_id, BskyPost::META_URI, $uri ); |
| 146 | + |
| 147 | + $method = new \ReflectionMethod( Reaction_Sync::class, 'find_post_by_bsky_uri' ); |
| 148 | + |
| 149 | + $this->assertSame( $post_id, $method->invoke( null, $uri ) ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * The thread-index fallback must also resolve a supported non-`post` type. |
| 154 | + * |
| 155 | + * `find_post_by_bsky_uri()` scopes BOTH lookups — the single-record |
| 156 | + * `META_URI` key and the `META_URI_INDEX` thread fallback Publisher |
| 157 | + * populates for every teaser-thread reply. The other regression tests |
| 158 | + * only seed `META_URI`, so they never reach the second query. This one |
| 159 | + * seeds only the index key on a `page` to guard the fallback branch |
| 160 | + * against a future change that drops its `post_type` scope. |
| 161 | + */ |
| 162 | + public function test_find_post_by_bsky_uri_thread_index_resolves_supported_non_default_post_type() { |
| 163 | + \update_option( 'atmosphere_support_post_types', array( 'post', 'page' ) ); |
| 164 | + |
| 165 | + $post_id = self::factory()->post->create( |
| 166 | + array( |
| 167 | + 'post_type' => 'page', |
| 168 | + 'post_status' => 'publish', |
| 169 | + ) |
| 170 | + ); |
| 171 | + $reply_uri = 'at://did:plc:test123/app.bsky.feed.post/pagereply123'; |
| 172 | + |
| 173 | + \add_post_meta( $post_id, BskyPost::META_URI_INDEX, $reply_uri ); |
| 174 | + |
| 175 | + $method = new \ReflectionMethod( Reaction_Sync::class, 'find_post_by_bsky_uri' ); |
| 176 | + |
| 177 | + $this->assertSame( $post_id, $method->invoke( null, $reply_uri ) ); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * A reaction on an unsupported post type must NOT resolve. |
| 182 | + * |
| 183 | + * Pins the upper bound of the scoping: the resolver covers exactly the |
| 184 | + * federated types, no more. Without this, a future widening back to |
| 185 | + * `post_type => 'any'` would silently start importing reactions onto |
| 186 | + * content the site never federated, and every positive test would still |
| 187 | + * pass. Here `page` carries matching meta but is absent from the |
| 188 | + * supported list, so the lookup must miss. |
| 189 | + */ |
| 190 | + public function test_find_post_by_bsky_uri_ignores_unsupported_post_type() { |
| 191 | + \update_option( 'atmosphere_support_post_types', array( 'post' ) ); |
| 192 | + |
| 193 | + $post_id = self::factory()->post->create( |
| 194 | + array( |
| 195 | + 'post_type' => 'page', |
| 196 | + 'post_status' => 'publish', |
| 197 | + ) |
| 198 | + ); |
| 199 | + $uri = 'at://did:plc:test123/app.bsky.feed.post/unsupported123'; |
| 200 | + |
| 201 | + \update_post_meta( $post_id, BskyPost::META_URI, $uri ); |
| 202 | + |
| 203 | + $method = new \ReflectionMethod( Reaction_Sync::class, 'find_post_by_bsky_uri' ); |
| 204 | + |
| 205 | + $this->assertFalse( $method->invoke( null, $uri ) ); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * An empty supported-types list must resolve nothing, not fall back to `post`. |
| 210 | + * |
| 211 | + * When a site owner unticks every type, the option is stored as an empty |
| 212 | + * array. Passing `post_type => array()` to `get_posts()` silently defaults |
| 213 | + * to `post_type => 'post'`, which would reintroduce the original miss (and |
| 214 | + * resolve reactions onto content the Publisher no longer federates). The |
| 215 | + * resolver must short-circuit to false instead: a matching `post` exists |
| 216 | + * here, yet nothing is federated, so nothing should resolve. |
| 217 | + */ |
| 218 | + public function test_find_post_by_bsky_uri_returns_false_when_no_supported_types() { |
| 219 | + \update_option( 'atmosphere_support_post_types', array() ); |
| 220 | + |
| 221 | + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); |
| 222 | + $uri = 'at://did:plc:test123/app.bsky.feed.post/notypes123'; |
| 223 | + |
| 224 | + \update_post_meta( $post_id, BskyPost::META_URI, $uri ); |
| 225 | + |
| 226 | + $method = new \ReflectionMethod( Reaction_Sync::class, 'find_post_by_bsky_uri' ); |
| 227 | + |
| 228 | + $this->assertFalse( $method->invoke( null, $uri ) ); |
| 229 | + } |
| 230 | + |
78 | 231 | /** |
79 | 232 | * Test that find_comment_by_source_id returns the correct comment. |
80 | 233 | */ |
|
0 commit comments