| | 1 | <?php |
| | 2 | /** |
| | 3 | * Block serialization tests. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage Blocks |
| | 7 | * @since 5.3.3 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests for block serialization functions |
| | 12 | * |
| | 13 | * @since 5.3.3 |
| | 14 | * |
| | 15 | * @group blocks |
| | 16 | */ |
| | 17 | class WP_Test_Block_Serialization extends WP_UnitTestCase { |
| | 18 | |
| | 19 | /** |
| | 20 | * @dataProvider data_serialize_identity_from_parsed |
| | 21 | */ |
| | 22 | function test_serialize_identity_from_parsed( $original ) { |
| | 23 | $blocks = parse_blocks( $original ); |
| | 24 | |
| | 25 | $actual = serialize_blocks( $blocks ); |
| | 26 | $expected = $original; |
| | 27 | |
| | 28 | $this->assertEquals( $expected, $actual ); |
| | 29 | } |
| | 30 | |
| | 31 | function data_serialize_identity_from_parsed() { |
| | 32 | return array( |
| | 33 | // Void block. |
| | 34 | array( '<!-- wp:void /-->' ), |
| | 35 | |
| | 36 | // Freeform content ($block_name = null). |
| | 37 | array( 'Example.' ), |
| | 38 | |
| | 39 | // Block with content. |
| | 40 | array( '<!-- wp:content -->Example.<!-- /wp:content -->' ), |
| | 41 | |
| | 42 | // Block with attributes. |
| | 43 | array( '<!-- wp:attributes {"key":"value"} /-->' ), |
| | 44 | |
| | 45 | // Block with inner blocks. |
| | 46 | array( "<!-- wp:outer --><!-- wp:inner {\"key\":\"value\"} -->Example.<!-- /wp:inner -->\n\nExample.\n\n<!-- wp:void /--><!-- /wp:outer -->" ), |
| | 47 | |
| | 48 | // Block with attribute values that may conflict with HTML comment. |
| | 49 | array( '<!-- wp:attributes {"key":"\\u002d\\u002d\\u003c\\u003e\\u0026\\u0022"} /-->' ), |
| | 50 | ); |
| | 51 | } |
| | 52 | |
| | 53 | function test_serialized_block_name() { |
| | 54 | $this->assertEquals( null, strip_core_block_namespace( null ) ); |
| | 55 | $this->assertEquals( 'example', strip_core_block_namespace( 'example' ) ); |
| | 56 | $this->assertEquals( 'example', strip_core_block_namespace( 'core/example' ) ); |
| | 57 | $this->assertEquals( 'plugin/example', strip_core_block_namespace( 'plugin/example' ) ); |
| | 58 | } |
| | 59 | |
| | 60 | } |