Make WordPress Core

Ticket #23699: test-functions.diff

File test-functions.diff, 5.4 KB (added by joehoyle, 11 years ago)
  • tests/functions.php

     
    9898                        $this->assertFalse( path_is_absolute($path), "path_is_absolute('$path') should return false" );
    9999        }
    100100
    101 
    102101        function test_wp_unique_filename() {
    103                 /* this test requires:
    104                    - that you have dir + file 'data/images/test-image.png',
    105                    - and that this dir is writeable
    106                    - there is an image 'test-image.png' that will be used to test unique filenames
    107 
    108                    NB: there is a hardcoded dependency that the testing file is '.png'; however,
    109                        this limitation is arbitary, so change it if you like.
    110                 */
     102               
    111103                $testdir = DIR_TESTDATA . '/images/';
    112                 $testimg = 'test-image.png';
    113                 $this->assertTrue( file_exists($testdir) );
    114                 $this->assertTrue( is_writable($testdir) );
    115                 $this->assertTrue( file_exists($testdir . $testimg) );
     104               
     105                // sanity check
     106                $this->assertEquals( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Sanitiy check failed' );
    116107
    117                 $cases = array(
    118                         // null case
    119                         'null' . $testimg,
     108                // check number is appended for file already exists
     109                $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' );
     110                $this->assertEquals( 'test-image2.png', wp_unique_filename( $testdir, 'test-image2.png' ), 'Number not appended correctly' );
     111                $this->assertFileNotExists( $testdir . 'test-image2.png' );
    120112
    121                         // edge cases: '.png', 'abc.', 'abc', 'abc0', 'abc1', 'abc0.png', 'abc1.png' (num @ end)
    122                         '.png',
    123                         'abc',
    124                         'abc.',
    125                         'abc0',
    126                         'abc1',
    127                         'abc0.png',
    128                         'abc1.png',
     113                // check special chars
     114                $this->assertEquals( 'testtést-imagé.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' );
     115               
     116                // check special chars with potential conflicting name
     117                $this->assertEquals( 'tést-imagé.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' );
     118               
     119                // check with single quotes in name (somehow)
     120                $this->assertEquals( "abcdefgh.png", wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' );
    129121
    130                         // replacing # with _
    131                         str_replace('-', '#', $testimg), // test#image.png
    132                         str_replace('-', '##', $testimg), // test##image.png
    133                         str_replace(array('-', 'e'), '#', $testimg), // t#st#imag#.png
    134                         str_replace(array('-', 'e'), '##', $testimg), // t##st##imag##.png
     122                // check with single quotes in name (somehow)
     123                $this->assertEquals( "abcdefgh.png", wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' );
    135124
    136                         // replacing \ or ' with nothing
    137                         str_replace('-', '\\', $testimg), // test\image.png
    138                         str_replace('-', '\\\\', $testimg), // test\\image.png
    139                         str_replace(array('-', 'e'), '\\', $testimg), // t\st\imag\.png
    140                         str_replace(array('-', 'e'), '\\\\', $testimg), // t\\st\\imag\\.png
    141                         str_replace('-', "'", $testimg), // test'image.png
    142                         str_replace('-', "'", $testimg), // test''image.png
    143                         str_replace(array('-', 'e'), "'", $testimg), // t'st'imag'.png
    144                         str_replace(array('-', 'e'), "''", $testimg), // t''st''imag''.png
    145                         str_replace('-', "\'", $testimg), // test\'image.png
    146                         str_replace('-', "\'\'", $testimg), // test\'\'image.png
    147                         str_replace(array('-', 'e'), "\'", $testimg), // t\'st\'imag\'.png
    148                         str_replace(array('-', 'e'), "\'\'", $testimg), // t\'\'st\'\'imag\'\'.png
    149 
    150                         'test' . str_replace('e', 'é', $testimg), // testtést-imagé.png
    151 
    152                         '12%af34567890~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png', // kitchen sink
    153                         $testdir.'test-image-with-path.png',
    154                 );
    155 
    156                 // what we expect the replacements will do
    157                 $expected = array(
    158                                 'null' . $testimg,
    159 
    160                                 'png',
    161                                 'abc',
    162                                 'abc',
    163                                 'abc0',
    164                                 'abc1',
    165                                 'abc0.png',
    166                                 'abc1.png',
    167 
    168                                 'testimage.png',
    169                                 'testimage.png',
    170                                 'tstimag.png',
    171                                 'tstimag.png',
    172 
    173                                 'testimage.png',
    174                                 'testimage.png',
    175                                 'tstimag.png',
    176                                 'tstimag.png',
    177                                 'testimage.png',
    178                                 'testimage.png',
    179                                 'tstimag.png',
    180                                 'tstimag.png',
    181                                 'testimage.png',
    182                                 'testimage.png',
    183                                 'tstimag.png',
    184                                 'tstimag.png',
    185 
    186                                 'testtést-imagé.png',
    187 
    188                                 '12%af34567890@..%^_+qwerty-fghjkl-zx.png',
    189                                 str_replace( array( '\\', '/', ':' ), '', $testdir ).'test-image-with-path.png',
    190                         );
    191 
    192                 foreach ($cases as $key => $case) {
    193                         // make sure expected file doesn't exist already
    194                         // happens when tests fail and the unlinking doesn't happen
    195                         if( $expected[$key] !== $testimg && file_exists($testdir . $expected[$key]) )
    196                                 unlink($testdir . $expected[$key]);
    197 
    198                         // -- TEST 1: the replacement is as expected
    199                         $this->assertEquals( $expected[$key], wp_unique_filename($testdir, $case, NULL), $case );
    200                         // -- end TEST 1
    201 
    202                         // -- TEST 2: the renaming will produce a unique name
    203                         // create the expected file
    204                         copy($testdir . $testimg, $testdir . $expected[$key]);
    205                         // test that wp_unique_filename actually returns a unique filename
    206                         $this->assertFileNotExists( $testdir . wp_unique_filename($testdir, $case, NULL) );
    207                         // -- end TEST 2
    208 
    209                         // cleanup
    210                         if( $expected[$key] !== $testimg &&  file_exists($testdir . $expected[$key]) )
    211                                 unlink($testdir . $expected[$key]);
    212                 }
     125                // test crazy name (useful for regression tests)
     126                $this->assertEquals( '12%af34567890@..%^_+qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' );
     127               
    213128        }
    214129
    215130        /**