create-block-theme/tests/CbtThemeLocale/escapeTextContent.php
Jeff Ong 512b9f80df
Process inner html of blocks when escaping text content (#719)
* WIP working to convert attributes and URLs.

* Ensure media is added to local.

* Use a list of tokens and sprintf to generate the formatted string.

* Only format the string if tokens are present.

* Update content with html test.

Update remaining tests with inner markup.

Try and fix tests.

Format tests.

* Remove whitespace.

* Process all attributes
Escape at the end.
Provide better translation note.

* Refactor token processing to its own class.

* Update tests with string replacements and translation.

* Check if % exists in the text and escape it.

* Add a test case for a localizing text that includes a %.

* Add new line at the end of translators note

* Reformat numbers in translators note

* Update test

* Move new line to theme-locale

* Handle self closing tags

* Refactor how new line is added

* Attempt to fix tests

* Attempt to fix tests again

---------

Co-authored-by: Grant Kinney <1699996+creativecoder@users.noreply.github.com>
Co-authored-by: Sarah Norris <sarah@sekai.co.uk>
Co-authored-by: Matias Benedetto <matias.benedetto@gmail.com>
2024-10-23 14:06:06 -03:00

57 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/base.php';
/**
* Tests for the CBT_Theme_Locale::escape_text_content method.
*
* @package Create_Block_Theme
* @covers CBT_Theme_Locale::escape_text_content
* @group locale
*/
class CBT_Theme_Locale_EscapeTextContent extends CBT_Theme_Locale_UnitTestCase {
protected function call_private_method( $method_name, $args = array() ) {
$reflection = new ReflectionClass( 'CBT_Theme_Locale' );
$method = $reflection->getMethod( $method_name );
$method->setAccessible( true );
return $method->invokeArgs( null, $args );
}
public function test_escape_text_content() {
$string = 'This is a test text.';
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$this->assertEquals( "<?php esc_html_e('This is a test text.', 'test-locale-theme');?>", $escaped_string );
}
public function test_escape_text_content_with_single_quote() {
$string = "This is a test text with a single quote '";
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$this->assertEquals( "<?php esc_html_e('This is a test text with a single quote \\'', 'test-locale-theme');?>", $escaped_string );
}
public function test_escape_text_content_with_double_quote() {
$string = 'This is a test text with a double quote "';
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$this->assertEquals( "<?php esc_html_e('This is a test text with a double quote \"', 'test-locale-theme');?>", $escaped_string );
}
public function test_escape_text_content_with_html() {
$string = '<p>This is a test text with HTML.</p>';
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$expected_output = '<?php /* Translators: 1. is the start of a \'p\' HTML element, 2. is the end of a \'p\' HTML element */' . " \n" . 'echo sprintf( esc_html__( \'%1$sThis is a test text with HTML.%2$s\', \'test-locale-theme\' ), \'<p>\', \'</p>\' ); ?>';
$this->assertEquals( $expected_output, $escaped_string );
}
public function test_escape_text_content_with_already_escaped_string() {
$string = "<?php esc_html_e('This is a test text.', 'test-locale-theme');?>";
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$this->assertEquals( $string, $escaped_string );
}
public function test_escape_text_content_with_non_string() {
$string = null;
$escaped_string = $this->call_private_method( 'escape_text_content', array( $string ) );
$this->assertEquals( $string, $escaped_string );
}
}