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