From 3a5f37a4dc3a2c20070688b396d75880dce9885b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Fri, 15 May 2026 17:08:04 -0400 Subject: [PATCH 1/5] Add unit tests for `wp_ajax_oembed_cache` function --- .../ajax-actions/wpAjaxOembedCache.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php new file mode 100644 index 0000000000000..e23aca909ea4a --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php @@ -0,0 +1,90 @@ +post->create( + array( + 'post_content' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', + ) + ); + } + + /** + * Tests oEmbed caching via AJAX. + * + * @ticket 65252 + */ + public function test_oembed_cache(): void { + global $wp_embed; + + // Mock the user. + $this->_setRole( 'administrator' ); + + $_GET['post'] = self::$post_id; + + // We want to verify that WP_Embed::cache_oembed was called. + // Since we can't easily mock the global $wp_embed object without affecting other things, + // we'll check if the cache was actually attempted by looking at post meta + // or by mocking the method if possible. + // However, WP_Embed::cache_oembed() triggers shortcodes and autoembeds. + // A simpler way is to check if it dies with 0 as expected. + + try { + $this->_handleAjax( 'oembed_cache' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = (string) $e->getMessage(); + } + + $this->assertSame( '0', $this->_last_response ); + } + + /** + * Tests oEmbed caching with invalid post ID. + * + * @ticket 65252 + */ + public function test_oembed_cache_invalid_post(): void { + $this->_setRole( 'administrator' ); + + $_GET['post'] = 99999; + + try { + $this->_handleAjax( 'oembed_cache' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = (string) $e->getMessage(); + } + + $this->assertSame( '0', $this->_last_response ); + } +} From f5cc8db00637937981cd12f7ff53a5b789d9eec4 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 17:57:12 -0400 Subject: [PATCH 2/5] Add unit tests for `wp_ajax_autocomplete_user` function --- .../ajax-actions/autocompleteUser.php | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php new file mode 100644 index 0000000000000..a37f871f82067 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php @@ -0,0 +1,151 @@ +user->create( + array( + 'user_login' => 'testuser', + 'user_email' => 'testuser@example.com', + 'first_name' => 'Test', + 'last_name' => 'User', + ) + ); + } + + /** + * Tests user autocomplete via AJAX. + * + * @ticket 65252 + * + * @dataProvider data_autocomplete_user + * + * @param array $request_params Request parameters. + * @param array $expected Expected results. + * @param bool $is_member Whether the user should be a member of the blog. + */ + public function test_autocomplete_user( array $request_params, array $expected, bool $is_member = true ): void { + if ( ! is_multisite() ) { + $this->markTestSkipped( 'wp_ajax_autocomplete_user() is multisite only.' ); + } + + // Mock the user with necessary capabilities. + $this->_setRole( 'administrator' ); + wp_set_current_user( self::$user_id ); + grant_super_admin( self::$user_id ); + + if ( ! $is_member ) { + remove_user_from_blog( self::$user_id, get_current_blog_id() ); + } else { + add_user_to_blog( get_current_blog_id(), self::$user_id, 'subscriber' ); + } + + $_GET = array_merge( $_GET, $request_params ); + $_POST = array_merge( $_POST, $request_params ); + $_REQUEST = array_merge( $_REQUEST, $request_params ); + + try { + add_action( 'admin_init', 'wp_ajax_autocomplete_user', 1 ); + $this->_handleAjax( 'autocomplete_user' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = (string) $e->getMessage(); + } + + $response = json_decode( $this->_last_response, true ); + + $this->assertIsArray( $response, 'Response should be a JSON array' ); + + foreach ( $expected as $index => $expected_item ) { + $this->assertArrayHasKey( $index, $response, "Response should have index $index" ); + $this->assertSame( $expected_item['label'], $response[ $index ]['label'], "Label mismatch at index $index" ); + $this->assertSame( $expected_item['value'], $response[ $index ]['value'], "Value mismatch at index $index" ); + } + } + + /** + * Data provider for test_autocomplete_user. + * + * @return array + */ + public function data_autocomplete_user(): array { + return array( + 'search by login' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'search', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser', + ), + ), + ), + 'add by login (exclude existing)' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'add', + ), + 'expected' => array(), + ), + 'add by login (include non-member)' => array( + 'request_params' => array( + 'term' => 'testuser', + 'autocomplete_type' => 'add', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser', + ), + ), + 'is_member' => false, + ), + 'search by email' => array( + 'request_params' => array( + 'term' => 'testuser@example.com', + 'autocomplete_type' => 'search', + 'autocomplete_field' => 'user_email', + ), + 'expected' => array( + array( + 'label' => 'testuser (testuser@example.com)', + 'value' => 'testuser@example.com', + ), + ), + ), + ); + } +} From 2d29bae3708bbb6394bd677b3870e2712f9b167a Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 18:01:59 -0400 Subject: [PATCH 3/5] Delete tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php --- .../ajax-actions/wpAjaxOembedCache.php | 90 ------------------- 1 file changed, 90 deletions(-) delete mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php deleted file mode 100644 index e23aca909ea4a..0000000000000 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxOembedCache.php +++ /dev/null @@ -1,90 +0,0 @@ -post->create( - array( - 'post_content' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', - ) - ); - } - - /** - * Tests oEmbed caching via AJAX. - * - * @ticket 65252 - */ - public function test_oembed_cache(): void { - global $wp_embed; - - // Mock the user. - $this->_setRole( 'administrator' ); - - $_GET['post'] = self::$post_id; - - // We want to verify that WP_Embed::cache_oembed was called. - // Since we can't easily mock the global $wp_embed object without affecting other things, - // we'll check if the cache was actually attempted by looking at post meta - // or by mocking the method if possible. - // However, WP_Embed::cache_oembed() triggers shortcodes and autoembeds. - // A simpler way is to check if it dies with 0 as expected. - - try { - $this->_handleAjax( 'oembed_cache' ); - } catch ( WPAjaxDieContinueException $e ) { - $this->_last_response = (string) $e->getMessage(); - } catch ( WPAjaxDieStopException $e ) { - $this->_last_response = (string) $e->getMessage(); - } - - $this->assertSame( '0', $this->_last_response ); - } - - /** - * Tests oEmbed caching with invalid post ID. - * - * @ticket 65252 - */ - public function test_oembed_cache_invalid_post(): void { - $this->_setRole( 'administrator' ); - - $_GET['post'] = 99999; - - try { - $this->_handleAjax( 'oembed_cache' ); - } catch ( WPAjaxDieContinueException $e ) { - $this->_last_response = (string) $e->getMessage(); - } catch ( WPAjaxDieStopException $e ) { - $this->_last_response = (string) $e->getMessage(); - } - - $this->assertSame( '0', $this->_last_response ); - } -} From 20babdcf72935817574e4a22c200807ca032fa90 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 18:04:37 -0400 Subject: [PATCH 4/5] Fix formatting and alignment in autocompleteUser.php --- .../admin/includes/ajax-actions/autocompleteUser.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php index a37f871f82067..7a75b3fb230d1 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php @@ -66,8 +66,8 @@ public function test_autocomplete_user( array $request_params, array $expected, add_user_to_blog( get_current_blog_id(), self::$user_id, 'subscriber' ); } - $_GET = array_merge( $_GET, $request_params ); - $_POST = array_merge( $_POST, $request_params ); + $_GET = array_merge( $_GET, $request_params ); + $_POST = array_merge( $_POST, $request_params ); $_REQUEST = array_merge( $_REQUEST, $request_params ); try { @@ -101,7 +101,7 @@ public function test_autocomplete_user( array $request_params, array $expected, */ public function data_autocomplete_user(): array { return array( - 'search by login' => array( + 'search by login' => array( 'request_params' => array( 'term' => 'testuser', 'autocomplete_type' => 'search', @@ -113,7 +113,7 @@ public function data_autocomplete_user(): array { ), ), ), - 'add by login (exclude existing)' => array( + 'add by login (exclude existing)' => array( 'request_params' => array( 'term' => 'testuser', 'autocomplete_type' => 'add', @@ -125,7 +125,7 @@ public function data_autocomplete_user(): array { 'term' => 'testuser', 'autocomplete_type' => 'add', ), - 'expected' => array( + 'expected' => array( array( 'label' => 'testuser (testuser@example.com)', 'value' => 'testuser', @@ -133,7 +133,7 @@ public function data_autocomplete_user(): array { ), 'is_member' => false, ), - 'search by email' => array( + 'search by email' => array( 'request_params' => array( 'term' => 'testuser@example.com', 'autocomplete_type' => 'search', From 2a799bb2002ab96a49b89c16ea74f2186a1ba0ed Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 19 May 2026 18:07:29 -0400 Subject: [PATCH 5/5] Update autocompleteUser.php --- .../tests/admin/includes/ajax-actions/autocompleteUser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php index 7a75b3fb230d1..a73bba4879757 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/autocompleteUser.php @@ -125,7 +125,7 @@ public function data_autocomplete_user(): array { 'term' => 'testuser', 'autocomplete_type' => 'add', ), - 'expected' => array( + 'expected' => array( array( 'label' => 'testuser (testuser@example.com)', 'value' => 'testuser',