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..fba5408041c43 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuGetMetabox.php @@ -0,0 +1,172 @@ +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 ); + + self::factory()->post->create( + array( + 'post_title' => 'Post Metabox Test', + ) + ); + + $_POST = array( + 'action' => 'menu-get-metabox', + 'item-type' => 'post_type', + 'item-object' => 'post', + ); + + try { + $this->_handleAjax( 'menu-get-metabox' ); + } catch ( WPAjaxDieContinueException $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="posttype-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 ( WPAjaxDieContinueException $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="taxonomy-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 ); + } +}