WPSlash.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_WPSlash extends WP_UnitTestCase {
  6. /**
  7. * @ticket 42195
  8. *
  9. * @dataProvider data_wp_slash
  10. *
  11. * @param string $value
  12. * @param string $expected
  13. */
  14. public function test_wp_slash( $value, $expected ) {
  15. $this->assertSame( $expected, wp_slash( $value ) );
  16. }
  17. /**
  18. * Data provider for test_wp_slash().
  19. *
  20. * @return array {
  21. * @type array {
  22. * @type mixed $value The value passed to wp_slash().
  23. * @type string $expected The expected output of wp_slash().
  24. * }
  25. * }
  26. */
  27. public function data_wp_slash() {
  28. return array(
  29. array( 123, 123 ),
  30. array( 123.4, 123.4 ),
  31. array( true, true ),
  32. array( false, false ),
  33. array(
  34. array(
  35. 'hello',
  36. null,
  37. '"string"',
  38. 125.41,
  39. ),
  40. array(
  41. 'hello',
  42. null,
  43. '\"string\"',
  44. 125.41,
  45. ),
  46. ),
  47. array( "first level 'string'", "first level \'string\'" ),
  48. );
  49. }
  50. /**
  51. * @ticket 24106
  52. */
  53. function test_adds_slashes() {
  54. $old = "I can't see, isn't that it?";
  55. $new = "I can\'t see, isn\'t that it?";
  56. $this->assertSame( $new, wp_slash( $old ) );
  57. $this->assertSame( "I can\\\\\'t see, isn\\\\\'t that it?", wp_slash( $new ) );
  58. $this->assertSame( array( 'a' => $new ), wp_slash( array( 'a' => $old ) ) ); // Keyed array.
  59. $this->assertSame( array( $new ), wp_slash( array( $old ) ) ); // Non-keyed.
  60. }
  61. /**
  62. * @ticket 24106
  63. */
  64. function test_preserves_original_datatype() {
  65. $this->assertTrue( wp_slash( true ) );
  66. $this->assertFalse( wp_slash( false ) );
  67. $this->assertSame( 4, wp_slash( 4 ) );
  68. $this->assertSame( 'foo', wp_slash( 'foo' ) );
  69. $arr = array(
  70. 'a' => true,
  71. 'b' => false,
  72. 'c' => 4,
  73. 'd' => 'foo',
  74. );
  75. $arr['e'] = $arr; // Add a sub-array.
  76. $this->assertSame( $arr, wp_slash( $arr ) ); // Keyed array.
  77. $this->assertSame( array_values( $arr ), wp_slash( array_values( $arr ) ) ); // Non-keyed.
  78. $obj = new stdClass;
  79. foreach ( $arr as $k => $v ) {
  80. $obj->$k = $v;
  81. }
  82. $this->assertSame( $obj, wp_slash( $obj ) );
  83. }
  84. /**
  85. * @ticket 24106
  86. */
  87. function test_add_even_more_slashes() {
  88. $old = 'single\\slash double\\\\slash triple\\\\\\slash';
  89. $new = 'single\\\\slash double\\\\\\\\slash triple\\\\\\\\\\\\slash';
  90. $this->assertSame( $new, wp_slash( $old ) );
  91. $this->assertSame( array( 'a' => $new ), wp_slash( array( 'a' => $old ) ) ); // Keyed array.
  92. $this->assertSame( array( $new ), wp_slash( array( $old ) ) ); // Non-keyed.
  93. }
  94. }