From 70fa3d26bf236e576b75c41330867fb10aa57649 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 19:27:59 -0400 Subject: [PATCH 1/3] Ajax: Add unit tests for wp_ajax_delete_link() Co-authored-by: Junie --- .../includes/ajax-actions/deleteLink.php | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php new file mode 100644 index 0000000000000..2914dcc80c0fd --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php @@ -0,0 +1,142 @@ +user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + } + + /** + * Setup before each test method. + */ + public function set_up(): void { + parent::set_up(); + update_option( 'link_manager_enabled', 1 ); + add_action( 'admin_init', 'wp_ajax_delete_link', 1 ); + } + + /** + * Tests successful link deletion. + * + * @ticket 65252 + */ + public function test_delete_link_success(): void { + wp_set_current_user( self::$admin_id ); + + $link_id = $this->factory->bookmark->create(); + + $_POST = array( + 'id' => $link_id, + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), + ); + + try { + $this->_handleAjax( 'delete_link' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 (success).' ); + } + + $this->assertNull( get_bookmark( $link_id ), 'Link should be deleted.' ); + } + + /** + * Tests link deletion failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_delete_link_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $link_id = $this->factory->bookmark->create(); + + $_POST = array( + 'id' => $link_id, + '_ajax_nonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'delete_link' ); + } + + /** + * Tests link deletion failure due to insufficient permissions. + * + * @ticket 65252 + */ + public function test_delete_link_insufficient_permissions(): void { + wp_set_current_user( self::$subscriber_id ); + + $link_id = $this->factory->bookmark->create(); + + $_POST = array( + 'id' => $link_id, + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), + ); + + try { + $this->_handleAjax( 'delete_link' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage(), 'AJAX response should be -1 (insufficient permissions).' ); + } + + $this->assertNotNull( get_bookmark( $link_id ), 'Link should NOT be deleted.' ); + } + + /** + * Tests link deletion with non-existent ID. + * + * @ticket 65252 + */ + public function test_delete_link_non_existent_id(): void { + wp_set_current_user( self::$admin_id ); + + $link_id = 99999; + + $_POST = array( + 'id' => $link_id, + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), + ); + + try { + $this->_handleAjax( 'delete_link' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 if link doesn\'t exist (idempotent behavior).' ); + } + } +} From 0fe16b8e1e797c032c216676a2615f1b893cf597 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 21 Oct 2026 19:16:23 -0400 Subject: [PATCH 2/3] Ajax: Update `wp_ajax_delete_link` tests for consistency and improved assertions - Refactor factory usage to align with static methods. - Replace `delete_link` action with `delete-link` for consistency. - Update exception handling to separate expected outcomes. - Simplify assertions for better readability. --- .../includes/ajax-actions/deleteLink.php | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php index 2914dcc80c0fd..da88b43911677 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php @@ -34,6 +34,8 @@ class Tests_wp_ajax_delete_link extends WP_Ajax_UnitTestCase { /** * Setup test fixtures. + * + * @param WP_UnitTest_Factory $factory */ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); @@ -46,7 +48,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void public function set_up(): void { parent::set_up(); update_option( 'link_manager_enabled', 1 ); - add_action( 'admin_init', 'wp_ajax_delete_link', 1 ); } /** @@ -57,7 +58,7 @@ public function set_up(): void { public function test_delete_link_success(): void { wp_set_current_user( self::$admin_id ); - $link_id = $this->factory->bookmark->create(); + $link_id = self::factory()->bookmark->create(); $_POST = array( 'id' => $link_id, @@ -65,11 +66,15 @@ public function test_delete_link_success(): void { ); try { - $this->_handleAjax( 'delete_link' ); + $this->_handleAjax( 'delete-link' ); } catch ( WPAjaxDieStopException $e ) { - $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 (success).' ); + // Expect success. + $this->_last_response = $e->getMessage(); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success. } + $this->assertSame( '1', $this->_last_response ); $this->assertNull( get_bookmark( $link_id ), 'Link should be deleted.' ); } @@ -81,7 +86,7 @@ public function test_delete_link_success(): void { public function test_delete_link_invalid_nonce(): void { wp_set_current_user( self::$admin_id ); - $link_id = $this->factory->bookmark->create(); + $link_id = self::factory()->bookmark->create(); $_POST = array( 'id' => $link_id, @@ -91,7 +96,7 @@ public function test_delete_link_invalid_nonce(): void { $this->expectException( WPAjaxDieStopException::class ); $this->expectExceptionMessage( '-1' ); - $this->_handleAjax( 'delete_link' ); + $this->_handleAjax( 'delete-link' ); } /** @@ -102,20 +107,17 @@ public function test_delete_link_invalid_nonce(): void { public function test_delete_link_insufficient_permissions(): void { wp_set_current_user( self::$subscriber_id ); - $link_id = $this->factory->bookmark->create(); + $link_id = self::factory()->bookmark->create(); $_POST = array( 'id' => $link_id, '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), ); - try { - $this->_handleAjax( 'delete_link' ); - } catch ( WPAjaxDieStopException $e ) { - $this->assertSame( '-1', $e->getMessage(), 'AJAX response should be -1 (insufficient permissions).' ); - } + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); - $this->assertNotNull( get_bookmark( $link_id ), 'Link should NOT be deleted.' ); + $this->_handleAjax( 'delete-link' ); } /** @@ -134,9 +136,15 @@ public function test_delete_link_non_existent_id(): void { ); try { - $this->_handleAjax( 'delete_link' ); + $this->_handleAjax( 'delete-link' ); } catch ( WPAjaxDieStopException $e ) { - $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 if link doesn\'t exist (idempotent behavior).' ); + // Expect success. + $this->_last_response = $e->getMessage(); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success. } + + $this->assertSame( '1', $this->_last_response ); + $this->assertNull( get_bookmark( $link_id ), 'no Link and no error.' ); } } From 36265c9f85201e5c35ebf01fe70f900b2df513d0 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 22 Oct 2026 17:20:41 -0400 Subject: [PATCH 3/3] Apply suggestion from @mindctrl Co-authored-by: John Parris --- .../tests/admin/includes/ajax-actions/deleteLink.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php index da88b43911677..6a621ca6ab40b 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php @@ -93,10 +93,14 @@ public function test_delete_link_invalid_nonce(): void { '_ajax_nonce' => 'invalid-nonce', ); - $this->expectException( WPAjaxDieStopException::class ); - $this->expectExceptionMessage( '-1' ); + try { + $this->_handleAjax( 'delete-link' ); + $this->fail( 'Expected link deletion to stop due to an invalid nonce.' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage() ); + } - $this->_handleAjax( 'delete-link' ); + $this->assertNotNull( get_bookmark( $link_id ), 'Link should not be deleted when the nonce is invalid.' ); } /**