| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group formatting |
| 5 | * @covers ::excerpt_remove_blocks |
| 6 | */ |
| 7 | class Tests_Formatting_ExcerptRemoveBlocks extends WP_UnitTestCase { |
| 8 | |
| 9 | public static $post_id; |
| 10 | |
| 11 | public $content = ' |
| 12 | <!-- wp:paragraph --> |
| 13 | <p>paragraph</p> |
| 14 | <!-- /wp:paragraph --> |
| 15 | <!-- wp:latest-posts {"postsToShow":3,"displayPostDate":true,"order":"asc","orderBy":"title"} /--> |
| 16 | <!-- wp:spacer --> |
| 17 | <div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div> |
| 18 | <!-- /wp:spacer -->'; |
| 19 | |
| 20 | public $filtered_content = ' |
| 21 | |
| 22 | <p>paragraph</p> |
| 23 | |
| 24 | |
| 25 | '; |
| 26 | |
| 27 | /** |
| 28 | * Fake block rendering function. |
| 29 | * |
| 30 | * @since 5.2.0 |
| 31 | * |
| 32 | * @return string Block output. |
| 33 | */ |
| 34 | function render_fake_block() { |
| 35 | return get_the_excerpt( self::$post_id ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Set up. |
| 40 | * |
| 41 | * @since 5.2.0 |
| 42 | */ |
| 43 | function setUp() { |
| 44 | parent::setUp(); |
| 45 | self::$post_id = $this->factory()->post->create( |
| 46 | array( |
| 47 | 'post_excerpt' => '', // Empty excerpt, so it has to be generated. |
| 48 | 'post_content' => '<!-- wp:core/fake /-->', |
| 49 | ) |
| 50 | ); |
| 51 | register_block_type( |
| 52 | 'core/fake', |
| 53 | array( |
| 54 | 'render_callback' => array( $this, 'render_fake_block' ), |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tear down. |
| 61 | * |
| 62 | * @since 5.2.0 |
| 63 | */ |
| 64 | function tearDown() { |
| 65 | parent::tearDown(); |
| 66 | $registry = WP_Block_Type_Registry::get_instance(); |
| 67 | $registry->unregister( 'core/fake' ); |
| 68 | wp_delete_post( self::$post_id, true ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests excerpt_remove_blocks(). |
| 73 | * |
| 74 | * @ticket 46133 |
| 75 | */ |
| 76 | function test_excerpt_remove_blocks() { |
| 77 | // Simple dynamic block.. |
| 78 | $content = '<!-- wp:core/block /-->'; |
| 79 | |
| 80 | $this->assertEmpty( excerpt_remove_blocks( $content ) ); |
| 81 | |
| 82 | // Dynamic block with options, embedded in other content. |
| 83 | $this->assertEquals( $this->filtered_content, excerpt_remove_blocks( $this->content ) ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests that dynamic blocks don't cause an out-of-memory error. |
| 88 | * |
| 89 | * When dynamic blocks happen to generate an excerpt, they can cause an |
| 90 | * infinite loop if that block is part of the post's content. |
| 91 | * |
| 92 | * `wp_trim_excerpt()` applies the `the_content` filter, which has |
| 93 | * `do_blocks` attached to it, trying to render the block which again will |
| 94 | * attempt to return an excerpt of that post. |
| 95 | * |
| 96 | * This infinite loop can be avoided by stripping dynamic blocks before |
| 97 | * `the_content` gets applied, just like shortcodes. |
| 98 | * |
| 99 | * @ticket 46133 |
| 100 | */ |
| 101 | function test_excerpt_infinite_loop() { |
| 102 | $query = new WP_Query( |
| 103 | array( |
| 104 | 'post__in' => array( self::$post_id ), |
| 105 | ) |
| 106 | ); |
| 107 | $query->the_post(); |
| 108 | $this->assertEmpty( do_blocks( '<!-- wp:core/fake /-->' ) ); |
| 109 | } |
| 110 | } |