Make WordPress Core

Changeset 56262


Ignore:
Timestamp:
07/18/2023 04:07:49 PM (18 months ago)
Author:
Bernhard Reiter
Message:

Editor: Add test for context setting in Comment Template block.

Test that commentId context is correctly set and made available by the Comment Template block to the render_block_context filter (at priority 2 and higher), and to the render_block filter (important when programmatically inserting child blocks into the Comment Template block).

Furthermore, test that child blocks inserted via the render_block_data filter are retained and thus present at render_block filter stage.

Props andrewserong, ramonopoly, peterwilsoncc, costdev, mukesh27, flixos90.
Fixes #58839.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/renderCommentTemplate.php

    r55822 r56262  
    513513        );
    514514    }
     515
     516    /**
     517     * Tests that the Comment Template block makes comment ID context available to programmatically inserted child blocks.
     518     *
     519     * @ticket 58839
     520     *
     521     * @covers ::render_block_core_comment_template
     522     * @covers ::block_core_comment_template_render_comments
     523     */
     524    public function test_rendering_comment_template_sets_comment_id_context() {
     525        $render_block_context_callback = new MockAction();
     526        add_filter( 'render_block_context', array( $render_block_context_callback, 'filter' ), 2, 3 );
     527
     528        $parsed_comment_author_name_block = parse_blocks( '<!-- wp:comment-author-name /-->' )[0];
     529        $comment_author_name_block        = new WP_Block(
     530            $parsed_comment_author_name_block,
     531            array(
     532                'commentId' => self::$comment_ids[0],
     533            )
     534        );
     535        $comment_author_name_block_markup = $comment_author_name_block->render();
     536
     537        add_filter(
     538            'render_block',
     539            static function( $block_content, $block ) use ( $parsed_comment_author_name_block ) {
     540                /*
     541                * Insert a Comment Author Name block (which requires `commentId`
     542                * block context to work) after the Comment Content block.
     543                */
     544                if ( 'core/comment-content' !== $block['blockName'] ) {
     545                    return $block_content;
     546                }
     547
     548                $inserted_content = render_block( $parsed_comment_author_name_block );
     549                return $inserted_content . $block_content;
     550            },
     551            10,
     552            3
     553        );
     554
     555        $parsed_blocks = parse_blocks(
     556            '<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
     557        );
     558        $block         = new WP_Block(
     559            $parsed_blocks[0],
     560            array(
     561                'postId' => self::$custom_post->ID,
     562            )
     563        );
     564        $markup        = $block->render();
     565
     566        $this->assertStringContainsString( $comment_author_name_block_markup, $markup );
     567
     568        $args    = $render_block_context_callback->get_args();
     569        $context = $args[0][0];
     570        $this->assertArrayHasKey(
     571            'commentId',
     572            $context,
     573            "commentId block context wasn't set for render_block_context filter at priority 2."
     574        );
     575        $this->assertSame(
     576            strval( self::$comment_ids[0] ),
     577            $context['commentId'],
     578            "commentId block context wasn't set correctly."
     579        );
     580    }
     581
     582    /**
     583     * Tests that an inner block added via the render_block_data filter is retained at render_block stage.
     584     *
     585     * @ticket 58839
     586     *
     587     * @covers ::render_block_core_comment_template
     588     * @covers ::block_core_comment_template_render_comments
     589     */
     590    public function test_inner_block_inserted_by_render_block_data_is_retained() {
     591        $render_block_callback = new MockAction();
     592        add_filter( 'render_block', array( $render_block_callback, 'filter' ), 10, 3 );
     593
     594        $render_block_data_callback = static function( $parsed_block ) {
     595            // Add a Social Links block to a Comment Template block's inner blocks.
     596            if ( 'core/comment-template' === $parsed_block['blockName'] ) {
     597                $inserted_block_markup = <<<END
     598<!-- wp:social-links -->
     599<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--></ul>
     600<!-- /wp:social-links -->'
     601END;
     602
     603                $inserted_blocks = parse_blocks( $inserted_block_markup );
     604
     605                $parsed_block['innerBlocks'][] = $inserted_blocks[0];
     606            }
     607            return $parsed_block;
     608        };
     609
     610        add_filter( 'render_block_data', $render_block_data_callback, 10, 1 );
     611        $parsed_blocks = parse_blocks(
     612            '<!-- wp:comments --><!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template --><!-- /wp:comments -->'
     613        );
     614        $block         = new WP_Block(
     615            $parsed_blocks[0],
     616            array(
     617                'postId' => self::$custom_post->ID,
     618            )
     619        );
     620        $block->render();
     621        remove_filter( 'render_block_data', $render_block_data_callback );
     622
     623        $this->assertSame(
     624            5,
     625            $render_block_callback->get_call_count(),
     626            "render_block filter wasn't called the correct number of 5 times."
     627        );
     628
     629        $args = $render_block_callback->get_args();
     630        $this->assertSame(
     631            'core/comment-content',
     632            $args[0][2]->name,
     633            "render_block filter didn't receive Comment Content block instance upon first call."
     634        );
     635        $this->assertSame(
     636            'core/comment-template',
     637            $args[1][2]->name,
     638            "render_block filter didn't receive Comment Template block instance upon second call."
     639        );
     640        $this->assertCount(
     641            2,
     642            $args[1][2]->inner_blocks,
     643            "Inner block inserted by render_block_data filter wasn't retained."
     644        );
     645        $this->assertInstanceOf(
     646            'WP_Block',
     647            $args[1][2]->inner_blocks[1],
     648            "Inner block inserted by render_block_data isn't a WP_Block class instance."
     649        );
     650        $this->assertSame(
     651            'core/social-links',
     652            $args[1][2]->inner_blocks[1]->name,
     653            "Inner block inserted by render_block_data isn't named as expected."
     654        );
     655    }
    515656}
Note: See TracChangeset for help on using the changeset viewer.