From b53fdd82a0fbf97b1335c19109a9e2bf2f7f251e Mon Sep 17 00:00:00 2001 From: Muhibul Haque Date: Sat, 7 Nov 2026 11:45:41 +0600 Subject: [PATCH 1/2] Tests: Cover wp_dependencies_unique_hosts(). See #65819. Props devmuhib009. --- .../general/wpDependenciesUniqueHosts.php | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/phpunit/tests/general/wpDependenciesUniqueHosts.php diff --git a/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php b/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php new file mode 100644 index 0000000000000..fa2053d8301f3 --- /dev/null +++ b/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php @@ -0,0 +1,102 @@ +old_wp_scripts = $GLOBALS['wp_scripts'] ?? null; + $this->old_wp_styles = $GLOBALS['wp_styles'] ?? null; + $this->old_server_name = $_SERVER['SERVER_NAME'] ?? ''; + + $GLOBALS['wp_scripts'] = new WP_Scripts(); + $GLOBALS['wp_styles'] = new WP_Styles(); + } + + public function tear_down() { + $GLOBALS['wp_scripts'] = $this->old_wp_scripts; + $GLOBALS['wp_styles'] = $this->old_wp_styles; + $_SERVER['SERVER_NAME'] = $this->old_server_name; + parent::tear_down(); + } + + /** + * Tests that an empty queue returns an empty array. + * + * @ticket 65819 + */ + public function test_wp_dependencies_unique_hosts_empty() { + $this->assertSame( array(), wp_dependencies_unique_hosts() ); + } + + /** + * Tests extracting unique hosts from queued scripts and styles. + * + * @ticket 65819 + */ + public function test_wp_dependencies_unique_hosts_extracts_hosts() { + $_SERVER['SERVER_NAME'] = 'example.org'; + + wp_enqueue_script( 'external-script', 'https://cdn.example.com/script.js', array(), null ); + wp_enqueue_style( 'external-style', 'https://assets.example.net/style.css', array(), null ); + + $hosts = wp_dependencies_unique_hosts(); + + $this->assertSame( array( 'cdn.example.com', 'assets.example.net' ), $hosts ); + } + + /** + * Tests that duplicate hosts across scripts and styles are deduplicated. + * + * @ticket 65819 + */ + public function test_wp_dependencies_unique_hosts_deduplicates_hosts() { + $_SERVER['SERVER_NAME'] = 'example.org'; + + wp_enqueue_script( 'cdn-script-1', 'https://cdn.example.com/script1.js', array(), null ); + wp_enqueue_script( 'cdn-script-2', 'https://cdn.example.com/script2.js', array(), null ); + wp_enqueue_style( 'cdn-style-1', 'https://cdn.example.com/style.css', array(), null ); + + $hosts = wp_dependencies_unique_hosts(); + + $this->assertSame( array( 'cdn.example.com' ), $hosts ); + } + + /** + * Tests that hosts matching $_SERVER['SERVER_NAME'] are excluded. + * + * @ticket 65819 + */ + public function test_wp_dependencies_unique_hosts_excludes_local_server_name() { + $_SERVER['SERVER_NAME'] = 'local.example.org'; + + wp_enqueue_script( 'local-script', 'https://local.example.org/js/app.js', array(), null ); + wp_enqueue_script( 'external-script', 'https://cdn.example.com/lib.js', array(), null ); + + $hosts = wp_dependencies_unique_hosts(); + + $this->assertSame( array( 'cdn.example.com' ), $hosts ); + } + + /** + * Tests that relative URLs without a host are ignored. + * + * @ticket 65819 + */ + public function test_wp_dependencies_unique_hosts_ignores_relative_urls() { + wp_enqueue_script( 'relative-script', '/wp-includes/js/script.js', array(), null ); + + $this->assertSame( array(), wp_dependencies_unique_hosts() ); + } +} From 9da850bffae0cd283720df9bcdc41c5434601df1 Mon Sep 17 00:00:00 2001 From: Muhibul Haque Date: Sat, 7 Nov 2026 20:50:53 +0600 Subject: [PATCH 2/2] Tests: Cover wp_dependencies_unique_hosts(). See #66613. Props devmuhib009. --- .../tests/general/wpDependenciesUniqueHosts.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php b/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php index fa2053d8301f3..35a2d8cd04fa3 100644 --- a/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php +++ b/tests/phpunit/tests/general/wpDependenciesUniqueHosts.php @@ -34,7 +34,7 @@ public function tear_down() { /** * Tests that an empty queue returns an empty array. * - * @ticket 65819 + * @ticket 66613 */ public function test_wp_dependencies_unique_hosts_empty() { $this->assertSame( array(), wp_dependencies_unique_hosts() ); @@ -43,7 +43,7 @@ public function test_wp_dependencies_unique_hosts_empty() { /** * Tests extracting unique hosts from queued scripts and styles. * - * @ticket 65819 + * @ticket 66613 */ public function test_wp_dependencies_unique_hosts_extracts_hosts() { $_SERVER['SERVER_NAME'] = 'example.org'; @@ -59,7 +59,7 @@ public function test_wp_dependencies_unique_hosts_extracts_hosts() { /** * Tests that duplicate hosts across scripts and styles are deduplicated. * - * @ticket 65819 + * @ticket 66613 */ public function test_wp_dependencies_unique_hosts_deduplicates_hosts() { $_SERVER['SERVER_NAME'] = 'example.org'; @@ -76,7 +76,7 @@ public function test_wp_dependencies_unique_hosts_deduplicates_hosts() { /** * Tests that hosts matching $_SERVER['SERVER_NAME'] are excluded. * - * @ticket 65819 + * @ticket 66613 */ public function test_wp_dependencies_unique_hosts_excludes_local_server_name() { $_SERVER['SERVER_NAME'] = 'local.example.org'; @@ -92,7 +92,7 @@ public function test_wp_dependencies_unique_hosts_excludes_local_server_name() { /** * Tests that relative URLs without a host are ignored. * - * @ticket 65819 + * @ticket 66613 */ public function test_wp_dependencies_unique_hosts_ignores_relative_urls() { wp_enqueue_script( 'relative-script', '/wp-includes/js/script.js', array(), null );