From 19a5a063120d5aabeb9f743b50a85d60fa5d5ac5 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 12:54:02 -0400 Subject: [PATCH 1/3] Tests: Add unit tests for wp_ajax_menu_get_metabox() Co-authored-by: Junie --- .../includes/ajax-actions/menuGetMetabox.php | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php new file mode 100644 index 0000000000000..50eed35e9dc9f --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php @@ -0,0 +1,166 @@ +user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + } + + /** + * Tests retrieval of post type metabox. + * + * @ticket 65252 + */ + public function test_menu_get_metabox_post_type(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'post_type', + 'item-object' => 'post', + ); + + try { + $this->_handleAjax( 'menu-get-metabox' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect the function to die after echoing JSON. + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response ); + $this->assertSame( 'posttype-post', $response['replace-id'] ); + $this->assertStringContainsString( 'id="add-post"', $response['markup'] ); + $this->assertStringContainsString( 'Post', $response['markup'] ); + } + + /** + * Tests retrieval of taxonomy metabox. + * + * @ticket 65252 + */ + public function test_menu_get_metabox_taxonomy(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'taxonomy', + 'item-object' => 'category', + ); + + try { + $this->_handleAjax( 'menu-get-metabox' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect the function to die after echoing JSON. + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response ); + $this->assertSame( 'taxonomy-category', $response['replace-id'] ); + $this->assertStringContainsString( 'id="add-category"', $response['markup'] ); + $this->assertStringContainsString( 'Categories', $response['markup'] ); + } + + /** + * Tests failure due to insufficient permissions. + * + * @ticket 65252 + */ + public function test_menu_get_metabox_insufficient_permissions(): void { + wp_set_current_user( self::$subscriber_id ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'post_type', + 'item-object' => 'post', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'menu-get-metabox' ); + } + + /** + * Tests behavior with invalid item-type. + * + * @ticket 65252 + */ + public function test_menu_get_metabox_invalid_item_type(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'invalid', + 'item-object' => 'post', + ); + + try { + $this->_handleAjax( 'menu-get-metabox' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect it to die without outputting anything if item-type is invalid. + } + + $this->assertEmpty( $this->_last_response ); + } + + /** + * Tests behavior with invalid item-object. + * + * @ticket 65252 + */ + public function test_menu_get_metabox_invalid_item_object(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'post_type', + 'item-object' => 'non_existent_post_type', + ); + + try { + $this->_handleAjax( 'menu-get-metabox' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect it to die without outputting anything if item-object is invalid. + } + + $this->assertEmpty( $this->_last_response ); + } +} From 624fa07bbb0c964f807d4bcc903655c8c18b1d7d Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 13:43:26 -0400 Subject: [PATCH 2/3] Tests: Fix exception handling in menuGetMetabox tests --- .../tests/admin/includes/ajax-actions/menuGetMetabox.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php index 50eed35e9dc9f..21c8ec22653b7 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php @@ -58,7 +58,7 @@ public function test_menu_get_metabox_post_type(): void { try { $this->_handleAjax( 'menu-get-metabox' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // We expect the function to die after echoing JSON. } @@ -86,7 +86,7 @@ public function test_menu_get_metabox_taxonomy(): void { try { $this->_handleAjax( 'menu-get-metabox' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // We expect the function to die after echoing JSON. } From ece807fd67fd4c3d3fa95bfb4655e1be304b36bd Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 16:32:17 -0400 Subject: [PATCH 3/3] Tests: Fix metabox markup assertions in menuGetMetabox tests --- .../admin/includes/ajax-actions/menuGetMetabox.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php index 21c8ec22653b7..fba5408041c43 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php @@ -50,6 +50,12 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void public function test_menu_get_metabox_post_type(): void { wp_set_current_user( self::$admin_id ); + self::factory()->post->create( + array( + 'post_title' => 'Post Metabox Test', + ) + ); + $_POST = array( 'action' => 'menu-get-metabox', 'item-type' => 'post_type', @@ -66,7 +72,7 @@ public function test_menu_get_metabox_post_type(): void { $this->assertIsArray( $response ); $this->assertSame( 'posttype-post', $response['replace-id'] ); - $this->assertStringContainsString( 'id="add-post"', $response['markup'] ); + $this->assertStringContainsString( 'id="posttype-post"', $response['markup'] ); $this->assertStringContainsString( 'Post', $response['markup'] ); } @@ -94,7 +100,7 @@ public function test_menu_get_metabox_taxonomy(): void { $this->assertIsArray( $response ); $this->assertSame( 'taxonomy-category', $response['replace-id'] ); - $this->assertStringContainsString( 'id="add-category"', $response['markup'] ); + $this->assertStringContainsString( 'id="taxonomy-category"', $response['markup'] ); $this->assertStringContainsString( 'Categories', $response['markup'] ); }