|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/base.php'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for the CBT_Theme_Locale::escape_text_content_of_blocks method with block attributes. |
| 7 | + * |
| 8 | + * @package Create_Block_Theme |
| 9 | + * @covers CBT_Theme_Locale::escape_text_content_of_blocks |
| 10 | + * @covers CBT_Theme_Locale::escape_block_attribute_strings |
| 11 | + * @group locale |
| 12 | + */ |
| 13 | +class CBT_Theme_Locale_EscapeBlockAttributes extends CBT_Theme_Locale_UnitTestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * @dataProvider data_test_escape_block_attributes |
| 17 | + */ |
| 18 | + public function test_escape_block_attributes( $block_markup, $expected_markup ) { |
| 19 | + // Parse the block markup. |
| 20 | + $blocks = parse_blocks( $block_markup ); |
| 21 | + // Escape the text content of the blocks. |
| 22 | + $escaped_blocks = CBT_Theme_Locale::escape_text_content_of_blocks( $blocks ); |
| 23 | + // Serialize the blocks to get the markup. |
| 24 | + $escaped_markup = serialize_blocks( $escaped_blocks ); |
| 25 | + // Process block attributes after serialization to avoid JSON encoding of PHP tags. |
| 26 | + $escaped_markup = CBT_Theme_Locale::escape_block_attribute_strings( $escaped_markup ); |
| 27 | + |
| 28 | + $this->assertEquals( $expected_markup, $escaped_markup, 'The markup result is not as the expected one.' ); |
| 29 | + } |
| 30 | + |
| 31 | + public function data_test_escape_block_attributes() { |
| 32 | + return array( |
| 33 | + |
| 34 | + 'search block with label, placeholder, and buttonText' => array( |
| 35 | + 'block_markup' => '<!-- wp:search {"label":"Search","placeholder":"Type here...","buttonText":"Search"} /-->', |
| 36 | + 'expected_markup' => '<!-- wp:search {"label":"<?php esc_attr_e(\'Search\', \'test-locale-theme\');?>","placeholder":"<?php esc_attr_e(\'Type here...\', \'test-locale-theme\');?>","buttonText":"<?php esc_attr_e(\'Search\', \'test-locale-theme\');?>"} /-->', |
| 37 | + ), |
| 38 | + |
| 39 | + 'query-pagination-previous with label' => array( |
| 40 | + 'block_markup' => '<!-- wp:query-pagination-previous {"label":"Previous"} /-->', |
| 41 | + 'expected_markup' => '<!-- wp:query-pagination-previous {"label":"<?php esc_attr_e(\'Previous\', \'test-locale-theme\');?>"} /-->', |
| 42 | + ), |
| 43 | + |
| 44 | + 'query-pagination-next with label' => array( |
| 45 | + 'block_markup' => '<!-- wp:query-pagination-next {"label":"Next"} /-->', |
| 46 | + 'expected_markup' => '<!-- wp:query-pagination-next {"label":"<?php esc_attr_e(\'Next\', \'test-locale-theme\');?>"} /-->', |
| 47 | + ), |
| 48 | + |
| 49 | + 'comments-pagination-next with label' => array( |
| 50 | + 'block_markup' => '<!-- wp:comments-pagination-next {"label":"⪻ newer"} /-->', |
| 51 | + 'expected_markup' => '<!-- wp:comments-pagination-next {"label":"<?php esc_attr_e(\'⪻ newer\', \'test-locale-theme\');?>"} /-->', |
| 52 | + ), |
| 53 | + |
| 54 | + 'comments-pagination-previous with label' => array( |
| 55 | + 'block_markup' => '<!-- wp:comments-pagination-previous {"label":"older ⪼"} /-->', |
| 56 | + 'expected_markup' => '<!-- wp:comments-pagination-previous {"label":"<?php esc_attr_e(\'older ⪼\', \'test-locale-theme\');?>"} /-->', |
| 57 | + ), |
| 58 | + |
| 59 | + 'post-excerpt with moreText' => array( |
| 60 | + 'block_markup' => '<!-- wp:post-excerpt {"moreText":"— read more","showMoreOnNewLine":false} /-->', |
| 61 | + 'expected_markup' => '<!-- wp:post-excerpt {"moreText":"<?php esc_attr_e(\'— read more\', \'test-locale-theme\');?>","showMoreOnNewLine":false} /-->', |
| 62 | + ), |
| 63 | + |
| 64 | + 'post-navigation-link with label' => array( |
| 65 | + 'block_markup' => '<!-- wp:post-navigation-link {"label":"Custom Label"} /-->', |
| 66 | + 'expected_markup' => '<!-- wp:post-navigation-link {"label":"<?php esc_attr_e(\'Custom Label\', \'test-locale-theme\');?>"} /-->', |
| 67 | + ), |
| 68 | + |
| 69 | + 'search block with only some attributes' => array( |
| 70 | + 'block_markup' => '<!-- wp:search {"placeholder":"Search..."} /-->', |
| 71 | + 'expected_markup' => '<!-- wp:search {"placeholder":"<?php esc_attr_e(\'Search...\', \'test-locale-theme\');?>"} /-->', |
| 72 | + ), |
| 73 | + |
| 74 | + 'query pagination blocks in context' => array( |
| 75 | + 'block_markup' => '<!-- wp:query-pagination --> |
| 76 | +<!-- wp:query-pagination-previous {"label":"Previous"} /--> |
| 77 | +<!-- wp:query-pagination-numbers /--> |
| 78 | +<!-- wp:query-pagination-next {"label":"Next"} /--> |
| 79 | +<!-- /wp:query-pagination -->', |
| 80 | + 'expected_markup' => '<!-- wp:query-pagination --> |
| 81 | +<!-- wp:query-pagination-previous {"label":"<?php esc_attr_e(\'Previous\', \'test-locale-theme\');?>"} /--> |
| 82 | +<!-- wp:query-pagination-numbers /--> |
| 83 | +<!-- wp:query-pagination-next {"label":"<?php esc_attr_e(\'Next\', \'test-locale-theme\');?>"} /--> |
| 84 | +<!-- /wp:query-pagination -->', |
| 85 | + ), |
| 86 | + |
| 87 | + 'block without attributes should remain unchanged' => array( |
| 88 | + 'block_markup' => '<!-- wp:search /-->', |
| 89 | + 'expected_markup' => '<!-- wp:search /-->', |
| 90 | + ), |
| 91 | + |
| 92 | + 'block with only non-translatable attributes should remain unchanged' => array( |
| 93 | + 'block_markup' => '<!-- wp:search {"showLabel":false,"buttonPosition":"button-inside"} /-->', |
| 94 | + 'expected_markup' => '<!-- wp:search {"showLabel":false,"buttonPosition":"button-inside"} /-->', |
| 95 | + ), |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments