Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/functions.php

    r42228 r42343  
    3030    /**
    3131     * Get the MIME type of a file
     32     *
    3233     * @param string $filename
    3334     * @return string
     
    3637        $mime_type = '';
    3738        if ( extension_loaded( 'fileinfo' ) ) {
    38             $finfo = new finfo();
     39            $finfo     = new finfo();
    3940            $mime_type = $finfo->file( $filename, FILEINFO_MIME );
    4041        }
     
    5960            'test-image-zip.tiff',
    6061            'test-image.jpg',
    61             );
    62 
    63         foreach ($files as $file) {
    64             $this->assertTrue( file_is_valid_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return true" );
     62        );
     63
     64        foreach ( $files as $file ) {
     65            $this->assertTrue( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" );
    6566        }
    6667    }
     
    7273            'test-image.tga',
    7374            'test-image.sgi',
    74             );
    75 
    76         foreach ($files as $file) {
    77             $this->assertFalse( file_is_valid_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return false" );
     75        );
     76
     77        foreach ( $files as $file ) {
     78            $this->assertFalse( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return false" );
    7879        }
    7980    }
     
    8586            'test-image.png',
    8687            'test-image.jpg',
    87             );
    88 
    89         foreach ($files as $file) {
    90             $this->assertTrue( file_is_displayable_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return true" );
     88        );
     89
     90        foreach ( $files as $file ) {
     91            $this->assertTrue( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" );
    9192        }
    9293    }
     
    106107            'test-image.psd',
    107108            'test-image-zip.tiff',
    108             );
    109 
    110         foreach ($files as $file) {
    111             $this->assertFalse( file_is_displayable_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return false" );
     109        );
     110
     111        foreach ( $files as $file ) {
     112            $this->assertFalse( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return false" );
    112113        }
    113114    }
     
    115116    /**
    116117     * Test save image file and mime_types
     118     *
    117119     * @ticket 6821
    118120     */
     
    128130            'image/jpeg',
    129131            'image/gif',
    130             'image/png'
     132            'image/png',
    131133        );
    132134
    133135        // Test each image editor engine
    134         $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     136        $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    135137        foreach ( $classes as $class ) {
    136138
     
    140142            }
    141143
    142             $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     144            $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    143145            $loaded = $img->load();
    144146
     
    150152
    151153                $file = wp_tempnam();
    152                 $ret = wp_save_image_file( $file, $img, $mime_type, 1 );
     154                $ret  = wp_save_image_file( $file, $img, $mime_type, 1 );
    153155                $this->assertNotEmpty( $ret );
    154156                $this->assertNotInstanceOf( 'WP_Error', $ret );
     
    167169    /**
    168170     * Test that a passed mime type overrides the extension in the filename
     171     *
    169172     * @ticket 6821
    170173     */
     
    175178
    176179        // Test each image editor engine
    177         $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     180        $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    178181        foreach ( $classes as $class ) {
    179182
     
    183186            }
    184187
    185             $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     188            $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    186189            $loaded = $img->load();
    187190
    188191            // Save the file
    189192            $mime_type = 'image/gif';
    190             $file = wp_tempnam( 'tmp.jpg' );
    191             $ret = $img->save( $file, $mime_type );
     193            $file      = wp_tempnam( 'tmp.jpg' );
     194            $ret       = $img->save( $file, $mime_type );
    192195
    193196            // Make assertions
     
    205208    /**
    206209     * Test that mime types are correctly inferred from file extensions
     210     *
    207211     * @ticket 6821
    208212     */
     
    219223            'gif'  => 'image/gif',
    220224            'png'  => 'image/png',
    221             'unk'  => 'image/jpeg' // Default, unknown
     225            'unk'  => 'image/jpeg', // Default, unknown
    222226        );
    223227
    224228        // Test each image editor engine
    225         $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     229        $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    226230        foreach ( $classes as $class ) {
    227231
     
    231235            }
    232236
    233             $img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
     237            $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    234238            $loaded = $img->load();
    235239
     
    245249
    246250                $file = wp_unique_filename( $temp, uniqid() . ".$ext" );
    247                 $ret = $img->save( trailingslashit( $temp ) . $file );
     251                $ret  = $img->save( trailingslashit( $temp ) . $file );
    248252                $this->assertNotEmpty( $ret );
    249253                $this->assertNotInstanceOf( 'WP_Error', $ret );
     
    273277
    274278        // Then, test with editors.
    275         $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
     279        $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    276280        foreach ( $classes as $class ) {
    277281            // If the image editor isn't available, skip it
     
    289293
    290294    public function test_wp_crop_image_file() {
    291         if ( !function_exists( 'imagejpeg' ) )
     295        if ( ! function_exists( 'imagejpeg' ) ) {
    292296            $this->fail( 'jpeg support unavailable' );
    293 
    294         $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg',
    295             0, 0, 100, 100, 100, 100 );
     297        }
     298
     299        $file = wp_crop_image(
     300            DIR_TESTDATA . '/images/canola.jpg',
     301            0, 0, 100, 100, 100, 100
     302        );
    296303        $this->assertNotInstanceOf( 'WP_Error', $file );
    297304        $this->assertFileExists( $file );
    298305        $image = wp_get_image_editor( $file );
    299         $size = $image->get_size();
     306        $size  = $image->get_size();
    300307        $this->assertEquals( 100, $size['height'] );
    301308        $this->assertEquals( 100, $size['width'] );
     
    305312
    306313    public function test_wp_crop_image_url() {
    307         if ( !function_exists( 'imagejpeg' ) )
     314        if ( ! function_exists( 'imagejpeg' ) ) {
    308315            $this->fail( 'jpeg support unavailable' );
     316        }
    309317
    310318        if ( ! extension_loaded( 'openssl' ) ) {
     
    312320        }
    313321
    314         $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg',
     322        $file = wp_crop_image(
     323            'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg',
    315324            0, 0, 100, 100, 100, 100, false,
    316             DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg' );
     325            DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg'
     326        );
    317327        $this->assertNotInstanceOf( 'WP_Error', $file );
    318328        $this->assertFileExists( $file );
    319329        $image = wp_get_image_editor( $file );
    320         $size = $image->get_size();
     330        $size  = $image->get_size();
    321331        $this->assertEquals( 100, $size['height'] );
    322332        $this->assertEquals( 100, $size['width'] );
     
    326336
    327337    public function test_wp_crop_image_file_not_exist() {
    328         $file = wp_crop_image( DIR_TESTDATA . '/images/canoladoesnotexist.jpg',
    329             0, 0, 100, 100, 100, 100 );
     338        $file = wp_crop_image(
     339            DIR_TESTDATA . '/images/canoladoesnotexist.jpg',
     340            0, 0, 100, 100, 100, 100
     341        );
    330342        $this->assertInstanceOf( 'WP_Error', $file );
    331343    }
     
    336348        }
    337349
    338         $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg',
    339             0, 0, 100, 100, 100, 100 );
     350        $file = wp_crop_image(
     351            'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg',
     352            0, 0, 100, 100, 100, 100
     353        );
    340354        $this->assertInstanceOf( 'WP_Error', $file );
    341355    }
     
    352366        add_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) );
    353367
    354         $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg',
    355             0, 0, 100, 100, 100, 100 );
     368        $file = wp_crop_image(
     369            DIR_TESTDATA . '/images/canola.jpg',
     370            0, 0, 100, 100, 100, 100
     371        );
    356372        $this->assertInstanceOf( 'WP_Error', $file );
    357373
     
    372388        copy( $orig_file, $test_file );
    373389
    374         $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array(
    375             'post_mime_type' => 'application/pdf',
    376         ) );
     390        $attachment_id = $this->factory->attachment->create_object(
     391            $test_file, 0, array(
     392                'post_mime_type' => 'application/pdf',
     393            )
     394        );
    377395
    378396        $this->assertNotEmpty( $attachment_id );
     
    381399            'sizes' => array(
    382400                'thumbnail' => array(
    383                     'file'      => "wordpress-gsoc-flyer-pdf-116x150.jpg",
     401                    'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
    384402                    'width'     => 116,
    385403                    'height'    => 150,
    386                     'mime-type' => "image/jpeg",
     404                    'mime-type' => 'image/jpeg',
    387405                ),
    388406                'medium'    => array(
    389                     'file'      => "wordpress-gsoc-flyer-pdf-232x300.jpg",
     407                    'file'      => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
    390408                    'width'     => 232,
    391409                    'height'    => 300,
    392                     'mime-type' => "image/jpeg",
     410                    'mime-type' => 'image/jpeg',
    393411                ),
    394412                'large'     => array(
    395                     'file'      => "wordpress-gsoc-flyer-pdf-791x1024.jpg",
     413                    'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
    396414                    'width'     => 791,
    397415                    'height'    => 1024,
    398                     'mime-type' => "image/jpeg",
     416                    'mime-type' => 'image/jpeg',
    399417                ),
    400418                'full'      => array(
    401                     'file'      => "wordpress-gsoc-flyer-pdf.jpg",
     419                    'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
    402420                    'width'     => 1088,
    403421                    'height'    => 1408,
    404                     'mime-type' => "image/jpeg",
     422                    'mime-type' => 'image/jpeg',
    405423                ),
    406424            ),
     
    412430        unlink( $test_file );
    413431        foreach ( $metadata['sizes'] as $size ) {
    414             unlink ( '/tmp/' . $size['file'] );
     432            unlink( '/tmp/' . $size['file'] );
    415433        }
    416434    }
     
    428446        copy( $orig_file, $test_file );
    429447
    430         $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array(
    431             'post_mime_type' => 'application/pdf',
    432         ) );
     448        $attachment_id = $this->factory->attachment->create_object(
     449            $test_file, 0, array(
     450                'post_mime_type' => 'application/pdf',
     451            )
     452        );
    433453
    434454        $this->assertNotEmpty( $attachment_id );
     
    453473        unlink( $test_file );
    454474        foreach ( $metadata['sizes'] as $size ) {
    455             unlink ( '/tmp/' . $size['file'] );
     475            unlink( '/tmp/' . $size['file'] );
    456476        }
    457477    }
     
    466486    /**
    467487     * Test PDF preview doesn't overwrite existing JPEG.
     488     *
    468489     * @ticket 39875
    469490     */
     
    483504        copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path );
    484505
    485         $attachment_id = $this->factory->attachment->create_object( $pdf_path, 0, array(
    486             'post_mime_type' => 'application/pdf',
    487         ) );
    488 
    489         $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path );
     506        $attachment_id = $this->factory->attachment->create_object(
     507            $pdf_path, 0, array(
     508                'post_mime_type' => 'application/pdf',
     509            )
     510        );
     511
     512        $metadata     = wp_generate_attachment_metadata( $attachment_id, $pdf_path );
    490513        $preview_path = '/tmp/' . $metadata['sizes']['full']['file'];
    491514
Note: See TracChangeset for help on using the changeset viewer.