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' ); |
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' ); |
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 | |