From 3dc25249a4758934cdd6d9e1ed79cd95b0e7a3b4 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 13 May 2026 15:04:36 -0400 Subject: [PATCH 1/4] Tests: Add unit tests for wp_ajax_fetch_list() Co-authored-by: Junie --- .../includes/ajax-actions/wpAjaxFetchList.php | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php new file mode 100644 index 0000000000000..2dc24c73726fe --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php @@ -0,0 +1,173 @@ +_setRole( 'administrator' ); + + // Set up the $_GET request. + $list_class = 'WP_Posts_List_Table'; + $_GET = array( + 'list_args' => array( + 'class' => $list_class, + 'screen' => array( + 'id' => 'edit-post', + ), + ), + '_ajax_fetch_list_nonce' => wp_create_nonce( "fetch-list-$list_class" ), + ); + + // Make the request. + try { + $this->_handleAjax( 'fetch-list' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected exception. + unset( $e ); + } catch ( WPAjaxDieStopException $e ) { + // Expected exception. + unset( $e ); + } catch ( Exception $e ) { + $this->fail( 'Unexpected exception: ' . $e->getMessage() ); + } + + if ( empty( $this->_last_response ) ) { + $this->fail( 'Ajax response was empty' ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response ); + $this->assertArrayHasKey( 'rows', $response ); + $this->assertStringContainsString( 'No posts found.', $response['rows'] ); + } + + /** + * Tests fetching a list table with items. + * + * @ticket 65237 + */ + public function test_wp_ajax_fetch_list_with_items(): void { + $this->_setRole( 'administrator' ); + + // Create a post. + self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); + + $list_class = 'WP_Posts_List_Table'; + $_GET = array( + 'list_args' => array( + 'class' => $list_class, + 'screen' => array( + 'id' => 'edit-post', + ), + ), + '_ajax_fetch_list_nonce' => wp_create_nonce( "fetch-list-$list_class" ), + ); + + try { + $this->_handleAjax( 'fetch-list' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response ); + $this->assertArrayHasKey( 'rows', $response ); + $this->assertStringContainsString( 'Test Post', $response['rows'] ); + $this->assertSame( '1 item', $response['total_items_i18n'] ); + } + + /** + * Tests fetching a list table with an invalid nonce. + * + * @ticket 65237 + */ + public function test_wp_ajax_fetch_list_invalid_nonce(): void { + $this->_setRole( 'administrator' ); + + $list_class = 'WP_Posts_List_Table'; + $_GET = array( + 'list_args' => array( + 'class' => $list_class, + ), + '_ajax_fetch_list_nonce' => 'invalid-nonce', + ); + + $this->expectException( 'WPAjaxDieStopException' ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'fetch-list' ); + } + + /** + * Tests fetching a list table with an invalid class. + * + * @ticket 65237 + */ + public function test_wp_ajax_fetch_list_invalid_class(): void { + $this->_setRole( 'administrator' ); + + $list_class = 'Invalid_List_Table'; + $_GET = array( + 'list_args' => array( + 'class' => $list_class, + ), + '_ajax_fetch_list_nonce' => wp_create_nonce( "fetch-list-$list_class" ), + ); + + $this->expectException( 'WPAjaxDieStopException' ); + $this->expectExceptionMessage( '0' ); + + $this->_handleAjax( 'fetch-list' ); + } + + /** + * Tests fetching a list table as an unprivileged user. + * + * @ticket 65237 + */ + public function test_wp_ajax_fetch_list_unprivileged_user(): void { + // Become a subscriber. + $this->_setRole( 'subscriber' ); + + $list_class = 'WP_Posts_List_Table'; + $_GET = array( + 'list_args' => array( + 'class' => $list_class, + 'screen' => array( + 'id' => 'edit-post', + ), + ), + '_ajax_fetch_list_nonce' => wp_create_nonce( "fetch-list-$list_class" ), + ); + + $this->expectException( 'WPAjaxDieStopException' ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'fetch-list' ); + } +} From f31e03a56336c3129a617124a8427d3c1e1aa74e Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 13 May 2026 15:40:15 -0400 Subject: [PATCH 2/4] Tests: Improve code alignment in wp_ajax_fetch_list() unit tests. --- .../includes/ajax-actions/wpAjaxFetchList.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php index 2dc24c73726fe..6cb132090c9c8 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php @@ -29,8 +29,8 @@ public function test_wp_ajax_fetch_list(): void { // Set up the $_GET request. $list_class = 'WP_Posts_List_Table'; - $_GET = array( - 'list_args' => array( + $_GET = array( + 'list_args' => array( 'class' => $list_class, 'screen' => array( 'id' => 'edit-post', @@ -75,8 +75,8 @@ public function test_wp_ajax_fetch_list_with_items(): void { self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( - 'list_args' => array( + $_GET = array( + 'list_args' => array( 'class' => $list_class, 'screen' => array( 'id' => 'edit-post', @@ -110,7 +110,7 @@ public function test_wp_ajax_fetch_list_invalid_nonce(): void { $this->_setRole( 'administrator' ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( + $_GET = array( 'list_args' => array( 'class' => $list_class, ), @@ -132,7 +132,7 @@ public function test_wp_ajax_fetch_list_invalid_class(): void { $this->_setRole( 'administrator' ); $list_class = 'Invalid_List_Table'; - $_GET = array( + $_GET = array( 'list_args' => array( 'class' => $list_class, ), @@ -155,8 +155,8 @@ public function test_wp_ajax_fetch_list_unprivileged_user(): void { $this->_setRole( 'subscriber' ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( - 'list_args' => array( + $_GET = array( + 'list_args' => array( 'class' => $list_class, 'screen' => array( 'id' => 'edit-post', From 78c4f24692a099c920e024fab3cf65ec3324ce17 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 13 May 2026 15:49:43 -0400 Subject: [PATCH 3/4] Fix formatting of $_GET initialization in ajax-actions --- .../tests/admin/includes/ajax-actions/wpAjaxFetchList.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php index 6cb132090c9c8..4b310103214f7 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php @@ -29,7 +29,8 @@ public function test_wp_ajax_fetch_list(): void { // Set up the $_GET request. $list_class = 'WP_Posts_List_Table'; - $_GET = array( + + $_GET = array( 'list_args' => array( 'class' => $list_class, 'screen' => array( From 3e9a3870ae66c2a1e33d118b425fa873986bd71e Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 13 May 2026 15:52:17 -0400 Subject: [PATCH 4/4] Update wpAjaxFetchList.php --- .../includes/ajax-actions/wpAjaxFetchList.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php index 4b310103214f7..cc90c87b6460b 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxFetchList.php @@ -29,7 +29,7 @@ public function test_wp_ajax_fetch_list(): void { // Set up the $_GET request. $list_class = 'WP_Posts_List_Table'; - + $_GET = array( 'list_args' => array( 'class' => $list_class, @@ -76,7 +76,8 @@ public function test_wp_ajax_fetch_list_with_items(): void { self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( + + $_GET = array( 'list_args' => array( 'class' => $list_class, 'screen' => array( @@ -111,7 +112,8 @@ public function test_wp_ajax_fetch_list_invalid_nonce(): void { $this->_setRole( 'administrator' ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( + + $_GET = array( 'list_args' => array( 'class' => $list_class, ), @@ -133,7 +135,8 @@ public function test_wp_ajax_fetch_list_invalid_class(): void { $this->_setRole( 'administrator' ); $list_class = 'Invalid_List_Table'; - $_GET = array( + + $_GET = array( 'list_args' => array( 'class' => $list_class, ), @@ -156,7 +159,8 @@ public function test_wp_ajax_fetch_list_unprivileged_user(): void { $this->_setRole( 'subscriber' ); $list_class = 'WP_Posts_List_Table'; - $_GET = array( + + $_GET = array( 'list_args' => array( 'class' => $list_class, 'screen' => array(