serialization.php 1.6 KB

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