|
7 | 7 |
|
8 | 8 | namespace PWCC\SimpleSearchSubmission\Tests; |
9 | 9 |
|
| 10 | +use PWCC\SimpleSearchSubmission; |
10 | 11 | use WP_UnitTestCase; |
11 | 12 | use WP_UnitTest_Factory; |
12 | 13 |
|
@@ -242,4 +243,172 @@ public function test_no_ping_on_custom_private_post_status() { |
242 | 243 | unset( $wp_post_statuses['custom_private'] ); |
243 | 244 | $this->assertNotPing( get_permalink( $post_id ), 'Custom private post status should not ping.' ); |
244 | 245 | } |
| 246 | + |
| 247 | + /** |
| 248 | + * Ensure de-indexed URLs are pinged once only. |
| 249 | + */ |
| 250 | + public function test_no_duplicate_pings_on_deindexed_url() { |
| 251 | + $post_id = self::$post_ids['publish']; |
| 252 | + |
| 253 | + // Update the post slug. |
| 254 | + wp_update_post( |
| 255 | + array( |
| 256 | + 'ID' => $post_id, |
| 257 | + 'post_name' => 'updated-test-post-publish', |
| 258 | + ) |
| 259 | + ); |
| 260 | + |
| 261 | + // Ensure both old and new URLs are pinged. |
| 262 | + $this->assertPing( home_url( '/2025/test-post-publish/' ), 'Ping should include the old post URL after slug change.' ); |
| 263 | + $this->assertPing( home_url( '/2025/updated-test-post-publish/' ), 'Ping should include the new post URL after slug change.' ); |
| 264 | + // Ensure last ping URL is the updated URL. |
| 265 | + $last_ping_url = SimpleSearchSubmission\get_last_post_ping_url( $post_id ); |
| 266 | + $this->assertSame( home_url( '/2025/updated-test-post-publish/' ), $last_ping_url, 'Last ping URL should be the updated URL.' ); |
| 267 | + |
| 268 | + // Clear the ping list. |
| 269 | + $this->pings = array(); |
| 270 | + |
| 271 | + // Update the post content. |
| 272 | + wp_update_post( |
| 273 | + array( |
| 274 | + 'ID' => $post_id, |
| 275 | + 'post_content' => 'Another update to Test Post', |
| 276 | + ) |
| 277 | + ); |
| 278 | + |
| 279 | + // Ensure only the new URL is pinged again. |
| 280 | + $this->assertNotPing( home_url( '/2025/test-post-publish/' ), 'Old post URL should not be pinged again after content update.' ); |
| 281 | + $this->assertPing( home_url( '/2025/updated-test-post-publish/' ), 'New post URL should be pinged again after content update.' ); |
| 282 | + // Ensure last ping URL is the updated URL. |
| 283 | + $last_ping_url = SimpleSearchSubmission\get_last_post_ping_url( $post_id ); |
| 284 | + $this->assertSame( home_url( '/2025/updated-test-post-publish/' ), $last_ping_url, 'Last ping URL should be the updated URL after the second ping.' ); |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * Ensure de-indexed URLs are pinged only once for private post type transitions. |
| 289 | + */ |
| 290 | + public function test_no_duplicate_pings_on_private_post_type_transition() { |
| 291 | + $post_id = self::$post_ids['publish']; |
| 292 | + |
| 293 | + // Set the post to a private status. |
| 294 | + wp_update_post( |
| 295 | + array( |
| 296 | + 'ID' => $post_id, |
| 297 | + 'post_status' => 'private', |
| 298 | + ) |
| 299 | + ); |
| 300 | + |
| 301 | + // Ensure the old URL is pinged. |
| 302 | + $this->assertPing( home_url( '/2025/test-post-publish/' ), 'Old post URL should be pinged after status change.' ); |
| 303 | + |
| 304 | + // Clear the ping list. |
| 305 | + $this->pings = array(); |
| 306 | + |
| 307 | + // Set the post to a draft. |
| 308 | + wp_update_post( |
| 309 | + array( |
| 310 | + 'ID' => $post_id, |
| 311 | + 'post_status' => 'draft', |
| 312 | + ) |
| 313 | + ); |
| 314 | + // Ensure the old URL is not pinged again. |
| 315 | + $this->assertNotPing( home_url( '/2025/test-post-publish/' ), 'Old post URL should not be pinged again after second status change.' ); |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Ensure previously deindexed URLs that now redirect are re-pinged. |
| 320 | + */ |
| 321 | + public function test_republished_post_with_slug_change_pings_old_urls() { |
| 322 | + $post_id = self::$post_ids['publish']; |
| 323 | + |
| 324 | + // Set the post to draft. |
| 325 | + wp_update_post( |
| 326 | + array( |
| 327 | + 'ID' => $post_id, |
| 328 | + 'post_status' => 'draft', |
| 329 | + ) |
| 330 | + ); |
| 331 | + |
| 332 | + // Ensure the old post URL is pinged. |
| 333 | + $this->assertPing( home_url( '/2025/test-post-publish/' ), 'Old post URL should be pinged after unpublishing.' ); |
| 334 | + |
| 335 | + // Clear the ping list. |
| 336 | + $this->pings = array(); |
| 337 | + |
| 338 | + // Update the post slug and re-publish all in one update. |
| 339 | + wp_update_post( |
| 340 | + array( |
| 341 | + 'ID' => $post_id, |
| 342 | + 'post_name' => 'final-test-post-publish', |
| 343 | + 'post_status' => 'publish', |
| 344 | + ) |
| 345 | + ); |
| 346 | + |
| 347 | + // Ensure both URLs are pinged as the old URL is now a redirect. |
| 348 | + $this->assertPing( home_url( '/2025/test-post-publish/' ), 'Old post URL should be pinged after slug and status update to index the redirect.' ); |
| 349 | + $this->assertPing( home_url( '/2025/final-test-post-publish/' ), 'New post URL should be pinged after slug and status update.' ); |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * Ensure updating the pinged URLs lists appends the new URLs correctly. |
| 354 | + */ |
| 355 | + public function test_update_post_ping_urls_appends_correctly() { |
| 356 | + $post_id = self::$post_ids['publish']; |
| 357 | + |
| 358 | + // Update the post slug for the first time. |
| 359 | + wp_update_post( |
| 360 | + array( |
| 361 | + 'ID' => $post_id, |
| 362 | + 'post_name' => 'first-update-test-post-publish', |
| 363 | + ) |
| 364 | + ); |
| 365 | + |
| 366 | + $last_ping_url = SimpleSearchSubmission\get_last_post_ping_url( $post_id ); |
| 367 | + $this->assertSame( home_url( '/2025/first-update-test-post-publish/' ), $last_ping_url, 'Last ping URL should be the first updated URL.' ); |
| 368 | + |
| 369 | + // Update the post slug for the second time. |
| 370 | + wp_update_post( |
| 371 | + array( |
| 372 | + 'ID' => $post_id, |
| 373 | + 'post_name' => 'second-update-test-post-publish', |
| 374 | + ) |
| 375 | + ); |
| 376 | + |
| 377 | + $last_ping_url = SimpleSearchSubmission\get_last_post_ping_url( $post_id ); |
| 378 | + $this->assertSame( home_url( '/2025/second-update-test-post-publish/' ), $last_ping_url, 'Last ping URL should be the second updated URL.' ); |
| 379 | + |
| 380 | + // Restore the first updated URL. |
| 381 | + wp_update_post( |
| 382 | + array( |
| 383 | + 'ID' => $post_id, |
| 384 | + 'post_name' => 'first-update-test-post-publish', |
| 385 | + ) |
| 386 | + ); |
| 387 | + |
| 388 | + $last_ping_url = SimpleSearchSubmission\get_last_post_ping_url( $post_id ); |
| 389 | + $this->assertSame( home_url( '/2025/first-update-test-post-publish/' ), $last_ping_url, 'Last ping URL should be the restored first updated URL.' ); |
| 390 | + } |
| 391 | + |
| 392 | + /** |
| 393 | + * Ensure URL list never contains duplicates. |
| 394 | + */ |
| 395 | + public function test_update_post_ping_urls_no_duplicates() { |
| 396 | + $post_id = self::$post_ids['publish']; |
| 397 | + |
| 398 | + // Update the ping list (no need to actually ping). |
| 399 | + SimpleSearchSubmission\add_post_ping_urls( $post_id, array( home_url( '/2025/test-post-publish/' ) ) ); |
| 400 | + SimpleSearchSubmission\add_post_ping_urls( $post_id, array( home_url( '/2025/test-post-publish-two/' ) ) ); |
| 401 | + SimpleSearchSubmission\add_post_ping_urls( $post_id, array( home_url( '/2025/test-post-publish-two/' ) ) ); |
| 402 | + SimpleSearchSubmission\add_post_ping_urls( $post_id, array( home_url( '/2025/test-post-publish/' ) ) ); |
| 403 | + |
| 404 | + // Ensure there are no duplicates. |
| 405 | + $ping_urls = SimpleSearchSubmission\get_post_ping_urls( $post_id ); |
| 406 | + $this->assertCount( 2, $ping_urls, 'Ping URL list should not contain both URLs.' ); |
| 407 | + |
| 408 | + $expected = array( |
| 409 | + home_url( '/2025/test-post-publish-two/' ), |
| 410 | + home_url( '/2025/test-post-publish/' ), |
| 411 | + ); |
| 412 | + $this->assertSame( $expected, $ping_urls, 'Ping URL list should not contain duplicates.' ); |
| 413 | + } |
245 | 414 | } |
0 commit comments