|
| 1 | +<?php |
| 2 | +require_once __DIR__ . '/base.php'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Tests for the CBT_Theme_Locale::escape_attribute method. |
| 6 | + * |
| 7 | + * @package Create_Block_Theme |
| 8 | + * @covers CBT_Theme_Locale::escape_attribute |
| 9 | + * @group locale |
| 10 | + */ |
| 11 | +class CBT_Theme_Locale_EscapeAttribute extends CBT_Theme_Locale_UnitTestCase { |
| 12 | + |
| 13 | + protected function call_private_method( $method_name, $args = array() ) { |
| 14 | + $reflection = new ReflectionClass( 'CBT_Theme_Locale' ); |
| 15 | + $method = $reflection->getMethod( $method_name ); |
| 16 | + $method->setAccessible( true ); |
| 17 | + return $method->invokeArgs( null, $args ); |
| 18 | + } |
| 19 | + |
| 20 | + public function test_escape_attribute() { |
| 21 | + $string = 'This is a test attribute.'; |
| 22 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 23 | + $expected_string = "<?php esc_attr_e('This is a test attribute.', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; |
| 24 | + $this->assertEquals( $expected_string, $escaped_string ); |
| 25 | + } |
| 26 | + |
| 27 | + public function test_escape_attribute_with_single_quote() { |
| 28 | + $string = "This is a test attribute with a single quote '"; |
| 29 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 30 | + $expected_string = "<?php esc_attr_e('This is a test attribute with a single quote \\'', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; |
| 31 | + $this->assertEquals( $expected_string, $escaped_string ); |
| 32 | + } |
| 33 | + |
| 34 | + public function test_escape_attribute_with_double_quote() { |
| 35 | + $string = 'This is a test attribute with a double quote "'; |
| 36 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 37 | + $expected_string = "<?php esc_attr_e('This is a test attribute with a double quote \"', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; |
| 38 | + $this->assertEquals( $expected_string, $escaped_string ); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_escape_attribute_with_empty_string() { |
| 42 | + $string = ''; |
| 43 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 44 | + $this->assertEquals( $string, $escaped_string ); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_escape_attribute_with_already_escaped_string() { |
| 48 | + $string = "<?php esc_attr_e('This is already escaped.', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; |
| 49 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 50 | + $this->assertEquals( $string, $escaped_string ); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_escape_attribute_with_non_string() { |
| 54 | + $string = null; |
| 55 | + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); |
| 56 | + $this->assertEquals( $string, $escaped_string ); |
| 57 | + } |
| 58 | +} |
0 commit comments