Make WordPress Core

Changeset 56759


Ignore:
Timestamp:
10/03/2023 08:27:51 AM (9 months ago)
Author:
gziolo
Message:

Tests: Cover Block Hooks integration with a custom block theme

Adds a simplified version of Twenty Twenty-Three theme that helps testing Block Hooks integration. The theme contains:

  • The required index.html template.
  • The optional single.html template used with tests.
  • 3 template parts where two of them reference patterns.
  • 3 patterns referenced in the templates and the template parts.

New tests automatically register 4 custom blocks with the test theme where each of them hooks into another block using all four target relative positions: before, after, firstChild, lastChild.

The tests verify that the block gets hooked into the correct positions when targeting:

  • template
  • template part
  • pattern

Props ockham, costdev.
See #59313, #59383.
Follow-up [56610].

Location:
trunk/tests/phpunit
Files:
28 added
2 edited

Legend:

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

    r56704 r56759  
    1 <?php
    2 /**
    3  * Tests for block hooks feature functions.
    4  *
    5  * @package WordPress
    6  * @subpackage Blocks
    7  *
    8  * @since 6.4.0
    9  *
    10  * @group blocks
    11  */
    12 class Tests_Blocks_BlockHooks extends WP_UnitTestCase {
    13 
    14     /**
    15      * Tear down after each test.
    16      *
    17      * @since 6.4.0
    18      */
    19     public function tear_down() {
    20         $registry = WP_Block_Type_Registry::get_instance();
    21 
    22         foreach ( array( 'tests/my-block', 'tests/my-container-block' ) as $block_name ) {
    23             if ( $registry->is_registered( $block_name ) ) {
    24                 $registry->unregister( $block_name );
    25             }
    26         }
    27 
    28         parent::tear_down();
    29     }
    30 
    31     /**
    32      * @ticket 59383
    33      *
    34      * @covers ::get_hooked_blocks
    35      */
    36     public function test_get_hooked_blocks_no_match_found() {
    37         $result = get_hooked_blocks( 'tests/no-hooked-blocks' );
    38 
    39         $this->assertSame( array(), $result );
    40     }
    41 
    42     /**
    43      * @ticket 59383
    44      *
    45      * @covers ::get_hooked_blocks
    46      */
    47     public function test_get_hooked_blocks_matches_found() {
    48         register_block_type(
    49             'tests/injected-one',
    50             array(
    51                 'block_hooks' => array(
    52                     'tests/hooked-at-before'           => 'before',
    53                     'tests/hooked-at-after'            => 'after',
    54                     'tests/hooked-at-before-and-after' => 'before',
    55                 ),
    56             )
    57         );
    58         register_block_type(
    59             'tests/injected-two',
    60             array(
    61                 'block_hooks' => array(
    62                     'tests/hooked-at-before'           => 'before',
    63                     'tests/hooked-at-after'            => 'after',
    64                     'tests/hooked-at-before-and-after' => 'after',
    65                     'tests/hooked-at-first-child'      => 'first_child',
    66                     'tests/hooked-at-last-child'       => 'last_child',
    67                 ),
    68             )
    69         );
    70 
    71         $this->assertSame(
    72             array(
    73                 'before' => array(
    74                     'tests/injected-one',
    75                     'tests/injected-two',
    76                 ),
    77             ),
    78             get_hooked_blocks( 'tests/hooked-at-before' ),
    79             'block hooked at the before position'
    80         );
    81         $this->assertSame(
    82             array(
    83                 'after' => array(
    84                     'tests/injected-one',
    85                     'tests/injected-two',
    86                 ),
    87             ),
    88             get_hooked_blocks( 'tests/hooked-at-after' ),
    89             'block hooked at the after position'
    90         );
    91         $this->assertSame(
    92             array(
    93                 'first_child' => array(
    94                     'tests/injected-two',
    95                 ),
    96             ),
    97             get_hooked_blocks( 'tests/hooked-at-first-child' ),
    98             'block hooked at the first child position'
    99         );
    100         $this->assertSame(
    101             array(
    102                 'last_child' => array(
    103                     'tests/injected-two',
    104                 ),
    105             ),
    106             get_hooked_blocks( 'tests/hooked-at-last-child' ),
    107             'block hooked at the last child position'
    108         );
    109         $this->assertSame(
    110             array(
    111                 'before' => array(
    112                     'tests/injected-one',
    113                 ),
    114                 'after'  => array(
    115                     'tests/injected-two',
    116                 ),
    117             ),
    118             get_hooked_blocks( 'tests/hooked-at-before-and-after' ),
    119             'block hooked before one block and after another'
    120         );
    121     }
    122 }
  • trunk/tests/phpunit/tests/theme/themeDir.php

    r56629 r56759  
    188188            'Block Theme Post Content Default',
    189189            'Block Theme with defined Typography Fonts',
     190            'Block Theme with Hooked Blocks',
    190191            'Empty `fontFace` in theme.json - no webfonts defined',
    191192            'A theme with the Update URI header',
Note: See TracChangeset for help on using the changeset viewer.