WordPress.org

Make WordPress Core

Changeset 35179


Ignore:
Timestamp:
10/15/2015 01:42:27 AM (5 years ago)
Author:
wonderboymusic
Message:

Unit Tests: make a fixture in Tests_Media to represent the large image, instead of creating it 10 times.

See #30017, #33968.

File:
1 edited

Legend:

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

    r35178 r35179  
    66 */
    77class Tests_Media extends WP_UnitTestCase {
     8    protected static $large_id;
     9
     10    public static function setUpBeforeClass() {
     11        parent::setUpBeforeClass();
     12
     13        $factory = new WP_UnitTest_Factory();
     14
     15        $filename = DIR_TESTDATA . '/images/test-image-large.png';
     16        self::$large_id = $factory->attachment->create_upload_object( $filename );
     17
     18        self::commit_transaction();
     19    }
     20
     21    public static function tearDownAfterClass() {
     22        parent::tearDownAfterClass();
     23
     24        wp_delete_attachment( self::$large_id );
     25
     26        self::commit_transaction();
     27    }
    828
    929    function setUp() {
     
    710730     */
    711731    function test_wp_get_attachment_image_srcset_array() {
    712         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    713         $id = $this->factory->attachment->create_upload_object( $filename );
    714 
    715732        $year_month = date('Y/m');
    716         $image = wp_get_attachment_metadata( $id );
     733        $image = wp_get_attachment_metadata( self::$large_id );
    717734
    718735        $expected = array(
     
    738755
    739756        foreach ( $sizes as $size ) {
    740             $this->assertSame( $expected, wp_get_attachment_image_srcset_array( $id, $size ) );
     757            $this->assertSame( $expected, wp_get_attachment_image_srcset_array( self::$large_id, $size ) );
    741758        }
    742759    }
     
    791808     */
    792809    function test_wp_get_attachment_image_srcset_array_with_edits() {
    793         // Make an image.
    794         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    795         $id = $this->factory->attachment->create_upload_object( $filename );
    796810        // For this test we're going to mock metadata changes from an edit.
    797811        // Start by getting the attachment metadata.
    798         $meta = wp_get_attachment_metadata( $id );
     812        $meta = wp_get_attachment_metadata( self::$large_id );
    799813
    800814        // Copy hash generation method used in wp_save_image().
     
    807821
    808822        // Save edited metadata.
    809         wp_update_attachment_metadata( $id, $meta );
    810 
    811         // Get the edited image and observe that a hash was created.
    812         $img_url = wp_get_attachment_url( $id );
     823        wp_update_attachment_metadata( self::$large_id, $meta );
    813824
    814825        // Calculate a srcset array.
    815         $sizes = wp_get_attachment_image_srcset_array( $id, 'medium' );
     826        $sizes = wp_get_attachment_image_srcset_array( self::$large_id, 'medium' );
    816827
    817828        // Test to confirm all sources in the array include the same edit hash.
     
    825836     */
    826837    function test_wp_get_attachment_image_srcset_array_false() {
    827         // Make an image.
    828         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    829         $id = $this->factory->attachment->create_upload_object( $filename );
    830838        $sizes = wp_get_attachment_image_srcset_array( 99999, 'foo' );
    831839
     
    846854        $srcset = wp_get_attachment_image_srcset_array( $id, 'medium' );
    847855
     856        // Remove filter.
     857        remove_filter( 'wp_generate_attachment_metadata', array( $this, '_test_wp_get_attachment_image_srcset_array_no_width_filter' ) );
     858
    848859        // The srcset should be false.
    849860        $this->assertFalse( $srcset );
    850 
    851         // Remove filter.
    852         remove_filter( 'wp_generate_attachment_metadata', array( $this, '_test_wp_get_attachment_image_srcset_array_no_width_filter' ) );
    853861    }
    854862
     
    866874     */
    867875    function test_wp_get_attachment_image_srcset() {
    868         // Make an image.
    869         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    870         $id = $this->factory->attachment->create_upload_object( $filename );
    871         $sizes = wp_get_attachment_image_srcset( $id, 'full-size' );
    872 
    873         $image = wp_get_attachment_metadata( $id );
     876        $sizes = wp_get_attachment_image_srcset( self::$large_id, 'full-size' );
     877
     878        $image = wp_get_attachment_metadata( self::$large_id );
    874879        $year_month = date('Y/m');
    875880
     
    887892     */
    888893    function test_wp_get_attachment_image_srcset_single_srcset() {
    889         // Make an image.
    890         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    891         $id = $this->factory->attachment->create_upload_object( $filename );
    892894        /*
    893895         * In our tests, thumbnails will only return a single srcset candidate,
    894896         * so we shouldn't return a srcset value in order to avoid unneeded markup.
    895897         */
    896         $sizes = wp_get_attachment_image_srcset( $id, 'thumbnail' );
     898        $sizes = wp_get_attachment_image_srcset( self::$large_id, 'thumbnail' );
    897899
    898900        $this->assertFalse( $sizes );
     
    903905     */
    904906    function test_wp_get_attachment_image_sizes() {
    905         // Make an image.
    906         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    907         $id = $this->factory->attachment->create_upload_object( $filename );
    908 
    909 
    910907        global $content_width;
    911908
     
    922919
    923920            $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
    924             $sizes = wp_get_attachment_image_sizes( $id, $int );
     921            $sizes = wp_get_attachment_image_sizes( self::$large_id, $int );
    925922
    926923            $this->assertSame($expected, $sizes);
     
    932929     */
    933930    function test_wp_get_attachment_image_sizes_with_args() {
    934         // Make an image.
    935         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    936         $id = $this->factory->attachment->create_upload_object( $filename );
    937 
    938 
    939931        $args = array(
    940932            'sizes' => array(
     
    956948
    957949        $expected = '(min-width: 60em) 10em, (min-width: 30em) 20em, calc(100vm - 30px)';
    958         $sizes = wp_get_attachment_image_sizes( $id, 'medium', $args );
    959 
    960         $this->assertSame($expected, $sizes);
     950        $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $args );
     951
     952        $this->assertSame( $expected, $sizes );
    961953    }
    962954
     
    968960        add_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );
    969961
    970         // Make an image.
    971         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    972         $id = $this->factory->attachment->create_upload_object( $filename );
    973 
    974         $sizes = wp_get_attachment_image_sizes($id, 'medium');
     962        $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium' );
     963
     964        remove_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );
    975965
    976966        // Evaluate that the sizes returned is what we expected.
    977967        $this->assertSame( $sizes, '100vm');
    978 
    979         remove_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );
    980968    }
    981969
     
    992980     */
    993981    function test_wp_make_content_images_responsive() {
    994         // Make an image.
    995         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    996         $id = $this->factory->attachment->create_upload_object( $filename );
    997 
    998         $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( $id, 'medium' ) );
    999         $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $id, 'medium' ) );
     982        $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, 'medium' ) );
     983        $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, 'medium' ) );
    1000984
    1001985        // Function used to build HTML for the editor.
    1002         $img = get_image_tag( $id, '', '', '', 'medium' );
     986        $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
    1003987        $img_no_size = str_replace( 'size-', '', $img );
    1004988        $img_no_size_id = str_replace( 'wp-image-', 'id-', $img_no_size );
     
    10461030     */
    10471031    function test_wp_make_content_images_responsive_with_preexisting_srcset() {
    1048         // Make an image.
    1049         $filename = DIR_TESTDATA . '/images/test-image-large.png';
    1050         $id = $this->factory->attachment->create_upload_object( $filename );
    1051 
    10521032        // Generate HTML and add a dummy srcset attribute.
    1053         $image_html = get_image_tag( $id, '', '', '', 'medium' );
     1033        $image_html = get_image_tag( self::$large_id, '', '', '', 'medium' );
    10541034        $image_html = preg_replace('|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html );
    10551035
Note: See TracChangeset for help on using the changeset viewer.