Changeset 328 in tests
- Timestamp:
- 01/20/2011 02:54:26 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wp-testcase/test_includes_functions.php
r279 r328 2 2 3 3 class TestFunctions extends WPTestCase { 4 var $cache = NULL;5 6 4 function setUp() { 7 5 parent::setUp(); … … 47 45 } 48 46 function test_size_format() { 49 $this->knownWPBug(5246);50 47 $kb = 1024; 51 48 $mb = $kb*1024; … … 66 63 $this->assertFalse(size_format(array())); 67 64 } 68 65 69 66 function test_path_is_absolute() { 70 67 if ( !is_callable('path_is_absolute') ) 71 68 $this->markTestSkipped(); 72 69 73 70 $absolute_paths = array( 74 71 '/', … … 109 106 110 107 function test_wp_unique_filename() { 111 $this->knownWPBug(6294);112 113 108 /* 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', 115 110 - and that this dir is writeable 116 111 - there is an image 'test-image.png' that will be used to test unique filenames 117 112 118 113 NB: there is a hardcoded dependency that the testing file is '.png'; however, 119 114 this limitation is arbitary, so change it if you like. … … 124 119 $this->assertTrue( is_writable($testdir) ); 125 120 $this->assertTrue( file_exists($testdir . $testimg) ); 126 121 127 122 $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 171 161 // what we expect the replacements will do 172 162 $expected = array( 173 163 'null' . $testimg, 174 175 ' .png',164 165 'png', 176 166 'abc', 177 167 'abc', … … 180 170 'abc0.png', 181 171 '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 207 195 ); 208 209 foreach ($cases as $key => $case) 210 { 196 197 foreach ($cases as $key => $case) { 211 198 // make sure expected file doesn't exist already 212 199 // happens when tests fail and the unlinking doesn't happen … … 215 202 216 203 // -- TEST 1: the replacement is as expected 217 218 204 $this->assertEquals( $expected[$key], wp_unique_filename($testdir, $case, NULL), $case ); 219 220 205 // -- 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 226 208 // create the expected file 227 209 copy($testdir . $testimg, $testdir . $expected[$key]); 228 229 210 // test that wp_unique_filename actually returns a unique filename 230 211 $this->assertFileNotExists( $testdir . wp_unique_filename($testdir, $case, NULL) ); 231 232 212 // -- end TEST 2 233 234 235 213 236 214 // cleanup 237 215 if( $expected[$key] !== $testimg && file_exists($testdir . $expected[$key]) ) … … 239 217 } 240 218 } 241 219 242 220 function test_is_serialized() { 243 221 $this->knownWPBug(9930); 244 222 $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 ); 256 236 foreach ( $cases as $case ) 257 237 $this->assertTrue( is_serialized($case), "Serialized data: $case" ); 258 238 259 239 $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 ); 264 245 foreach ( $not_serialized as $case ) 265 246 $this->assertFalse( is_serialized($case), "Test data: $case" ); 266 247 } 267 248 268 249 } 269 250 270 251 class TestHTTPFunctions extends WPTestCase { 271 272 252 function test_head_request() { 273 253 // this url give a direct 200 response 274 254 $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 ) ); 279 259 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 280 260 $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 284 264 function test_head_redirect() { 285 // this url will 30 2redirect265 // this url will 301 redirect 286 266 $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 306 271 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 } 315 278 316 279 function test_get_request() { 317 280 $url = 'http://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 318 281 $file = tempnam('/tmp', 'testfile'); 319 282 320 283 $headers = wp_get_http($url, $file); 321 284 322 285 // should return the same headers as a head request 323 286 $this->assertTrue( is_array($headers) ); … … 325 288 $this->assertEquals( '40148', $headers['content-length'] ); 326 289 $this->assertEquals( '200', $headers['response'] ); 327 290 328 291 // make sure the file is ok 329 292 $this->assertEquals( 40148, filesize($file) ); … … 335 298 $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 336 299 $file = tempnam('/tmp', 'testfile'); 337 300 338 301 $headers = wp_get_http($url, $file); 339 302 340 303 // should return the same headers as a head request 341 304 $this->assertTrue( is_array($headers) ); … … 343 306 $this->assertEquals( '40148', $headers['content-length'] ); 344 307 $this->assertEquals( '200', $headers['response'] ); 345 308 346 309 // make sure the file is ok 347 310 $this->assertEquals( 40148, filesize($file) ); … … 353 316 $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 354 317 $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 } 363 322 } 364 323
Note: See TracChangeset
for help on using the changeset viewer.