WordPress.org

Make WordPress Core

Changeset 35751


Ignore:
Timestamp:
12/01/2015 08:44:54 PM (4 years ago)
Author:
wonderboymusic
Message:

Unit Tests: fix responsive image unit tests. Correct the logic in video shortcode unit test for width.

Props joemcgill, wonderboymusic.
Fixes #34790.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r35678 r35751  
    426426        $height = 480;
    427427
     428        $w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
     429        if ( $width > $w ) {
     430            $width = $w;
     431        }
     432
    428433        $post_id = get_post() ? get_the_ID() : 0;
    429434
    430435        $video =<<<VIDEO
    431 [video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
     436[video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
    432437<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
    433438<source type="video/webm" src="myvideo.webm" />
     
    441446VIDEO;
    442447
    443         $w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
    444         $h = ceil( ( $height * $w ) / $width );
     448
     449        $h = ceil( ( $height * $width ) / $width );
    445450
    446451        $content = apply_filters( 'the_content', $video );
    447452
    448         $expected = '<div style="width: ' . $w . 'px; " class="wp-video">' .
     453        $expected = '<div style="width: ' . $width . 'px; " class="wp-video">' .
    449454            "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
    450             '<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $w . '" height="' . $h . '" preload="metadata" controls="controls">' .
     455            '<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
    451456            '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
    452457            '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
     
    740745     */
    741746    function test_wp_calculate_image_srcset() {
     747        global $_wp_additional_image_sizes;
     748
    742749        $year_month = date('Y/m');
    743750        $image_meta = wp_get_attachment_metadata( self::$large_id );
    744751        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
    745752
    746         $expected = $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' .
    747                 $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['medium_large']['file'] . ' ' . $image_meta['sizes']['medium_large']['width'] . 'w, ' .
    748                 $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' .
    749                 $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
    750 
    751753        // Set up test cases for all expected size names.
    752         $sizes = array( 'medium', 'medium_large', 'large', 'full' );
    753 
    754         foreach ( $sizes as $size ) {
    755             $image_url = wp_get_attachment_image_url( self::$large_id, $size );
    756             $size_array = $this->_get_image_size_array_from_name( $size );
    757             $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    758         }
     754        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
     755
     756        // Add any soft crop intermediate sizes.
     757        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
     758            if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
     759                $intermediates[] = $name;
     760            }
     761        }
     762
     763        $expected = "";
     764
     765        foreach( $image_meta['sizes'] as $name => $size ) {
     766            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
     767            if ( in_array( $name, $intermediates ) ) {
     768                $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
     769            }
     770        }
     771
     772        // Add the full size width at the end.
     773        $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
     774
     775        foreach ( $intermediates as $int ) {
     776            $image_url = wp_get_attachment_image_url( self::$large_id, $int );
     777            $size_array = $this->_get_image_size_array_from_name( $int );
     778            $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     779        }
    759780    }
    760781
     
    763784     */
    764785    function test_wp_calculate_image_srcset_no_date_uploads() {
     786        global $_wp_additional_image_sizes;
     787
    765788        // Save the current setting for uploads folders
    766789        $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );
     
    776799        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
    777800
    778         $expected = $uploads_dir_url . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' .
    779                 $uploads_dir_url . $image_meta['sizes']['medium_large']['file'] . ' ' . $image_meta['sizes']['medium_large']['width'] . 'w, ' .
    780                 $uploads_dir_url . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' .
    781                 $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
    782 
    783801        // Set up test cases for all expected size names.
    784         $sizes = array( 'medium', 'medium_large', 'large', 'full' );
    785 
    786         foreach ( $sizes as $size ) {
    787             $size_array = $this->_get_image_size_array_from_name( $size );
    788             $image_url = wp_get_attachment_image_url( $id, $size );
    789             $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    790         }
     802        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
     803
     804        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
     805            if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
     806                $intermediates[] = $name;
     807            }
     808        }
     809
     810        $expected = "";
     811
     812        foreach( $image_meta['sizes'] as $name => $size ) {
     813            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
     814            if ( in_array( $name, $intermediates ) ) {
     815                $expected .= $uploads_dir_url . $size['file'] . ' ' . $size['width'] . 'w, ';
     816            }
     817        }
     818
     819        // Add the full size width at the end.
     820        $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
     821
     822        foreach ( $intermediates as $int ) {
     823            $size_array = $this->_get_image_size_array_from_name( $int );
     824            $image_url = wp_get_attachment_image_url( $id, $int );
     825            $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     826        }
    791827
    792828        // Remove the attachment
     
    860896     */
    861897    function test_wp_get_attachment_image_srcset() {
     898        global $_wp_additional_image_sizes;
     899
    862900        $image_meta = wp_get_attachment_metadata( self::$large_id );
    863901        $size_array = array( 1600, 1200 ); // full size
     
    866904
    867905        $year_month = date('Y/m');
    868 
    869         $expected = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month = date('Y/m') . '/'
    870             . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ';
    871         $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month = date('Y/m') . '/'
    872             . $image_meta['sizes']['medium_large']['file'] . ' ' . $image_meta['sizes']['medium_large']['width'] . 'w, ';
    873         $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month = date('Y/m') . '/'
    874             . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ';
    875         $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
    876 
    877         $this->assertSame( $expected, $srcset );
     906        $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     907
     908        // Set up test cases for all expected size names.
     909        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
     910
     911        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
     912            if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
     913                $intermediates[] = $name;
     914            }
     915        }
     916
     917        $expected = "";
     918
     919        foreach( $image_meta['sizes'] as $name => $size ) {
     920            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
     921            if ( in_array( $name, $intermediates ) ) {
     922                $expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
     923            }
     924        }
     925
     926        $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
     927
     928        $this->assertSame( $expected, $srcset );
    878929    }
    879930
     
    913964     */
    914965    function test_wp_get_attachment_image_sizes() {
    915         // Test sizes against the default WP sizes.
    916         $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
    917 
    918         foreach( $intermediates as $int_size ) {
    919             $image = wp_get_attachment_image_src( self::$large_id, $int_size );
     966        // Test sizes against the default WP sizes.
     967        $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
     968
     969        // Make sure themes aren't filtering the sizes array.
     970        remove_all_filters( 'wp_calculate_image_sizes' );
     971
     972        foreach( $intermediates as $int_size ) {
     973            $image = wp_get_attachment_image_src( self::$large_id, $int_size );
    920974
    921975            $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
     
    931985    function test_wp_calculate_image_sizes() {
    932986        // Test sizes against the default WP sizes.
    933         $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
    934         $image_meta = wp_get_attachment_metadata( self::$large_id );
    935 
    936         foreach( $intermediates as $int_size ) {
    937             $size_array = $this->_get_image_size_array_from_name( $int_size );
     987        $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
     988        $image_meta = wp_get_attachment_metadata( self::$large_id );
     989
     990        // Make sure themes aren't filtering the sizes array.
     991        remove_all_filters( 'wp_calculate_image_sizes' );
     992
     993        foreach( $intermediates as $int_size ) {
     994            $size_array = $this->_get_image_size_array_from_name( $int_size );
    938995            $image_src = $image_meta['sizes'][$int_size]['file'];
    939996            list( $width, $height ) = $size_array;
Note: See TracChangeset for help on using the changeset viewer.