|
9 | 9 | * @group blocks
|
10 | 10 | */
|
11 | 11 | class Tests_Blocks_Editor extends WP_UnitTestCase {
|
12 |
| - |
13 | 12 | /**
|
14 | 13 | * Sets up each test method.
|
15 | 14 | */
|
@@ -631,8 +630,8 @@ function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $cont
|
631 | 630 |
|
632 | 631 | $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
|
633 | 632 | $this->assertStringContainsString( 'wp.apiFetch.createPreloadingMiddleware', $after );
|
634 |
| - $this->assertStringContainsString( '"\/wp\/v2\/blocks"', $after ); |
635 |
| - $this->assertStringContainsString( '"\/wp\/v2\/types"', $after ); |
| 633 | + $this->assertStringContainsString( '"/wp/v2/blocks"', $after ); |
| 634 | + $this->assertStringContainsString( '"/wp/v2/types"', $after ); |
636 | 635 | }
|
637 | 636 |
|
638 | 637 | /**
|
@@ -697,28 +696,75 @@ public function data_block_editor_rest_api_preload_adds_missing_leading_slash()
|
697 | 696 | return array(
|
698 | 697 | 'a string without a slash' => array(
|
699 | 698 | 'preload_paths' => array( 'wp/v2/blocks' ),
|
700 |
| - 'expected' => '\/wp\/v2\/blocks', |
| 699 | + 'expected' => '/wp/v2/blocks', |
701 | 700 | ),
|
702 | 701 | 'a string with a slash' => array(
|
703 | 702 | 'preload_paths' => array( '/wp/v2/blocks' ),
|
704 |
| - 'expected' => '\/wp\/v2\/blocks', |
| 703 | + 'expected' => '/wp/v2/blocks', |
705 | 704 | ),
|
706 | 705 | 'a string starting with a question mark' => array(
|
707 | 706 | 'preload_paths' => array( '?context=edit' ),
|
708 | 707 | 'expected' => '/?context=edit',
|
709 | 708 | ),
|
710 | 709 | 'an array with a string without a slash' => array(
|
711 | 710 | 'preload_paths' => array( array( 'wp/v2/blocks', 'OPTIONS' ) ),
|
712 |
| - 'expected' => '\/wp\/v2\/blocks', |
| 711 | + 'expected' => '/wp/v2/blocks', |
713 | 712 | ),
|
714 | 713 | 'an array with a string with a slash' => array(
|
715 | 714 | 'preload_paths' => array( array( '/wp/v2/blocks', 'OPTIONS' ) ),
|
716 |
| - 'expected' => '\/wp\/v2\/blocks', |
| 715 | + 'expected' => '/wp/v2/blocks', |
717 | 716 | ),
|
718 | 717 | 'an array with a string starting with a question mark' => array(
|
719 | 718 | 'preload_paths' => array( array( '?context=edit', 'OPTIONS' ) ),
|
720 |
| - 'expected' => '\/?context=edit', |
| 719 | + 'expected' => '/?context=edit', |
721 | 720 | ),
|
722 | 721 | );
|
723 | 722 | }
|
| 723 | + |
| 724 | + /** |
| 725 | + * @ticket 62797 |
| 726 | + * |
| 727 | + * @covers ::block_editor_rest_api_preload |
| 728 | + * |
| 729 | + * Some valid JSON-encoded data is dangerous to embed in HTML without appropriate |
| 730 | + * escaping. This test includes prints an example of such data that would prevent |
| 731 | + * the enclosing `<script>` from closing on its apparent closer and remain open. |
| 732 | + */ |
| 733 | + public function test_ensure_preload_data_script_tag_closes() { |
| 734 | + add_theme_support( 'html5', array( 'script' ) ); |
| 735 | + register_rest_route( |
| 736 | + 'test/v0', |
| 737 | + 'test-62797', |
| 738 | + array( |
| 739 | + 'methods' => 'GET', |
| 740 | + 'callback' => function () { |
| 741 | + return '<!-- unclosed comment and a script tag <script></script>'; |
| 742 | + }, |
| 743 | + 'permission_callback' => '__return_true', |
| 744 | + ) |
| 745 | + ); |
| 746 | + |
| 747 | + // Prevent a bunch of noisy or unstable data from being included in the test output. |
| 748 | + wp_scripts()->registered['wp-api-fetch']->ver = 'test'; |
| 749 | + wp_scripts()->registered['wp-api-fetch']->extra['after'] = array(); |
| 750 | + |
| 751 | + block_editor_rest_api_preload( |
| 752 | + array( '/test/v0/test-62797' ), |
| 753 | + new WP_Block_Editor_Context() |
| 754 | + ); |
| 755 | + |
| 756 | + ob_start(); |
| 757 | + wp_scripts()->do_item( 'wp-api-fetch' ); |
| 758 | + $output = ob_get_clean(); |
| 759 | + |
| 760 | + $baseurl = site_url(); |
| 761 | + $expected = <<<HTML |
| 762 | +<script src="{$baseurl}/wp-includes/js/dist/api-fetch.min.js?ver=test" id="wp-api-fetch-js"></script> |
| 763 | +<script id="wp-api-fetch-js-after"> |
| 764 | +wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {"/test/v0/test-62797":{"body":["\\u003C!-- unclosed comment and a script tag \\u003Cscript\\u003E\\u003C/script\\u003E"],"headers":{"Allow":"GET"}}} ) ); |
| 765 | +</script> |
| 766 | +
|
| 767 | +HTML; |
| 768 | + $this->assertEqualHTML( $expected, $output ); |
| 769 | + } |
724 | 770 | }
|
0 commit comments