| | 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 | parent::tearDown(); |
| | 34 | } |
| | 35 | |
| | 36 | function test_add_and_remove_theme_support() { |
| | 37 | $this->_add_theme_support(); |
| | 38 | $this->assertTrue( current_theme_supports( 'custom-header' ) ); |
| | 39 | remove_theme_support( 'custom-header' ); |
| | 40 | $this->assertFalse( current_theme_supports( 'custom-header' ) ); |
| | 41 | } |
| | 42 | |
| | 43 | function test_get_header_image_without_registered_default() { |
| | 44 | $this->_add_theme_support(); |
| | 45 | $image = get_header_image(); |
| | 46 | $this->assertFalse( has_header_image() ); |
| | 47 | $this->assertEmpty( $image ); |
| | 48 | } |
| | 49 | |
| | 50 | function test_get_header_image_with_registered_default() { |
| | 51 | $default = 'http://localhost/default-header.jpg'; |
| | 52 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 53 | |
| | 54 | $image = get_header_image(); |
| | 55 | $this->assertTrue( has_header_image() ); |
| | 56 | $this->assertEquals( $default, $image ); |
| | 57 | } |
| | 58 | |
| | 59 | function test_get_header_image_from_theme_mod() { |
| | 60 | $default = 'http://localhost/default-header.jpg'; |
| | 61 | $custom = 'http://localhost/custom-header.jpg'; |
| | 62 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 63 | |
| | 64 | set_theme_mod( 'header_image', $custom ); |
| | 65 | $image = get_header_image(); |
| | 66 | $this->assertEquals( $custom, $image ); |
| | 67 | $this->assertTrue( has_header_image() ); |
| | 68 | |
| | 69 | set_theme_mod( 'header_image', 'remove-header' ); |
| | 70 | $image = get_header_image(); |
| | 71 | $this->assertFalse( has_header_image() ); |
| | 72 | $this->assertFalse( $image ); |
| | 73 | } |
| | 74 | |
| | 75 | function test_get_header_image_tag_without_registered_default_image() { |
| | 76 | $this->_add_theme_support(); |
| | 77 | $html = get_header_image_tag(); |
| | 78 | $this->assertEmpty( $html ); |
| | 79 | } |
| | 80 | |
| | 81 | function test_get_header_image_tag_with_registered_default_image() { |
| | 82 | $default = 'http://localhost/default-header.jpg'; |
| | 83 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 84 | |
| | 85 | $html = get_header_image_tag(); |
| | 86 | $this->assertStringStartsWith( '<img ', $html ); |
| | 87 | $this->assertContains( sprintf( 'src="%s"', $default ), $html ); |
| | 88 | } |
| | 89 | |
| | 90 | /** |
| | 91 | * @ticket 38633 |
| | 92 | */ |
| | 93 | function test_get_header_image_tag_with_registered_default_image_and_remove_header_theme_mod() { |
| | 94 | $default = 'http://localhost/default-header.jpg'; |
| | 95 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 96 | |
| | 97 | set_theme_mod( 'header_image', 'remove-header' ); |
| | 98 | $html = get_header_image_tag(); |
| | 99 | $this->assertEmpty( $html ); |
| | 100 | } |
| | 101 | |
| | 102 | function test_get_header_image_tag_with_registered_default_image_and_custom_theme_mod() { |
| | 103 | $default = 'http://localhost/default-header.jpg'; |
| | 104 | $custom = 'http://localhost/custom-header.jpg'; |
| | 105 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 106 | |
| | 107 | set_theme_mod( 'header_image', $custom ); |
| | 108 | $html = get_header_image_tag(); |
| | 109 | $this->assertStringStartsWith( '<img ', $html ); |
| | 110 | $this->assertContains( sprintf( 'src="%s"', $custom ), $html ); |
| | 111 | } |
| | 112 | |
| | 113 | function test_get_custom_header_markup_without_registered_default_image() { |
| | 114 | $this->_add_theme_support(); |
| | 115 | |
| | 116 | $html = get_custom_header_markup(); |
| | 117 | $this->assertFalse( has_custom_header() ); |
| | 118 | $this->assertEmpty( $html ); |
| | 119 | |
| | 120 | // The container should always be returned in the Customizer preview. |
| | 121 | $this->_set_customize_previewing( true ); |
| | 122 | $html = get_custom_header_markup(); |
| | 123 | $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html ); |
| | 124 | } |
| | 125 | |
| | 126 | function test_get_custom_header_markup_with_registered_default_image() { |
| | 127 | $default = 'http://localhost/default-header.jpg'; |
| | 128 | $this->_add_theme_support( array( 'default-image' => $default ) ); |
| | 129 | $html = get_custom_header_markup(); |
| | 130 | $this->assertTrue( has_custom_header() ); |
| | 131 | $this->assertStringStartsWith( '<div id="wp-custom-header" class="wp-custom-header">', $html ); |
| | 132 | $this->assertContains( sprintf( 'src="%s"', $default ), $html ); |
| | 133 | } |
| | 134 | |
| | 135 | function test_get_header_video_url() { |
| | 136 | $this->_add_theme_support( array( 'video' => true ) ); |
| | 137 | |
| | 138 | $this->assertFalse( has_header_video() ); |
| | 139 | set_theme_mod( 'header_video', self::$header_video_id ); |
| | 140 | $this->assertTrue( has_header_video() ); |
| | 141 | $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() ); |
| | 142 | } |
| | 143 | |
| | 144 | function test_get_external_header_video_url() { |
| | 145 | $external = 'http://example.com/custom-video.mp4'; |
| | 146 | $this->_add_theme_support( array( 'video' => true ) ); |
| | 147 | |
| | 148 | $this->assertFalse( has_header_video() ); |
| | 149 | set_theme_mod( 'external_header_video', $external ); |
| | 150 | $this->assertTrue( has_header_video() ); |
| | 151 | $this->assertEquals( $external, get_header_video_url() ); |
| | 152 | } |
| | 153 | |
| | 154 | function test_get_header_video_url_prefers_local_video() { |
| | 155 | $external = 'http://example.com/custom-video.mp4'; |
| | 156 | $this->_add_theme_support( array( 'video' => true ) ); |
| | 157 | |
| | 158 | set_theme_mod( 'header_video', self::$header_video_id ); |
| | 159 | set_theme_mod( 'external_header_video', $external ); |
| | 160 | $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() ); |
| | 161 | } |
| | 162 | |
| | 163 | function test_get_custom_header_markup_with_video_and_without_an_image() { |
| | 164 | $custom = 'http://localhost/custom-video.mp4'; |
| | 165 | $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) ); |
| | 166 | |
| | 167 | set_theme_mod( 'external_header_video', $custom ); |
| | 168 | $html = get_custom_header_markup(); |
| | 169 | $this->assertTrue( has_header_video() ); |
| | 170 | $this->assertTrue( has_custom_header() ); |
| | 171 | $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html ); |
| | 172 | } |
| | 173 | |
| | 174 | function test_header_script_is_not_enqueued_by_the_custom_header_markup_without_video() { |
| | 175 | $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) ); |
| | 176 | |
| | 177 | ob_start(); |
| | 178 | the_custom_header_markup(); |
| | 179 | ob_end_clean(); |
| | 180 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| | 181 | |
| | 182 | set_theme_mod( 'header_image', 'http://localhost/custom-header.jpg' ); |
| | 183 | |
| | 184 | ob_start(); |
| | 185 | the_custom_header_markup(); |
| | 186 | ob_end_clean(); |
| | 187 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| | 188 | } |
| | 189 | |
| | 190 | function test_header_script_is_not_enqueued_by_the_custom_header_markup_when_active_callback_is_false() { |
| | 191 | $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_false' ) ); |
| | 192 | set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' ); |
| | 193 | |
| | 194 | ob_start(); |
| | 195 | the_custom_header_markup(); |
| | 196 | ob_end_clean(); |
| | 197 | $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| | 198 | } |
| | 199 | |
| | 200 | function test_header_script_is_enqueued_by_the_custom_header_markup_without_video_when_previewing_in_customizer() { |
| | 201 | $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) ); |
| | 202 | $this->_set_customize_previewing( true ); |
| | 203 | |
| | 204 | ob_start(); |
| | 205 | the_custom_header_markup(); |
| | 206 | ob_end_clean(); |
| | 207 | $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| | 208 | } |
| | 209 | |
| | 210 | function test_header_script_is_enqueued_by_the_custom_header_markup_with_video() { |
| | 211 | $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) ); |
| | 212 | set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' ); |
| | 213 | |
| | 214 | ob_start(); |
| | 215 | the_custom_header_markup(); |
| | 216 | ob_end_clean(); |
| | 217 | $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) ); |
| | 218 | } |
| | 219 | |
| | 220 | /** |
| | 221 | * @ticket 38738 |
| | 222 | */ |
| | 223 | function test_video_header_callback_front_page_from_front_page() { |
| | 224 | $this->_add_theme_support( array( |
| | 225 | 'video' => true, |
| | 226 | ) ); |
| | 227 | |
| | 228 | $this->go_to( home_url() ); |
| | 229 | |
| | 230 | $result = is_header_video_active(); |
| | 231 | |
| | 232 | $this->assertTrue( $result ); |
| | 233 | } |
| | 234 | |
| | 235 | /** |
| | 236 | * @ticket 38738 |
| | 237 | */ |
| | 238 | function test_video_header_callback_front_page_from_elsewhere() { |
| | 239 | $this->_add_theme_support( array( |
| | 240 | 'video' => true, |
| | 241 | ) ); |
| | 242 | |
| | 243 | $p = self::factory()->post->create( array( |
| | 244 | 'post_status' => 'publish', |
| | 245 | ) ); |
| | 246 | |
| | 247 | $this->go_to( get_permalink( $p ) ); |
| | 248 | |
| | 249 | $result = is_header_video_active(); |
| | 250 | |
| | 251 | $this->assertFalse( $result ); |
| | 252 | } |
| | 253 | |
| | 254 | /** |
| | 255 | * @ticket 38738 |
| | 256 | */ |
| | 257 | function test_video_header_callback_globally_from_front_page() { |
| | 258 | $this->_add_theme_support( array( |
| | 259 | 'video' => true, |
| | 260 | 'video-active-callback' => '__return_true', |
| | 261 | ) ); |
| | 262 | |
| | 263 | $this->go_to( home_url() ); |
| | 264 | |
| | 265 | $result = is_header_video_active(); |
| | 266 | |
| | 267 | $this->assertTrue( $result ); |
| | 268 | } |
| | 269 | |
| | 270 | /** |
| | 271 | * @ticket 38738 |
| | 272 | */ |
| | 273 | function test_video_header_callback_globally_from_elsewhere() { |
| | 274 | $this->_add_theme_support( array( |
| | 275 | 'video' => true, |
| | 276 | 'video-active-callback' => '__return_true', |
| | 277 | ) ); |
| | 278 | |
| | 279 | $p = self::factory()->post->create( array( |
| | 280 | 'post_status' => 'publish', |
| | 281 | ) ); |
| | 282 | |
| | 283 | $this->go_to( get_permalink( $p ) ); |
| | 284 | |
| | 285 | $result = is_header_video_active(); |
| | 286 | |
| | 287 | $this->assertTrue( $result ); |
| | 288 | } |
| | 289 | |
| | 290 | /** |
| | 291 | * @ticket 38738 |
| | 292 | */ |
| | 293 | function test_video_header_callback_globally_with_negative_filter() { |
| | 294 | $this->_add_theme_support( array( |
| | 295 | 'video' => true, |
| | 296 | 'video-active-callback' => '__return_true', |
| | 297 | ) ); |
| | 298 | |
| | 299 | $p = self::factory()->post->create( array( |
| | 300 | 'post_status' => 'publish', |
| | 301 | ) ); |
| | 302 | |
| | 303 | $this->go_to( get_permalink( $p ) ); |
| | 304 | |
| | 305 | add_filter( 'is_header_video_active', '__return_false' ); |
| | 306 | $result = is_header_video_active(); |
| | 307 | remove_filter( 'is_header_video_active', '__return_false' ); |
| | 308 | |
| | 309 | $this->assertFalse( $result ); |
| | 310 | } |
| | 311 | |
| | 312 | /** |
| | 313 | * Adds arguments directly to the $_wp_theme_features global. Calling |
| | 314 | * add_theme_support( 'custom-header' ) will poison subsequent tests since |
| | 315 | * it defines constants. |
| | 316 | */ |
| | 317 | function _add_theme_support( $args = array() ) { |
| | 318 | global $_wp_theme_features; |
| | 319 | |
| | 320 | $_wp_theme_features['custom-header'][0] = wp_parse_args( $args, array( |
| | 321 | 'default-image' => '', |
| | 322 | 'random-default' => false, |
| | 323 | 'width' => 0, |
| | 324 | 'height' => 0, |
| | 325 | 'flex-height' => false, |
| | 326 | 'flex-width' => false, |
| | 327 | 'default-text-color' => '', |
| | 328 | 'header-text' => true, |
| | 329 | 'uploads' => true, |
| | 330 | 'wp-head-callback' => '', |
| | 331 | 'admin-head-callback' => '', |
| | 332 | 'admin-preview-callback' => '', |
| | 333 | 'video' => false, |
| | 334 | 'video-active-callback' => 'is_front_page', |
| | 335 | ) ); |
| | 336 | } |
| | 337 | |
| | 338 | function _set_customize_previewing( $value ) { |
| | 339 | $class = new ReflectionClass( 'WP_Customize_Manager' ); |
| | 340 | $property = $class->getProperty( 'previewing' ); |
| | 341 | $property->setAccessible( true ); |
| | 342 | $property->setValue( $this->customize_manager, $value ); |
| | 343 | } |
| | 344 | } |