| | 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_dns_prefetching() { |
| | 33 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| | 34 | "<link rel='dns-prefetch' href='http://wordpress.org'>\r\n" . |
| | 35 | "<link rel='dns-prefetch' href='https://google.com'>\r\n" . |
| | 36 | "<link rel='dns-prefetch' href='make.wordpress.org'>\r\n"; |
| | 37 | |
| | 38 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ), 10, 2 ); |
| | 39 | |
| | 40 | $actual = get_echo( 'wp_resource_hints' ); |
| | 41 | |
| | 42 | remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_domains' ) ); |
| | 43 | |
| | 44 | $this->assertEquals( $expected, $actual ); |
| | 45 | } |
| | 46 | |
| | 47 | function _add_dns_prefetch_domains( $hints, $method ) { |
| | 48 | if ( 'dns-prefetch' === $method ) { |
| | 49 | $hints[] = 'http://wordpress.org'; |
| | 50 | $hints[] = 'https://google.com'; |
| | 51 | $hints[] = '//make.wordpress.org'; |
| | 52 | } |
| | 53 | |
| | 54 | return $hints; |
| | 55 | } |
| | 56 | |
| | 57 | function test_prerender() { |
| | 58 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| | 59 | "<link rel='prerender' href='https://make.wordpress.org/great-again'>\r\n" . |
| | 60 | "<link rel='prerender' href='http://jobs.wordpress.net'>\r\n" . |
| | 61 | "<link rel='prerender' href='//core.trac.wordpress.org'>\r\n"; |
| | 62 | |
| | 63 | add_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ), 10, 2 ); |
| | 64 | |
| | 65 | $actual = get_echo( 'wp_resource_hints' ); |
| | 66 | |
| | 67 | remove_filter( 'wp_resource_hints', array( $this, '_add_prerender_urls' ) ); |
| | 68 | |
| | 69 | $this->assertEquals( $expected, $actual ); |
| | 70 | } |
| | 71 | |
| | 72 | function _add_prerender_urls( $hints, $method ) { |
| | 73 | if ( 'prerender' === $method ) { |
| | 74 | $hints[] = 'https://make.wordpress.org/great-again'; |
| | 75 | $hints[] = 'http://jobs.wordpress.net'; |
| | 76 | $hints[] = '//core.trac.wordpress.org'; |
| | 77 | } |
| | 78 | |
| | 79 | return $hints; |
| | 80 | } |
| | 81 | |
| | 82 | function test_parse_url_dns_prefetch() { |
| | 83 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| | 84 | "<link rel='dns-prefetch' href='http://make.wordpress.org'>\r\n"; |
| | 85 | |
| | 86 | add_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ), 10, 2 ); |
| | 87 | |
| | 88 | $actual = get_echo( 'wp_resource_hints' ); |
| | 89 | |
| | 90 | remove_filter( 'wp_resource_hints', array( $this, '_add_dns_prefetch_long_urls' ) ); |
| | 91 | |
| | 92 | $this->assertEquals( $expected, $actual ); |
| | 93 | } |
| | 94 | |
| | 95 | function _add_dns_prefetch_long_urls( $hints, $method ) { |
| | 96 | if ( 'dns-prefetch' === $method ) { |
| | 97 | $hints[] = 'http://make.wordpress.org/wp-includes/css/editor.css'; |
| | 98 | } |
| | 99 | |
| | 100 | return $hints; |
| | 101 | } |
| | 102 | |
| | 103 | function test_dns_prefetch_styles() { |
| | 104 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| | 105 | "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n"; |
| | 106 | |
| | 107 | $args = array( |
| | 108 | 'family' => 'Open+Sans:400', |
| | 109 | 'subset' => 'latin', |
| | 110 | ); |
| | 111 | |
| | 112 | wp_enqueue_style( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) ); |
| | 113 | |
| | 114 | $actual = get_echo( 'wp_resource_hints' ); |
| | 115 | |
| | 116 | $this->assertEquals( $expected, $actual ); |
| | 117 | |
| | 118 | } |
| | 119 | |
| | 120 | function test_dns_prefetch_scripts() { |
| | 121 | $expected = "<link rel='preconnect' href='http://s.w.org'>\r\n" . |
| | 122 | "<link rel='dns-prefetch' href='http://fonts.googleapis.com'>\r\n"; |
| | 123 | |
| | 124 | $args = array( |
| | 125 | 'family' => 'Open+Sans:400', |
| | 126 | 'subset' => 'latin', |
| | 127 | ); |
| | 128 | |
| | 129 | wp_enqueue_script( 'googlefonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) ); |
| | 130 | |
| | 131 | $actual = get_echo( 'wp_resource_hints' ); |
| | 132 | |
| | 133 | $this->assertEquals( $expected, $actual ); |
| | 134 | } |
| | 135 | |
| | 136 | } |