block-parser.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * WP_Block_Parser tests.
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Tests for WP_Block_Parser
  11. *
  12. * @since 5.0.0
  13. *
  14. * @group blocks
  15. */
  16. class WP_Test_Block_Parser extends WP_UnitTestCase {
  17. /**
  18. * The location of the fixtures to test with.
  19. *
  20. * @since 5.0.0
  21. * @var string
  22. */
  23. protected static $fixtures_dir;
  24. /**
  25. * @ticket 45109
  26. */
  27. public function data_parsing_test_filenames() {
  28. self::$fixtures_dir = DIR_TESTDATA . '/blocks/fixtures';
  29. $fixture_filenames = array_merge(
  30. glob( self::$fixtures_dir . '/*.json' ),
  31. glob( self::$fixtures_dir . '/*.html' )
  32. );
  33. $fixture_filenames = array_values(
  34. array_unique(
  35. array_map(
  36. array( $this, 'clean_fixture_filename' ),
  37. $fixture_filenames
  38. )
  39. )
  40. );
  41. return array_map(
  42. array( $this, 'pass_parser_fixture_filenames' ),
  43. $fixture_filenames
  44. );
  45. }
  46. /**
  47. * @dataProvider data_parsing_test_filenames
  48. * @ticket 45109
  49. */
  50. public function test_default_parser_output( $html_filename, $parsed_json_filename ) {
  51. $html_path = self::$fixtures_dir . '/' . $html_filename;
  52. $parsed_json_path = self::$fixtures_dir . '/' . $parsed_json_filename;
  53. foreach ( array( $html_path, $parsed_json_path ) as $filename ) {
  54. if ( ! file_exists( $filename ) ) {
  55. throw new Exception( "Missing fixture file: '$filename'" );
  56. }
  57. }
  58. $html = self::strip_r( file_get_contents( $html_path ) );
  59. $expected_parsed = json_decode( self::strip_r( file_get_contents( $parsed_json_path ) ), true );
  60. $parser = new WP_Block_Parser();
  61. $result = json_decode( json_encode( $parser->parse( $html ) ), true );
  62. $this->assertSame(
  63. $expected_parsed,
  64. $result,
  65. "File '$parsed_json_filename' does not match expected value"
  66. );
  67. }
  68. /**
  69. * Helper function to remove relative paths and extension from a filename, leaving just the fixture name.
  70. *
  71. * @since 5.0.0
  72. *
  73. * @param string $filename The filename to clean.
  74. * @return string The cleaned fixture name.
  75. */
  76. protected function clean_fixture_filename( $filename ) {
  77. $filename = wp_basename( $filename );
  78. $filename = preg_replace( '/\..+$/', '', $filename );
  79. return $filename;
  80. }
  81. /**
  82. * Helper function to return the filenames needed to test the parser output.
  83. *
  84. * @since 5.0.0
  85. *
  86. * @param string $filename The cleaned fixture name.
  87. * @return array The input and expected output filenames for that fixture.
  88. */
  89. protected function pass_parser_fixture_filenames( $filename ) {
  90. return array(
  91. "$filename.html",
  92. "$filename.parsed.json",
  93. );
  94. }
  95. /**
  96. * Helper function to remove '\r' characters from a string.
  97. *
  98. * @since 5.0.0
  99. *
  100. * @param string $input The string to remove '\r' from.
  101. * @return string The input string, with '\r' characters removed.
  102. */
  103. protected function strip_r( $input ) {
  104. return str_replace( "\r", '', $input );
  105. }
  106. }