WordPress.org

Make WordPress Core

Changeset 40825


Ignore:
Timestamp:
05/23/2017 08:55:16 PM (12 months ago)
Author:
obenland
Message:

Themes: More unit tests for Custom Header

Custom Header functionality is largely untested. This adds tests for existing
behavior as well as changes introduced in [39227].

Props bradyvercher.
See #38639.
Fixes #39241.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/customHeader.php

    r39240 r40825  
    77    static $post;
    88
     9    protected static $header_video_id;
     10
    911    public static function wpSetUpBeforeClass( $factory ) {
    1012        self::$post = self::factory()->post->create( array(
    1113            'post_status' => 'publish',
    1214        ) );
     15
     16        $file = DIR_TESTDATA . '/uploads/small-video.mp4';
     17        self::$header_video_id = $factory->attachment->create_upload_object( $file );
     18    }
     19
     20    function setUp() {
     21        parent::setUp();
     22
     23        require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
     24        $GLOBALS['wp_customize'] = new WP_Customize_Manager();
     25        $this->customize_manager = $GLOBALS['wp_customize'];
     26
     27        wp_dequeue_script( 'wp-custom-header' );
    1328    }
    1429
    1530    function tearDown() {
     31        $this->customize_manager = null;
     32        unset( $GLOBALS['wp_customize'] );
     33
    1634        remove_theme_support( 'custom-header' );
     35        remove_theme_mod( 'header_image' );
     36        remove_theme_mod( 'header_image_data' );
     37        remove_theme_mod( 'header_video' );
     38        remove_theme_mod( 'external_header_video' );
     39
    1740        parent::tearDown();
     41    }
     42
     43    function test_add_and_remove_theme_support() {
     44        $this->_add_theme_support();
     45        $this->assertTrue( current_theme_supports( 'custom-header' ) );
     46        remove_theme_support( 'custom-header' );
     47        $this->assertFalse( current_theme_supports( 'custom-header' ) );
     48    }
     49
     50    function test_get_header_image_without_registered_default() {
     51        $this->_add_theme_support();
     52        $image = get_header_image();
     53        $this->assertFalse( has_header_image() );
     54        $this->assertEmpty( $image );
     55    }
     56
     57    function test_get_header_image_with_registered_default() {
     58        $default = 'http://localhost/default-header.jpg';
     59        $this->_add_theme_support( array( 'default-image' => $default ) );
     60
     61        $image = get_header_image();
     62        $this->assertTrue( has_header_image() );
     63        $this->assertEquals( $default, $image );
     64    }
     65
     66    function test_get_header_image_from_theme_mod() {
     67        $default = 'http://localhost/default-header.jpg';
     68        $custom = 'http://localhost/custom-header.jpg';
     69        $this->_add_theme_support( array( 'default-image' => $default ) );
     70
     71        set_theme_mod( 'header_image', $custom );
     72        $image = get_header_image();
     73        $this->assertEquals( $custom, $image );
     74        $this->assertTrue( has_header_image() );
     75
     76        set_theme_mod( 'header_image', 'remove-header' );
     77        $image = get_header_image();
     78        $this->assertFalse( has_header_image() );
     79        $this->assertFalse( $image );
     80    }
     81
     82    function test_get_header_image_tag_without_registered_default_image() {
     83        $this->_add_theme_support();
     84        $html = get_header_image_tag();
     85        $this->assertEmpty( $html );
     86    }
     87
     88    function test_get_header_image_tag_with_registered_default_image() {
     89        $default = 'http://localhost/default-header.jpg';
     90        $this->_add_theme_support( array( 'default-image' => $default ) );
     91
     92        $html = get_header_image_tag();
     93        $this->assertStringStartsWith( '<img ', $html );
     94        $this->assertContains( sprintf( 'src="%s"', $default ), $html );
     95    }
     96
     97    /**
     98     * @ticket 38633
     99     */
     100    function test_get_header_image_tag_with_registered_default_image_and_remove_header_theme_mod() {
     101        $default = 'http://localhost/default-header.jpg';
     102        $this->_add_theme_support( array( 'default-image' => $default ) );
     103
     104        set_theme_mod( 'header_image', 'remove-header' );
     105        $html = get_header_image_tag();
     106        $this->assertEmpty( $html );
     107    }
     108
     109    function test_get_header_image_tag_with_registered_default_image_and_custom_theme_mod() {
     110        $default = 'http://localhost/default-header.jpg';
     111        $custom = 'http://localhost/custom-header.jpg';
     112        $this->_add_theme_support( array( 'default-image' => $default ) );
     113
     114        set_theme_mod( 'header_image', $custom );
     115        $html = get_header_image_tag();
     116        $this->assertStringStartsWith( '<img ', $html );
     117        $this->assertContains( sprintf( 'src="%s"', $custom ), $html );
     118    }
     119
     120    function test_get_custom_header_markup_without_registered_default_image() {
     121        $this->_add_theme_support();
     122
     123        $html = get_custom_header_markup();
     124        $this->assertFalse( has_custom_header() );
     125        $this->assertEmpty( $html );
     126
     127        // The container should always be returned in the Customizer preview.
     128        $this->_set_customize_previewing( true );
     129        $html = get_custom_header_markup();
     130        $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html );
     131    }
     132
     133    function test_get_custom_header_markup_with_registered_default_image() {
     134        $default = 'http://localhost/default-header.jpg';
     135        $this->_add_theme_support( array( 'default-image' => $default ) );
     136        $html = get_custom_header_markup();
     137        $this->assertTrue( has_custom_header() );
     138        $this->assertStringStartsWith( '<div id="wp-custom-header" class="wp-custom-header">', $html );
     139        $this->assertContains( sprintf( 'src="%s"', $default ), $html );
     140    }
     141
     142    function test_get_header_video_url() {
     143        $this->_add_theme_support( array( 'video' => true ) );
     144
     145        $this->assertFalse( has_header_video() );
     146        set_theme_mod( 'header_video', self::$header_video_id );
     147        $this->assertTrue( has_header_video() );
     148        $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() );
     149    }
     150
     151    function test_get_external_header_video_url() {
     152        $external = 'http://example.com/custom-video.mp4';
     153        $this->_add_theme_support( array( 'video' => true ) );
     154
     155        $this->assertFalse( has_header_video() );
     156        set_theme_mod( 'external_header_video', $external );
     157        $this->assertTrue( has_header_video() );
     158        $this->assertEquals( $external, get_header_video_url() );
     159    }
     160
     161    function test_get_header_video_url_prefers_local_video() {
     162        $external = 'http://example.com/custom-video.mp4';
     163        $this->_add_theme_support( array( 'video' => true ) );
     164
     165        set_theme_mod( 'header_video', self::$header_video_id );
     166        set_theme_mod( 'external_header_video', $external );
     167        $this->assertEquals( wp_get_attachment_url( self::$header_video_id ), get_header_video_url() );
     168    }
     169
     170    function test_get_custom_header_markup_with_video_and_without_an_image() {
     171        $custom = 'http://localhost/custom-video.mp4';
     172        $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) );
     173
     174        set_theme_mod( 'external_header_video', $custom );
     175        $html = get_custom_header_markup();
     176        $this->assertTrue( has_header_video() );
     177        $this->assertTrue( has_custom_header() );
     178        $this->assertEquals( '<div id="wp-custom-header" class="wp-custom-header"></div>', $html );
     179    }
     180
     181    function test_header_script_is_not_enqueued_by_the_custom_header_markup_without_video() {
     182        $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) );
     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        set_theme_mod( 'header_image', 'http://localhost/custom-header.jpg' );
     190
     191        ob_start();
     192        the_custom_header_markup();
     193        ob_end_clean();
     194        $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) );
     195    }
     196
     197    function test_header_script_is_not_enqueued_by_the_custom_header_markup_when_active_callback_is_false() {
     198        $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_false' ) );
     199        set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' );
     200
     201        ob_start();
     202        the_custom_header_markup();
     203        ob_end_clean();
     204        $this->assertFalse( wp_script_is( 'wp-custom-header', 'enqueued' ) );
     205    }
     206
     207    function test_header_script_is_enqueued_by_the_custom_header_markup_without_video_when_previewing_in_customizer() {
     208        $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) );
     209        $this->_set_customize_previewing( true );
     210
     211        ob_start();
     212        the_custom_header_markup();
     213        ob_end_clean();
     214        $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) );
     215    }
     216
     217    function test_header_script_is_enqueued_by_the_custom_header_markup_with_video() {
     218        $this->_add_theme_support( array( 'video' => true, 'video-active-callback' => '__return_true' ) );
     219        set_theme_mod( 'external_header_video', 'http://localhost/custom-video.mp4' );
     220
     221        ob_start();
     222        the_custom_header_markup();
     223        ob_end_clean();
     224        $this->assertTrue( wp_script_is( 'wp-custom-header', 'enqueued' ) );
    18225    }
    19226
     
    123330        ) );
    124331    }
     332
     333    function _set_customize_previewing( $value ) {
     334        $class = new ReflectionClass( 'WP_Customize_Manager' );
     335        $property = $class->getProperty( 'previewing' );
     336        $property->setAccessible( true );
     337        $property->setValue( $this->customize_manager, $value );
     338    }
    125339}
Note: See TracChangeset for help on using the changeset viewer.