From 3f5712a450a54ab184f526db7ae01595c59a2ac0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Wed, 3 Jul 2024 19:41:21 +0400 Subject: [PATCH 1/7] Update escaping function --- includes/create-theme/theme-locale.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 343b855e..1b4f0937 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -22,7 +22,7 @@ public static function escape_string( $string ) { } $string = addcslashes( $string, "'" ); - return "<?php esc_html_e('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; + return "<?php echo wp_kses_post( __('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "') );?>"; } /** From dcd1fc3627106fde283edba8a6b7c5bafd398618 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Wed, 3 Jul 2024 19:42:38 +0400 Subject: [PATCH 2/7] update tests --- tests/CbtThemeLocale/escapeString.php | 11 ++-- .../escapeTextContentOfBlocks.php | 36 ++++++------- tests/test-theme-templates.php | 50 +++++++++---------- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/tests/CbtThemeLocale/escapeString.php b/tests/CbtThemeLocale/escapeString.php index b9ff7361..0daedc61 100644 --- a/tests/CbtThemeLocale/escapeString.php +++ b/tests/CbtThemeLocale/escapeString.php @@ -13,29 +13,29 @@ class CBT_Theme_Locale_EscapeString extends CBT_Theme_Locale_UnitTestCase { public function test_escape_string() { $string = 'This is a test text.'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php esc_html_e('This is a test text.', 'test-locale-theme');?>", $escaped_string ); + $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text.', 'test-locale-theme') );?>", $escaped_string ); } public function test_escape_string_with_single_quote() { $string = "This is a test text with a single quote '"; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php esc_html_e('This is a test text with a single quote \\'', 'test-locale-theme');?>", $escaped_string ); + $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text with a single quote \\'', 'test-locale-theme') );?>", $escaped_string ); } public function test_escape_string_with_double_quote() { $string = 'This is a test text with a double quote "'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php esc_html_e('This is a test text with a double quote \"', 'test-locale-theme');?>", $escaped_string ); + $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text with a double quote \"', 'test-locale-theme') );?>", $escaped_string ); } public function test_escape_string_with_html() { $string = '<p>This is a test text with HTML.</p>'; $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php esc_html_e('<p>This is a test text with HTML.</p>', 'test-locale-theme');?>", $escaped_string ); + $this->assertEquals( "<?php echo wp_kses_post( __('<p>This is a test text with HTML.</p>', 'test-locale-theme') );?>", $escaped_string ); } public function test_escape_string_with_already_escaped_string() { - $string = "<?php esc_html_e('This is a test text.', 'test-locale-theme');?>"; + $string = "<?php echo wp_kses_post( __('This is a test text.', 'test-locale-theme') );?>"; $escaped_string = CBT_Theme_Locale::escape_string( $string ); $this->assertEquals( $string, $escaped_string ); } @@ -46,3 +46,4 @@ public function test_escape_string_with_non_string() { $this->assertEquals( $string, $escaped_string ); } } + diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 7df7b62f..9c279c85 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -30,7 +30,7 @@ public function data_test_escape_text_content_of_blocks() { 'paragraph' => array( 'block_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">This is a test text.</p><!-- /wp:paragraph -->', - 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php esc_html_e(\'This is a test text.\', \'test-locale-theme\');?></p><!-- /wp:paragraph -->', + 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php echo wp_kses_post( __(\'This is a test text.\', \'test-locale-theme\') );?></p><!-- /wp:paragraph -->', ), 'paragraph on nested groups' => array( @@ -46,7 +46,7 @@ public function data_test_escape_text_content_of_blocks() { '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|50","left":"var:preset|spacing|50","right":"var:preset|spacing|50"}}},"layout":{"type":"constrained","contentSize":"","wideSize":""}} --> <div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--50);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)"><!-- wp:group {"style":{"spacing":{"blockGap":"0px"}},"layout":{"type":"constrained","contentSize":"565px"}} --> <div class="wp-block-group"><!-- wp:paragraph {"align":"center"} --> - <p class="has-text-align-center"><?php esc_html_e(\'This is a test text.\', \'test-locale-theme\');?></p> + <p class="has-text-align-center"><?php echo wp_kses_post( __(\'This is a test text.\', \'test-locale-theme\') );?></p> <!-- /wp:paragraph --></div> <!-- /wp:group --></div> <!-- /wp:group -->', @@ -59,7 +59,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e(\'A passion for creating spaces\', \'test-locale-theme\');?></h1> + <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php echo wp_kses_post( __(\'A passion for creating spaces\', \'test-locale-theme\') );?></h1> <!-- /wp:heading -->', ), @@ -70,7 +70,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e(\'A passion for creating spaces\', \'test-locale-theme\');?></h2> + <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php echo wp_kses_post( __(\'A passion for creating spaces\', \'test-locale-theme\') );?></h2> <!-- /wp:heading -->', ), @@ -90,13 +90,13 @@ public function data_test_escape_text_content_of_blocks() { 'expected_markup' => '<!-- wp:list {"style":{"typography":{"lineHeight":"1.75"}},"className":"is-style-checkmark-list"} --> <ul style="line-height:1.75" class="is-style-checkmark-list"><!-- wp:list-item --> - <li><?php esc_html_e(\'Collaborate with fellow architects.\', \'test-locale-theme\');?></li> + <li><?php echo wp_kses_post( __(\'Collaborate with fellow architects.\', \'test-locale-theme\') );?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php esc_html_e(\'Showcase your projects.\', \'test-locale-theme\');?></li> + <li><?php echo wp_kses_post( __(\'Showcase your projects.\', \'test-locale-theme\') );?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php esc_html_e(\'Experience the world of architecture.\', \'test-locale-theme\');?></li> + <li><?php echo wp_kses_post( __(\'Experience the world of architecture.\', \'test-locale-theme\') );?></li> <!-- /wp:list-item --></ul> <!-- /wp:list -->', ), @@ -108,7 +108,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:verse -->', 'expected_markup' => '<!-- wp:verse {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} --> - <pre class="wp-block-verse"><?php esc_html_e(\'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\');?></pre> + <pre class="wp-block-verse"><?php echo wp_kses_post( __(\'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\') );?></pre> <!-- /wp:verse -->', ), @@ -119,7 +119,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:button -->', 'expected_markup' => '<!-- wp:button --> - <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php esc_html_e(\'Sign up\', \'test-locale-theme\');?></a></div> + <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php echo wp_kses_post( __(\'Sign up\', \'test-locale-theme\') );?></a></div> <!-- /wp:button -->', ), @@ -130,7 +130,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:image -->', 'expected_markup' => '<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} --> - <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_html_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>"/></figure> + <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php echo wp_kses_post( __(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\') );?>"/></figure> <!-- /wp:image -->', ), @@ -143,8 +143,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:cover -->', 'expected_markup' => '<!-- wp:cover {"url":"http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg","id":39,"alt":"Alternative text for cover image","dimRatio":50,"customOverlayColor":"#1d2b2f","layout":{"type":"constrained"}} --> - <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_html_e(\'Alternative text for cover image\', \'test-locale-theme\');?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> - <p class="has-text-align-center has-large-font-size"><?php esc_html_e(\'This is a cover caption\', \'test-locale-theme\');?></p> + <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php echo wp_kses_post( __(\'Alternative text for cover image\', \'test-locale-theme\') );?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> + <p class="has-text-align-center has-large-font-size"><?php echo wp_kses_post( __(\'This is a cover caption\', \'test-locale-theme\') );?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:cover -->', ), @@ -158,8 +158,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:media-text -->', 'expected_markup' => '<!-- wp:media-text {"mediaId":39,"mediaLink":"http://localhost/wp1/image/","mediaType":"image"} --> - <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_html_e(\'This is alt text\', \'test-locale-theme\');?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> - <p><?php esc_html_e(\'Media text content test.\', \'test-locale-theme\');?></p> + <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php echo wp_kses_post( __(\'This is alt text\', \'test-locale-theme\') );?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> + <p><?php echo wp_kses_post( __(\'Media text content test.\', \'test-locale-theme\') );?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:media-text -->', ), @@ -171,7 +171,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:pullquote -->', 'expected_markup' => '<!-- wp:pullquote --> - <figure class="wp-block-pullquote"><blockquote><p><?php esc_html_e(\'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\');?></p><cite><?php esc_html_e(\'Diego Armando Maradona\', \'test-locale-theme\');?></cite></blockquote></figure> + <figure class="wp-block-pullquote"><blockquote><p><?php echo wp_kses_post( __(\'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\') );?></p><cite><?php echo wp_kses_post( __(\'Diego Armando Maradona\', \'test-locale-theme\') );?></cite></blockquote></figure> <!-- /wp:pullquote -->', ), @@ -182,14 +182,10 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:table -->', 'expected_markup' => '<!-- wp:table --> - <figure class="wp-block-table"><table><tbody><tr><td><?php esc_html_e(\'Team\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'Points\', \'test-locale-theme\');?></td></tr><tr><td><?php esc_html_e(\'Boca\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'74\', \'test-locale-theme\');?></td></tr><tr><td><?php esc_html_e(\'River\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'2\', \'test-locale-theme\');?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php esc_html_e(\'Score table\', \'test-locale-theme\');?></figcaption></figure> + <figure class="wp-block-table"><table><tbody><tr><td><?php echo wp_kses_post( __(\'Team\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'Points\', \'test-locale-theme\') );?></td></tr><tr><td><?php echo wp_kses_post( __(\'Boca\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'74\', \'test-locale-theme\') );?></td></tr><tr><td><?php echo wp_kses_post( __(\'River\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'2\', \'test-locale-theme\') );?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php echo wp_kses_post( __(\'Score table\', \'test-locale-theme\') );?></figcaption></figure> <!-- /wp:table -->', ), ); } } - - - - diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 8ed6f10b..c4be5d49 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -6,13 +6,13 @@ class Test_Create_Block_Theme_Templates extends WP_UnitTestCase { /** - * Ensure that the string in a template is replaced with the appropraite PHP code + * Ensure that the string in a template is replaced with the appropriate PHP code */ public function test_paragraphs_are_localized() { $template = new stdClass(); $template->content = '<!-- wp:paragraph --><p>This is text to localize</p><!-- /wp:paragraph -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<p><?php esc_html_e('This is text to localize', '');?></p>", $new_template->content ); + $this->assertStringContainsString( "<p><?php echo wp_kses_post( __('This is text to localize', '') );?></p>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } @@ -37,7 +37,7 @@ public function test_paragraphs_in_groups_are_localized() { </div> <!-- /wp:group -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'This is text to localize', $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is text to localize', '') );?>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } @@ -49,7 +49,7 @@ public function test_buttons_are_localized() { </div> <!-- /wp:button -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'This is text to localize', $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is text to localize', '') );?>", $new_template->content ); $this->assertStringNotContainsString( '<a class="wp-block-button__link wp-element-button">This is text to localize</a>', $new_template->content ); } @@ -61,7 +61,7 @@ public function test_headings_are_localized() { <!-- /wp:heading --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'This is a heading to localize.', $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is a heading to localize.', '') );?>", $new_template->content ); $this->assertStringNotContainsString( '<h2 class="wp-block-heading">This is a heading to localize.</h2>', $new_template->content ); } @@ -135,8 +135,8 @@ public function test_properly_encode_quotes_and_doublequotes() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - /* That looks like a mess, but what it should look like for REAL is <?php echo esc_attr_e( '"This" is a \'test\'', '' ); ?> */ - $this->assertStringContainsString( '<?php esc_html_e(\'"This" is a \\\'test\\\'\', \'\');?>', $escaped_template->content ); + /* That looks like a mess, but what it should look like for REAL is <?php echo wp_kses_post( __('"This" is a \'test\'', '' ); ?> */ + $this->assertStringContainsString( "<?php echo wp_kses_post( __('\"This\" is a \\'test\\'', '') );?>", $escaped_template->content ); } public function test_properly_encode_lessthan_and_greaterthan() { @@ -146,7 +146,7 @@ public function test_properly_encode_lessthan_and_greaterthan() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( '<?php esc_html_e(\'<This> is a <test>\', \'\');?>', $escaped_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('<This> is a <test>', '') );?>", $escaped_template->content ); } public function test_properly_encode_html_markup() { @@ -156,7 +156,7 @@ public function test_properly_encode_html_markup() { <!-- /wp:paragraph -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( '<?php esc_html_e(\'<strong>Bold</strong> text has feelings <> TOO\', \'\');?>', $escaped_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('<strong>Bold</strong> text has feelings <> TOO', '') );?>", $escaped_template->content ); } public function test_localize_alt_text_from_image() { @@ -167,7 +167,7 @@ public function test_localize_alt_text_from_image() { <!-- /wp:image --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'alt="<?php esc_html_e(\'This is alt text\', \'\');?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>"', $new_template->content ); } public function test_localize_alt_text_from_cover() { @@ -176,7 +176,7 @@ public function test_localize_alt_text_from_cover() { <!-- wp:cover {"url":"http://example.com/file.jpg","alt":"This is alt text"} --> <div class="wp-block-cover"> <span aria-hidden="true" class="wp-block-cover__background"></span> - <img class="wp-block-cover__image-background" alt="This is alt text" src="http://example.com/file.jpg" data-object-fit="cover"/> + <img class="wp-block-cover__image-background" alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>" src="http://example.com/file.jpg" data-object-fit="cover"/> <div class="wp-block-cover__inner-container"> <!-- wp:paragraph --> <p></p> @@ -187,7 +187,7 @@ public function test_localize_alt_text_from_cover() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); // Check the markup attribute - $this->assertStringContainsString( 'alt="<?php esc_html_e(\'This is alt text\', \'\');?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>"', $new_template->content ); } public function test_localize_quote() { @@ -201,8 +201,8 @@ public function test_localize_quote() { </blockquote> <!-- /wp:quote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e('This is my Quote', '');?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('Citation too', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my Quote', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Citation too', '') );?>", $new_template->content ); } public function test_localize_pullquote() { @@ -216,8 +216,8 @@ public function test_localize_pullquote() { </figure> <!-- /wp:pullquote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e('This is my Quote', '');?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('Citation too', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my Quote', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Citation too', '') );?>", $new_template->content ); } public function test_localize_list() { @@ -234,7 +234,7 @@ public function test_localize_list() { </ul> <!-- /wp:list -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<li><?php esc_html_e('Item One', '');?></li>", $new_template->content ); + $this->assertStringContainsString( "<li><?php echo wp_kses_post( __('Item One', '') );?></li>", $new_template->content ); } public function test_localize_verse() { @@ -243,7 +243,7 @@ public function test_localize_verse() { <pre class="wp-block-verse">Here is some <strong>verse</strong> to localize</pre> <!-- /wp:verse -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e('Here is some <strong>verse</strong> to localize', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Here is some <strong>verse</strong> to localize', '') );?>", $new_template->content ); } public function test_localize_table() { @@ -274,10 +274,10 @@ public function test_localize_table() { </figure> <!-- /wp:table -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<td><?php esc_html_e('Apples', '');?></td>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('Header One', '');?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('Footer One', '');?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('This is my caption', '');?>", $new_template->content ); + $this->assertStringContainsString( "<td><?php echo wp_kses_post( __('Apples', '') );?></td>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Header One', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Footer One', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my caption', '') );?>", $new_template->content ); } public function test_localize_media_text() { @@ -295,8 +295,8 @@ public function test_localize_media_text() { </div> <!-- /wp:media-text -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e('Content to Localize', '');?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e('Alt Text Is Here', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Content to Localize', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __('Alt Text Is Here', '') );?>", $new_template->content ); } public function test_localize_cover_block_children() { @@ -314,7 +314,7 @@ public function test_localize_cover_block_children() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( '<p><?php esc_html_e(\'This is text to localize\', \'\');?></p>', $new_template->content ); + $this->assertStringContainsString( "<p><?php echo wp_kses_post( __('This is text to localize', '') );?></p>", $new_template->content ); } public function test_localize_nested_cover_block_children() { From 0bc416393709dabeee8f9999476162fa839abaf5 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Thu, 4 Jul 2024 13:08:04 +0400 Subject: [PATCH 3/7] add method specific for attributes --- includes/create-theme/theme-locale.php | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 1b4f0937..3d0b3510 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -5,12 +5,12 @@ class CBT_Theme_Locale { /** - * Escape a string for localization. + * Escape text for localization. * * @param string $string The string to escape. * @return string The escaped string. */ - public static function escape_string( $string ) { + private static function escape_text_content( $string ) { // Avoid escaping if the text is not a string. if ( ! is_string( $string ) ) { return $string; @@ -25,6 +25,27 @@ public static function escape_string( $string ) { return "<?php echo wp_kses_post( __('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "') );?>"; } + /** + * Escape an html element attribute for localization. + * + * @param string $string The string to escape. + * @return string The escaped string. + */ + private static function escape_attribute( $string ) { + // Avoid escaping if the text is not a string. + if ( ! is_string( $string ) ) { + return $string; + } + + // Check if the text is already escaped. + if ( str_starts_with( $string, '<?php' ) ) { + return $string; + } + + $string = addcslashes( $string, "'" ); + return "<?php esc_attr_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' );?>"; + } + /** * Get a replacement pattern for escaping the text from the html content of a block. * @@ -109,7 +130,7 @@ public static function escape_text_content_of_blocks( $blocks ) { return preg_replace_callback( $pattern, function( $matches ) { - return $matches[1] . self::escape_string( $matches[2] ) . $matches[3]; + return $matches[1] . self::escape_text_content( $matches[2] ) . $matches[3]; }, $content ); @@ -125,7 +146,7 @@ function( $matches ) { return preg_replace_callback( $pattern, function( $matches ) { - return 'alt="' . self::escape_string( $matches[1] ) . '"'; + return 'alt="' . self::escape_attribute( $matches[1] ) . '"'; }, $content ); From 86a18b2a340becda79adf3a52974f738aafe1ec8 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Thu, 4 Jul 2024 13:08:20 +0400 Subject: [PATCH 4/7] update tests --- includes/create-theme/theme-patterns.php | 2 +- tests/CbtThemeLocale/escapeString.php | 49 ------------------- .../escapeTextContentOfBlocks.php | 6 +-- tests/test-theme-templates.php | 8 +-- 4 files changed, 8 insertions(+), 57 deletions(-) delete mode 100644 tests/CbtThemeLocale/escapeString.php diff --git a/includes/create-theme/theme-patterns.php b/includes/create-theme/theme-patterns.php index e7de4890..5ff62fd6 100644 --- a/includes/create-theme/theme-patterns.php +++ b/includes/create-theme/theme-patterns.php @@ -38,7 +38,7 @@ public static function escape_alt_for_pattern( $html ) { public static function escape_text_for_pattern( $text ) { if ( $text && trim( $text ) !== '' ) { $escaped_text = addslashes( $text ); - return "<?php echo esc_attr_e( '" . $escaped_text . "', '" . wp_get_theme()->get( 'Name' ) . "' ); ?>"; + return "<?php esc_attr_e( '" . $escaped_text . "', '" . wp_get_theme()->get( 'Name' ) . "' ); ?>"; } } diff --git a/tests/CbtThemeLocale/escapeString.php b/tests/CbtThemeLocale/escapeString.php deleted file mode 100644 index 0daedc61..00000000 --- a/tests/CbtThemeLocale/escapeString.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php - -require_once __DIR__ . '/base.php'; - -/** - * Tests for the CBT_Theme_Locale::escape_string method. - * - * @package Create_Block_Theme - * @covers CBT_Theme_Locale::escape_string - * @group locale - */ -class CBT_Theme_Locale_EscapeString extends CBT_Theme_Locale_UnitTestCase { - public function test_escape_string() { - $string = 'This is a test text.'; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text.', 'test-locale-theme') );?>", $escaped_string ); - } - - public function test_escape_string_with_single_quote() { - $string = "This is a test text with a single quote '"; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text with a single quote \\'', 'test-locale-theme') );?>", $escaped_string ); - } - - public function test_escape_string_with_double_quote() { - $string = 'This is a test text with a double quote "'; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php echo wp_kses_post( __('This is a test text with a double quote \"', 'test-locale-theme') );?>", $escaped_string ); - } - - public function test_escape_string_with_html() { - $string = '<p>This is a test text with HTML.</p>'; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( "<?php echo wp_kses_post( __('<p>This is a test text with HTML.</p>', 'test-locale-theme') );?>", $escaped_string ); - } - - public function test_escape_string_with_already_escaped_string() { - $string = "<?php echo wp_kses_post( __('This is a test text.', 'test-locale-theme') );?>"; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( $string, $escaped_string ); - } - - public function test_escape_string_with_non_string() { - $string = null; - $escaped_string = CBT_Theme_Locale::escape_string( $string ); - $this->assertEquals( $string, $escaped_string ); - } -} - diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 9c279c85..59d86e44 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -130,7 +130,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:image -->', 'expected_markup' => '<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} --> - <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php echo wp_kses_post( __(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\') );?>"/></figure> + <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e( \'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\' );?>"/></figure> <!-- /wp:image -->', ), @@ -143,7 +143,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:cover -->', 'expected_markup' => '<!-- wp:cover {"url":"http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg","id":39,"alt":"Alternative text for cover image","dimRatio":50,"customOverlayColor":"#1d2b2f","layout":{"type":"constrained"}} --> - <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php echo wp_kses_post( __(\'Alternative text for cover image\', \'test-locale-theme\') );?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> + <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_attr_e( \'Alternative text for cover image\', \'test-locale-theme\' );?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> <p class="has-text-align-center has-large-font-size"><?php echo wp_kses_post( __(\'This is a cover caption\', \'test-locale-theme\') );?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:cover -->', @@ -158,7 +158,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:media-text -->', 'expected_markup' => '<!-- wp:media-text {"mediaId":39,"mediaLink":"http://localhost/wp1/image/","mediaType":"image"} --> - <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php echo wp_kses_post( __(\'This is alt text\', \'test-locale-theme\') );?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> + <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_attr_e( \'This is alt text\', \'test-locale-theme\' );?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> <p><?php echo wp_kses_post( __(\'Media text content test.\', \'test-locale-theme\') );?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:media-text -->', diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index c4be5d49..2f48d9f5 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -167,7 +167,7 @@ public function test_localize_alt_text_from_image() { <!-- /wp:image --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' );?>"', $new_template->content ); } public function test_localize_alt_text_from_cover() { @@ -176,7 +176,7 @@ public function test_localize_alt_text_from_cover() { <!-- wp:cover {"url":"http://example.com/file.jpg","alt":"This is alt text"} --> <div class="wp-block-cover"> <span aria-hidden="true" class="wp-block-cover__background"></span> - <img class="wp-block-cover__image-background" alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>" src="http://example.com/file.jpg" data-object-fit="cover"/> + <img class="wp-block-cover__image-background" alt="This is alt text" src="http://example.com/file.jpg" data-object-fit="cover"/> <div class="wp-block-cover__inner-container"> <!-- wp:paragraph --> <p></p> @@ -187,7 +187,7 @@ public function test_localize_alt_text_from_cover() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); // Check the markup attribute - $this->assertStringContainsString( 'alt="<?php echo wp_kses_post( __(\'This is alt text\', \'\') );?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' );?>"', $new_template->content ); } public function test_localize_quote() { @@ -296,7 +296,7 @@ public function test_localize_media_text() { <!-- /wp:media-text -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); $this->assertStringContainsString( "<?php echo wp_kses_post( __('Content to Localize', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Alt Text Is Here', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_attr_e( 'Alt Text Is Here', '' );?>", $new_template->content ); } public function test_localize_cover_block_children() { From 3e53a9aa90c378d4e4a9d8424c3dc9248c804832 Mon Sep 17 00:00:00 2001 From: Grant Kinney <hi@grant.mk> Date: Mon, 8 Jul 2024 14:05:09 -0500 Subject: [PATCH 5/7] Updates to only use wp_kses_post when strings contain html --- includes/create-theme/theme-locale.php | 20 +++++- .../escapeTextContentOfBlocks.php | 32 +++++----- tests/test-theme-templates.php | 63 ++++++++++++------- 3 files changed, 75 insertions(+), 40 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 3d0b3510..857a5745 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -16,13 +16,24 @@ private static function escape_text_content( $string ) { return $string; } + // Check if string is empty. + if ( '' === $string ) { + return $string; + } + // Check if the text is already escaped. if ( str_starts_with( $string, '<?php' ) ) { return $string; } $string = addcslashes( $string, "'" ); - return "<?php echo wp_kses_post( __('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "') );?>"; + + // Check if string contains html. + if ( strip_tags( $string ) !== $string ) { + return "<?php echo wp_kses_post( __( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ) ); ?>"; + } + + return "<?php esc_html_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ); ?>"; } /** @@ -37,13 +48,18 @@ private static function escape_attribute( $string ) { return $string; } + // Check if string is empty. + if ( '' === $string ) { + return $string; + } + // Check if the text is already escaped. if ( str_starts_with( $string, '<?php' ) ) { return $string; } $string = addcslashes( $string, "'" ); - return "<?php esc_attr_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' );?>"; + return "<?php esc_attr_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ); ?>"; } /** diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 59d86e44..7ffb7c28 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -30,7 +30,7 @@ public function data_test_escape_text_content_of_blocks() { 'paragraph' => array( 'block_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">This is a test text.</p><!-- /wp:paragraph -->', - 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php echo wp_kses_post( __(\'This is a test text.\', \'test-locale-theme\') );?></p><!-- /wp:paragraph -->', + 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php esc_html_e( \'This is a test text.\', \'test-locale-theme\' ); ?></p><!-- /wp:paragraph -->', ), 'paragraph on nested groups' => array( @@ -46,7 +46,7 @@ public function data_test_escape_text_content_of_blocks() { '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|50","left":"var:preset|spacing|50","right":"var:preset|spacing|50"}}},"layout":{"type":"constrained","contentSize":"","wideSize":""}} --> <div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--50);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)"><!-- wp:group {"style":{"spacing":{"blockGap":"0px"}},"layout":{"type":"constrained","contentSize":"565px"}} --> <div class="wp-block-group"><!-- wp:paragraph {"align":"center"} --> - <p class="has-text-align-center"><?php echo wp_kses_post( __(\'This is a test text.\', \'test-locale-theme\') );?></p> + <p class="has-text-align-center"><?php esc_html_e( \'This is a test text.\', \'test-locale-theme\' ); ?></p> <!-- /wp:paragraph --></div> <!-- /wp:group --></div> <!-- /wp:group -->', @@ -59,7 +59,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php echo wp_kses_post( __(\'A passion for creating spaces\', \'test-locale-theme\') );?></h1> + <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e( \'A passion for creating spaces\', \'test-locale-theme\' ); ?></h1> <!-- /wp:heading -->', ), @@ -70,7 +70,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php echo wp_kses_post( __(\'A passion for creating spaces\', \'test-locale-theme\') );?></h2> + <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e( \'A passion for creating spaces\', \'test-locale-theme\' ); ?></h2> <!-- /wp:heading -->', ), @@ -90,13 +90,13 @@ public function data_test_escape_text_content_of_blocks() { 'expected_markup' => '<!-- wp:list {"style":{"typography":{"lineHeight":"1.75"}},"className":"is-style-checkmark-list"} --> <ul style="line-height:1.75" class="is-style-checkmark-list"><!-- wp:list-item --> - <li><?php echo wp_kses_post( __(\'Collaborate with fellow architects.\', \'test-locale-theme\') );?></li> + <li><?php esc_html_e( \'Collaborate with fellow architects.\', \'test-locale-theme\' ); ?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php echo wp_kses_post( __(\'Showcase your projects.\', \'test-locale-theme\') );?></li> + <li><?php esc_html_e( \'Showcase your projects.\', \'test-locale-theme\' ); ?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php echo wp_kses_post( __(\'Experience the world of architecture.\', \'test-locale-theme\') );?></li> + <li><?php esc_html_e( \'Experience the world of architecture.\', \'test-locale-theme\' ); ?></li> <!-- /wp:list-item --></ul> <!-- /wp:list -->', ), @@ -108,7 +108,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:verse -->', 'expected_markup' => '<!-- wp:verse {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} --> - <pre class="wp-block-verse"><?php echo wp_kses_post( __(\'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\') );?></pre> + <pre class="wp-block-verse"><?php echo wp_kses_post( __( \'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\' ) ); ?></pre> <!-- /wp:verse -->', ), @@ -119,7 +119,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:button -->', 'expected_markup' => '<!-- wp:button --> - <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php echo wp_kses_post( __(\'Sign up\', \'test-locale-theme\') );?></a></div> + <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php esc_html_e( \'Sign up\', \'test-locale-theme\' ); ?></a></div> <!-- /wp:button -->', ), @@ -130,7 +130,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:image -->', 'expected_markup' => '<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} --> - <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e( \'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\' );?>"/></figure> + <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e( \'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\' ); ?>"/></figure> <!-- /wp:image -->', ), @@ -143,8 +143,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:cover -->', 'expected_markup' => '<!-- wp:cover {"url":"http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg","id":39,"alt":"Alternative text for cover image","dimRatio":50,"customOverlayColor":"#1d2b2f","layout":{"type":"constrained"}} --> - <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_attr_e( \'Alternative text for cover image\', \'test-locale-theme\' );?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> - <p class="has-text-align-center has-large-font-size"><?php echo wp_kses_post( __(\'This is a cover caption\', \'test-locale-theme\') );?></p> + <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_attr_e( \'Alternative text for cover image\', \'test-locale-theme\' ); ?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> + <p class="has-text-align-center has-large-font-size"><?php esc_html_e( \'This is a cover caption\', \'test-locale-theme\' ); ?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:cover -->', ), @@ -158,8 +158,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:media-text -->', 'expected_markup' => '<!-- wp:media-text {"mediaId":39,"mediaLink":"http://localhost/wp1/image/","mediaType":"image"} --> - <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_attr_e( \'This is alt text\', \'test-locale-theme\' );?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> - <p><?php echo wp_kses_post( __(\'Media text content test.\', \'test-locale-theme\') );?></p> + <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_attr_e( \'This is alt text\', \'test-locale-theme\' ); ?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> + <p><?php esc_html_e( \'Media text content test.\', \'test-locale-theme\' ); ?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:media-text -->', ), @@ -171,7 +171,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:pullquote -->', 'expected_markup' => '<!-- wp:pullquote --> - <figure class="wp-block-pullquote"><blockquote><p><?php echo wp_kses_post( __(\'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\') );?></p><cite><?php echo wp_kses_post( __(\'Diego Armando Maradona\', \'test-locale-theme\') );?></cite></blockquote></figure> + <figure class="wp-block-pullquote"><blockquote><p><?php esc_html_e( \'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\' ); ?></p><cite><?php esc_html_e( \'Diego Armando Maradona\', \'test-locale-theme\' ); ?></cite></blockquote></figure> <!-- /wp:pullquote -->', ), @@ -182,7 +182,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:table -->', 'expected_markup' => '<!-- wp:table --> - <figure class="wp-block-table"><table><tbody><tr><td><?php echo wp_kses_post( __(\'Team\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'Points\', \'test-locale-theme\') );?></td></tr><tr><td><?php echo wp_kses_post( __(\'Boca\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'74\', \'test-locale-theme\') );?></td></tr><tr><td><?php echo wp_kses_post( __(\'River\', \'test-locale-theme\') );?></td><td><?php echo wp_kses_post( __(\'2\', \'test-locale-theme\') );?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php echo wp_kses_post( __(\'Score table\', \'test-locale-theme\') );?></figcaption></figure> + <figure class="wp-block-table"><table><tbody><tr><td><?php esc_html_e( \'Team\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'Points\', \'test-locale-theme\' ); ?></td></tr><tr><td><?php esc_html_e( \'Boca\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'74\', \'test-locale-theme\' ); ?></td></tr><tr><td><?php esc_html_e( \'River\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'2\', \'test-locale-theme\' ); ?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php esc_html_e( \'Score table\', \'test-locale-theme\' ); ?></figcaption></figure> <!-- /wp:table -->', ), diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 2f48d9f5..ce8fc79a 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -12,10 +12,18 @@ public function test_paragraphs_are_localized() { $template = new stdClass(); $template->content = '<!-- wp:paragraph --><p>This is text to localize</p><!-- /wp:paragraph -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<p><?php echo wp_kses_post( __('This is text to localize', '') );?></p>", $new_template->content ); + $this->assertStringContainsString( "<p><?php esc_html_e( 'This is text to localize', '' ); ?></p>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } + public function test_empty_paragraphs_are_not_localized() { + $template = new stdClass(); + $template->content = '<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->'; + $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); + $this->assertStringContainsString( '<p></p>', $new_template->content ); + $this->assertStringNotContainsString( 'esc_html_e', $new_template->content ); + } + /** * Ensure that escape_text_in_template is not called when the localizeText flag is set to false */ @@ -37,7 +45,7 @@ public function test_paragraphs_in_groups_are_localized() { </div> <!-- /wp:group -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is text to localize', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is text to localize', '' ); ?>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } @@ -49,7 +57,7 @@ public function test_buttons_are_localized() { </div> <!-- /wp:button -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is text to localize', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is text to localize', '' ); ?>", $new_template->content ); $this->assertStringNotContainsString( '<a class="wp-block-button__link wp-element-button">This is text to localize</a>', $new_template->content ); } @@ -61,7 +69,7 @@ public function test_headings_are_localized() { <!-- /wp:heading --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is a heading to localize.', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is a heading to localize.', '' ); ?>", $new_template->content ); $this->assertStringNotContainsString( '<h2 class="wp-block-heading">This is a heading to localize.</h2>', $new_template->content ); } @@ -136,7 +144,7 @@ public function test_properly_encode_quotes_and_doublequotes() { $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); /* That looks like a mess, but what it should look like for REAL is <?php echo wp_kses_post( __('"This" is a \'test\'', '' ); ?> */ - $this->assertStringContainsString( "<?php echo wp_kses_post( __('\"This\" is a \\'test\\'', '') );?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( '\"This\" is a \\'test\\'', '' ); ?>", $escaped_template->content ); } public function test_properly_encode_lessthan_and_greaterthan() { @@ -146,7 +154,7 @@ public function test_properly_encode_lessthan_and_greaterthan() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('<This> is a <test>', '') );?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( '<This> is a <test>', '' ); ?>", $escaped_template->content ); } public function test_properly_encode_html_markup() { @@ -156,7 +164,18 @@ public function test_properly_encode_html_markup() { <!-- /wp:paragraph -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('<strong>Bold</strong> text has feelings <> TOO', '') );?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __( '<strong>Bold</strong> text has feelings <> TOO', '' ) ); ?>", $escaped_template->content ); + } + + public function test_empty_alt_text_is_not_localized() { + $template = new stdClass(); + $template->content = ' + <!-- wp:image --> + <figure class="wp-block-image"><img src="http://example.com/file.jpg" alt="" /></figure> + <!-- /wp:image --> + '; + $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); + $this->assertStringContainsString( 'alt=""', $new_template->content ); } public function test_localize_alt_text_from_image() { @@ -167,7 +186,7 @@ public function test_localize_alt_text_from_image() { <!-- /wp:image --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' );?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' ); ?>"', $new_template->content ); } public function test_localize_alt_text_from_cover() { @@ -187,7 +206,7 @@ public function test_localize_alt_text_from_cover() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); // Check the markup attribute - $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' );?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' ); ?>"', $new_template->content ); } public function test_localize_quote() { @@ -201,8 +220,8 @@ public function test_localize_quote() { </blockquote> <!-- /wp:quote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my Quote', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Citation too', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is my Quote', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Citation too', '' ); ?>", $new_template->content ); } public function test_localize_pullquote() { @@ -216,8 +235,8 @@ public function test_localize_pullquote() { </figure> <!-- /wp:pullquote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my Quote', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Citation too', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is my Quote', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Citation too', '' ); ?>", $new_template->content ); } public function test_localize_list() { @@ -234,7 +253,7 @@ public function test_localize_list() { </ul> <!-- /wp:list -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<li><?php echo wp_kses_post( __('Item One', '') );?></li>", $new_template->content ); + $this->assertStringContainsString( "<li><?php esc_html_e( 'Item One', '' ); ?></li>", $new_template->content ); } public function test_localize_verse() { @@ -243,7 +262,7 @@ public function test_localize_verse() { <pre class="wp-block-verse">Here is some <strong>verse</strong> to localize</pre> <!-- /wp:verse -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Here is some <strong>verse</strong> to localize', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<?php echo wp_kses_post( __( 'Here is some <strong>verse</strong> to localize', '' ) ); ?>", $new_template->content ); } public function test_localize_table() { @@ -274,10 +293,10 @@ public function test_localize_table() { </figure> <!-- /wp:table -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<td><?php echo wp_kses_post( __('Apples', '') );?></td>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Header One', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Footer One', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('This is my caption', '') );?>", $new_template->content ); + $this->assertStringContainsString( "<td><?php esc_html_e( 'Apples', '' ); ?></td>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Header One', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Footer One', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'This is my caption', '' ); ?>", $new_template->content ); } public function test_localize_media_text() { @@ -295,8 +314,8 @@ public function test_localize_media_text() { </div> <!-- /wp:media-text -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __('Content to Localize', '') );?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_attr_e( 'Alt Text Is Here', '' );?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Content to Localize', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_attr_e( 'Alt Text Is Here', '' ); ?>", $new_template->content ); } public function test_localize_cover_block_children() { @@ -314,7 +333,7 @@ public function test_localize_cover_block_children() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<p><?php echo wp_kses_post( __('This is text to localize', '') );?></p>", $new_template->content ); + $this->assertStringContainsString( "<p><?php esc_html_e( 'This is text to localize', '' ); ?></p>", $new_template->content ); } public function test_localize_nested_cover_block_children() { From 75cc3d2a2026fbb0608dfbf700051ee44a423274 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Thu, 25 Jul 2024 13:07:48 -0300 Subject: [PATCH 6/7] remove wp_kses_post as escaping function --- includes/create-theme/theme-locale.php | 5 ----- tests/CbtThemeLocale/escapeTextContentOfBlocks.php | 2 +- tests/test-theme-templates.php | 6 +++--- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 857a5745..d5bb78a5 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -28,11 +28,6 @@ private static function escape_text_content( $string ) { $string = addcslashes( $string, "'" ); - // Check if string contains html. - if ( strip_tags( $string ) !== $string ) { - return "<?php echo wp_kses_post( __( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ) ); ?>"; - } - return "<?php esc_html_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ); ?>"; } diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 7ffb7c28..296cb15d 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -108,7 +108,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:verse -->', 'expected_markup' => '<!-- wp:verse {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} --> - <pre class="wp-block-verse"><?php echo wp_kses_post( __( \'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\' ) ); ?></pre> + <pre class="wp-block-verse"><?php esc_html_e( \'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\' ); ?></pre> <!-- /wp:verse -->', ), diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index ce8fc79a..0ab57e2c 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -143,7 +143,7 @@ public function test_properly_encode_quotes_and_doublequotes() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - /* That looks like a mess, but what it should look like for REAL is <?php echo wp_kses_post( __('"This" is a \'test\'', '' ); ?> */ + /* That looks like a mess, but what it should look like for REAL is <?php esc_html_e('"This" is a \'test\'', '' ); ?> */ $this->assertStringContainsString( "<?php esc_html_e( '\"This\" is a \\'test\\'', '' ); ?>", $escaped_template->content ); } @@ -164,7 +164,7 @@ public function test_properly_encode_html_markup() { <!-- /wp:paragraph -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __( '<strong>Bold</strong> text has feelings <> TOO', '' ) ); ?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( '<strong>Bold</strong> text has feelings <> TOO', '' ); ?>", $escaped_template->content ); } public function test_empty_alt_text_is_not_localized() { @@ -262,7 +262,7 @@ public function test_localize_verse() { <pre class="wp-block-verse">Here is some <strong>verse</strong> to localize</pre> <!-- /wp:verse -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php echo wp_kses_post( __( 'Here is some <strong>verse</strong> to localize', '' ) ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e( 'Here is some <strong>verse</strong> to localize', '' ); ?>", $new_template->content ); } public function test_localize_table() { From 90fe266094ac9b52fa8bebf9ee7478b3211bef36 Mon Sep 17 00:00:00 2001 From: Matias Benedetto <matias.benedetto@gmail.com> Date: Fri, 30 Aug 2024 14:21:13 -0300 Subject: [PATCH 7/7] update tests --- includes/create-theme/theme-locale.php | 4 +- includes/create-theme/theme-patterns.php | 2 +- tests/CbtThemeLocale/escapeAttribute.php | 58 +++++++++++++++++++ tests/CbtThemeLocale/escapeTextContent.php | 56 ++++++++++++++++++ .../escapeTextContentOfBlocks.php | 32 +++++----- tests/test-theme-templates.php | 46 +++++++-------- 6 files changed, 156 insertions(+), 42 deletions(-) create mode 100644 tests/CbtThemeLocale/escapeAttribute.php create mode 100644 tests/CbtThemeLocale/escapeTextContent.php diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index d5bb78a5..783d8715 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -28,7 +28,7 @@ private static function escape_text_content( $string ) { $string = addcslashes( $string, "'" ); - return "<?php esc_html_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ); ?>"; + return "<?php esc_html_e('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; } /** @@ -54,7 +54,7 @@ private static function escape_attribute( $string ) { } $string = addcslashes( $string, "'" ); - return "<?php esc_attr_e( '" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "' ); ?>"; + return "<?php esc_attr_e('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; } /** diff --git a/includes/create-theme/theme-patterns.php b/includes/create-theme/theme-patterns.php index 5ff62fd6..23224c2d 100644 --- a/includes/create-theme/theme-patterns.php +++ b/includes/create-theme/theme-patterns.php @@ -38,7 +38,7 @@ public static function escape_alt_for_pattern( $html ) { public static function escape_text_for_pattern( $text ) { if ( $text && trim( $text ) !== '' ) { $escaped_text = addslashes( $text ); - return "<?php esc_attr_e( '" . $escaped_text . "', '" . wp_get_theme()->get( 'Name' ) . "' ); ?>"; + return "<?php esc_attr_e('" . $escaped_text . "', '" . wp_get_theme()->get( 'Name' ) . "');?>"; } } diff --git a/tests/CbtThemeLocale/escapeAttribute.php b/tests/CbtThemeLocale/escapeAttribute.php new file mode 100644 index 00000000..9f511d06 --- /dev/null +++ b/tests/CbtThemeLocale/escapeAttribute.php @@ -0,0 +1,58 @@ +<?php +require_once __DIR__ . '/base.php'; + +/** + * Tests for the CBT_Theme_Locale::escape_attribute method. + * + * @package Create_Block_Theme + * @covers CBT_Theme_Locale::escape_attribute + * @group locale + */ +class CBT_Theme_Locale_EscapeAttribute 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_attribute() { + $string = 'This is a test attribute.'; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $expected_string = "<?php esc_attr_e('This is a test attribute.', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; + $this->assertEquals( $expected_string, $escaped_string ); + } + + public function test_escape_attribute_with_single_quote() { + $string = "This is a test attribute with a single quote '"; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $expected_string = "<?php esc_attr_e('This is a test attribute with a single quote \\'', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; + $this->assertEquals( $expected_string, $escaped_string ); + } + + public function test_escape_attribute_with_double_quote() { + $string = 'This is a test attribute with a double quote "'; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $expected_string = "<?php esc_attr_e('This is a test attribute with a double quote \"', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; + $this->assertEquals( $expected_string, $escaped_string ); + } + + public function test_escape_attribute_with_empty_string() { + $string = ''; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $this->assertEquals( $string, $escaped_string ); + } + + public function test_escape_attribute_with_already_escaped_string() { + $string = "<?php esc_attr_e('This is already escaped.', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $this->assertEquals( $string, $escaped_string ); + } + + public function test_escape_attribute_with_non_string() { + $string = null; + $escaped_string = $this->call_private_method( 'escape_attribute', array( $string ) ); + $this->assertEquals( $string, $escaped_string ); + } +} diff --git a/tests/CbtThemeLocale/escapeTextContent.php b/tests/CbtThemeLocale/escapeTextContent.php new file mode 100644 index 00000000..32609243 --- /dev/null +++ b/tests/CbtThemeLocale/escapeTextContent.php @@ -0,0 +1,56 @@ +<?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 ) ); + $this->assertEquals( "<?php esc_html_e('<p>This is a test text with HTML.</p>', 'test-locale-theme');?>", $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 ); + } +} diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 296cb15d..2fee4adf 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -30,7 +30,7 @@ public function data_test_escape_text_content_of_blocks() { 'paragraph' => array( 'block_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">This is a test text.</p><!-- /wp:paragraph -->', - 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php esc_html_e( \'This is a test text.\', \'test-locale-theme\' ); ?></p><!-- /wp:paragraph -->', + 'expected_markup' => '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center"><?php esc_html_e(\'This is a test text.\', \'test-locale-theme\');?></p><!-- /wp:paragraph -->', ), 'paragraph on nested groups' => array( @@ -46,7 +46,7 @@ public function data_test_escape_text_content_of_blocks() { '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|50","left":"var:preset|spacing|50","right":"var:preset|spacing|50"}}},"layout":{"type":"constrained","contentSize":"","wideSize":""}} --> <div class="wp-block-group alignfull" style="padding-top:var(--wp--preset--spacing--50);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)"><!-- wp:group {"style":{"spacing":{"blockGap":"0px"}},"layout":{"type":"constrained","contentSize":"565px"}} --> <div class="wp-block-group"><!-- wp:paragraph {"align":"center"} --> - <p class="has-text-align-center"><?php esc_html_e( \'This is a test text.\', \'test-locale-theme\' ); ?></p> + <p class="has-text-align-center"><?php esc_html_e(\'This is a test text.\', \'test-locale-theme\');?></p> <!-- /wp:paragraph --></div> <!-- /wp:group --></div> <!-- /wp:group -->', @@ -59,7 +59,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e( \'A passion for creating spaces\', \'test-locale-theme\' ); ?></h1> + <h1 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e(\'A passion for creating spaces\', \'test-locale-theme\');?></h1> <!-- /wp:heading -->', ), @@ -70,7 +70,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:heading -->', 'expected_markup' => '<!-- wp:heading {"textAlign":"center","className":"is-style-asterisk"} --> - <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e( \'A passion for creating spaces\', \'test-locale-theme\' ); ?></h2> + <h2 class="wp-block-heading has-text-align-center is-style-asterisk"><?php esc_html_e(\'A passion for creating spaces\', \'test-locale-theme\');?></h2> <!-- /wp:heading -->', ), @@ -90,13 +90,13 @@ public function data_test_escape_text_content_of_blocks() { 'expected_markup' => '<!-- wp:list {"style":{"typography":{"lineHeight":"1.75"}},"className":"is-style-checkmark-list"} --> <ul style="line-height:1.75" class="is-style-checkmark-list"><!-- wp:list-item --> - <li><?php esc_html_e( \'Collaborate with fellow architects.\', \'test-locale-theme\' ); ?></li> + <li><?php esc_html_e(\'Collaborate with fellow architects.\', \'test-locale-theme\');?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php esc_html_e( \'Showcase your projects.\', \'test-locale-theme\' ); ?></li> + <li><?php esc_html_e(\'Showcase your projects.\', \'test-locale-theme\');?></li> <!-- /wp:list-item --> <!-- wp:list-item --> - <li><?php esc_html_e( \'Experience the world of architecture.\', \'test-locale-theme\' ); ?></li> + <li><?php esc_html_e(\'Experience the world of architecture.\', \'test-locale-theme\');?></li> <!-- /wp:list-item --></ul> <!-- /wp:list -->', ), @@ -108,7 +108,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:verse -->', 'expected_markup' => '<!-- wp:verse {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} --> - <pre class="wp-block-verse"><?php esc_html_e( \'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\' ); ?></pre> + <pre class="wp-block-verse"><?php esc_html_e(\'Ya somos el olvido que seremos.<br>El polvo elemental que nos ignora<br>y que fue el rojo Adán y que es ahora<br>todos los hombres, y que no veremos.\', \'test-locale-theme\');?></pre> <!-- /wp:verse -->', ), @@ -119,7 +119,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:button -->', 'expected_markup' => '<!-- wp:button --> - <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php esc_html_e( \'Sign up\', \'test-locale-theme\' ); ?></a></div> + <div class="wp-block-button"><a class="wp-block-button__link wp-element-button"><?php esc_html_e(\'Sign up\', \'test-locale-theme\');?></a></div> <!-- /wp:button -->', ), @@ -130,7 +130,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:image -->', 'expected_markup' => '<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} --> - <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e( \'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\' ); ?>"/></figure> + <figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>"/></figure> <!-- /wp:image -->', ), @@ -143,8 +143,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:cover -->', 'expected_markup' => '<!-- wp:cover {"url":"http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg","id":39,"alt":"Alternative text for cover image","dimRatio":50,"customOverlayColor":"#1d2b2f","layout":{"type":"constrained"}} --> - <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_attr_e( \'Alternative text for cover image\', \'test-locale-theme\' ); ?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> - <p class="has-text-align-center has-large-font-size"><?php esc_html_e( \'This is a cover caption\', \'test-locale-theme\' ); ?></p> + <div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim" style="background-color:#1d2b2f"></span><img class="wp-block-cover__image-background wp-image-39" alt="<?php esc_attr_e(\'Alternative text for cover image\', \'test-locale-theme\');?>" src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} --> + <p class="has-text-align-center has-large-font-size"><?php esc_html_e(\'This is a cover caption\', \'test-locale-theme\');?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:cover -->', ), @@ -158,8 +158,8 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:media-text -->', 'expected_markup' => '<!-- wp:media-text {"mediaId":39,"mediaLink":"http://localhost/wp1/image/","mediaType":"image"} --> - <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_attr_e( \'This is alt text\', \'test-locale-theme\' ); ?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> - <p><?php esc_html_e( \'Media text content test.\', \'test-locale-theme\' ); ?></p> + <div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="http://localhost/wp1/wp-content/uploads/2024/05/image.jpeg" alt="<?php esc_attr_e(\'This is alt text\', \'test-locale-theme\');?>" class="wp-image-39 size-full"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…"} --> + <p><?php esc_html_e(\'Media text content test.\', \'test-locale-theme\');?></p> <!-- /wp:paragraph --></div></div> <!-- /wp:media-text -->', ), @@ -171,7 +171,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:pullquote -->', 'expected_markup' => '<!-- wp:pullquote --> - <figure class="wp-block-pullquote"><blockquote><p><?php esc_html_e( \'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\' ); ?></p><cite><?php esc_html_e( \'Diego Armando Maradona\', \'test-locale-theme\' ); ?></cite></blockquote></figure> + <figure class="wp-block-pullquote"><blockquote><p><?php esc_html_e(\'Yo me equivoqué y pagué, pero la pelota no se mancha.\', \'test-locale-theme\');?></p><cite><?php esc_html_e(\'Diego Armando Maradona\', \'test-locale-theme\');?></cite></blockquote></figure> <!-- /wp:pullquote -->', ), @@ -182,7 +182,7 @@ public function data_test_escape_text_content_of_blocks() { <!-- /wp:table -->', 'expected_markup' => '<!-- wp:table --> - <figure class="wp-block-table"><table><tbody><tr><td><?php esc_html_e( \'Team\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'Points\', \'test-locale-theme\' ); ?></td></tr><tr><td><?php esc_html_e( \'Boca\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'74\', \'test-locale-theme\' ); ?></td></tr><tr><td><?php esc_html_e( \'River\', \'test-locale-theme\' ); ?></td><td><?php esc_html_e( \'2\', \'test-locale-theme\' ); ?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php esc_html_e( \'Score table\', \'test-locale-theme\' ); ?></figcaption></figure> + <figure class="wp-block-table"><table><tbody><tr><td><?php esc_html_e(\'Team\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'Points\', \'test-locale-theme\');?></td></tr><tr><td><?php esc_html_e(\'Boca\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'74\', \'test-locale-theme\');?></td></tr><tr><td><?php esc_html_e(\'River\', \'test-locale-theme\');?></td><td><?php esc_html_e(\'2\', \'test-locale-theme\');?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php esc_html_e(\'Score table\', \'test-locale-theme\');?></figcaption></figure> <!-- /wp:table -->', ), diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 0ab57e2c..e2842800 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -12,7 +12,7 @@ public function test_paragraphs_are_localized() { $template = new stdClass(); $template->content = '<!-- wp:paragraph --><p>This is text to localize</p><!-- /wp:paragraph -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<p><?php esc_html_e( 'This is text to localize', '' ); ?></p>", $new_template->content ); + $this->assertStringContainsString( "<p><?php esc_html_e('This is text to localize', '');?></p>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } @@ -45,7 +45,7 @@ public function test_paragraphs_in_groups_are_localized() { </div> <!-- /wp:group -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is text to localize', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is text to localize', '');?>", $new_template->content ); $this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content ); } @@ -57,7 +57,7 @@ public function test_buttons_are_localized() { </div> <!-- /wp:button -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is text to localize', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is text to localize', '');?>", $new_template->content ); $this->assertStringNotContainsString( '<a class="wp-block-button__link wp-element-button">This is text to localize</a>', $new_template->content ); } @@ -69,7 +69,7 @@ public function test_headings_are_localized() { <!-- /wp:heading --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is a heading to localize.', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is a heading to localize.', '');?>", $new_template->content ); $this->assertStringNotContainsString( '<h2 class="wp-block-heading">This is a heading to localize.</h2>', $new_template->content ); } @@ -143,8 +143,8 @@ public function test_properly_encode_quotes_and_doublequotes() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - /* That looks like a mess, but what it should look like for REAL is <?php esc_html_e('"This" is a \'test\'', '' ); ?> */ - $this->assertStringContainsString( "<?php esc_html_e( '\"This\" is a \\'test\\'', '' ); ?>", $escaped_template->content ); + /* That looks like a mess, but what it should look like for REAL is <?php esc_html_e('"This" is a \'test\'', '');?> */ + $this->assertStringContainsString( "<?php esc_html_e('\"This\" is a \\'test\\'', '');?>", $escaped_template->content ); } public function test_properly_encode_lessthan_and_greaterthan() { @@ -154,7 +154,7 @@ public function test_properly_encode_lessthan_and_greaterthan() { <!-- /wp:heading -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( '<This> is a <test>', '' ); ?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('<This> is a <test>', '');?>", $escaped_template->content ); } public function test_properly_encode_html_markup() { @@ -164,7 +164,7 @@ public function test_properly_encode_html_markup() { <!-- /wp:paragraph -->'; $escaped_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( '<strong>Bold</strong> text has feelings <> TOO', '' ); ?>", $escaped_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('<strong>Bold</strong> text has feelings <> TOO', '');?>", $escaped_template->content ); } public function test_empty_alt_text_is_not_localized() { @@ -186,7 +186,7 @@ public function test_localize_alt_text_from_image() { <!-- /wp:image --> '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' ); ?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e(\'This is alt text\', \'\');?>"', $new_template->content ); } public function test_localize_alt_text_from_cover() { @@ -206,7 +206,7 @@ public function test_localize_alt_text_from_cover() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); // Check the markup attribute - $this->assertStringContainsString( 'alt="<?php esc_attr_e( \'This is alt text\', \'\' ); ?>"', $new_template->content ); + $this->assertStringContainsString( 'alt="<?php esc_attr_e(\'This is alt text\', \'\');?>"', $new_template->content ); } public function test_localize_quote() { @@ -220,8 +220,8 @@ public function test_localize_quote() { </blockquote> <!-- /wp:quote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is my Quote', '' ); ?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e( 'Citation too', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is my Quote', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Citation too', '');?>", $new_template->content ); } public function test_localize_pullquote() { @@ -235,8 +235,8 @@ public function test_localize_pullquote() { </figure> <!-- /wp:pullquote -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is my Quote', '' ); ?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e( 'Citation too', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is my Quote', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Citation too', '');?>", $new_template->content ); } public function test_localize_list() { @@ -253,7 +253,7 @@ public function test_localize_list() { </ul> <!-- /wp:list -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<li><?php esc_html_e( 'Item One', '' ); ?></li>", $new_template->content ); + $this->assertStringContainsString( "<li><?php esc_html_e('Item One', '');?></li>", $new_template->content ); } public function test_localize_verse() { @@ -262,7 +262,7 @@ public function test_localize_verse() { <pre class="wp-block-verse">Here is some <strong>verse</strong> to localize</pre> <!-- /wp:verse -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'Here is some <strong>verse</strong> to localize', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Here is some <strong>verse</strong> to localize', '');?>", $new_template->content ); } public function test_localize_table() { @@ -293,10 +293,10 @@ public function test_localize_table() { </figure> <!-- /wp:table -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<td><?php esc_html_e( 'Apples', '' ); ?></td>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e( 'Header One', '' ); ?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e( 'Footer One', '' ); ?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_html_e( 'This is my caption', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<td><?php esc_html_e('Apples', '');?></td>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Header One', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Footer One', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('This is my caption', '');?>", $new_template->content ); } public function test_localize_media_text() { @@ -314,8 +314,8 @@ public function test_localize_media_text() { </div> <!-- /wp:media-text -->'; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<?php esc_html_e( 'Content to Localize', '' ); ?>", $new_template->content ); - $this->assertStringContainsString( "<?php esc_attr_e( 'Alt Text Is Here', '' ); ?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_html_e('Content to Localize', '');?>", $new_template->content ); + $this->assertStringContainsString( "<?php esc_attr_e('Alt Text Is Here', '');?>", $new_template->content ); } public function test_localize_cover_block_children() { @@ -333,7 +333,7 @@ public function test_localize_cover_block_children() { '; $new_template = CBT_Theme_Templates::escape_text_in_template( $template ); - $this->assertStringContainsString( "<p><?php esc_html_e( 'This is text to localize', '' ); ?></p>", $new_template->content ); + $this->assertStringContainsString( "<p><?php esc_html_e('This is text to localize', '');?></p>", $new_template->content ); } public function test_localize_nested_cover_block_children() {