Make WordPress Core

Ticket #46133: 46133.2.diff

File 46133.2.diff, 3.2 KB (added by pento, 6 years ago)
  • new file tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php

    diff --git a/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php b/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php
    new file mode 100644
    index 0000000000..166bb1222b
    - +  
     1<?php
     2
     3/**
     4 * @group formatting
     5 * @covers ::excerpt_remove_blocks
     6 */
     7class 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<!-- wp:columns {"columns":1} -->
     20<div class="wp-block-columns has-1-columns">
     21        <!-- wp:column -->
     22        <div class="wp-block-column">
     23                <!-- wp:archives {"displayAsDropdown":false,"showPostCounts":false} /-->
     24        </div>
     25        <!-- /wp:column -->
     26</div>
     27<!-- /wp:columns -->
     28';
     29
     30        public $filtered_content = '
     31
     32<p>paragraph</p>
     33
     34
     35<div class="wp-block-columns has-1-columns">
     36       
     37        <div class="wp-block-column">
     38               
     39        </div>
     40       
     41</div>
     42
     43';
     44
     45        /**
     46         * Fake block rendering function.
     47         *
     48         * @since 5.2.0
     49         *
     50         * @return string Block output.
     51         */
     52        function render_fake_block() {
     53                return get_the_excerpt( self::$post_id );
     54        }
     55
     56        /**
     57         * Set up.
     58         *
     59         * @since 5.2.0
     60         */
     61        function setUp() {
     62                parent::setUp();
     63                self::$post_id = $this->factory()->post->create(
     64                        array(
     65                                'post_excerpt' => '', // Empty excerpt, so it has to be generated.
     66                                'post_content' => '<!-- wp:core/fake /-->',
     67                        )
     68                );
     69                register_block_type(
     70                        'core/fake',
     71                        array(
     72                                'render_callback' => array( $this, 'render_fake_block' ),
     73                        )
     74                );
     75        }
     76
     77        /**
     78         * Tear down.
     79         *
     80         * @since 5.2.0
     81         */
     82        function tearDown() {
     83                parent::tearDown();
     84                $registry = WP_Block_Type_Registry::get_instance();
     85                $registry->unregister( 'core/fake' );
     86                wp_delete_post( self::$post_id, true );
     87        }
     88
     89        /**
     90         * Tests excerpt_remove_blocks().
     91         *
     92         * @ticket 46133
     93         */
     94        function test_excerpt_remove_blocks() {
     95                // Simple dynamic block..
     96                $content = '<!-- wp:core/block /-->';
     97
     98                $this->assertEmpty( excerpt_remove_blocks( $content ) );
     99
     100                // Dynamic block with options, embedded in other content.
     101                $this->assertEquals( $this->filtered_content, excerpt_remove_blocks( $this->content ) );
     102        }
     103
     104        /**
     105         * Tests that dynamic blocks don't cause an out-of-memory error.
     106         *
     107         * When dynamic blocks happen to generate an excerpt, they can cause an
     108         * infinite loop if that block is part of the post's content.
     109         *
     110         * `wp_trim_excerpt()` applies the `the_content` filter, which has
     111         * `do_blocks` attached to it, trying to render the block which again will
     112         * attempt to return an excerpt of that post.
     113         *
     114         * This infinite loop can be avoided by stripping dynamic blocks before
     115         * `the_content` gets applied, just like shortcodes.
     116         *
     117         * @ticket 46133
     118         */
     119        function test_excerpt_infinite_loop() {
     120                $query = new WP_Query(
     121                        array(
     122                                'post__in' => array( self::$post_id ),
     123                        )
     124                );
     125                $query->the_post();
     126                $this->assertEmpty( do_blocks( '<!-- wp:core/fake /-->' ) );
     127        }
     128}