SanitizeFileName.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase {
  6. function test_munges_extensions() {
  7. // r17990
  8. $file_name = sanitize_file_name( 'test.phtml.txt' );
  9. $this->assertSame( 'test.phtml_.txt', $file_name );
  10. }
  11. function test_removes_special_chars() {
  12. $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', '’', '«', '»', '”', '“', chr( 0 ) );
  13. $string = 'test';
  14. foreach ( $special_chars as $char ) {
  15. $string .= $char;
  16. }
  17. $string .= 'test';
  18. $this->assertSame( 'testtest', sanitize_file_name( $string ) );
  19. }
  20. /**
  21. * @ticket 22363
  22. */
  23. function test_removes_accents() {
  24. $in = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ';
  25. $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy';
  26. $this->assertSame( $out, sanitize_file_name( $in ) );
  27. }
  28. /**
  29. * Test that spaces are correctly replaced with dashes.
  30. *
  31. * @ticket 16330
  32. */
  33. function test_replaces_spaces() {
  34. $urls = array(
  35. 'unencoded space.png' => 'unencoded-space.png',
  36. 'encoded-space.jpg' => 'encoded-space.jpg',
  37. 'plus+space.jpg' => 'plusspace.jpg',
  38. 'multi %20 +space.png' => 'multi-20-space.png',
  39. );
  40. foreach ( $urls as $test => $expected ) {
  41. $this->assertSame( $expected, sanitize_file_name( $test ) );
  42. }
  43. }
  44. function test_replaces_any_number_of_hyphens_with_one_hyphen() {
  45. $this->assertSame( 'a-t-t', sanitize_file_name( 'a----t----t' ) );
  46. }
  47. function test_trims_trailing_hyphens() {
  48. $this->assertSame( 'a-t-t', sanitize_file_name( 'a----t----t----' ) );
  49. }
  50. function test_replaces_any_amount_of_whitespace_with_one_hyphen() {
  51. $this->assertSame( 'a-t', sanitize_file_name( 'a t' ) );
  52. $this->assertSame( 'a-t', sanitize_file_name( "a \n\n\nt" ) );
  53. }
  54. /**
  55. * @ticket 16226
  56. */
  57. function test_replaces_percent_sign() {
  58. $this->assertSame( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
  59. }
  60. function test_replaces_unnamed_file_extensions() {
  61. // Test filenames with both supported and unsupported extensions.
  62. $this->assertSame( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) );
  63. $this->assertSame( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) );
  64. }
  65. function test_replaces_unnamed_file_extensionless() {
  66. // Test a filenames that becomes extensionless.
  67. $this->assertSame( 'no-extension', sanitize_file_name( '_.no-extension' ) );
  68. }
  69. /**
  70. * @dataProvider data_wp_filenames
  71. */
  72. function test_replaces_invalid_utf8_characters( $input, $expected ) {
  73. $this->assertSame( $expected, sanitize_file_name( $input ) );
  74. }
  75. function data_wp_filenames() {
  76. return array(
  77. array( urldecode( '%B1myfile.png' ), 'myfile.png' ),
  78. array( urldecode( '%B1myfile' ), 'myfile' ),
  79. array( 'demo bar.png', 'demo-bar.png' ),
  80. array( 'demo' . json_decode( '"\u00a0"' ) . 'bar.png', 'demo-bar.png' ),
  81. );
  82. }
  83. }