Make WordPress Core

Changeset 56761


Ignore:
Timestamp:
10/03/2023 08:52:54 AM (3 years ago)
Author:
gziolo
Message:

Tests: Rename and improve the blocks/context.php file to follow handbook

Renames context.php and Tests_Blocks_Context to renderBlock.php and Tests_Blocks_RenderBlock. See https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#test-classes.

Simplifies also the tear_down method by using the same convention as in [56759].

Props costdev, ockham.
Follow-up [48224].
See #49927.

File:
1 moved

Legend:

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

    r56760 r56761  
    11<?php
    22/**
    3  * Tests for block context functions.
     3 * Tests for render block functions.
    44 *
    55 * @package WordPress
     
    99 * @group blocks
    1010 */
    11 class Tests_Blocks_Context extends WP_UnitTestCase {
    12 
    13         /**
    14          * Registered block names.
    15          *
    16          * @var string[]
    17          */
    18         private $registered_block_names = array();
     11class Tests_Blocks_RenderBlock extends WP_UnitTestCase {
    1912
    2013        /**
     
    3932         */
    4033        public function tear_down() {
    41                 while ( ! empty( $this->registered_block_names ) ) {
    42                         $block_name = array_pop( $this->registered_block_names );
    43                         unregister_block_type( $block_name );
     34                // Removes test block types registered by test cases.
     35                $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
     36                foreach ( $block_types as $block_type ) {
     37                        $block_name = $block_type->name;
     38                        if ( str_starts_with( $block_name, 'tests/' ) ) {
     39                                unregister_block_type( $block_name );
     40                        }
    4441                }
    4542
    4643                parent::tear_down();
    47         }
    48 
    49         /**
    50          * Registers a block type.
    51          *
    52          * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
    53          *                                   complete WP_Block_Type instance. In case a WP_Block_Type
    54          *                                   is provided, the $args parameter will be ignored.
    55          * @param array                $args {
    56          *     Optional. Array of block type arguments. Any arguments may be defined, however the
    57          *     ones described below are supported by default. Default empty array.
    58          *
    59          *     @type callable $render_callback Callback used to render blocks of this block type.
    60          * }
    61          */
    62         protected function register_block_type( $name, $args ) {
    63                 register_block_type( $name, $args );
    64 
    65                 $this->registered_block_names[] = $name;
    6644        }
    6745
     
    7149         *
    7250         * @ticket 49927
     51         *
     52         * @covers ::register_block_type
     53         * @covers ::render_block
    7354         */
    7455        public function test_provides_block_context() {
    7556                $provided_context = array();
    7657
    77                 $this->register_block_type(
    78                         'gutenberg/test-context-provider',
     58                register_block_type(
     59                        'tests/context-provider',
    7960                        array(
    8061                                'attributes'       => array(
     
    9475                                ),
    9576                                'provides_context' => array(
    96                                         'gutenberg/contextWithAssigned'   => 'contextWithAssigned',
    97                                         'gutenberg/contextWithDefault'    => 'contextWithDefault',
    98                                         'gutenberg/contextWithoutDefault' => 'contextWithoutDefault',
    99                                         'gutenberg/contextNotRequested'   => 'contextNotRequested',
     77                                        'tests/contextWithAssigned'   => 'contextWithAssigned',
     78                                        'tests/contextWithDefault'    => 'contextWithDefault',
     79                                        'tests/contextWithoutDefault' => 'contextWithoutDefault',
     80                                        'tests/contextNotRequested'   => 'contextNotRequested',
    10081                                ),
    10182                        )
    10283                );
    10384
    104                 $this->register_block_type(
    105                         'gutenberg/test-context-consumer',
     85                register_block_type(
     86                        'tests/context-consumer',
    10687                        array(
    10788                                'uses_context'    => array(
    108                                         'gutenberg/contextWithDefault',
    109                                         'gutenberg/contextWithAssigned',
    110                                         'gutenberg/contextWithoutDefault',
     89                                        'tests/contextWithDefault',
     90                                        'tests/contextWithAssigned',
     91                                        'tests/contextWithoutDefault',
    11192                                ),
    11293                                'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
     
    119100
    120101                $parsed_blocks = parse_blocks(
    121                         '<!-- wp:gutenberg/test-context-provider {"contextWithAssigned":10} -->' .
    122                         '<!-- wp:gutenberg/test-context-consumer /-->' .
    123                         '<!-- /wp:gutenberg/test-context-provider -->'
     102                        '<!-- wp:tests/context-provider {"contextWithAssigned":10} -->' .
     103                        '<!-- wp:tests/context-consumer /-->' .
     104                        '<!-- /wp:tests/context-provider -->'
    124105                );
    125106
     
    128109                $this->assertSame(
    129110                        array(
    130                                 'gutenberg/contextWithDefault'  => 0,
    131                                 'gutenberg/contextWithAssigned' => 10,
     111                                'tests/contextWithDefault'  => 0,
     112                                'tests/contextWithAssigned' => 10,
    132113                        ),
    133114                        $provided_context[0]
     
    140121         *
    141122         * @ticket 49927
     123         *
     124         * @covers ::register_block_type
     125         * @covers ::render_block
    142126         */
    143127        public function test_provides_default_context() {
     
    146130                $provided_context = array();
    147131
    148                 $this->register_block_type(
    149                         'gutenberg/test-context-consumer',
     132                register_block_type(
     133                        'tests/context-consumer',
    150134                        array(
    151135                                'uses_context'    => array( 'postId', 'postType' ),
     
    158142                );
    159143
    160                 $parsed_blocks = parse_blocks( '<!-- wp:gutenberg/test-context-consumer /-->' );
     144                $parsed_blocks = parse_blocks( '<!-- wp:tests/context-consumer /-->' );
    161145
    162146                render_block( $parsed_blocks[0] );
     
    175159         *
    176160         * @ticket 49927
     161         *
     162         * @covers ::register_block_type
     163         * @covers ::render_block
    177164         */
    178165        public function test_default_context_is_filterable() {
    179166                $provided_context = array();
    180167
    181                 $this->register_block_type(
    182                         'gutenberg/test-context-consumer',
     168                register_block_type(
     169                        'tests/context-consumer',
    183170                        array(
    184171                                'uses_context'    => array( 'example' ),
     
    196183                };
    197184
    198                 $parsed_blocks = parse_blocks( '<!-- wp:gutenberg/test-context-consumer /-->' );
     185                $parsed_blocks = parse_blocks( '<!-- wp:tests/context-consumer /-->' );
    199186
    200187                add_filter( 'render_block_context', $filter_block_context );
Note: See TracChangeset for help on using the changeset viewer.