| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group template |
| 5 | * @ticket 34292 |
| 6 | */ |
| 7 | class Tests_WP_Resource_Hints extends WP_UnitTestCase { |
| 8 | private $old_wp_scripts; |
| 9 | |
| 10 | function setUp() { |
| 11 | parent::setUp(); |
| 12 | $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; |
| 13 | remove_action( 'wp_default_scripts', 'wp_default_scripts' ); |
| 14 | $GLOBALS['wp_scripts'] = new WP_Scripts(); |
| 15 | $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' ); |
| 16 | } |
| 17 | |
| 18 | function tearDown() { |
| 19 | $GLOBALS['wp_scripts'] = $this->old_wp_scripts; |
| 20 | add_action( 'wp_default_scripts', 'wp_default_scripts' ); |
| 21 | parent::tearDown(); |
| 22 | } |
| 23 | |
| 24 | function test_should_have_defaults_on_frontend() { |
| 25 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n"; |
| 26 | |
| 27 | $this->expectOutputString( $expected ); |
| 28 | |
| 29 | wp_resource_hints(); |
| 30 | } |
| 31 | |
| 32 | function test_edit_post_screen() { |
| 33 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 34 | "<link rel='prefetch' href='" . admin_url( 'css/edit.css' ) . "'>\r\n" . |
| 35 | "<link rel='prefetch' href='" . includes_url( 'js/tinymce/tinymce.min.js' ) . "'>\r\n"; |
| 36 | |
| 37 | set_current_screen( 'edit.php' ); |
| 38 | |
| 39 | $actual = get_echo( 'wp_resource_hints' ); |
| 40 | |
| 41 | set_current_screen( 'front' ); |
| 42 | |
| 43 | $this->assertEquals( $expected, $actual ); |
| 44 | } |
| 45 | |
| 46 | function test_users_screen() { |
| 47 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 48 | "<link rel='prefetch' href='" . admin_url( 'js/user-profile.js' ) . "'>\r\n"; |
| 49 | |
| 50 | set_current_screen( 'users.php' ); |
| 51 | |
| 52 | $actual = get_echo( 'wp_resource_hints' ); |
| 53 | |
| 54 | set_current_screen( 'front' ); |
| 55 | |
| 56 | $this->assertEquals( $expected, $actual ); |
| 57 | } |
| 58 | |
| 59 | function test_dns_prefetching() { |
| 60 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 61 | "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" . |
| 62 | "<link rel='dns-prefetch' href='https://google.com'>\r\n" . |
| 63 | "<link rel='dns-prefetch' href='make.wordpress.org'>\r\n"; |
| 64 | |
| 65 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 ); |
| 66 | |
| 67 | $actual = get_echo( 'wp_resource_hints' ); |
| 68 | |
| 69 | remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) ); |
| 70 | |
| 71 | $this->assertEquals( $expected, $actual ); |
| 72 | } |
| 73 | |
| 74 | function _add_dns_prefetch_domains( $hints, $method ) { |
| 75 | if ( 'dns-prefetch' === $method ) { |
| 76 | $hints[] = 'http://wordpress.org'; |
| 77 | $hints[] = 'https://google.com'; |
| 78 | $hints[] = '//make.wordpress.org'; |
| 79 | } |
| 80 | |
| 81 | return $hints; |
| 82 | } |
| 83 | |
| 84 | function test_prerender() { |
| 85 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 86 | "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" . |
| 87 | "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" . |
| 88 | "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n"; |
| 89 | |
| 90 | add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 ); |
| 91 | |
| 92 | $actual = get_echo( 'wp_resource_hints' ); |
| 93 | |
| 94 | remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) ); |
| 95 | |
| 96 | $this->assertEquals( $expected, $actual ); |
| 97 | } |
| 98 | |
| 99 | function _add_prerender_urls( $hints, $method ) { |
| 100 | if ( 'prerender' === $method ) { |
| 101 | $hints[] = 'https://make.wordpress.org/great-again'; |
| 102 | $hints[] = 'http://jobs.wordpress.net'; |
| 103 | $hints[] = '//core.trac.wordpress.org'; |
| 104 | } |
| 105 | |
| 106 | return $hints; |
| 107 | } |
| 108 | |
| 109 | function test_parse_url_dns_prefetch() { |
| 110 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 111 | "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n"; |
| 112 | |
| 113 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 ); |
| 114 | |
| 115 | $actual = get_echo( 'wp_resource_hints' ); |
| 116 | |
| 117 | remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ) ); |
| 118 | |
| 119 | $this->assertEquals( $expected, $actual ); |
| 120 | } |
| 121 | |
| 122 | function _add_dns_prefetch_long_urls( $hints, $method ) { |
| 123 | if ( 'dns-prefetch' === $method ) { |
| 124 | $hints[] = 'http://make.wordpress.org/wp-includes/css/editor.css'; |
| 125 | } |
| 126 | |
| 127 | return $hints; |
| 128 | } |
| 129 | |
| 130 | function test_dns_prefetch_styles() { |
| 131 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 132 | "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n"; |
| 133 | |
| 134 | $args = array( |
| 135 | 'family' => 'Open+Sans:400', |
| 136 | 'subset' => 'latin', |
| 137 | ); |
| 138 | |
| 139 | wp_enqueue_style( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) ); |
| 140 | |
| 141 | $actual = get_echo( 'wp_resource_hints' ); |
| 142 | |
| 143 | $this->assertEquals( $expected, $actual ); |
| 144 | |
| 145 | } |
| 146 | |
| 147 | function test_dns_prefetch_scripts() { |
| 148 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| 149 | "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n"; |
| 150 | |
| 151 | $args = array( |
| 152 | 'family' => 'Open+Sans:400', |
| 153 | 'subset' => 'latin', |
| 154 | ); |
| 155 | |
| 156 | wp_enqueue_script( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) ); |
| 157 | |
| 158 | $actual = get_echo( 'wp_resource_hints' ); |
| 159 | |
| 160 | $this->assertEquals( $expected, $actual ); |
| 161 | } |
| 162 | |
| 163 | } |