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/post/attachments.php

    r40531 r42343  
    1919
    2020        $upload = wp_upload_bits( $filename, null, $contents );
    21         $this->assertTrue( empty($upload['error']) );
     21        $this->assertTrue( empty( $upload['error'] ) );
    2222    }
    2323
     
    2525
    2626        // this image is smaller than the thumbnail size so it won't have one
    27         $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
    28         $contents = file_get_contents($filename);
    29 
    30         $upload = wp_upload_bits(basename($filename), null, $contents);
    31         $this->assertTrue( empty($upload['error']) );
    32 
    33         $id = $this->_make_attachment($upload);
     27        $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     28        $contents = file_get_contents( $filename );
     29
     30        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     31        $this->assertTrue( empty( $upload['error'] ) );
     32
     33        $id = $this->_make_attachment( $upload );
    3434
    3535        // intermediate copies should not exist
    36         $this->assertFalse( image_get_intermediate_size($id, 'thumbnail') );
    37         $this->assertFalse( image_get_intermediate_size($id, 'medium') );
    38         $this->assertFalse( image_get_intermediate_size($id, 'medium_large') );
     36        $this->assertFalse( image_get_intermediate_size( $id, 'thumbnail' ) );
     37        $this->assertFalse( image_get_intermediate_size( $id, 'medium' ) );
     38        $this->assertFalse( image_get_intermediate_size( $id, 'medium_large' ) );
    3939
    4040        // medium, medium_large, and full size will both point to the original
    41         $downsize = image_downsize($id, 'medium');
    42         $this->assertEquals( basename( $upload['file'] ), basename($downsize[0]) );
     41        $downsize = image_downsize( $id, 'medium' );
     42        $this->assertEquals( basename( $upload['file'] ), basename( $downsize[0] ) );
    4343        $this->assertEquals( 50, $downsize[1] );
    4444        $this->assertEquals( 50, $downsize[2] );
    4545
    46         $downsize = image_downsize($id, 'medium_large');
    47         $this->assertEquals( basename( $upload['file'] ), basename($downsize[0]) );
     46        $downsize = image_downsize( $id, 'medium_large' );
     47        $this->assertEquals( basename( $upload['file'] ), basename( $downsize[0] ) );
    4848        $this->assertEquals( 50, $downsize[1] );
    4949        $this->assertEquals( 50, $downsize[2] );
    5050
    51         $downsize = image_downsize($id, 'full');
    52         $this->assertEquals( basename( $upload['file'] ), basename($downsize[0]) );
     51        $downsize = image_downsize( $id, 'full' );
     52        $this->assertEquals( basename( $upload['file'] ), basename( $downsize[0] ) );
    5353        $this->assertEquals( 50, $downsize[1] );
    5454        $this->assertEquals( 50, $downsize[2] );
     
    5757
    5858    function test_insert_image_thumb_only() {
    59         if ( !function_exists( 'imagejpeg' ) )
     59        if ( ! function_exists( 'imagejpeg' ) ) {
    6060            $this->fail( 'jpeg support unavailable' );
     61        }
    6162
    6263        update_option( 'medium_size_w', 0 );
    6364        update_option( 'medium_size_h', 0 );
    6465
    65         $filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
    66         $contents = file_get_contents($filename);
    67 
    68         $upload = wp_upload_bits(basename($filename), null, $contents);
    69         $this->assertTrue( empty($upload['error']) );
    70 
    71         $id = $this->_make_attachment($upload);
     66        $filename = ( DIR_TESTDATA . '/images/a2-small.jpg' );
     67        $contents = file_get_contents( $filename );
     68
     69        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     70        $this->assertTrue( empty( $upload['error'] ) );
     71
     72        $id = $this->_make_attachment( $upload );
    7273
    7374        // intermediate copies should exist: thumbnail only
    74         $thumb = image_get_intermediate_size($id, 'thumbnail');
     75        $thumb = image_get_intermediate_size( $id, 'thumbnail' );
    7576        $this->assertEquals( 'a2-small-150x150.jpg', $thumb['file'] );
    7677
    7778        $uploads = wp_upload_dir();
    78         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
    79 
    80         $this->assertFalse( image_get_intermediate_size($id, 'medium') );
    81         $this->assertFalse( image_get_intermediate_size($id, 'medium_large') );
     79        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     80
     81        $this->assertFalse( image_get_intermediate_size( $id, 'medium' ) );
     82        $this->assertFalse( image_get_intermediate_size( $id, 'medium_large' ) );
    8283
    8384        // the thumb url should point to the thumbnail intermediate
    84         $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
     85        $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url( $id ) );
    8586
    8687        // image_downsize() should return the correct images and sizes
    87         $downsize = image_downsize($id, 'thumbnail');
    88         $this->assertEquals( 'a2-small-150x150.jpg', basename($downsize[0]) );
     88        $downsize = image_downsize( $id, 'thumbnail' );
     89        $this->assertEquals( 'a2-small-150x150.jpg', basename( $downsize[0] ) );
    8990        $this->assertEquals( 150, $downsize[1] );
    9091        $this->assertEquals( 150, $downsize[2] );
    9192
    9293        // medium, medium_large, and full will both point to the original
    93         $downsize = image_downsize($id, 'medium');
    94         $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
     94        $downsize = image_downsize( $id, 'medium' );
     95        $this->assertEquals( 'a2-small.jpg', basename( $downsize[0] ) );
    9596        $this->assertEquals( 400, $downsize[1] );
    9697        $this->assertEquals( 300, $downsize[2] );
    9798
    98         $downsize = image_downsize($id, 'medium_large');
    99         $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
     99        $downsize = image_downsize( $id, 'medium_large' );
     100        $this->assertEquals( 'a2-small.jpg', basename( $downsize[0] ) );
    100101        $this->assertEquals( 400, $downsize[1] );
    101102        $this->assertEquals( 300, $downsize[2] );
    102103
    103         $downsize = image_downsize($id, 'full');
    104         $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
     104        $downsize = image_downsize( $id, 'full' );
     105        $this->assertEquals( 'a2-small.jpg', basename( $downsize[0] ) );
    105106        $this->assertEquals( 400, $downsize[1] );
    106107        $this->assertEquals( 300, $downsize[2] );
     
    109110
    110111    function test_insert_image_medium_sizes() {
    111         if ( !function_exists( 'imagejpeg' ) )
     112        if ( ! function_exists( 'imagejpeg' ) ) {
    112113            $this->fail( 'jpeg support unavailable' );
    113 
    114         update_option('medium_size_w', 400);
    115         update_option('medium_size_h', 0);
    116 
    117         update_option('medium_large_size_w', 600);
    118         update_option('medium_large_size_h', 0);
    119 
    120         $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
    121         $contents = file_get_contents($filename);
    122 
    123         $upload = wp_upload_bits(basename($filename), null, $contents);
    124         $this->assertTrue( empty($upload['error']) );
    125 
    126         $id = $this->_make_attachment($upload);
     114        }
     115
     116        update_option( 'medium_size_w', 400 );
     117        update_option( 'medium_size_h', 0 );
     118
     119        update_option( 'medium_large_size_w', 600 );
     120        update_option( 'medium_large_size_h', 0 );
     121
     122        $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
     123        $contents = file_get_contents( $filename );
     124
     125        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     126        $this->assertTrue( empty( $upload['error'] ) );
     127
     128        $id      = $this->_make_attachment( $upload );
    127129        $uploads = wp_upload_dir();
    128130
    129131        // intermediate copies should exist: thumbnail and medium
    130         $thumb = image_get_intermediate_size($id, 'thumbnail');
     132        $thumb = image_get_intermediate_size( $id, 'thumbnail' );
    131133        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
    132         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
    133 
    134         $medium = image_get_intermediate_size($id, 'medium');
     134        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     135
     136        $medium = image_get_intermediate_size( $id, 'medium' );
    135137        $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
    136         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path']) );
    137 
    138         $medium_large = image_get_intermediate_size($id, 'medium_large');
     138        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
     139
     140        $medium_large = image_get_intermediate_size( $id, 'medium_large' );
    139141        $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
    140         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']) );
     142        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
    141143
    142144        // the thumb url should point to the thumbnail intermediate
    143         $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
     145        $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url( $id ) );
    144146
    145147        // image_downsize() should return the correct images and sizes
    146         $downsize = image_downsize($id, 'thumbnail');
    147         $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($downsize[0]) );
     148        $downsize = image_downsize( $id, 'thumbnail' );
     149        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename( $downsize[0] ) );
    148150        $this->assertEquals( 150, $downsize[1] );
    149151        $this->assertEquals( 150, $downsize[2] );
    150152
    151         $downsize = image_downsize($id, 'medium');
    152         $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', basename($downsize[0]) );
     153        $downsize = image_downsize( $id, 'medium' );
     154        $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', basename( $downsize[0] ) );
    153155        $this->assertEquals( 400, $downsize[1] );
    154156        $this->assertEquals( 602, $downsize[2] );
    155157
    156         $downsize = image_downsize($id, 'medium_large');
    157         $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', basename($downsize[0]) );
     158        $downsize = image_downsize( $id, 'medium_large' );
     159        $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', basename( $downsize[0] ) );
    158160        $this->assertEquals( 600, $downsize[1] );
    159161        $this->assertEquals( 904, $downsize[2] );
    160162
    161         $downsize = image_downsize($id, 'full');
    162         $this->assertEquals( '2007-06-17DSC_4173.jpg', basename($downsize[0]) );
     163        $downsize = image_downsize( $id, 'full' );
     164        $this->assertEquals( '2007-06-17DSC_4173.jpg', basename( $downsize[0] ) );
    163165        $this->assertEquals( 680, $downsize[1] );
    164166        $this->assertEquals( 1024, $downsize[2] );
     
    167169
    168170    function test_insert_image_delete() {
    169         if ( !function_exists( 'imagejpeg' ) )
     171        if ( ! function_exists( 'imagejpeg' ) ) {
    170172            $this->fail( 'jpeg support unavailable' );
    171 
    172         update_option('medium_size_w', 400);
    173         update_option('medium_size_h', 0);
    174 
    175         update_option('medium_large_size_w', 600);
    176         update_option('medium_large_size_h', 0);
    177 
    178         $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
    179         $contents = file_get_contents($filename);
    180 
    181         $upload = wp_upload_bits(basename($filename), null, $contents);
    182         $this->assertTrue( empty($upload['error']) );
    183 
    184         $id = $this->_make_attachment($upload);
     173        }
     174
     175        update_option( 'medium_size_w', 400 );
     176        update_option( 'medium_size_h', 0 );
     177
     178        update_option( 'medium_large_size_w', 600 );
     179        update_option( 'medium_large_size_h', 0 );
     180
     181        $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
     182        $contents = file_get_contents( $filename );
     183
     184        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     185        $this->assertTrue( empty( $upload['error'] ) );
     186
     187        $id      = $this->_make_attachment( $upload );
    185188        $uploads = wp_upload_dir();
    186189
    187190        // check that the file and intermediates exist
    188         $thumb = image_get_intermediate_size($id, 'thumbnail');
     191        $thumb = image_get_intermediate_size( $id, 'thumbnail' );
    189192        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
    190         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
    191 
    192         $medium = image_get_intermediate_size($id, 'medium');
     193        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
     194
     195        $medium = image_get_intermediate_size( $id, 'medium' );
    193196        $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
    194         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path']) );
    195 
    196         $medium_large = image_get_intermediate_size($id, 'medium_large');
     197        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
     198
     199        $medium_large = image_get_intermediate_size( $id, 'medium_large' );
    197200        $this->assertEquals( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
    198         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path']) );
    199 
    200         $meta = wp_get_attachment_metadata($id);
     201        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
     202
     203        $meta     = wp_get_attachment_metadata( $id );
    201204        $original = $meta['file'];
    202         $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original) );
     205        $this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
    203206
    204207        // now delete the attachment and make sure all files are gone
    205         wp_delete_attachment($id);
    206 
    207         $this->assertFalse( is_file($thumb['path']) );
    208         $this->assertFalse( is_file($medium['path']) );
    209         $this->assertFalse( is_file($medium_large['path']) );
    210         $this->assertFalse( is_file($original) );
     208        wp_delete_attachment( $id );
     209
     210        $this->assertFalse( is_file( $thumb['path'] ) );
     211        $this->assertFalse( is_file( $medium['path'] ) );
     212        $this->assertFalse( is_file( $medium_large['path'] ) );
     213        $this->assertFalse( is_file( $original ) );
    211214    }
    212215
    213216    /**
    214217     * GUID should never be empty
     218     *
    215219     * @ticket 18310
    216220     * @ticket 21963
     
    218222    function test_insert_image_without_guid() {
    219223        // this image is smaller than the thumbnail size so it won't have one
    220         $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
    221         $contents = file_get_contents($filename);
    222 
    223         $upload = wp_upload_bits(basename($filename), null, $contents);
    224         $this->assertTrue( empty($upload['error']) );
     224        $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     225        $contents = file_get_contents( $filename );
     226
     227        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     228        $this->assertTrue( empty( $upload['error'] ) );
    225229
    226230        $upload['url'] = '';
    227         $id = $this->_make_attachment( $upload );
     231        $id            = $this->_make_attachment( $upload );
    228232
    229233        $guid = get_the_guid( $id );
     
    236240    function test_update_attachment_fields() {
    237241        $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
    238         $contents = file_get_contents($filename);
     242        $contents = file_get_contents( $filename );
    239243
    240244        $upload = wp_upload_bits( basename( $filename ), null, $contents );
     
    247251        $post = get_post( $id, ARRAY_A );
    248252
    249         $post['post_title'] = 'title';
     253        $post['post_title']   = 'title';
    250254        $post['post_excerpt'] = 'caption';
    251255        $post['post_content'] = 'description';
     
    273277        $this->assertEquals( $attachment->post_parent, 0 );
    274278
    275         $post_id = wp_insert_post( array( 'post_content' => rand_str(), 'post_title' => rand_str() ) );
     279        $post_id = wp_insert_post(
     280            array(
     281                'post_content' => rand_str(),
     282                'post_title'   => rand_str(),
     283            )
     284        );
    276285
    277286        // Assert that the attachment has a parent
     
    350359
    351360        // Ensure that server host matches the host of wp_upload_dir().
    352         $upload_dir = wp_upload_dir();
     361        $upload_dir           = wp_upload_dir();
    353362        $_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
    354363
     
    378387
    379388        // Ensure that server host matches the host of wp_upload_dir().
    380         $upload_dir = wp_upload_dir();
     389        $upload_dir           = wp_upload_dir();
    381390        $_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
    382391
     
    388397
    389398    /**
    390     * @ticket 15928
    391     */
     399     * @ticket 15928
     400     */
    392401    public function test_wp_get_attachment_url_should_not_force_https_when_administering_over_https_but_siteurl_is_not_https() {
    393402        $siteurl = get_option( 'siteurl' );
     
    418427     */
    419428    public function test_wp_get_attachment_url_should_force_https_when_administering_over_https_and_siteurl_is_https() {
    420         // Set https upload URL 
     429        // Set https upload URL
    421430        add_filter( 'upload_dir', '_upload_dir_https' );
    422431
     
    446455        $contents = file_get_contents( $filename );
    447456
    448         $upload = wp_upload_bits( basename( $filename ), null, $contents );
     457        $upload        = wp_upload_bits( basename( $filename ), null, $contents );
    449458        $attachment_id = $this->_make_attachment( $upload );
    450459
     
    464473        $contents = file_get_contents( $filename );
    465474
    466         $upload = wp_upload_bits( basename( $filename ), null, $contents );
     475        $upload        = wp_upload_bits( basename( $filename ), null, $contents );
    467476        $attachment_id = $this->_make_attachment( $upload );
    468477
Note: See TracChangeset for help on using the changeset viewer.