From d48818d355d23f4d0d43e070635057e03f1f91e6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 19:12:19 -0400 Subject: [PATCH 1/2] Ajax: Add unit tests for wp_ajax_delete_tag() Co-authored-by: Junie --- .../admin/includes/ajax-actions/deleteTag.php | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php b/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php new file mode 100644 index 0000000000000..833f2ead7dcdb --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php @@ -0,0 +1,173 @@ +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(); + add_action( 'admin_init', 'wp_ajax_delete_tag', 1 ); + } + + /** + * Tests successful tag deletion. + * + * @ticket 65252 + */ + public function test_delete_tag_success(): void { + wp_set_current_user( self::$admin_id ); + + $tag_id = $this->factory->tag->create(); + + $_POST = array( + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', + '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), + ); + + try { + $this->_handleAjax( 'delete_tag' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 (success).' ); + } + + $this->assertNull( get_term( $tag_id, 'post_tag' ), 'Tag should be deleted.' ); + } + + /** + * Tests successful category deletion. + * + * @ticket 65252 + */ + public function test_delete_category_success(): void { + wp_set_current_user( self::$admin_id ); + + $cat_id = $this->factory->category->create(); + + $_POST = array( + 'tag_ID' => $cat_id, + 'taxonomy' => 'category', + '_ajax_nonce' => wp_create_nonce( "delete-tag_$cat_id" ), + ); + + try { + $this->_handleAjax( 'delete_tag' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage(), 'AJAX response should be 1 (success).' ); + } + + $this->assertNull( get_term( $cat_id, 'category' ), 'Category should be deleted.' ); + } + + /** + * Tests tag deletion failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_delete_tag_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $tag_id = $this->factory->tag->create(); + + $_POST = array( + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', + '_ajax_nonce' => 'invalid-nonce', + ); + + try { + $this->_handleAjax( 'delete_tag' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage(), 'AJAX response should be -1 (invalid nonce).' ); + } + + $this->assertNotNull( get_term( $tag_id, 'post_tag' ), 'Tag should NOT be deleted.' ); + } + + /** + * Tests tag deletion failure due to insufficient permissions. + * + * @ticket 65252 + */ + public function test_delete_tag_insufficient_permissions(): void { + wp_set_current_user( self::$subscriber_id ); + + $tag_id = $this->factory->tag->create(); + + $_POST = array( + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', + '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), + ); + + try { + $this->_handleAjax( 'delete_tag' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage(), 'AJAX response should be -1 (insufficient permissions).' ); + } + + $this->assertNotNull( get_term( $tag_id, 'post_tag' ), 'Tag should NOT be deleted.' ); + } + + /** + * Tests tag deletion with non-existent tag ID. + * + * @ticket 65252 + */ + public function test_delete_tag_non_existent_id(): void { + wp_set_current_user( self::$admin_id ); + + $tag_id = 99999; + + $_POST = array( + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', + '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), + ); + + try { + $this->_handleAjax( 'delete_tag' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage(), 'AJAX response should be -1 because permission check fails for non-existent tag.' ); + } + } +} From e0753f806d44514c216b4114ccc69e384ddea86a Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 19:15:26 -0400 Subject: [PATCH 2/2] Refactor tag_ID and taxonomy assignment in tests --- .../admin/includes/ajax-actions/deleteTag.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php b/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php index 833f2ead7dcdb..e3828d7298728 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/deleteTag.php @@ -59,8 +59,8 @@ public function test_delete_tag_success(): void { $tag_id = $this->factory->tag->create(); $_POST = array( - 'tag_ID' => $tag_id, - 'taxonomy' => 'post_tag', + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), ); @@ -84,8 +84,8 @@ public function test_delete_category_success(): void { $cat_id = $this->factory->category->create(); $_POST = array( - 'tag_ID' => $cat_id, - 'taxonomy' => 'category', + 'tag_ID' => $cat_id, + 'taxonomy' => 'category', '_ajax_nonce' => wp_create_nonce( "delete-tag_$cat_id" ), ); @@ -109,8 +109,8 @@ public function test_delete_tag_invalid_nonce(): void { $tag_id = $this->factory->tag->create(); $_POST = array( - 'tag_ID' => $tag_id, - 'taxonomy' => 'post_tag', + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', '_ajax_nonce' => 'invalid-nonce', ); @@ -134,8 +134,8 @@ public function test_delete_tag_insufficient_permissions(): void { $tag_id = $this->factory->tag->create(); $_POST = array( - 'tag_ID' => $tag_id, - 'taxonomy' => 'post_tag', + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), ); @@ -159,8 +159,8 @@ public function test_delete_tag_non_existent_id(): void { $tag_id = 99999; $_POST = array( - 'tag_ID' => $tag_id, - 'taxonomy' => 'post_tag', + 'tag_ID' => $tag_id, + 'taxonomy' => 'post_tag', '_ajax_nonce' => wp_create_nonce( "delete-tag_$tag_id" ), );