Changeset 54861 for trunk/tests/phpunit/tests/functions/wpFilesize.php
- Timestamp:
- 11/22/2022 03:02:26 PM (2 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/functions/wpFilesize.php
r54859 r54861 2 2 3 3 /** 4 * Tests for the wp_filesize() function. 5 * 4 6 * @group functions.php 7 * @covers ::wp_filesize 5 8 */ 6 class Tests_Functions extends WP_UnitTestCase { 7 public function test_wp_parse_args_object() { 8 $x = new MockClass; 9 $x->_baba = 5; 10 $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase 11 $x->a = array( 5, 111, 'x' ); 12 $this->assertSame( 13 array( 14 '_baba' => 5, 15 'yZ' => 'baba', 16 'a' => array( 5, 111, 'x' ), 17 ), 18 wp_parse_args( $x ) 19 ); 20 $y = new MockClass; 21 $this->assertSame( array(), wp_parse_args( $y ) ); 22 } 23 24 public function test_wp_parse_args_array() { 25 // Arrays. 26 $a = array(); 27 $this->assertSame( array(), wp_parse_args( $a ) ); 28 $b = array( 29 '_baba' => 5, 30 'yZ' => 'baba', 31 'a' => array( 5, 111, 'x' ), 32 ); 33 $this->assertSame( 34 array( 35 '_baba' => 5, 36 'yZ' => 'baba', 37 'a' => array( 5, 111, 'x' ), 38 ), 39 wp_parse_args( $b ) 40 ); 41 } 42 43 public function test_wp_parse_args_defaults() { 44 $x = new MockClass; 45 $x->_baba = 5; 46 $x->yZ = 'baba'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase 47 $x->a = array( 5, 111, 'x' ); 48 $d = array( 'pu' => 'bu' ); 49 $this->assertSame( 50 array( 51 'pu' => 'bu', 52 '_baba' => 5, 53 'yZ' => 'baba', 54 'a' => array( 5, 111, 'x' ), 55 ), 56 wp_parse_args( $x, $d ) 57 ); 58 $e = array( '_baba' => 6 ); 59 $this->assertSame( 60 array( 61 '_baba' => 5, 62 'yZ' => 'baba', 63 'a' => array( 5, 111, 'x' ), 64 ), 65 wp_parse_args( $x, $e ) 66 ); 67 } 68 69 public function test_wp_parse_args_other() { 70 $b = true; 71 wp_parse_str( $b, $s ); 72 $this->assertSame( $s, wp_parse_args( $b ) ); 73 $q = 'x=5&_baba=dudu&'; 74 wp_parse_str( $q, $ss ); 75 $this->assertSame( $ss, wp_parse_args( $q ) ); 76 } 77 78 /** 79 * @ticket 30753 80 */ 81 public function test_wp_parse_args_boolean_strings() { 82 $args = wp_parse_args( 'foo=false&bar=true' ); 83 $this->assertIsString( $args['foo'] ); 84 $this->assertIsString( $args['bar'] ); 85 } 86 87 /** 88 * @ticket 35972 89 */ 90 public function test_bool_from_yn() { 91 $this->assertTrue( bool_from_yn( 'Y' ) ); 92 $this->assertTrue( bool_from_yn( 'y' ) ); 93 $this->assertFalse( bool_from_yn( 'n' ) ); 94 } 95 96 public function test_path_is_absolute() { 97 $absolute_paths = array( 98 '/', 99 '/foo/', 100 '/foo', 101 '/FOO/bar', 102 '/foo/bar/', 103 '/foo/../bar/', 104 '\\WINDOWS', 105 'C:\\', 106 'C:\\WINDOWS', 107 '\\\\sambashare\\foo', 108 ); 109 foreach ( $absolute_paths as $path ) { 110 $this->assertTrue( path_is_absolute( $path ), "path_is_absolute('$path') should return true" ); 111 } 112 } 113 114 public function test_path_is_not_absolute() { 115 $relative_paths = array( 116 '', 117 '.', 118 '..', 119 '../foo', 120 '../', 121 '../foo.bar', 122 'foo/bar', 123 'foo', 124 'FOO', 125 '..\\WINDOWS', 126 ); 127 foreach ( $relative_paths as $path ) { 128 $this->assertFalse( path_is_absolute( $path ), "path_is_absolute('$path') should return false" ); 129 } 130 } 131 132 /** 133 * Tests path_join(). 134 * 135 * @ticket 55897 136 * @dataProvider path_join_data_provider 137 */ 138 public function test_path_join( $base, $path, $expected ) { 139 $this->assertSame( $expected, path_join( $base, $path ) ); 140 } 141 142 /** 143 * Data provider for test_path_join(). 144 * 145 * @return string[][] 146 */ 147 public function path_join_data_provider() { 148 return array( 149 // Absolute paths. 150 'absolute path should return path' => array( 151 'base' => 'base', 152 'path' => '/path', 153 'expected' => '/path', 154 ), 155 'windows path with slashes' => array( 156 'base' => 'base', 157 'path' => '//path', 158 'expected' => '//path', 159 ), 160 'windows path with backslashes' => array( 161 'base' => 'base', 162 'path' => '\\\\path', 163 'expected' => '\\\\path', 164 ), 165 // Non-absolute paths. 166 'join base and path' => array( 167 'base' => 'base', 168 'path' => 'path', 169 'expected' => 'base/path', 170 ), 171 'strip trailing slashes in base' => array( 172 'base' => 'base///', 173 'path' => 'path', 174 'expected' => 'base/path', 175 ), 176 'empty path' => array( 177 'base' => 'base', 178 'path' => '', 179 'expected' => 'base/', 180 ), 181 'empty base' => array( 182 'base' => '', 183 'path' => 'path', 184 'expected' => '/path', 185 ), 186 'empty path and base' => array( 187 'base' => '', 188 'path' => '', 189 'expected' => '/', 190 ), 191 ); 192 } 193 194 /** 195 * @ticket 33265 196 * @ticket 35996 197 * 198 * @dataProvider data_wp_normalize_path 199 */ 200 public function test_wp_normalize_path( $path, $expected ) { 201 $this->assertSame( $expected, wp_normalize_path( $path ) ); 202 } 203 204 public function data_wp_normalize_path() { 205 return array( 206 // Windows paths. 207 array( 'C:\\www\\path\\', 'C:/www/path/' ), 208 array( 'C:\\www\\\\path\\', 'C:/www/path/' ), 209 array( 'c:/www/path', 'C:/www/path' ), 210 array( 'c:\\www\\path\\', 'C:/www/path/' ), // Uppercase drive letter. 211 array( 'c:\\\\www\\path\\', 'C:/www/path/' ), 212 array( '\\\\Domain\\DFSRoots\\share\\path\\', '//Domain/DFSRoots/share/path/' ), 213 array( '\\\\Server\\share\\path', '//Server/share/path' ), 214 array( '\\\\Server\\share', '//Server/share' ), 215 216 // Linux paths. 217 array( '/www/path/', '/www/path/' ), 218 array( '/www/path/////', '/www/path/' ), 219 array( '/www/path', '/www/path' ), 220 221 // PHP stream wrappers. 222 array( 'php://input', 'php://input' ), 223 array( 'http://example.com//path.ext', 'http://example.com/path.ext' ), 224 array( 'file://c:\\www\\path\\', 'file://C:/www/path/' ), 225 ); 226 } 227 228 public function test_wp_unique_filename() { 229 230 $testdir = DIR_TESTDATA . '/images/'; 231 232 // Sanity check. 233 $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcdefg.png' ), 'Test non-existing file, file name should be unchanged.' ); 234 235 // Ensure correct images exist. 236 $this->assertFileExists( $testdir . 'test-image.png', 'Test image does not exist' ); 237 $this->assertFileDoesNotExist( $testdir . 'test-image-1.png' ); 238 239 // Check number is appended if file already exists. 240 $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.png' ), 'File name not unique, number not appended.' ); 241 242 // Check file with uppercase extension. 243 $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'test-image.PNG' ), 'File name with uppercase extension not unique, number not appended.' ); 244 245 // Check file name with already added number. 246 $this->assertSame( 'test-image-2-1.gif', wp_unique_filename( $testdir, 'test-image-2.gif' ), 'File name not unique, number not appended correctly.' ); 247 248 // Check special chars. 249 $this->assertSame( 'testtest-image.png', wp_unique_filename( $testdir, 'testtést-imagé.png' ), 'Filename with special chars failed' ); 250 251 // Check special chars with potential conflicting name. 252 $this->assertSame( 'test-image-1.png', wp_unique_filename( $testdir, 'tést-imagé.png' ), 'Filename with special chars failed' ); 253 254 // Check with single quotes in name (somehow). 255 $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, "abcdefg'h.png" ), 'File with quote failed' ); 256 257 // Check with double quotes in name (somehow). 258 $this->assertSame( 'abcdefgh.png', wp_unique_filename( $testdir, 'abcdefg"h.png' ), 'File with quote failed' ); 259 260 // Test crazy name (useful for regression tests). 261 $this->assertSame( '12af34567890@..^_qwerty-fghjkl-zx.png', wp_unique_filename( $testdir, '12%af34567890#~!@#$..%^&*()|_+qwerty fgh`jkl zx<>?:"{}[]="\'/?.png' ), 'Failed crazy file name' ); 262 263 // Test slashes in names. 264 $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\fg.png' ), 'Slash not removed' ); 265 $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\fg.png' ), 'Double slashed not removed' ); 266 $this->assertSame( 'abcdefg.png', wp_unique_filename( $testdir, 'abcde\\\fg.png' ), 'Tripple slashed not removed' ); 267 } 268 269 /** 270 * @ticket 42437 271 */ 272 public function test_unique_filename_with_dimension_like_filename() { 273 $testdir = DIR_TESTDATA . '/images/'; 274 275 add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 276 277 // Test collision with "dimension-like" original filename. 278 $this->assertSame( 'one-blue-pixel-100x100-1.png', wp_unique_filename( $testdir, 'one-blue-pixel-100x100.png' ) ); 279 // Test collision with existing sub-size filename. 280 // Existing files: one-blue-pixel-100x100.png, one-blue-pixel-1-100x100.png. 281 $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.png' ) ); 282 // Same as above with upper case extension. 283 $this->assertSame( 'one-blue-pixel-2.png', wp_unique_filename( $testdir, 'one-blue-pixel.PNG' ) ); 284 285 remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 286 } 287 288 // Callback to patch "basedir" when used in `wp_unique_filename()`. 289 public function upload_dir_patch_basedir( $upload_dir ) { 290 $upload_dir['basedir'] = DIR_TESTDATA . '/images/'; 291 return $upload_dir; 292 } 293 294 /** 295 * @ticket 53668 296 */ 297 public function test_wp_unique_filename_with_additional_image_extension() { 298 $testdir = DIR_TESTDATA . '/images/'; 299 300 add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 301 302 // Set conversions for uploaded images. 303 add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); 304 305 // Ensure the test images exist. 306 $this->assertFileExists( $testdir . 'test-image-1-100x100.jpg', 'test-image-1-100x100.jpg does not exist' ); 307 $this->assertFileExists( $testdir . 'test-image-2.gif', 'test-image-2.gif does not exist' ); 308 $this->assertFileExists( $testdir . 'test-image-3.jpg', 'test-image-3.jpg does not exist' ); 309 $this->assertFileExists( $testdir . 'test-image-4.png', 'test-image-4.png does not exist' ); 310 311 // Standard test: file does not exist and there are no possible intersections with other files. 312 $this->assertSame( 313 'abcdef.png', 314 wp_unique_filename( $testdir, 'abcdef.png' ), 315 'The abcdef.png, abcdef.gif, and abcdef.jpg images do not exist. The file name should not be changed.' 316 ); 317 318 // Actual clash recognized. 319 $this->assertSame( 320 'canola-1.jpg', 321 wp_unique_filename( $testdir, 'canola.jpg' ), 322 'The canola.jpg image exists. The file name should be unique.' 323 ); 324 325 // Same name with different extension and the image will be converted. 326 $this->assertSame( 327 'canola-1.png', 328 wp_unique_filename( $testdir, 'canola.png' ), 329 'The canola.jpg image exists. Uploading canola.png that will be converted to canola.jpg should produce unique file name.' 330 ); 331 332 // Same name with different uppercase extension and the image will be converted. 333 $this->assertSame( 334 'canola-1.png', 335 wp_unique_filename( $testdir, 'canola.PNG' ), 336 'The canola.jpg image exists. Uploading canola.PNG that will be converted to canola.jpg should produce unique file name.' 337 ); 338 339 // Actual clash with several images with different extensions. 340 $this->assertSame( 341 'test-image-5.png', 342 wp_unique_filename( $testdir, 'test-image.png' ), 343 'The test-image.png, test-image-1-100x100.jpg, test-image-2.gif, test-image-3.jpg, and test-image-4.png images exist.' . 344 'All of them may clash when creating sub-sizes or regenerating thumbnails in the future. The filename should be unique.' 345 ); 346 347 // Possible clash with regenerated thumbnails in the future. 348 $this->assertSame( 349 'codeispoetry-1.jpg', 350 wp_unique_filename( $testdir, 'codeispoetry.jpg' ), 351 'The codeispoetry.png image exists. When regenerating thumbnails for it they will be converted to JPG.' . 352 'The name of the newly uploaded codeispoetry.jpg should be made unique.' 353 ); 354 355 remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); 356 remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); 357 } 358 359 /** 360 * Changes the output format when editing images. When uploading a PNG file 361 * it will be converted to JPEG, GIF to JPEG, and PICT to BMP 362 * (if the image editor in PHP supports it). 363 * 364 * @param array $formats 365 * 366 * @return array 367 */ 368 public function image_editor_output_format_handler( $formats ) { 369 $formats['image/png'] = 'image/jpeg'; 370 $formats['image/gif'] = 'image/jpeg'; 371 $formats['image/pct'] = 'image/bmp'; 372 373 return $formats; 374 } 375 376 /** 377 * @group add_query_arg 378 */ 379 public function test_add_query_arg() { 380 $old_req_uri = $_SERVER['REQUEST_URI']; 381 382 $urls = array( 383 '/', 384 '/2012/07/30/', 385 'edit.php', 386 admin_url( 'edit.php' ), 387 admin_url( 'edit.php', 'https' ), 388 ); 389 390 $frag_urls = array( 391 '/#frag', 392 '/2012/07/30/#frag', 393 'edit.php#frag', 394 admin_url( 'edit.php#frag' ), 395 admin_url( 'edit.php#frag', 'https' ), 396 ); 397 398 foreach ( $urls as $url ) { 399 $_SERVER['REQUEST_URI'] = 'nothing'; 400 401 $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1', $url ) ); 402 $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); 403 $this->assertSame( 404 "$url?foo=2", 405 add_query_arg( 406 array( 407 'foo' => '1', 408 'foo' => '2', 409 ), 410 $url 411 ) 412 ); 413 $this->assertSame( 414 "$url?foo=1&bar=2", 415 add_query_arg( 416 array( 417 'foo' => '1', 418 'bar' => '2', 419 ), 420 $url 421 ) 422 ); 423 424 $_SERVER['REQUEST_URI'] = $url; 425 426 $this->assertSame( "$url?foo=1", add_query_arg( 'foo', '1' ) ); 427 $this->assertSame( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) ); 428 $this->assertSame( 429 "$url?foo=2", 430 add_query_arg( 431 array( 432 'foo' => '1', 433 'foo' => '2', 434 ) 435 ) 436 ); 437 $this->assertSame( 438 "$url?foo=1&bar=2", 439 add_query_arg( 440 array( 441 'foo' => '1', 442 'bar' => '2', 443 ) 444 ) 445 ); 446 } 447 448 foreach ( $frag_urls as $frag_url ) { 449 $_SERVER['REQUEST_URI'] = 'nothing'; 450 $url = str_replace( '#frag', '', $frag_url ); 451 452 $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) ); 453 $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) ); 454 $this->assertSame( 455 "$url?foo=2#frag", 456 add_query_arg( 457 array( 458 'foo' => '1', 459 'foo' => '2', 460 ), 461 $frag_url 462 ) 463 ); 464 $this->assertSame( 465 "$url?foo=1&bar=2#frag", 466 add_query_arg( 467 array( 468 'foo' => '1', 469 'bar' => '2', 470 ), 471 $frag_url 472 ) 473 ); 474 475 $_SERVER['REQUEST_URI'] = $frag_url; 476 477 $this->assertSame( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) ); 478 $this->assertSame( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) ); 479 $this->assertSame( 480 "$url?foo=2#frag", 481 add_query_arg( 482 array( 483 'foo' => '1', 484 'foo' => '2', 485 ) 486 ) 487 ); 488 $this->assertSame( 489 "$url?foo=1&bar=2#frag", 490 add_query_arg( 491 array( 492 'foo' => '1', 493 'bar' => '2', 494 ) 495 ) 496 ); 497 } 498 499 $qs_urls = array( 500 'baz=1', // #WP4903 501 '/?baz', 502 '/2012/07/30/?baz', 503 'edit.php?baz', 504 admin_url( 'edit.php?baz' ), 505 admin_url( 'edit.php?baz', 'https' ), 506 admin_url( 'edit.php?baz&za=1' ), 507 admin_url( 'edit.php?baz=1&za=1' ), 508 admin_url( 'edit.php?baz=0&za=0' ), 509 ); 510 511 foreach ( $qs_urls as $url ) { 512 $_SERVER['REQUEST_URI'] = 'nothing'; 513 514 $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1', $url ) ); 515 $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ), $url ) ); 516 $this->assertSame( 517 "$url&foo=2", 518 add_query_arg( 519 array( 520 'foo' => '1', 521 'foo' => '2', 522 ), 523 $url 524 ) 525 ); 526 $this->assertSame( 527 "$url&foo=1&bar=2", 528 add_query_arg( 529 array( 530 'foo' => '1', 531 'bar' => '2', 532 ), 533 $url 534 ) 535 ); 536 537 $_SERVER['REQUEST_URI'] = $url; 538 539 $this->assertSame( "$url&foo=1", add_query_arg( 'foo', '1' ) ); 540 $this->assertSame( "$url&foo=1", add_query_arg( array( 'foo' => '1' ) ) ); 541 $this->assertSame( 542 "$url&foo=2", 543 add_query_arg( 544 array( 545 'foo' => '1', 546 'foo' => '2', 547 ) 548 ) 549 ); 550 $this->assertSame( 551 "$url&foo=1&bar=2", 552 add_query_arg( 553 array( 554 'foo' => '1', 555 'bar' => '2', 556 ) 557 ) 558 ); 559 } 560 561 $_SERVER['REQUEST_URI'] = $old_req_uri; 562 } 563 564 /** 565 * @ticket 31306 566 */ 567 public function test_add_query_arg_numeric_keys() { 568 $url = add_query_arg( array( 'foo' => 'bar' ), '1=1' ); 569 $this->assertSame( '1=1&foo=bar', $url ); 570 571 $url = add_query_arg( 572 array( 573 'foo' => 'bar', 574 '1' => '2', 575 ), 576 '1=1' 577 ); 578 $this->assertSame( '1=2&foo=bar', $url ); 579 580 $url = add_query_arg( array( '1' => '2' ), 'foo=bar' ); 581 $this->assertSame( 'foo=bar&1=2', $url ); 582 } 583 584 /** 585 * Tests that add_query_arg removes the question mark when 586 * a parameter is set to false. 587 * 588 * @dataProvider data_add_query_arg_removes_question_mark 589 * 590 * @ticket 44499 591 * @group add_query_arg 592 * 593 * @covers ::add_query_arg 594 * 595 * @param string $url Url to test. 596 * @param string $expected Expected URL. 597 */ 598 public function test_add_query_arg_removes_question_mark( $url, $expected, $key = 'param', $value = false ) { 599 $this->assertSame( $expected, add_query_arg( $key, $value, $url ) ); 600 } 601 602 /** 603 * Data provider. 604 * 605 * @return array 606 */ 607 public function data_add_query_arg_removes_question_mark() { 608 return array( 609 'anchor' => array( 610 'url' => 'http://example.org?#anchor', 611 'expected' => 'http://example.org#anchor', 612 ), 613 '/ then anchor' => array( 614 'url' => 'http://example.org/?#anchor', 615 'expected' => 'http://example.org/#anchor', 616 ), 617 'invalid query param and anchor' => array( 618 'url' => 'http://example.org?param=value#anchor', 619 'expected' => 'http://example.org#anchor', 620 ), 621 '/ then invalid query param and anchor' => array( 622 'url' => 'http://example.org/?param=value#anchor', 623 'expected' => 'http://example.org/#anchor', 624 ), 625 '?#anchor when adding valid key/value args' => array( 626 'url' => 'http://example.org?#anchor', 627 'expected' => 'http://example.org?foo=bar#anchor', 628 'key' => 'foo', 629 'value' => 'bar', 630 ), 631 '/?#anchor when adding valid key/value args' => array( 632 'url' => 'http://example.org/?#anchor', 633 'expected' => 'http://example.org/?foo=bar#anchor', 634 'key' => 'foo', 635 'value' => 'bar', 636 ), 637 ); 638 } 639 640 /** 641 * @ticket 21594 642 */ 643 public function test_get_allowed_mime_types() { 644 $mimes = get_allowed_mime_types(); 645 646 $this->assertIsArray( $mimes ); 647 $this->assertNotEmpty( $mimes ); 648 649 add_filter( 'upload_mimes', '__return_empty_array' ); 650 $mimes = get_allowed_mime_types(); 651 $this->assertIsArray( $mimes ); 652 $this->assertEmpty( $mimes ); 653 654 remove_filter( 'upload_mimes', '__return_empty_array' ); 655 $mimes = get_allowed_mime_types(); 656 $this->assertIsArray( $mimes ); 657 $this->assertNotEmpty( $mimes ); 658 } 659 660 /** 661 * @ticket 21594 662 */ 663 public function test_wp_get_mime_types() { 664 $mimes = wp_get_mime_types(); 665 666 $this->assertIsArray( $mimes ); 667 $this->assertNotEmpty( $mimes ); 668 669 add_filter( 'mime_types', '__return_empty_array' ); 670 $mimes = wp_get_mime_types(); 671 $this->assertIsArray( $mimes ); 672 $this->assertEmpty( $mimes ); 673 674 remove_filter( 'mime_types', '__return_empty_array' ); 675 $mimes = wp_get_mime_types(); 676 $this->assertIsArray( $mimes ); 677 $this->assertNotEmpty( $mimes ); 678 679 // 'upload_mimes' should not affect wp_get_mime_types(). 680 add_filter( 'upload_mimes', '__return_empty_array' ); 681 $mimes = wp_get_mime_types(); 682 $this->assertIsArray( $mimes ); 683 $this->assertNotEmpty( $mimes ); 684 685 remove_filter( 'upload_mimes', '__return_empty_array' ); 686 $mimes2 = wp_get_mime_types(); 687 $this->assertIsArray( $mimes2 ); 688 $this->assertNotEmpty( $mimes2 ); 689 $this->assertSame( $mimes2, $mimes ); 690 } 691 692 /** 693 * @ticket 23688 694 */ 695 public function test_canonical_charset() { 696 $orig_blog_charset = get_option( 'blog_charset' ); 697 698 update_option( 'blog_charset', 'utf8' ); 699 $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); 700 701 update_option( 'blog_charset', 'utf-8' ); 702 $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); 703 704 update_option( 'blog_charset', 'UTF8' ); 705 $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); 706 707 update_option( 'blog_charset', 'UTF-8' ); 708 $this->assertSame( 'UTF-8', get_option( 'blog_charset' ) ); 709 710 update_option( 'blog_charset', 'ISO-8859-1' ); 711 $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); 712 713 update_option( 'blog_charset', 'ISO8859-1' ); 714 $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); 715 716 update_option( 'blog_charset', 'iso8859-1' ); 717 $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); 718 719 update_option( 'blog_charset', 'iso-8859-1' ); 720 $this->assertSame( 'ISO-8859-1', get_option( 'blog_charset' ) ); 721 722 // Arbitrary strings are passed through. 723 update_option( 'blog_charset', 'foobarbaz' ); 724 $this->assertSame( 'foobarbaz', get_option( 'blog_charset' ) ); 725 726 update_option( 'blog_charset', $orig_blog_charset ); 727 } 728 729 /** 730 * @ticket 43977 731 * @dataProvider data_wp_parse_list 732 */ 733 public function test_wp_parse_list( $expected, $actual ) { 734 $this->assertSame( $expected, array_values( wp_parse_list( $actual ) ) ); 735 } 736 737 public function data_wp_parse_list() { 738 return array( 739 array( array( '1', '2', '3', '4' ), '1,2,3,4' ), 740 array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ), 741 array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana' ), 742 array( array( '1', '2', 'apple', 'banana' ), '1, 2,apple,banana' ), 743 array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,,banana' ), 744 array( array( '1', '2', 'apple', 'banana' ), ',1,2,apple,banana' ), 745 array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana,' ), 746 array( array( '1', '2', 'apple', 'banana' ), '1,2 ,apple,banana' ), 747 array( array(), '' ), 748 array( array(), ',' ), 749 array( array(), ',,' ), 750 ); 751 } 752 753 /** 754 * @dataProvider data_wp_parse_id_list 755 */ 756 public function test_wp_parse_id_list( $expected, $actual ) { 757 $this->assertSame( $expected, array_values( wp_parse_id_list( $actual ) ) ); 758 } 759 760 public function data_wp_parse_id_list() { 761 return array( 762 array( array( 1, 2, 3, 4 ), '1,2,3,4' ), 763 array( array( 1, 2, 3, 4 ), '1, 2,,3,4' ), 764 array( array( 1, 2, 3, 4 ), '1,2,2,3,4' ), 765 array( array( 1, 2, 3, 4 ), array( '1', '2', '3', '4', '3' ) ), 766 array( array( 1, 2, 3, 4 ), array( 1, '2', 3, '4' ) ), 767 array( array( 1, 2, 3, 4 ), '-1,2,-3,4' ), 768 array( array( 1, 2, 3, 4 ), array( -1, 2, '-3', '4' ) ), 769 ); 770 } 771 772 /** 773 * @dataProvider data_wp_parse_slug_list 774 */ 775 public function test_wp_parse_slug_list( $expected, $actual ) { 776 $this->assertSame( $expected, array_values( wp_parse_slug_list( $actual ) ) ); 777 } 778 779 public function data_wp_parse_slug_list() { 780 return array( 781 array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ), 782 array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple, banana,,carrot,dog' ), 783 array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple banana carrot dog' ), 784 array( array( 'apple', 'banana-carrot', 'd-o-g' ), array( 'apple ', 'banana carrot', 'd o g' ) ), 785 ); 786 } 787 788 /** 789 * @dataProvider data_device_can_upload 790 */ 791 public function test_device_can_upload( $user_agent, $expected ) { 792 $_SERVER['HTTP_USER_AGENT'] = $user_agent; 793 $actual = _device_can_upload(); 794 unset( $_SERVER['HTTP_USER_AGENT'] ); 795 $this->assertSame( $expected, $actual ); 796 } 797 798 public function data_device_can_upload() { 799 return array( 800 // iPhone iOS 5.0.1, Safari 5.1. 801 array( 802 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A406)', 803 false, 804 ), 805 // iPad iOS 3.2, Safari 4.0.4. 806 array( 807 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10', 808 false, 809 ), 810 // iPod iOS 4.3.3, Safari 5.0.2. 811 array( 812 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5', 813 false, 814 ), 815 // iPhone iOS 6.0.0, Safari 6.0. 816 array( 817 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25', 818 true, 819 ), 820 // iPad iOS 6.0.0, Safari 6.0. 821 array( 822 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25', 823 true, 824 ), 825 // Android 2.2, Android Webkit Browser. 826 array( 827 'Mozilla/5.0 (Android 2.2; Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4', 828 true, 829 ), 830 // BlackBerry 9900, BlackBerry browser. 831 array( 832 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+', 833 true, 834 ), 835 // Windows Phone 8.0, Internet Explorer 10.0. 836 array( 837 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)', 838 true, 839 ), 840 // Ubuntu desktop, Firefox 41.0. 841 array( 842 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0', 843 true, 844 ), 845 ); 846 } 847 848 /** 849 * @ticket 9064 850 */ 851 public function test_wp_extract_urls() { 852 $original_urls = array( 853 'http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html', 854 'http://this.com', 855 'http://127.0.0.1', 856 'http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437', 857 'http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html', 858 'http://wordpress-core.com:8080/', 859 'http://www.website.com:5000', 860 'http://wordpress-core/?346236346326&2134362574863.437', 861 'http://افغانستا.icom.museum', 862 'http://الجزائر.icom.museum', 863 'http://österreich.icom.museum', 864 'http://বাংলাদেশ.icom.museum', 865 'http://беларусь.icom.museum', 866 'http://belgië.icom.museum', 867 'http://българия.icom.museum', 868 'http://تشادر.icom.museum', 869 'http://中国.icom.museum', 870 // 'http://القمر.icom.museum', // Comoros http://القمر.icom.museum 871 // 'http://κυπρος.icom.museum', // Cyprus http://κυπρος.icom.museum 872 'http://českárepublika.icom.museum', 873 // 'http://مصر.icom.museum', // Egypt http://مصر.icom.museum 874 'http://ελλάδα.icom.museum', 875 'http://magyarország.icom.museum', 876 'http://ísland.icom.museum', 877 'http://भारत.icom.museum', 878 'http://ايران.icom.museum', 879 'http://éire.icom.museum', 880 'http://איקו״ם.ישראל.museum', 881 'http://日本.icom.museum', 882 'http://الأردن.icom.museum', 883 'http://қазақстан.icom.museum', 884 'http://한국.icom.museum', 885 'http://кыргызстан.icom.museum', 886 'http://ລາວ.icom.museum', 887 'http://لبنان.icom.museum', 888 'http://македонија.icom.museum', 889 // 'http://موريتانيا.icom.museum', // Mauritania http://موريتانيا.icom.museum 890 'http://méxico.icom.museum', 891 'http://монголулс.icom.museum', 892 // 'http://المغرب.icom.museum', // Morocco http://المغرب.icom.museum 893 'http://नेपाल.icom.museum', 894 // 'http://عمان.icom.museum', // Oman http://عمان.icom.museum 895 'http://قطر.icom.museum', 896 'http://românia.icom.museum', 897 'http://россия.иком.museum', 898 'http://србијаицрнагора.иком.museum', 899 'http://இலங்கை.icom.museum', 900 'http://españa.icom.museum', 901 'http://ไทย.icom.museum', 902 'http://تونس.icom.museum', 903 'http://türkiye.icom.museum', 904 'http://украина.icom.museum', 905 'http://việtnam.icom.museum', 906 'ftp://127.0.0.1/', 907 'http://www.woo.com/video?v=exvUH2qKLTU', 908 'http://taco.com?burrito=enchilada#guac', 909 'http://example.org/?post_type=post&p=4', 910 'http://example.org/?post_type=post&p=5', 911 'http://example.org/?post_type=post&p=6', 912 'http://typo-in-query.org/?foo=bar&baz=missing_semicolon', 913 ); 914 915 $blob = ' 916 http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html 917 918 http://this.com 919 920 http://127.0.0.1 921 922 http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437 923 924 http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html 925 926 http://wordpress-core.com:8080/ 927 928 http://www.website.com:5000 929 930 http://wordpress-core/?346236346326&2134362574863.437 931 932 http://افغانستا.icom.museum 933 http://الجزائر.icom.museum 934 http://österreich.icom.museum 935 http://বাংলাদেশ.icom.museum 936 http://беларусь.icom.museum 937 http://belgië.icom.museum 938 http://българия.icom.museum 939 http://تشادر.icom.museum 940 http://中国.icom.museum 941 http://českárepublika.icom.museum 942 http://ελλάδα.icom.museum 943 http://magyarország.icom.museum 944 http://ísland.icom.museum 945 http://भारत.icom.museum 946 http://ايران.icom.museum 947 http://éire.icom.museum 948 http://איקו״ם.ישראל.museum 949 http://日本.icom.museum 950 http://الأردن.icom.museum 951 http://қазақстан.icom.museum 952 http://한국.icom.museum 953 http://кыргызстан.icom.museum 954 http://ລາວ.icom.museum 955 http://لبنان.icom.museum 956 http://македонија.icom.museum 957 http://méxico.icom.museum 958 http://монголулс.icom.museum 959 http://नेपाल.icom.museum 960 http://قطر.icom.museum 961 http://românia.icom.museum 962 http://россия.иком.museum 963 http://србијаицрнагора.иком.museum 964 http://இலங்கை.icom.museum 965 http://españa.icom.museum 966 http://ไทย.icom.museum 967 http://تونس.icom.museum 968 http://türkiye.icom.museum 969 http://украина.icom.museum 970 http://việtnam.icom.museum 971 ftp://127.0.0.1/ 972 http://www.woo.com/video?v=exvUH2qKLTU 973 974 http://taco.com?burrito=enchilada#guac 975 976 http://example.org/?post_type=post&p=4 977 http://example.org/?post_type=post&p=5 978 http://example.org/?post_type=post&p=6 979 980 http://typo-in-query.org/?foo=bar&baz=missing_semicolon 981 '; 982 983 $urls = wp_extract_urls( $blob ); 984 $this->assertNotEmpty( $urls ); 985 $this->assertIsArray( $urls ); 986 $this->assertCount( count( $original_urls ), $urls ); 987 $this->assertSame( $original_urls, $urls ); 988 989 $exploded = array_values( array_filter( array_map( 'trim', explode( "\n", $blob ) ) ) ); 990 // wp_extract_urls() calls html_entity_decode(). 991 $decoded = array_map( 'html_entity_decode', $exploded ); 992 993 $this->assertSame( $decoded, $urls ); 994 $this->assertSame( $original_urls, $decoded ); 995 996 $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 997 incididunt ut labore http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html et dolore magna aliqua. 998 Ut http://this.com enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco http://127.0.0.1 999 laboris nisi ut aliquip ex http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437 ea 1000 commodo consequat. http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html Duis aute irure dolor in reprehenderit in voluptate 1001 velit esse http://wordpress-core.com:8080/ cillum dolore eu fugiat nulla <A href="http://www.website.com:5000">http://www.website.com:5000</B> pariatur. Excepteur sint occaecat cupidatat non proident, 1002 sunt in culpa qui officia deserunt mollit http://wordpress-core/?346236346326&2134362574863.437 anim id est laborum.'; 1003 1004 $urls = wp_extract_urls( $blob ); 1005 $this->assertNotEmpty( $urls ); 1006 $this->assertIsArray( $urls ); 1007 $this->assertCount( 8, $urls ); 1008 $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); 1009 1010 $blob = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 1011 incididunt ut labore <a href="http://woo.com/1,2,3,4,5,6/-1-2-3-4-/woo.html">343462^</a> et dolore magna aliqua. 1012 Ut <a href="http://this.com">&3640i6p1yi499</a> enim ad minim veniam, quis nostrud exercitation 16.06. to 18.06.2014 ullamco <a href="http://127.0.0.1">localhost</a> 1013 laboris nisi ut aliquip ex <a href="http://www111.urwyeoweytwutreyytqytwetowteuiiu.com/?346236346326&2134362574863.437">343462^</a> ea 1014 commodo consequat. <a href="http://wordpress-core/1,2,3,4,5,6/-1-2-3-4-/woo.html">343462^</a> Duis aute irure dolor in reprehenderit in voluptate 1015 velit esse <a href="http://wordpress-core.com:8080/">-3-4--321-64-4@#!$^$!@^@^</a> cillum dolore eu <A href="http://www.website.com:5000">http://www.website.com:5000</B> fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, 1016 sunt in culpa qui officia deserunt mollit <a href="http://wordpress-core/?346236346326&2134362574863.437">)(*&^%$</a> anim id est laborum.'; 1017 1018 $urls = wp_extract_urls( $blob ); 1019 $this->assertNotEmpty( $urls ); 1020 $this->assertIsArray( $urls ); 1021 $this->assertCount( 8, $urls ); 1022 $this->assertSame( array_slice( $original_urls, 0, 8 ), $urls ); 1023 } 1024 1025 /** 1026 * Tests for backward compatibility of `wp_extract_urls` to remove unused semicolons. 1027 * 1028 * @ticket 30580 1029 * 1030 * @covers ::wp_extract_urls 1031 */ 1032 public function test_wp_extract_urls_remove_semicolon() { 1033 $expected = array( 1034 'http://typo.com', 1035 'http://example.org/?post_type=post&p=8', 1036 ); 1037 $actual = wp_extract_urls( 1038 ' 1039 http://typo.com; 1040 http://example.org/?post_type=;p;o;s;t;&p=8; 1041 ' 1042 ); 1043 1044 $this->assertSame( $expected, $actual ); 1045 } 1046 1047 /** 1048 * @ticket 28786 1049 */ 1050 public function test_wp_json_encode() { 1051 $this->assertSame( wp_json_encode( 'a' ), '"a"' ); 1052 } 1053 1054 /** 1055 * @ticket 28786 1056 */ 1057 public function test_wp_json_encode_utf8() { 1058 $this->assertSame( wp_json_encode( '这' ), '"\u8fd9"' ); 1059 } 1060 1061 /** 1062 * @ticket 28786 1063 * @requires function mb_detect_order 1064 */ 1065 public function test_wp_json_encode_non_utf8() { 1066 $charsets = mb_detect_order(); 1067 $old_charsets = $charsets; 1068 if ( ! in_array( 'EUC-JP', $charsets, true ) ) { 1069 $charsets[] = 'EUC-JP'; 1070 mb_detect_order( $charsets ); 1071 } 1072 1073 $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); 1074 $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); 1075 1076 $this->assertSame( 'aあb', $utf8 ); 1077 1078 $this->assertSame( '"a\u3042b"', wp_json_encode( $eucjp ) ); 1079 1080 mb_detect_order( $old_charsets ); 1081 } 1082 1083 /** 1084 * @ticket 28786 1085 * @requires function mb_detect_order 1086 */ 1087 public function test_wp_json_encode_non_utf8_in_array() { 1088 $charsets = mb_detect_order(); 1089 $old_charsets = $charsets; 1090 if ( ! in_array( 'EUC-JP', $charsets, true ) ) { 1091 $charsets[] = 'EUC-JP'; 1092 mb_detect_order( $charsets ); 1093 } 1094 1095 $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); 1096 $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); 1097 1098 $this->assertSame( 'aあb', $utf8 ); 1099 1100 $this->assertSame( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) ); 1101 1102 mb_detect_order( $old_charsets ); 1103 } 1104 1105 /** 1106 * @ticket 28786 1107 */ 1108 public function test_wp_json_encode_array() { 1109 $this->assertSame( wp_json_encode( array( 'a' ) ), '["a"]' ); 1110 } 1111 1112 /** 1113 * @ticket 28786 1114 */ 1115 public function test_wp_json_encode_object() { 1116 $object = new stdClass; 1117 $object->a = 'b'; 1118 $this->assertSame( wp_json_encode( $object ), '{"a":"b"}' ); 1119 } 1120 1121 /** 1122 * @ticket 28786 1123 */ 1124 public function test_wp_json_encode_depth() { 1125 $data = array( array( array( 1, 2, 3 ) ) ); 1126 $json = wp_json_encode( $data, 0, 1 ); 1127 $this->assertFalse( $json ); 1128 1129 $data = array( 'あ', array( array( 1, 2, 3 ) ) ); 1130 $json = wp_json_encode( $data, 0, 1 ); 1131 $this->assertFalse( $json ); 1132 } 1133 1134 /** 1135 * @ticket 53238 1136 */ 1137 public function test_wp_json_file_decode() { 1138 $result = wp_json_file_decode( 1139 DIR_TESTDATA . '/blocks/notice/block.json' 1140 ); 1141 1142 $this->assertIsObject( $result ); 1143 $this->assertSame( 'tests/notice', $result->name ); 1144 } 1145 1146 /** 1147 * @ticket 53238 1148 */ 1149 public function test_wp_json_file_decode_associative_array() { 1150 $result = wp_json_file_decode( 1151 DIR_TESTDATA . '/blocks/notice/block.json', 1152 array( 'associative' => true ) 1153 ); 1154 1155 $this->assertIsArray( $result ); 1156 $this->assertSame( 'tests/notice', $result['name'] ); 1157 } 1158 1159 /** 1160 * @ticket 36054 1161 * @dataProvider datetime_provider 1162 */ 1163 public function test_mysql_to_rfc3339( $expected, $actual ) { 1164 $date_return = mysql_to_rfc3339( $actual ); 1165 1166 $this->assertIsString( $date_return, 'The date return must be a string' ); 1167 $this->assertNotEmpty( $date_return, 'The date return could not be an empty string' ); 1168 $this->assertSame( $expected, $date_return, 'The date does not match' ); 1169 $this->assertEquals( new DateTime( $expected ), new DateTime( $date_return ), 'The date is not the same after the call method' ); 1170 } 1171 1172 public function datetime_provider() { 1173 return array( 1174 array( '2016-03-15T18:54:46', '15-03-2016 18:54:46' ), 1175 array( '2016-03-02T19:13:25', '2016-03-02 19:13:25' ), 1176 array( '2016-03-02T19:13:00', '2016-03-02 19:13' ), 1177 array( '2016-03-02T19:13:00', '16-03-02 19:13' ), 1178 array( '2016-03-02T19:13:00', '16-03-02 19:13' ), 1179 ); 1180 } 1181 1182 /** 1183 * @ticket 35987 1184 */ 1185 public function test_wp_get_ext_types() { 1186 $extensions = wp_get_ext_types(); 1187 1188 $this->assertIsArray( $extensions ); 1189 $this->assertNotEmpty( $extensions ); 1190 1191 add_filter( 'ext2type', '__return_empty_array' ); 1192 $extensions = wp_get_ext_types(); 1193 $this->assertSame( array(), $extensions ); 1194 1195 remove_filter( 'ext2type', '__return_empty_array' ); 1196 $extensions = wp_get_ext_types(); 1197 $this->assertIsArray( $extensions ); 1198 $this->assertNotEmpty( $extensions ); 1199 } 1200 1201 /** 1202 * @ticket 35987 1203 */ 1204 public function test_wp_ext2type() { 1205 $extensions = wp_get_ext_types(); 1206 1207 foreach ( $extensions as $type => $extension_list ) { 1208 foreach ( $extension_list as $extension ) { 1209 $this->assertSame( $type, wp_ext2type( $extension ) ); 1210 $this->assertSame( $type, wp_ext2type( strtoupper( $extension ) ) ); 1211 } 1212 } 1213 1214 $this->assertNull( wp_ext2type( 'unknown_format' ) ); 1215 } 1216 1217 /** 1218 * Tests raising the memory limit. 1219 * 1220 * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the 1221 * test suite is -1, we can not test the memory limit negotiations. 1222 * 1223 * @ticket 32075 1224 */ 1225 public function test_wp_raise_memory_limit() { 1226 if ( -1 !== WP_MAX_MEMORY_LIMIT ) { 1227 $this->markTestSkipped( 'WP_MAX_MEMORY_LIMIT should be set to -1.' ); 1228 } 1229 1230 $ini_limit_before = ini_get( 'memory_limit' ); 1231 $raised_limit = wp_raise_memory_limit(); 1232 $ini_limit_after = ini_get( 'memory_limit' ); 1233 1234 $this->assertSame( $ini_limit_before, $ini_limit_after ); 1235 $this->assertFalse( $raised_limit ); 1236 $this->assertEquals( WP_MAX_MEMORY_LIMIT, $ini_limit_after ); 1237 } 1238 1239 /** 1240 * Tests wp_generate_uuid4(). 1241 * 1242 * @covers ::wp_generate_uuid4 1243 * @ticket 38164 1244 */ 1245 public function test_wp_generate_uuid4() { 1246 $uuids = array(); 1247 for ( $i = 0; $i < 20; $i += 1 ) { 1248 $uuid = wp_generate_uuid4(); 1249 $this->assertTrue( wp_is_uuid( $uuid, 4 ) ); 1250 $uuids[] = $uuid; 1251 } 1252 1253 $unique_uuids = array_unique( $uuids ); 1254 $this->assertSame( $uuids, $unique_uuids ); 1255 } 1256 1257 /** 1258 * Tests wp_is_uuid(). 1259 * 1260 * @covers ::wp_is_uuid 1261 * @ticket 39778 1262 */ 1263 public function test_wp_is_valid_uuid() { 1264 $uuids_v4 = array( 1265 '27fe2421-780c-44c5-b39b-fff753092b55', 1266 'b7c7713a-4ee9-45a1-87ed-944a90390fc7', 1267 'fbedbe35-7bf5-49cc-a5ac-0343bd94360a', 1268 '4c58e67e-123b-4290-a41c-5eeb6970fa3e', 1269 'f54f5b78-e414-4637-84a9-a6cdc94a1beb', 1270 'd1c533ac-abcf-44b6-9b0e-6477d2c91b09', 1271 '7fcd683f-e5fd-454a-a8b9-ed15068830da', 1272 '7962c750-e58c-470a-af0d-ec1eae453ff2', 1273 'a59878ce-9a67-4493-8ca0-a756b52804b3', 1274 '6faa519d-1e13-4415-bd6f-905ae3689d1d', 1275 ); 1276 1277 foreach ( $uuids_v4 as $uuid ) { 1278 $this->assertTrue( wp_is_uuid( $uuid, 4 ) ); 1279 } 1280 1281 $uuids = array( 1282 '00000000-0000-0000-0000-000000000000', // Nil. 1283 '9e3a0460-d72d-11e4-a631-c8e0eb141dab', // Version 1. 1284 '2c1d43b8-e6d7-376e-af7f-d4bde997cc3f', // Version 3. 1285 '39888f87-fb62-5988-a425-b2ea63f5b81e', // Version 5. 1286 ); 1287 1288 foreach ( $uuids as $uuid ) { 1289 $this->assertTrue( wp_is_uuid( $uuid ) ); 1290 $this->assertFalse( wp_is_uuid( $uuid, 4 ) ); 1291 } 1292 1293 $invalid_uuids = array( 1294 'a19d5192-ea41-41e6-b006', 1295 'this-is-not-valid', 1296 1234, 1297 true, 1298 array(), 1299 ); 1300 1301 foreach ( $invalid_uuids as $invalid_uuid ) { 1302 $this->assertFalse( wp_is_uuid( $invalid_uuid, 4 ) ); 1303 $this->assertFalse( wp_is_uuid( $invalid_uuid ) ); 1304 } 1305 } 1306 1307 /** 1308 * Tests wp_unique_id(). 1309 * 1310 * @covers ::wp_unique_id 1311 * @ticket 44883 1312 */ 1313 public function test_wp_unique_id() { 1314 1315 // Test without prefix. 1316 $ids = array(); 1317 for ( $i = 0; $i < 20; $i += 1 ) { 1318 $id = wp_unique_id(); 1319 $this->assertIsString( $id ); 1320 $this->assertIsNumeric( $id ); 1321 $ids[] = $id; 1322 } 1323 $this->assertSame( $ids, array_unique( $ids ) ); 1324 1325 // Test with prefix. 1326 $ids = array(); 1327 for ( $i = 0; $i < 20; $i += 1 ) { 1328 $id = wp_unique_id( 'foo-' ); 1329 $this->assertMatchesRegularExpression( '/^foo-\d+$/', $id ); 1330 $ids[] = $id; 1331 } 1332 $this->assertSame( $ids, array_unique( $ids ) ); 1333 } 1334 1335 /** 1336 * @ticket 40017 1337 * @dataProvider wp_get_image_mime 1338 */ 1339 public function test_wp_get_image_mime( $file, $expected ) { 1340 if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { 1341 $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); 1342 } 1343 1344 $this->assertSame( $expected, wp_get_image_mime( $file ) ); 1345 } 1346 1347 /** 1348 * @ticket 35725 1349 * @dataProvider data_wp_getimagesize 1350 */ 1351 public function test_wp_getimagesize( $file, $expected ) { 1352 if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { 1353 $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); 1354 } 1355 1356 $result = wp_getimagesize( $file ); 1357 1358 // The getimagesize() function varies in its response, so 1359 // let's restrict comparison to expected keys only. 1360 if ( is_array( $expected ) ) { 1361 foreach ( $expected as $k => $v ) { 1362 $this->assertArrayHasKey( $k, $result ); 1363 $this->assertSame( $expected[ $k ], $result[ $k ] ); 1364 } 1365 } else { 1366 $this->assertSame( $expected, $result ); 1367 } 1368 } 1369 1370 /** 1371 * @ticket 39550 1372 * @dataProvider wp_check_filetype_and_ext_data 1373 * @requires extension fileinfo 1374 */ 1375 public function test_wp_check_filetype_and_ext( $file, $filename, $expected ) { 1376 $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); 1377 } 1378 1379 /** 1380 * @ticket 39550 1381 * @group ms-excluded 1382 * @requires extension fileinfo 1383 */ 1384 public function test_wp_check_filetype_and_ext_with_filtered_svg() { 1385 $file = DIR_TESTDATA . '/uploads/video-play.svg'; 1386 $filename = 'video-play.svg'; 1387 1388 $expected = array( 1389 'ext' => 'svg', 1390 'type' => 'image/svg+xml', 1391 'proper_filename' => false, 1392 ); 1393 1394 add_filter( 'upload_mimes', array( $this, 'filter_mime_types_svg' ) ); 1395 $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); 1396 1397 // Cleanup. 1398 remove_filter( 'upload_mimes', array( $this, 'filter_mime_types_svg' ) ); 1399 } 1400 1401 /** 1402 * @ticket 39550 1403 * @group ms-excluded 1404 * @requires extension fileinfo 1405 */ 1406 public function test_wp_check_filetype_and_ext_with_filtered_woff() { 1407 if ( PHP_VERSION_ID >= 80100 ) { 1408 /* 1409 * For the time being, this test is marked skipped on PHP 8.1+ as a recent change introduced 1410 * an inconsistency with how the mime-type for WOFF files are handled compared to older versions. 1411 * 1412 * See https://core.trac.wordpress.org/ticket/56817 for more details. 1413 */ 1414 $this->markTestSkipped( 'This test currently fails on PHP 8.1+ and requires further investigation.' ); 1415 } 1416 1417 $file = DIR_TESTDATA . '/uploads/dashicons.woff'; 1418 $filename = 'dashicons.woff'; 1419 1420 $expected = array( 1421 'ext' => 'woff', 1422 'type' => 'application/font-woff', 1423 'proper_filename' => false, 1424 ); 1425 1426 add_filter( 'upload_mimes', array( $this, 'filter_mime_types_woff' ) ); 1427 $this->assertSame( $expected, wp_check_filetype_and_ext( $file, $filename ) ); 1428 1429 // Cleanup. 1430 remove_filter( 'upload_mimes', array( $this, 'filter_mime_types_woff' ) ); 1431 } 1432 1433 public function filter_mime_types_svg( $mimes ) { 1434 $mimes['svg'] = 'image/svg+xml'; 1435 return $mimes; 1436 } 1437 1438 public function filter_mime_types_woff( $mimes ) { 1439 $mimes['woff'] = 'application/font-woff'; 1440 return $mimes; 1441 } 1442 1443 /** 1444 * Data provider for test_wp_get_image_mime(). 1445 */ 1446 public function wp_get_image_mime() { 1447 $data = array( 1448 // Standard JPEG. 1449 array( 1450 DIR_TESTDATA . '/images/test-image.jpg', 1451 'image/jpeg', 1452 ), 1453 // Standard GIF. 1454 array( 1455 DIR_TESTDATA . '/images/test-image.gif', 1456 'image/gif', 1457 ), 1458 // Standard PNG. 1459 array( 1460 DIR_TESTDATA . '/images/test-image.png', 1461 'image/png', 1462 ), 1463 // Image with wrong extension. 1464 array( 1465 DIR_TESTDATA . '/images/test-image-mime-jpg.png', 1466 'image/jpeg', 1467 ), 1468 // Animated WebP. 1469 array( 1470 DIR_TESTDATA . '/images/webp-animated.webp', 1471 'image/webp', 1472 ), 1473 // Lossless WebP. 1474 array( 1475 DIR_TESTDATA . '/images/webp-lossless.webp', 1476 'image/webp', 1477 ), 1478 // Lossy WebP. 1479 array( 1480 DIR_TESTDATA . '/images/webp-lossy.webp', 1481 'image/webp', 1482 ), 1483 // Transparent WebP. 1484 array( 1485 DIR_TESTDATA . '/images/webp-transparent.webp', 1486 'image/webp', 1487 ), 1488 // Not an image. 1489 array( 1490 DIR_TESTDATA . '/uploads/dashicons.woff', 1491 false, 1492 ), 1493 ); 1494 1495 return $data; 1496 } 1497 1498 /** 1499 * Data profider for test_wp_getimagesize(). 1500 */ 1501 public function data_wp_getimagesize() { 1502 $data = array( 1503 // Standard JPEG. 1504 array( 1505 DIR_TESTDATA . '/images/test-image.jpg', 1506 array( 1507 50, 1508 50, 1509 IMAGETYPE_JPEG, 1510 'width="50" height="50"', 1511 'mime' => 'image/jpeg', 1512 ), 1513 ), 1514 // Standard GIF. 1515 array( 1516 DIR_TESTDATA . '/images/test-image.gif', 1517 array( 1518 50, 1519 50, 1520 IMAGETYPE_GIF, 1521 'width="50" height="50"', 1522 'mime' => 'image/gif', 1523 ), 1524 ), 1525 // Standard PNG. 1526 array( 1527 DIR_TESTDATA . '/images/test-image.png', 1528 array( 1529 50, 1530 50, 1531 IMAGETYPE_PNG, 1532 'width="50" height="50"', 1533 'mime' => 'image/png', 1534 ), 1535 ), 1536 // Image with wrong extension. 1537 array( 1538 DIR_TESTDATA . '/images/test-image-mime-jpg.png', 1539 array( 1540 50, 1541 50, 1542 IMAGETYPE_JPEG, 1543 'width="50" height="50"', 1544 'mime' => 'image/jpeg', 1545 ), 1546 ), 1547 // Animated WebP. 1548 array( 1549 DIR_TESTDATA . '/images/webp-animated.webp', 1550 array( 1551 100, 1552 100, 1553 IMAGETYPE_WEBP, 1554 'width="100" height="100"', 1555 'mime' => 'image/webp', 1556 ), 1557 ), 1558 // Lossless WebP. 1559 array( 1560 DIR_TESTDATA . '/images/webp-lossless.webp', 1561 array( 1562 1200, 1563 675, 1564 IMAGETYPE_WEBP, 1565 'width="1200" height="675"', 1566 'mime' => 'image/webp', 1567 ), 1568 ), 1569 // Lossy WebP. 1570 array( 1571 DIR_TESTDATA . '/images/webp-lossy.webp', 1572 array( 1573 1200, 1574 675, 1575 IMAGETYPE_WEBP, 1576 'width="1200" height="675"', 1577 'mime' => 'image/webp', 1578 ), 1579 ), 1580 // Transparent WebP. 1581 array( 1582 DIR_TESTDATA . '/images/webp-transparent.webp', 1583 array( 1584 1200, 1585 675, 1586 IMAGETYPE_WEBP, 1587 'width="1200" height="675"', 1588 'mime' => 'image/webp', 1589 ), 1590 ), 1591 // Not an image. 1592 array( 1593 DIR_TESTDATA . '/uploads/dashicons.woff', 1594 false, 1595 ), 1596 ); 1597 1598 return $data; 1599 } 1600 1601 public function wp_check_filetype_and_ext_data() { 1602 $data = array( 1603 // Standard image. 1604 array( 1605 DIR_TESTDATA . '/images/canola.jpg', 1606 'canola.jpg', 1607 array( 1608 'ext' => 'jpg', 1609 'type' => 'image/jpeg', 1610 'proper_filename' => false, 1611 ), 1612 ), 1613 // Image with wrong extension. 1614 array( 1615 DIR_TESTDATA . '/images/test-image-mime-jpg.png', 1616 'test-image-mime-jpg.png', 1617 array( 1618 'ext' => 'jpg', 1619 'type' => 'image/jpeg', 1620 'proper_filename' => 'test-image-mime-jpg.jpg', 1621 ), 1622 ), 1623 // Image without extension. 1624 array( 1625 DIR_TESTDATA . '/images/test-image-no-extension', 1626 'test-image-no-extension', 1627 array( 1628 'ext' => false, 1629 'type' => false, 1630 'proper_filename' => false, 1631 ), 1632 ), 1633 // Valid non-image file with an image extension. 1634 array( 1635 DIR_TESTDATA . '/formatting/big5.txt', 1636 'big5.jpg', 1637 array( 1638 'ext' => false, 1639 'type' => false, 1640 'proper_filename' => false, 1641 ), 1642 ), 1643 // Non-image file not allowed. 1644 array( 1645 DIR_TESTDATA . '/export/crazy-cdata.xml', 1646 'crazy-cdata.xml', 1647 array( 1648 'ext' => false, 1649 'type' => false, 1650 'proper_filename' => false, 1651 ), 1652 ), 1653 // Non-image file not allowed even if it's named like one. 1654 array( 1655 DIR_TESTDATA . '/export/crazy-cdata.xml', 1656 'crazy-cdata.jpg', 1657 array( 1658 'ext' => false, 1659 'type' => false, 1660 'proper_filename' => false, 1661 ), 1662 ), 1663 // Non-image file not allowed if it's named like something else. 1664 array( 1665 DIR_TESTDATA . '/export/crazy-cdata.xml', 1666 'crazy-cdata.doc', 1667 array( 1668 'ext' => false, 1669 'type' => false, 1670 'proper_filename' => false, 1671 ), 1672 ), 1673 // Non-image file not allowed even if it's named like one. 1674 array( 1675 DIR_TESTDATA . '/export/crazy-cdata.xml', 1676 'crazy-cdata.jpg', 1677 array( 1678 'ext' => false, 1679 'type' => false, 1680 'proper_filename' => false, 1681 ), 1682 ), 1683 // Non-image file not allowed if it's named like something else. 1684 array( 1685 DIR_TESTDATA . '/export/crazy-cdata.xml', 1686 'crazy-cdata.doc', 1687 array( 1688 'ext' => false, 1689 'type' => false, 1690 'proper_filename' => false, 1691 ), 1692 ), 1693 ); 1694 1695 // Test a few additional file types on single sites. 1696 if ( ! is_multisite() ) { 1697 $data = array_merge( 1698 $data, 1699 array( 1700 // Standard non-image file. 1701 array( 1702 DIR_TESTDATA . '/formatting/big5.txt', 1703 'big5.txt', 1704 array( 1705 'ext' => 'txt', 1706 'type' => 'text/plain', 1707 'proper_filename' => false, 1708 ), 1709 ), 1710 // Non-image file with wrong sub-type. 1711 array( 1712 DIR_TESTDATA . '/uploads/pages-to-word.docx', 1713 'pages-to-word.docx', 1714 array( 1715 'ext' => 'docx', 1716 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 1717 'proper_filename' => false, 1718 ), 1719 ), 1720 // FLAC file. 1721 array( 1722 DIR_TESTDATA . '/uploads/small-audio.flac', 1723 'small-audio.flac', 1724 array( 1725 'ext' => 'flac', 1726 'type' => 'audio/flac', 1727 'proper_filename' => false, 1728 ), 1729 ), 1730 // Assorted text/* sample files 1731 array( 1732 DIR_TESTDATA . '/uploads/test.vtt', 1733 'test.vtt', 1734 array( 1735 'ext' => 'vtt', 1736 'type' => 'text/vtt', 1737 'proper_filename' => false, 1738 ), 1739 ), 1740 array( 1741 DIR_TESTDATA . '/uploads/test.csv', 1742 'test.csv', 1743 array( 1744 'ext' => 'csv', 1745 'type' => 'text/csv', 1746 'proper_filename' => false, 1747 ), 1748 ), 1749 // RTF files. 1750 array( 1751 DIR_TESTDATA . '/uploads/test.rtf', 1752 'test.rtf', 1753 array( 1754 'ext' => 'rtf', 1755 'type' => 'application/rtf', 1756 'proper_filename' => false, 1757 ), 1758 ), 1759 ) 1760 ); 1761 } 1762 1763 return $data; 1764 } 1765 1766 /** 1767 * Test file path validation 1768 * 1769 * @ticket 42016 1770 * @dataProvider data_test_validate_file() 1771 * 1772 * @param string $file File path. 1773 * @param array $allowed_files List of allowed files. 1774 * @param int $expected Expected result. 1775 */ 1776 public function test_validate_file( $file, $allowed_files, $expected ) { 1777 $this->assertSame( $expected, validate_file( $file, $allowed_files ) ); 1778 } 1779 1780 /** 1781 * Data provider for file validation. 1782 * 1783 * @return array { 1784 * @type array $0... { 1785 * @type string $0 File path. 1786 * @type array $1 List of allowed files. 1787 * @type int $2 Expected result. 1788 * } 1789 * } 1790 */ 1791 public function data_test_validate_file() { 1792 return array( 1793 1794 // Allowed files: 1795 array( 1796 null, 1797 array(), 1798 0, 1799 ), 1800 array( 1801 '', 1802 array(), 1803 0, 1804 ), 1805 array( 1806 ' ', 1807 array(), 1808 0, 1809 ), 1810 array( 1811 '.', 1812 array(), 1813 0, 1814 ), 1815 array( 1816 '..', 1817 array(), 1818 0, 1819 ), 1820 array( 1821 './', 1822 array(), 1823 0, 1824 ), 1825 array( 1826 'foo.ext', 1827 array( 'foo.ext' ), 1828 0, 1829 ), 1830 array( 1831 'dir/foo.ext', 1832 array(), 1833 0, 1834 ), 1835 array( 1836 'foo..ext', 1837 array(), 1838 0, 1839 ), 1840 array( 1841 'dir/dir/../', 1842 array(), 1843 0, 1844 ), 1845 1846 // Directory traversal: 1847 array( 1848 '../', 1849 array(), 1850 1, 1851 ), 1852 array( 1853 '../../', 1854 array(), 1855 1, 1856 ), 1857 array( 1858 '../file.ext', 1859 array(), 1860 1, 1861 ), 1862 array( 1863 '../dir/../', 1864 array(), 1865 1, 1866 ), 1867 array( 1868 '/dir/dir/../../', 1869 array(), 1870 1, 1871 ), 1872 array( 1873 '/dir/dir/../../', 1874 array( '/dir/dir/../../' ), 1875 1, 1876 ), 1877 1878 // Windows drives: 1879 array( 1880 'c:', 1881 array(), 1882 2, 1883 ), 1884 array( 1885 'C:/WINDOWS/system32', 1886 array( 'C:/WINDOWS/system32' ), 1887 2, 1888 ), 1889 1890 // Disallowed files: 1891 array( 1892 'foo.ext', 1893 array( 'bar.ext' ), 1894 3, 1895 ), 1896 array( 1897 'foo.ext', 1898 array( '.ext' ), 1899 3, 1900 ), 1901 array( 1902 'path/foo.ext', 1903 array( 'foo.ext' ), 1904 3, 1905 ), 1906 1907 ); 1908 } 1909 1910 /** 1911 * Test stream URL validation. 1912 * 1913 * @dataProvider data_test_wp_is_stream 1914 * 1915 * @param string $path The resource path or URL. 1916 * @param bool $expected Expected result. 1917 */ 1918 public function test_wp_is_stream( $path, $expected ) { 1919 if ( ! extension_loaded( 'openssl' ) && false !== strpos( $path, 'https://' ) ) { 1920 $this->markTestSkipped( 'The openssl PHP extension is not loaded.' ); 1921 } 1922 1923 $this->assertSame( $expected, wp_is_stream( $path ) ); 1924 } 1925 1926 /** 1927 * Data provider for stream URL validation. 1928 * 1929 * @return array { 1930 * @type array $0... { 1931 * @type string $0 The resource path or URL. 1932 * @type bool $1 Expected result. 1933 * } 1934 * } 1935 */ 1936 public function data_test_wp_is_stream() { 1937 return array( 1938 // Legitimate stream examples. 1939 array( 'http://example.com', true ), 1940 array( 'https://example.com', true ), 1941 array( 'ftp://example.com', true ), 1942 array( 'file:///path/to/some/file', true ), 1943 array( 'php://some/php/file.php', true ), 1944 1945 // Non-stream examples. 1946 array( 'fakestream://foo/bar/baz', false ), 1947 array( '../../some/relative/path', false ), 1948 array( 'some/other/relative/path', false ), 1949 array( '/leading/relative/path', false ), 1950 ); 1951 } 1952 1953 /** 1954 * Test human_readable_duration(). 1955 * 1956 * @ticket 39667 1957 * @dataProvider data_test_human_readable_duration 1958 * 1959 * @param string $input Duration. 1960 * @param string $expected Expected human readable duration. 1961 */ 1962 public function test_human_readable_duration( $input, $expected ) { 1963 $this->assertSame( $expected, human_readable_duration( $input ) ); 1964 } 1965 1966 /** 1967 * Dataprovider for test_duration_format(). 1968 * 1969 * @return array { 1970 * @type array { 1971 * @type string $input Duration. 1972 * @type string $expect Expected human readable duration. 1973 * } 1974 * } 1975 */ 1976 public function data_test_human_readable_duration() { 1977 return array( 1978 // Valid ii:ss cases. 1979 array( '0:0', '0 minutes, 0 seconds' ), 1980 array( '00:00', '0 minutes, 0 seconds' ), 1981 array( '0:5', '0 minutes, 5 seconds' ), 1982 array( '0:05', '0 minutes, 5 seconds' ), 1983 array( '01:01', '1 minute, 1 second' ), 1984 array( '30:00', '30 minutes, 0 seconds' ), 1985 array( ' 30:00 ', '30 minutes, 0 seconds' ), 1986 // Valid HH:ii:ss cases. 1987 array( '0:0:0', '0 hours, 0 minutes, 0 seconds' ), 1988 array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1989 array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ), 1990 array( '01:01:01', '1 hour, 1 minute, 1 second' ), 1991 array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1992 array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ), 1993 array( '1234567890:59:59', '1234567890 hours, 59 minutes, 59 seconds' ), 1994 // Valid ii:ss cases with negative sign. 1995 array( '-00:00', '0 minutes, 0 seconds' ), 1996 array( '-3:00', '3 minutes, 0 seconds' ), 1997 array( '-03:00', '3 minutes, 0 seconds' ), 1998 array( '-30:00', '30 minutes, 0 seconds' ), 1999 // Valid HH:ii:ss cases with negative sign. 2000 array( '-00:00:00', '0 hours, 0 minutes, 0 seconds' ), 2001 array( '-1:02:00', '1 hour, 2 minutes, 0 seconds' ), 2002 // Invalid cases. 2003 array( null, false ), 2004 array( '', false ), 2005 array( ':', false ), 2006 array( '::', false ), 2007 array( array(), false ), 2008 array( 'Batman Begins !', false ), 2009 array( '', false ), 2010 array( '-1', false ), 2011 array( -1, false ), 2012 array( 0, false ), 2013 array( 1, false ), 2014 array( '00', false ), 2015 array( '30:-10', false ), 2016 array( ':30:00', false ), // Missing HH. 2017 array( 'MM:30:00', false ), // Invalid HH. 2018 array( '30:MM:00', false ), // Invalid ii. 2019 array( '30:30:MM', false ), // Invalid ss. 2020 array( '30:MM', false ), // Invalid ss. 2021 array( 'MM:00', false ), // Invalid ii. 2022 array( 'MM:MM', false ), // Invalid ii and ss. 2023 array( '10 :30', false ), // Containing a space. 2024 array( '59:61', false ), // Out of bound. 2025 array( '61:59', false ), // Out of bound. 2026 array( '3:59:61', false ), // Out of bound. 2027 array( '03:61:59', false ), // Out of bound. 2028 ); 2029 } 2030 2031 /** 2032 * @ticket 49404 2033 * @dataProvider data_test_wp_is_json_media_type 2034 */ 2035 public function test_wp_is_json_media_type( $input, $expected ) { 2036 $this->assertSame( $expected, wp_is_json_media_type( $input ) ); 2037 } 2038 2039 2040 public function data_test_wp_is_json_media_type() { 2041 return array( 2042 array( 'application/ld+json', true ), 2043 array( 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', true ), 2044 array( 'application/activity+json', true ), 2045 array( 'application/json+oembed', true ), 2046 array( 'application/json', true ), 2047 array( 'application/nojson', false ), 2048 array( 'application/no.json', false ), 2049 array( 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8', false ), 2050 array( 'application/activity+json, application/nojson', true ), 2051 ); 2052 } 2053 2054 /** 2055 * @ticket 53668 2056 */ 2057 public function test_wp_get_default_extension_for_mime_type() { 2058 $this->assertSame( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); 2059 $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); 2060 $this->assertSame( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); 2061 $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); 2062 $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); 2063 $this->assertFalse( wp_get_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' ); 2064 $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); 2065 $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); 2066 } 9 class Tests_Functions_wpFilesize extends WP_UnitTestCase { 2067 10 2068 11 /** 2069 12 * @ticket 49412 2070 * @covers ::wp_filesize2071 */2072 function test_wp_filesize_with_nonexistent_file() {2073 $file = 'nonexistent/file.jpg';2074 $this->assertSame( 0, wp_filesize( $file ) );2075 }2076 2077 /**2078 * @ticket 494122079 * @covers ::wp_filesize2080 13 */ 2081 14 function test_wp_filesize() { … … 2083 16 2084 17 $this->assertSame( filesize( $file ), wp_filesize( $file ) ); 18 } 2085 19 2086 $filter = function() { 2087 return 999; 2088 }; 20 /** 21 * @ticket 49412 22 */ 23 function test_wp_filesize_filters() { 24 $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; 2089 25 2090 add_filter( 'wp_filesize', $filter ); 26 add_filter( 27 'wp_filesize', 28 static function() { 29 return 999; 30 } 31 ); 2091 32 2092 33 $this->assertSame( 999, wp_filesize( $file ) ); 2093 34 2094 $pre_filter = function() { 2095 return 111; 2096 }; 2097 2098 add_filter( 'pre_wp_filesize', $pre_filter ); 35 add_filter( 36 'pre_wp_filesize', 37 static function() { 38 return 111; 39 } 40 ); 2099 41 2100 42 $this->assertSame( 111, wp_filesize( $file ) ); … … 2102 44 2103 45 /** 2104 * @ticket 55505 2105 * @covers ::wp_recursive_ksort 46 * @ticket 49412 2106 47 */ 2107 function test_wp_recursive_ksort() { 2108 // Create an array to test. 2109 $theme_json = array( 2110 'version' => 1, 2111 'settings' => array( 2112 'typography' => array( 2113 'fontFamilies' => array( 2114 'fontFamily' => 'DM Sans, sans-serif', 2115 'slug' => 'dm-sans', 2116 'name' => 'DM Sans', 2117 ), 2118 ), 2119 'color' => array( 2120 'palette' => array( 2121 array( 2122 'slug' => 'foreground', 2123 'color' => '#242321', 2124 'name' => 'Foreground', 2125 ), 2126 array( 2127 'slug' => 'background', 2128 'color' => '#FCFBF8', 2129 'name' => 'Background', 2130 ), 2131 array( 2132 'slug' => 'primary', 2133 'color' => '#71706E', 2134 'name' => 'Primary', 2135 ), 2136 array( 2137 'slug' => 'tertiary', 2138 'color' => '#CFCFCF', 2139 'name' => 'Tertiary', 2140 ), 2141 ), 2142 ), 2143 ), 2144 ); 48 function test_wp_filesize_with_nonexistent_file() { 49 $file = 'nonexistent/file.jpg'; 2145 50 2146 // Sort the array. 2147 wp_recursive_ksort( $theme_json ); 2148 2149 // Expected result. 2150 $expected_theme_json = array( 2151 'settings' => array( 2152 'color' => array( 2153 'palette' => array( 2154 array( 2155 'color' => '#242321', 2156 'name' => 'Foreground', 2157 'slug' => 'foreground', 2158 ), 2159 array( 2160 'color' => '#FCFBF8', 2161 'name' => 'Background', 2162 'slug' => 'background', 2163 ), 2164 array( 2165 'color' => '#71706E', 2166 'name' => 'Primary', 2167 'slug' => 'primary', 2168 ), 2169 array( 2170 'color' => '#CFCFCF', 2171 'name' => 'Tertiary', 2172 'slug' => 'tertiary', 2173 ), 2174 ), 2175 ), 2176 'typography' => array( 2177 'fontFamilies' => array( 2178 'fontFamily' => 'DM Sans, sans-serif', 2179 'name' => 'DM Sans', 2180 'slug' => 'dm-sans', 2181 ), 2182 ), 2183 ), 2184 'version' => 1, 2185 ); 2186 $this->assertSameSetsWithIndex( $theme_json, $expected_theme_json ); 51 $this->assertSame( 0, wp_filesize( $file ) ); 2187 52 } 2188 2189 53 }
Note: See TracChangeset
for help on using the changeset viewer.