From 47c77075bc7fccd2ef795af66967509c20f86fc8 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 20:23:20 -0400 Subject: [PATCH 1/3] Ajax: Add unit tests for wp_ajax_closed_postboxes() Co-authored-by: Junie --- .../includes/ajax-actions/closedPostboxes.php | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php b/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php new file mode 100644 index 0000000000000..1a24e8d5ec627 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php @@ -0,0 +1,128 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Setup before each test method. + */ + public function set_up(): void { + parent::set_up(); + add_action( 'admin_init', array( $this, 'hook_ajax_handler' ), 1 ); + } + + /** + * Hooks the AJAX handler to admin_init. + */ + public function hook_ajax_handler(): void { + if ( isset( $_POST['action'] ) && 'closed-postboxes' === $_POST['action'] ) { + wp_ajax_closed_postboxes(); + } + } + + /** + * Tests successful update of closed and hidden postboxes. + * + * @ticket 65252 + */ + public function test_closed_postboxes_success(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'closed-postboxes', + 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ), + 'closed' => 'postbox1,postbox2', + 'hidden' => 'postbox3,submitdiv', + 'page' => 'testpage', + ); + + try { + $this->_handleAjax( 'closed-postboxes' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } + + $closed = get_user_meta( self::$admin_id, 'closedpostboxes_testpage', true ); + $this->assertSame( array( 'postbox1', 'postbox2' ), $closed ); + + $hidden = get_user_meta( self::$admin_id, 'metaboxhidden_testpage', true ); + // 'submitdiv' should be removed as it is in the always-shown list. + $this->assertSame( array( 'postbox3' ), $hidden ); + } + + /** + * Tests update failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_closed_postboxes_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'closed-postboxes', + 'closedpostboxesnonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'closed-postboxes' ); + } + + /** + * Tests update failure due to invalid page key. + * + * @ticket 65252 + */ + public function test_closed_postboxes_invalid_page(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'closed-postboxes', + 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ), + 'page' => 'invalid page!', + ); + + try { + $this->_handleAjax( 'closed-postboxes' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '0', $e->getMessage() ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '0', $e->getMessage() ); + } + } + +} From 1044cfac156e4722ce1935b011e9f6c0431fe2cf Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 12:36:51 -0400 Subject: [PATCH 2/3] Tests: Add unit tests for wp_ajax_hidden_columns() Co-authored-by: Junie --- .../includes/ajax-actions/hiddenColumns.php | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/hiddenColumns.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/hiddenColumns.php b/tests/phpunit/tests/admin/includes/ajax-actions/hiddenColumns.php new file mode 100644 index 0000000000000..a4ce8c4d2aa47 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/hiddenColumns.php @@ -0,0 +1,149 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Setup before each test method. + */ + public function set_up(): void { + parent::set_up(); + add_action( 'admin_init', array( $this, 'hook_ajax_handler' ), 1 ); + } + + /** + * Hooks the AJAX handler to admin_init. + */ + public function hook_ajax_handler(): void { + if ( isset( $_POST['action'] ) && 'hidden-columns' === $_POST['action'] ) { + wp_ajax_hidden_columns(); + } + } + + /** + * Tests successful update of hidden columns. + * + * @ticket 65252 + */ + public function test_hidden_columns_success(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'hidden-columns', + 'screenoptionnonce' => wp_create_nonce( 'screen-options-nonce' ), + 'page' => 'testpage', + 'hidden' => 'column1,column2,column3', + ); + + try { + $this->_handleAjax( 'hidden-columns' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } + + $hidden = get_user_meta( self::$admin_id, 'managetestpagecolumnshidden', true ); + $this->assertSame( array( 'column1', 'column2', 'column3' ), $hidden ); + } + + /** + * Tests update failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_hidden_columns_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'hidden-columns', + 'screenoptionnonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'hidden-columns' ); + } + + /** + * Tests update failure due to invalid page key. + * + * @ticket 65252 + */ + public function test_hidden_columns_invalid_page(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'hidden-columns', + 'screenoptionnonce' => wp_create_nonce( 'screen-options-nonce' ), + 'page' => 'invalid page!', + ); + + try { + $this->_handleAjax( 'hidden-columns' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '0', $e->getMessage() ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '0', $e->getMessage() ); + } + } + + /** + * Tests update with empty hidden columns. + * + * @ticket 65252 + */ + public function test_hidden_columns_empty(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'hidden-columns', + 'screenoptionnonce' => wp_create_nonce( 'screen-options-nonce' ), + 'page' => 'testpage', + 'hidden' => '', + ); + + try { + $this->_handleAjax( 'hidden-columns' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '1', $e->getMessage() ); + } + + $hidden = get_user_meta( self::$admin_id, 'managetestpagecolumnshidden', true ); + $this->assertSame( array(), $hidden ); + } +} From f62f2a94257f8554125f002b6bdd3df6f4d17369 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 13:00:09 -0400 Subject: [PATCH 3/3] Delete tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php --- .../includes/ajax-actions/closedPostboxes.php | 128 ------------------ 1 file changed, 128 deletions(-) delete mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php b/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php deleted file mode 100644 index 1a24e8d5ec627..0000000000000 --- a/tests/phpunit/tests/admin/includes/ajax-actions/closedPostboxes.php +++ /dev/null @@ -1,128 +0,0 @@ -user->create( array( 'role' => 'administrator' ) ); - } - - /** - * Setup before each test method. - */ - public function set_up(): void { - parent::set_up(); - add_action( 'admin_init', array( $this, 'hook_ajax_handler' ), 1 ); - } - - /** - * Hooks the AJAX handler to admin_init. - */ - public function hook_ajax_handler(): void { - if ( isset( $_POST['action'] ) && 'closed-postboxes' === $_POST['action'] ) { - wp_ajax_closed_postboxes(); - } - } - - /** - * Tests successful update of closed and hidden postboxes. - * - * @ticket 65252 - */ - public function test_closed_postboxes_success(): void { - wp_set_current_user( self::$admin_id ); - - $_POST = array( - 'action' => 'closed-postboxes', - 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ), - 'closed' => 'postbox1,postbox2', - 'hidden' => 'postbox3,submitdiv', - 'page' => 'testpage', - ); - - try { - $this->_handleAjax( 'closed-postboxes' ); - } catch ( WPAjaxDieStopException $e ) { - $this->assertSame( '1', $e->getMessage() ); - } catch ( WPAjaxDieContinueException $e ) { - $this->assertSame( '1', $e->getMessage() ); - } - - $closed = get_user_meta( self::$admin_id, 'closedpostboxes_testpage', true ); - $this->assertSame( array( 'postbox1', 'postbox2' ), $closed ); - - $hidden = get_user_meta( self::$admin_id, 'metaboxhidden_testpage', true ); - // 'submitdiv' should be removed as it is in the always-shown list. - $this->assertSame( array( 'postbox3' ), $hidden ); - } - - /** - * Tests update failure due to invalid nonce. - * - * @ticket 65252 - */ - public function test_closed_postboxes_invalid_nonce(): void { - wp_set_current_user( self::$admin_id ); - - $_POST = array( - 'action' => 'closed-postboxes', - 'closedpostboxesnonce' => 'invalid-nonce', - ); - - $this->expectException( WPAjaxDieStopException::class ); - $this->expectExceptionMessage( '-1' ); - - $this->_handleAjax( 'closed-postboxes' ); - } - - /** - * Tests update failure due to invalid page key. - * - * @ticket 65252 - */ - public function test_closed_postboxes_invalid_page(): void { - wp_set_current_user( self::$admin_id ); - - $_POST = array( - 'action' => 'closed-postboxes', - 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ), - 'page' => 'invalid page!', - ); - - try { - $this->_handleAjax( 'closed-postboxes' ); - } catch ( WPAjaxDieStopException $e ) { - $this->assertSame( '0', $e->getMessage() ); - } catch ( WPAjaxDieContinueException $e ) { - $this->assertSame( '0', $e->getMessage() ); - } - } - -}