StripSlashesDeep.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @group formatting
  4. * @group slashes
  5. */
  6. class Tests_Formatting_StripSlashesDeep extends WP_UnitTestCase {
  7. /**
  8. * @ticket 18026
  9. */
  10. function test_preserves_original_datatype() {
  11. $this->assertTrue( stripslashes_deep( true ) );
  12. $this->assertFalse( stripslashes_deep( false ) );
  13. $this->assertSame( 4, stripslashes_deep( 4 ) );
  14. $this->assertSame( 'foo', stripslashes_deep( 'foo' ) );
  15. $arr = array(
  16. 'a' => true,
  17. 'b' => false,
  18. 'c' => 4,
  19. 'd' => 'foo',
  20. );
  21. $arr['e'] = $arr; // Add a sub-array.
  22. $this->assertSame( $arr, stripslashes_deep( $arr ) ); // Keyed array.
  23. $this->assertSame( array_values( $arr ), stripslashes_deep( array_values( $arr ) ) ); // Non-keyed.
  24. $obj = new stdClass;
  25. foreach ( $arr as $k => $v ) {
  26. $obj->$k = $v;
  27. }
  28. $this->assertSame( $obj, stripslashes_deep( $obj ) );
  29. }
  30. function test_strips_slashes() {
  31. $old = "I can\'t see, isn\'t that it?";
  32. $new = "I can't see, isn't that it?";
  33. $this->assertSame( $new, stripslashes_deep( $old ) );
  34. $this->assertSame( $new, stripslashes_deep( "I can\\'t see, isn\\'t that it?" ) );
  35. $this->assertSame( array( 'a' => $new ), stripslashes_deep( array( 'a' => $old ) ) ); // Keyed array.
  36. $this->assertSame( array( $new ), stripslashes_deep( array( $old ) ) ); // Non-keyed.
  37. $obj_old = new stdClass;
  38. $obj_old->a = $old;
  39. $obj_new = new stdClass;
  40. $obj_new->a = $new;
  41. $this->assertEquals( $obj_new, stripslashes_deep( $obj_old ) );
  42. }
  43. function test_permits_escaped_slash() {
  44. $txt = "I can't see, isn\'t that it?";
  45. $this->assertSame( $txt, stripslashes_deep( "I can\'t see, isn\\\'t that it?" ) );
  46. $this->assertSame( $txt, stripslashes_deep( "I can\'t see, isn\\\\\'t that it?" ) );
  47. }
  48. }