|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the `wp_dependencies_unique_hosts()` function. |
| 5 | + * |
| 6 | + * @group general |
| 7 | + * @group script |
| 8 | + * |
| 9 | + * @covers ::wp_dependencies_unique_hosts |
| 10 | + */ |
| 11 | +class Tests_General_WpDependenciesUniqueHosts extends WP_UnitTestCase { |
| 12 | + |
| 13 | + protected $old_wp_scripts; |
| 14 | + protected $old_wp_styles; |
| 15 | + protected $old_server_name; |
| 16 | + |
| 17 | + public function set_up() { |
| 18 | + parent::set_up(); |
| 19 | + |
| 20 | + $this->old_wp_scripts = $GLOBALS['wp_scripts'] ?? null; |
| 21 | + $this->old_wp_styles = $GLOBALS['wp_styles'] ?? null; |
| 22 | + $this->old_server_name = $_SERVER['SERVER_NAME'] ?? ''; |
| 23 | + |
| 24 | + $GLOBALS['wp_scripts'] = new WP_Scripts(); |
| 25 | + $GLOBALS['wp_styles'] = new WP_Styles(); |
| 26 | + } |
| 27 | + |
| 28 | + public function tear_down() { |
| 29 | + $GLOBALS['wp_scripts'] = $this->old_wp_scripts; |
| 30 | + $GLOBALS['wp_styles'] = $this->old_wp_styles; |
| 31 | + $_SERVER['SERVER_NAME'] = $this->old_server_name; |
| 32 | + |
| 33 | + parent::tear_down(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Tests that an empty queue returns an empty array. |
| 38 | + * |
| 39 | + * @ticket 66613 |
| 40 | + */ |
| 41 | + public function test_wp_dependencies_unique_hosts_empty() { |
| 42 | + $this->assertSame( array(), wp_dependencies_unique_hosts() ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Tests extracting unique hosts from queued scripts and styles. |
| 47 | + * |
| 48 | + * @ticket 66613 |
| 49 | + */ |
| 50 | + public function test_wp_dependencies_unique_hosts_extracts_hosts() { |
| 51 | + $_SERVER['SERVER_NAME'] = 'example.org'; |
| 52 | + |
| 53 | + wp_enqueue_script( 'external-script', 'https://cdn.example.com/script.js', array(), null ); |
| 54 | + wp_enqueue_style( 'external-style', 'https://assets.example.net/style.css', array(), null ); |
| 55 | + |
| 56 | + $hosts = wp_dependencies_unique_hosts(); |
| 57 | + |
| 58 | + $this->assertSame( array( 'cdn.example.com', 'assets.example.net' ), $hosts ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Tests that duplicate hosts across scripts and styles are deduplicated. |
| 63 | + * |
| 64 | + * @ticket 66613 |
| 65 | + */ |
| 66 | + public function test_wp_dependencies_unique_hosts_deduplicates_hosts() { |
| 67 | + $_SERVER['SERVER_NAME'] = 'example.org'; |
| 68 | + |
| 69 | + wp_enqueue_script( 'cdn-script-1', 'https://cdn.example.com/script1.js', array(), null ); |
| 70 | + wp_enqueue_script( 'cdn-script-2', 'https://cdn.example.com/script2.js', array(), null ); |
| 71 | + wp_enqueue_style( 'cdn-style-1', 'https://cdn.example.com/style.css', array(), null ); |
| 72 | + |
| 73 | + $hosts = wp_dependencies_unique_hosts(); |
| 74 | + |
| 75 | + $this->assertSame( array( 'cdn.example.com' ), $hosts ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Tests that hosts matching `$_SERVER['SERVER_NAME']` are excluded. |
| 80 | + * |
| 81 | + * @ticket 66613 |
| 82 | + */ |
| 83 | + public function test_wp_dependencies_unique_hosts_excludes_local_server_name() { |
| 84 | + $_SERVER['SERVER_NAME'] = 'local.example.org'; |
| 85 | + |
| 86 | + wp_enqueue_script( 'local-script', 'https://local.example.org/js/app.js', array(), null ); |
| 87 | + wp_enqueue_script( 'external-script', 'https://cdn.example.com/lib.js', array(), null ); |
| 88 | + |
| 89 | + $hosts = wp_dependencies_unique_hosts(); |
| 90 | + |
| 91 | + $this->assertSame( array( 'cdn.example.com' ), $hosts ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests that relative URLs without a host are ignored. |
| 96 | + * |
| 97 | + * @ticket 66613 |
| 98 | + */ |
| 99 | + public function test_wp_dependencies_unique_hosts_ignores_relative_urls() { |
| 100 | + wp_enqueue_script( 'relative-script', '/wp-includes/js/script.js', array(), null ); |
| 101 | + |
| 102 | + $this->assertSame( array(), wp_dependencies_unique_hosts() ); |
| 103 | + } |
| 104 | +} |
0 commit comments