Make WordPress Core

Changeset 328 in tests


Ignore:
Timestamp:
01/20/2011 02:54:26 PM (14 years ago)
Author:
duck_
Message:

Fixes and cleanup in test_includes_functions.php

TestFunctions: address failures in test_wp_unique_filename, extra tests in test_is_serialized
TestHTTPFunctions: use wp_remote_retrieve_response_code, remove redirect limit test using deprecated arguments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_includes_functions.php

    r279 r328  
    22
    33class TestFunctions extends WPTestCase {
    4     var $cache = NULL;
    5 
    64    function setUp() {
    75        parent::setUp();
     
    4745    }
    4846    function test_size_format() {
    49         $this->knownWPBug(5246);
    5047        $kb = 1024;
    5148        $mb = $kb*1024;
     
    6663        $this->assertFalse(size_format(array()));
    6764    }
    68    
     65
    6966    function test_path_is_absolute() {
    7067        if ( !is_callable('path_is_absolute') )
    7168            $this->markTestSkipped();
    72            
     69
    7370        $absolute_paths = array(
    7471            '/',
     
    109106
    110107    function test_wp_unique_filename() {
    111         $this->knownWPBug(6294);   
    112        
    113108        /* this test requires:
    114            - that you have dir + file 'wp-testdata/images/test-image.png', 
     109           - that you have dir + file 'wp-testdata/images/test-image.png',
    115110           - and that this dir is writeable
    116111           - there is an image 'test-image.png' that will be used to test unique filenames
    117        
     112
    118113           NB: there is a hardcoded dependency that the testing file is '.png'; however,
    119114               this limitation is arbitary, so change it if you like.
     
    124119        $this->assertTrue( is_writable($testdir) );
    125120        $this->assertTrue( file_exists($testdir . $testimg) );
    126        
     121
    127122        $cases = array(
    128                 // null case
    129                 'null' . $testimg,
    130            
    131                 // edge cases: '.png', 'abc.', 'abc',
    132                 // 'abc0', 'abc1', 'abc0.png', 'abc1.png' (num @ end)
    133                 '.png',
    134                 'abc',
    135                 'abc.',
    136                 'abc0',
    137                 'abc1',
    138                 'abc0.png',
    139                 'abc1.png',
    140                
    141               // replacing # with _
    142                 str_replace('-', '#', $testimg), // test#image.png
    143                 str_replace('-', '##', $testimg), // test##image.png
    144                 str_replace(array('-', 'e'), '#', $testimg), // t#st#imag#.png
    145                 str_replace(array('-', 'e'), '##', $testimg), // t##st##imag##.png
    146                
    147                 // replacing \ or ' with nothing
    148                 str_replace('-', '\\', $testimg), // test\image.png
    149                 str_replace('-', '\\\\', $testimg), // test\\image.png
    150                 str_replace(array('-', 'e'), '\\', $testimg), // t\st\imag\.png
    151                 str_replace(array('-', 'e'), '\\\\', $testimg), // t\\st\\imag\\.png
    152                 str_replace('-', "'", $testimg), // test'image.png
    153                 str_replace('-', "'", $testimg), // test''image.png
    154                 str_replace(array('-', 'e'), "'", $testimg), // t'st'imag'.png
    155                 str_replace(array('-', 'e'), "''", $testimg), // t''st''imag''.png
    156                 str_replace('-', "\'", $testimg), // test\'image.png
    157                 str_replace('-', "\'\'", $testimg), // test\'\'image.png
    158                 str_replace(array('-', 'e'), "\'", $testimg), // t\'st\'imag\'.png
    159                 str_replace(array('-', 'e'), "\'\'", $testimg), // t\'\'st\'\'imag\'\'.png
    160                
    161                 // sanitize_title_with_dashes
    162                 // incomplete coverage; attempts to cover the most common cases
    163                 str_replace('-', '%', $testimg), // test%image.png
    164                
    165                 'test' . str_replace('e', 'é', $testimg), // tést-imagé.png
    166                
    167                 '12%af34567890~!@#$..%^&*()_+qwerty  uiopasd fghjkl zxcvbnm<>?:"{}".png', // kitchen sink
    168                 $testdir.'test-image-with-path.png',
    169             );
    170        
     123            // null case
     124            'null' . $testimg,
     125
     126            // edge cases: '.png', 'abc.', 'abc', 'abc0', 'abc1', 'abc0.png', 'abc1.png' (num @ end)
     127            '.png',
     128            'abc',
     129            'abc.',
     130            'abc0',
     131            'abc1',
     132            'abc0.png',
     133            'abc1.png',
     134
     135            // replacing # with _
     136            str_replace('-', '#', $testimg), // test#image.png
     137            str_replace('-', '##', $testimg), // test##image.png
     138            str_replace(array('-', 'e'), '#', $testimg), // t#st#imag#.png
     139            str_replace(array('-', 'e'), '##', $testimg), // t##st##imag##.png
     140
     141            // replacing \ or ' with nothing
     142            str_replace('-', '\\', $testimg), // test\image.png
     143            str_replace('-', '\\\\', $testimg), // test\\image.png
     144            str_replace(array('-', 'e'), '\\', $testimg), // t\st\imag\.png
     145            str_replace(array('-', 'e'), '\\\\', $testimg), // t\\st\\imag\\.png
     146            str_replace('-', "'", $testimg), // test'image.png
     147            str_replace('-', "'", $testimg), // test''image.png
     148            str_replace(array('-', 'e'), "'", $testimg), // t'st'imag'.png
     149            str_replace(array('-', 'e'), "''", $testimg), // t''st''imag''.png
     150            str_replace('-', "\'", $testimg), // test\'image.png
     151            str_replace('-', "\'\'", $testimg), // test\'\'image.png
     152            str_replace(array('-', 'e'), "\'", $testimg), // t\'st\'imag\'.png
     153            str_replace(array('-', 'e'), "\'\'", $testimg), // t\'\'st\'\'imag\'\'.png
     154
     155            'test' . str_replace('e', 'é', $testimg), // testtést-imagé.png
     156
     157            '12%af34567890~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png', // kitchen sink
     158            $testdir.'test-image-with-path.png',
     159        );
     160
    171161        // what we expect the replacements will do
    172162        $expected = array(
    173163                'null' . $testimg,
    174                
    175                 '.png',
     164
     165                'png',
    176166                'abc',
    177167                'abc',
     
    180170                'abc0.png',
    181171                'abc1.png',
    182                
    183                 'testimage.png',
    184                 'testimage.png',
    185                 'tstimag.png',
    186                 'tstimag.png',
    187                
    188                 'testimage.png',
    189                 'testimage.png',
    190                 'tstimag.png',
    191                 'tstimag.png',
    192                 'testimage.png',
    193                 'testimage.png',
    194                 'tstimag.png',
    195                 'tstimag.png',
    196                 'testimage.png',
    197                 'testimage.png',
    198                 'tstimag.png',
    199                 'tstimag.png',
    200                
    201                 'testimage.png',
    202                
    203                 'testtest-image.png',
    204                
    205                 '12%af34567890_qwerty-uiopasd-fghjkl-zxcvbnm.png',
    206                 'test-image-with-path.png',
     172
     173                'testimage.png',
     174                'testimage.png',
     175                'tstimag.png',
     176                'tstimag.png',
     177
     178                'testimage.png',
     179                'testimage.png',
     180                'tstimag.png',
     181                'tstimag.png',
     182                'testimage.png',
     183                'testimage.png',
     184                'tstimag.png',
     185                'tstimag.png',
     186                'testimage.png',
     187                'testimage.png',
     188                'tstimag.png',
     189                'tstimag.png',
     190
     191                'testtést-imagé.png',
     192
     193                '12%af34567890@..%^_+qwerty-fghjkl-zx.png',
     194                str_replace( '/', '', $testdir ).'test-image-with-path.png', // may need array( '/', '\\', ':' ) -> '' for Windows
    207195            );
    208        
    209         foreach ($cases as $key => $case)
    210         {
     196
     197        foreach ($cases as $key => $case) {
    211198            // make sure expected file doesn't exist already
    212199            // happens when tests fail and the unlinking doesn't happen
     
    215202
    216203            // -- TEST 1: the replacement is as expected
    217            
    218204            $this->assertEquals( $expected[$key], wp_unique_filename($testdir, $case, NULL), $case );
    219            
    220205            // -- end TEST 1
    221            
    222            
    223            
    224             // -- TEST 2: the renaming will produce a unique name
    225            
     206
     207            // -- TEST 2: the renaming will produce a unique name
    226208            // create the expected file
    227209            copy($testdir . $testimg, $testdir . $expected[$key]);
    228            
    229210            // test that wp_unique_filename actually returns a unique filename
    230211            $this->assertFileNotExists( $testdir . wp_unique_filename($testdir, $case, NULL) );
    231            
    232212            // -- end TEST 2
    233            
    234            
    235            
     213
    236214            // cleanup
    237215            if( $expected[$key] !== $testimg &&  file_exists($testdir . $expected[$key]) )
     
    239217        }
    240218    }
    241    
     219
    242220    function test_is_serialized() {
    243221        $this->knownWPBug(9930);
    244222        $cases = array(
    245                        serialize("a\nb"),
    246                        serialize(-25),
    247                        serialize(25),
    248                        serialize(false),
    249                        serialize(null),
    250                        serialize(array()),
    251                        serialize(1.1),
    252                        serialize(2.1E+200),
    253                        serialize( (object)array('test' => true, '3', 4) )
    254                        );
    255 
     223            serialize(null),
     224            serialize(true),
     225            serialize(false),
     226            serialize(-25),
     227            serialize(25),
     228            serialize(1.1),
     229            serialize(2.1E+200),
     230            serialize('this string will be serialized'),
     231            serialize("a\nb"),
     232            serialize(array()),
     233            serialize(array(1,1,2,3,5,8,13)),
     234            serialize( (object)array('test' => true, '3', 4) )
     235        );
    256236        foreach ( $cases as $case )
    257237            $this->assertTrue( is_serialized($case), "Serialized data: $case" );
    258            
     238
    259239        $not_serialized = array(
    260                                     "a string",
    261                                     "garbage:a:0:garbage;"
    262                                 );
    263                                
     240            'a string',
     241            'garbage:a:0:garbage;',
     242            'b:4;',
     243            's:4:test;'
     244        );
    264245        foreach ( $not_serialized as $case )
    265246            $this->assertFalse( is_serialized($case), "Test data: $case" );
    266247    }
    267    
     248
    268249}
    269250
    270251class TestHTTPFunctions extends WPTestCase {
    271 
    272252    function test_head_request() {
    273253        // this url give a direct 200 response
    274254        $url = 'http://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    275        
    276         $headers = wp_get_http_headers($url);
    277        
    278         $this->assertTrue( is_array($headers) );
     255        $response = wp_remote_head( $url );
     256        $headers = wp_remote_retrieve_headers( $response );
     257
     258        $this->assertTrue( is_array( $headers ) );
    279259        $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    280260        $this->assertEquals( '40148', $headers['content-length'] );
    281         $this->assertEquals( '200', $headers['response'] );
    282     }
    283    
     261        $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
     262    }
     263
    284264    function test_head_redirect() {
    285         // this url will 302 redirect
     265        // this url will 301 redirect
    286266        $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    287        
    288         $headers = wp_get_http_headers($url);
    289        
    290         $this->assertTrue( is_array($headers) );
    291         $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    292         $this->assertEquals( '40148', $headers['content-length'] );
    293         $this->assertEquals( '200', $headers['response'] );
    294     }
    295    
    296     function test_head_redirect_limit_exceeded() {
    297         // this url will 302 redirect
    298         $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    299        
    300         // pretend we've already done 5 redirects
    301         $result = wp_get_http_headers($url, 6);
    302        
    303         $this->assertFalse($result);
    304     }
    305    
     267        $response = wp_remote_head( $url );
     268        $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
     269    }
     270
    306271    function test_head_404() {
    307         $url = 'http://asdftestblog1.wordpress.com/files/2007/09/asdfasdfasdf.jpg';
    308        
    309         $headers = wp_get_http_headers($url);
    310        
    311         $this->assertTrue( is_array($headers) );
    312         $this->assertEquals( '404', $headers['response'] );
    313     }
    314        
     272        $url = 'http://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
     273        $response = wp_remote_head( $url );
     274
     275        $this->assertTrue( is_array($response) );
     276        $this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) );
     277    }
    315278
    316279    function test_get_request() {
    317280        $url = 'http://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    318281        $file = tempnam('/tmp', 'testfile');
    319        
     282
    320283        $headers = wp_get_http($url, $file);
    321        
     284
    322285        // should return the same headers as a head request
    323286        $this->assertTrue( is_array($headers) );
     
    325288        $this->assertEquals( '40148', $headers['content-length'] );
    326289        $this->assertEquals( '200', $headers['response'] );
    327        
     290
    328291        // make sure the file is ok
    329292        $this->assertEquals( 40148, filesize($file) );
     
    335298        $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    336299        $file = tempnam('/tmp', 'testfile');
    337        
     300
    338301        $headers = wp_get_http($url, $file);
    339        
     302
    340303        // should return the same headers as a head request
    341304        $this->assertTrue( is_array($headers) );
     
    343306        $this->assertEquals( '40148', $headers['content-length'] );
    344307        $this->assertEquals( '200', $headers['response'] );
    345        
     308
    346309        // make sure the file is ok
    347310        $this->assertEquals( 40148, filesize($file) );
     
    353316        $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    354317        $file = tempnam('/tmp', 'testfile');
    355        
    356         // pretent we've already redirected 5 times
    357         $headers = wp_get_http($url, $file, 6);
    358         $this->assertFalse($headers);
    359        
    360     }
    361 
    362 
     318        // pretend we've already redirected 5 times
     319        $headers = wp_get_http( $url, $file, 6 );
     320        $this->assertFalse( $headers );
     321    }
    363322}
    364323
Note: See TracChangeset for help on using the changeset viewer.