| 1 | <?php |
| 2 | /** |
| 3 | * @group themes |
| 4 | */ |
| 5 | class Tests_Theme_Custom_Header extends WP_UnitTestCase { |
| 6 | protected static $header_video_id; |
| 7 | |
| 8 | public static function wpSetUpBeforeClass( $factory ) { |
| 9 | $file = DIR_TESTDATA . '/images/video.mp4'; |
| 10 | self::$header_video_id = $factory->attachment->create_upload_object( $file ); |
| 11 | } |
| 12 | |
| 13 | function setUp() { |
| 14 | parent::setUp(); |
| 15 | |
| 16 | require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); |
| 17 | $GLOBALS['wp_customize'] = new WP_Customize_Manager(); |
| 18 | $this->customize_manager = $GLOBALS['wp_customize']; |
| 19 | |
| 20 | wp_dequeue_script( 'wp-custom-header' ); |
| 21 | } |
| 22 | |
| 23 | function tearDown() { |
| 24 | $this->customize_manager = null; |
| 25 | unset( $GLOBALS['wp_customize'] ); |
| 26 | |
| 27 | remove_theme_support( 'custom-header' ); |
| 28 | remove_theme_mod( 'header_image' ); |
| 29 | remove_theme_mod( 'header_image_data' ); |
| 30 | remove_theme_mod( 'header_video' ); |
| 31 | remove_theme_mod( 'external_header_video' ); |
| 32 | |
| 33 | remove_filter( 'template_directory_uri', array( $this, '_template_directory_uri' ) ); |
| 34 | remove_filter( 'stylesheet_directory_uri', array( $this, '_stylesheet_directory_uri' ) ); |
| 35 | parent::tearDown(); |
| 36 | } |
| 37 | |
| 38 | function test_add_and_remove_theme_support() { |
| 39 | $this->_add_theme_support(); |
| 40 | $this->assertTrue( current_theme_supports( 'custom-header' ) ); |
| 41 | remove_theme_support( 'custom-header' ); |
| 42 | $this->assertFalse( current_theme_supports( 'custom-header' ) ); |
| 43 | } |
| 44 | |
| 45 | function test_get_header_image_without_registered_default() { |
| 46 | $this->_add_theme_support(); |
| 47 | $image = get_header_image(); |
| 48 | $this->assertFalse( has_header_image() ); |
| 49 | $this->assertEmpty( $image ); |
| 50 | } |
| 51 | |
| 52 | function test_get_header_image_with_registered_default() { |
| 53 | $default = 'http://localhost/default-header.jpg'; |
| 54 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 55 | |
| 56 | $image = get_header_image(); |
| 57 | $this->assertTrue( has_header_image() ); |
| 58 | $this->assertEquals( $default, $image ); |
| 59 | } |
| 60 | |
| 61 | function test_get_header_image_from_theme_mod() { |
| 62 | $default = 'http://localhost/default-header.jpg'; |
| 63 | $custom = 'http://localhost/custom-header.jpg'; |
| 64 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 65 | |
| 66 | set_theme_mod( 'header_image', $custom ); |
| 67 | $image = get_header_image(); |
| 68 | $this->assertEquals( $custom, $image ); |
| 69 | $this->assertTrue( has_header_image() ); |
| 70 | |
| 71 | set_theme_mod( 'header_image', 'remove-header' ); |
| 72 | $image = get_header_image(); |
| 73 | $this->assertFalse( has_header_image() ); |
| 74 | $this->assertFalse( $image ); |
| 75 | } |
| 76 | |
| 77 | function test_get_header_image_tag_without_registered_default_image() { |
| 78 | $this->_add_theme_support(); |
| 79 | $html = get_header_image_tag(); |
| 80 | $this->assertEmpty( $html ); |
| 81 | } |
| 82 | |
| 83 | function test_get_header_image_tag_with_registered_default_image() { |
| 84 | $default = 'http://localhost/default-header.jpg'; |
| 85 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 86 | |
| 87 | $html = get_header_image_tag(); |
| 88 | $this->assertStringStartsWith( '<img ', $html ); |
| 89 | $this->assertContains( sprintf( 'src="%s"', $default ), $html ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @ticket 38633 |
| 94 | */ |
| 95 | function test_get_header_image_tag_with_registered_default_image_and_remove_header_theme_mod() { |
| 96 | $default = 'http://localhost/default-header.jpg'; |
| 97 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 98 | |
| 99 | set_theme_mod( 'header_image', 'remove-header' ); |
| 100 | $html = get_header_image_tag(); |
| 101 | $this->assertEmpty( $html ); |
| 102 | } |
| 103 | |
| 104 | function test_get_header_image_tag_with_registered_default_image_and_custom_theme_mod() { |
| 105 | $default = 'http://localhost/default-header.jpg'; |
| 106 | $custom = 'http://localhost/custom-header.jpg'; |
| 107 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 108 | |
| 109 | set_theme_mod( 'header_image', $custom ); |
| 110 | $html = get_header_image_tag(); |
| 111 | $this->assertStringStartsWith( '<img ', $html ); |
| 112 | $this->assertContains( sprintf( 'src="%s"', $custom ), $html ); |
| 113 | } |
| 114 | |
| 115 | function test_get_custom_header_markup_without_registered_default_image() { |
| 116 | $this->_add_theme_support(); |
| 117 | |
| 118 | $html = get_custom_header_markup(); |
| 119 | $this->assertFalse( has_custom_header() ); |
| 120 | $this->assertEmpty( $html ); |
| 121 | |
| 122 | // The container should always be returned in the Customizer preview. |
| 123 | $this->_set_customize_previewing( true ); |
| 124 | $html = get_custom_header_markup(); |
| 125 | $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html ); |
| 126 | } |
| 127 | |
| 128 | function test_get_custom_header_markup_with_registered_default_image() { |
| 129 | $default = 'http://localhost/default-header.jpg'; |
| 130 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| 131 | $html = get_custom_header_markup(); |
| 132 | $this->assertTrue( has_custom_header() ); |
| 133 | $this->assertStringStartsWith( '<div id="wp-custom-header" class="wp-custom-header">', $html ); |
| 134 | $this->assertContains( sprintf( 'src="%s"', $default ), $html ); |
| 135 | } |
| 136 | |
| 137 | function test_get_header_video_url() { |
| 138 | $this->_add_theme_support( array( 'video' => true ) ); |
| 139 | |
| 140 | $this->assertFalse( has_header_video() ); |
| 141 | set_theme_mod( 'header_video', self::$header_video_id ); |
| 142 | $this->assertTrue( has_header_video() ); |
| 143 | $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() ); |
| 144 | } |
| 145 | |
| 146 | function test_get_external_header_video_url() { |
| 147 | $external = 'http://example.com/custom-video.mp4'; |
| 148 | $this->_add_theme_support( array( 'video' => true ) ); |
| 149 | |
| 150 | $this->assertFalse( has_header_video() ); |
| 151 | set_theme_mod( 'external_header_video', $external ); |
| 152 | $this->assertTrue( has_header_video() ); |
| 153 | $this->assertEquals( $external, get_header_video_url() ); |
| 154 | } |
| 155 | |
| 156 | function test_get_header_video_url_prefers_local_video() { |
| 157 | $external = 'http://example.com/custom-video.mp4'; |
| 158 | $this->_add_theme_support( array( 'video' => true ) ); |
| 159 | |
| 160 | set_theme_mod( 'header_video', self::$header_video_id ); |
| 161 | set_theme_mod( 'external_header_video', $external ); |
| 162 | $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() ); |
| 163 | } |
| 164 | |
| 165 | function test_get_custom_header_markup_with_video_and_without_an_image() { |
| 166 | $custom = 'http://localhost/custom-video.mp4'; |
| 167 | $this->_add_theme_support( array( 'video' => true ) ); |
| 168 | |
| 169 | set_theme_mod( 'external_header_video', $custom ); |
| 170 | $html = get_custom_header_markup(); |
| 171 | $this->assertTrue( has_header_video() ); |
| 172 | $this->assertTrue( has_custom_header() ); |
| 173 | $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html ); |
| 174 | } |
| 175 | |
| 176 | function test_header_script_is_not_enqueued_by_the_custom_header_markup_without_video() { |
| 177 | $this->_add_theme_support( array( 'video' => true ) ); |
| 178 | |
| 179 | ob_start(); |
| 180 | the_custom_header_markup( array( 'show_video' => true ) ); |
| 181 | ob_end_clean(); |
| 182 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| 183 | |
| 184 | set_theme_mod( 'header_image', 'http://localhost/custom-header.jpg' ); |
| 185 | |
| 186 | ob_start(); |
| 187 | the_custom_header_markup( array( 'show_video' => true ) ); |
| 188 | ob_end_clean(); |
| 189 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| 190 | } |
| 191 | |
| 192 | function test_header_script_is_not_enqueued_by_the_custom_header_markup_when_show_video_is_false() { |
| 193 | $this->_add_theme_support( array( 'video' => true ) ); |
| 194 | set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' ); |
| 195 | |
| 196 | ob_start(); |
| 197 | the_custom_header_markup( array( 'show_video' => false ) ); |
| 198 | ob_end_clean(); |
| 199 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| 200 | } |
| 201 | |
| 202 | function test_header_script_is_enqueued_by_the_custom_header_markup_without_video_when_previewing_in_customizer() { |
| 203 | $this->_add_theme_support( array( 'video' => true ) ); |
| 204 | $this->_set_customize_previewing( true ); |
| 205 | |
| 206 | ob_start(); |
| 207 | the_custom_header_markup( array( 'show_video' => true ) ); |
| 208 | ob_end_clean(); |
| 209 | $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| 210 | } |
| 211 | |
| 212 | function test_header_script_is_enqueued_by_the_custom_header_markup_with_video() { |
| 213 | $this->_add_theme_support( array( 'video' => true ) ); |
| 214 | set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' ); |
| 215 | |
| 216 | ob_start(); |
| 217 | the_custom_header_markup( array( 'show_video' => true ) ); |
| 218 | ob_end_clean(); |
| 219 | $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Adds arguments directly to the $_wp_theme_features global. Calling |
| 224 | * add_theme_support( 'custom-header' ) will poison subsequent tests since |
| 225 | * it defines constants. |
| 226 | */ |
| 227 | function _add_theme_support( $args = array() ) { |
| 228 | global $_wp_theme_features; |
| 229 | |
| 230 | $_wp_theme_features['custom-header'][0] = wp_parse_args( $args, array( |
| 231 | 'default-image' => '', |
| 232 | 'random-default' => false, |
| 233 | 'width' => 0, |
| 234 | 'height' => 0, |
| 235 | 'flex-height' => false, |
| 236 | 'flex-width' => false, |
| 237 | 'default-text-color' => '', |
| 238 | 'header-text' => true, |
| 239 | 'uploads' => true, |
| 240 | 'wp-head-callback' => '', |
| 241 | 'admin-head-callback' => '', |
| 242 | 'admin-preview-callback' => '', |
| 243 | 'video' => false, |
| 244 | ) ); |
| 245 | } |
| 246 | |
| 247 | function _set_customize_previewing( $value ) { |
| 248 | $class = new ReflectionClass( 'WP_Customize_Manager' ); |
| 249 | $property = $class->getProperty( 'previewing' ); |
| 250 | $property->setAccessible( true ); |
| 251 | $property->setValue( $this->customize_manager, $value ); |
| 252 | } |
| 253 | } |