Make WordPress Core

Changeset 63563


Ignore:
Timestamp:
09/09/2026 02:50:06 PM (2 months ago)
Author:
lancewillett
Message:

Tests: Reduce repeated core block style registration.

Register core block styles once per scenario, then check all block handles. Replace per-block data-provider cases with loops, retaining the assertions and adding block names to failure messages.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13457

Props sainathpoojary.
See #66074.

File:
1 edited

Legend:

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

    r61008 r63563  
    5555         * @ticket 58528
    5656         *
    57          * @dataProvider data_block_data
    58          *
    5957         * @covers ::register_core_block_style_handles
    6058         * @covers ::wp_should_load_separate_core_block_assets
    61          *
    62          * @param string $name   The block name.
    63          * @param array  $schema The block's schema.
    6459         */
    65         public function test_wp_should_load_separate_core_block_assets_false( $name, $schema ) {
     60        public function test_wp_should_load_separate_core_block_assets_false() {
    6661                add_filter( 'should_load_separate_core_block_assets', '__return_false' );
    67                 $this->assertFalse( wp_should_load_separate_core_block_assets(), 'Core blocks are not expected to load separate assets' );
    6862                register_core_block_style_handles();
    6963
    70                 foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
    71                         $style_handle = $schema[ $style_field ];
    72                         if ( is_array( $style_handle ) ) {
    73                                 continue;
     64                foreach ( $this->get_block_data() as $name => $schema ) {
     65                        $this->assertFalse( wp_should_load_separate_core_block_assets(), "Block {$name}: Core blocks are not expected to load separate assets" );
     66
     67                        foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
     68                                $style_handle = $schema[ $style_field ];
     69                                if ( is_array( $style_handle ) ) {
     70                                        continue;
     71                                }
     72
     73                                $this->assertArrayNotHasKey( $style_handle, $GLOBALS['wp_styles']->registered, "Block {$name}: The key should not exist, as this style should not be registered" );
    7474                        }
    75 
    76                         $this->assertArrayNotHasKey( $style_handle, $GLOBALS['wp_styles']->registered, 'The key should not exist, as this style should not be registered' );
    7775                }
    7876        }
    … …  
    8280         * @ticket 58528
    8381         *
    84          * @dataProvider data_block_data
    85          *
    8682         * @covers ::register_core_block_style_handles
    8783         * @covers ::wp_should_load_separate_core_block_assets
    88          *
    89          * @param string $name   The block name.
    90          * @param array  $schema The block's schema.
    9184         */
    92         public function test_wp_should_load_separate_core_block_assets_true( $name, $schema ) {
     85        public function test_wp_should_load_separate_core_block_assets_true() {
    9386                add_filter( 'should_load_separate_core_block_assets', '__return_true' );
    94                 $this->assertTrue( wp_should_load_separate_core_block_assets(), 'Core assets are expected to load separately' );
    9587                register_core_block_style_handles();
    9688
    9789                $wp_styles = $GLOBALS['wp_styles'];
    9890
    99                 foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
    100                         $style_handle = $schema[ $style_field ];
    101                         if ( is_array( $style_handle ) ) {
    102                                 continue;
    103                         }
     91                foreach ( $this->get_block_data() as $name => $schema ) {
     92                        $this->assertTrue( wp_should_load_separate_core_block_assets(), "Block {$name}: Core assets are expected to load separately" );
    10493
    105                         $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
    106                         if ( false === $wp_styles->registered[ $style_handle ]->src ) {
    107                                 $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, style path should not be set' );
    108                         } else {
    109                                 $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
    110                                 $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
    111                                 $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
    112                                 $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
     94                        foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
     95                                $style_handle = $schema[ $style_field ];
     96                                if ( is_array( $style_handle ) ) {
     97                                        continue;
     98                                }
     99
     100                                $this->assertArrayHasKey( $style_handle, $wp_styles->registered, "Block {$name}: The key should exist, as this style should be registered" );
     101                                if ( false === $wp_styles->registered[ $style_handle ]->src ) {
     102                                        $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: If source is false, style path should not be set" );
     103                                } else {
     104                                        $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, "Block {$name}: Source of style should contain the includes url" );
     105                                        $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path of the style should exist" );
     106                                        $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path key of the style should exist in extra array" );
     107                                        $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], "Block {$name}: The path key of the style should not be empty" );
     108                                }
    113109                        }
    114110                }
    … …  
    117113        /**
    118114         * @ticket 58560
    119          *
    120          * @dataProvider data_block_data
    121          *
    122          * @param string $name The block name.
    123115         */
    124         public function test_wp_should_load_separate_core_block_assets_current_theme_supports( $name ) {
     116        public function test_wp_should_load_separate_core_block_assets_current_theme_supports() {
    125117                add_filter( 'should_load_separate_core_block_assets', '__return_true' );
    126118                add_theme_support( 'wp-block-styles' );
    … …  
    129121                $wp_styles = $GLOBALS['wp_styles'];
    130122
    131                 $style_handle = "wp-block-{$name}-theme";
     123                foreach ( array_keys( $this->get_block_data() ) as $name ) {
     124                        $style_handle = "wp-block-{$name}-theme";
    132125
    133                 $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
    134                 if ( false === $wp_styles->registered[ $style_handle ]->src ) {
    135                         $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, style path should not be set' );
    136                 } else {
    137                         $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
    138                         $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
    139                         $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
    140                         $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
     126                        $this->assertArrayHasKey( $style_handle, $wp_styles->registered, "Block {$name}: The key should exist, as this style should be registered" );
     127                        if ( false === $wp_styles->registered[ $style_handle ]->src ) {
     128                                $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: If source is false, style path should not be set" );
     129                        } else {
     130                                $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, "Block {$name}: Source of style should contain the includes url" );
     131                                $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path of the style should exist" );
     132                                $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path key of the style should exist in extra array" );
     133                                $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], "Block {$name}: The path key of the style should not be empty" );
     134                        }
    141135                }
    142136        }
    … …  
    144138        /**
    145139         * @ticket 59715
    146          *
    147          * @dataProvider data_block_data
    148          *
    149          * @param string $name The block name.
    150140         */
    151         public function test_register_core_block_style_handles_should_load_rtl_stylesheets_for_rtl_text_direction( $name ) {
     141        public function test_register_core_block_style_handles_should_load_rtl_stylesheets_for_rtl_text_direction() {
    152142                global $wp_locale;
    153143
    … …  
    160150                $wp_styles = $GLOBALS['wp_styles'];
    161151
    162                 $style_handle = "wp-block-{$name}-theme";
    163 
    164152                $wp_locale->text_direction = $orig_text_dir;
    165153
    166                 $this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
    167                 if ( false === $wp_styles->registered[ $style_handle ]->src ) {
    168                         $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, style path should not be set' );
    169                 } else {
    170                         $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
    171                         $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
    172                         $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
    173                         $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
    174                         $this->assertArrayHasKey( 'rtl', $wp_styles->registered[ $style_handle ]->extra, 'The rtl key of the style should exist in extra array' );
     154                foreach ( array_keys( $this->get_block_data() ) as $name ) {
     155                        $style_handle = "wp-block-{$name}-theme";
     156
     157                        $this->assertArrayHasKey( $style_handle, $wp_styles->registered, "Block {$name}: The key should exist, as this style should be registered" );
     158                        if ( false === $wp_styles->registered[ $style_handle ]->src ) {
     159                                $this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: If source is false, style path should not be set" );
     160                        } else {
     161                                $this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, "Block {$name}: Source of style should contain the includes url" );
     162                                $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path of the style should exist" );
     163                                $this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The path key of the style should exist in extra array" );
     164                                $this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], "Block {$name}: The path key of the style should not be empty" );
     165                                $this->assertArrayHasKey( 'rtl', $wp_styles->registered[ $style_handle ]->extra, "Block {$name}: The rtl key of the style should exist in extra array" );
     166                        }
    175167                }
    176168        }
    177169
    178         public function data_block_data() {
     170        /**
     171         * Gets the blocks to check after registering styles once per scenario.
     172         *
     173         * @return array[] Block schemas keyed by block name.
     174         */
     175        private function get_block_data() {
    179176                $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
    180177
    … …  
    192189                        }
    193190
    194                         $data[ $name ] = array( $name, $schema );
     191                        $data[ $name ] = $schema;
    195192                }
    196193
Note: See TracChangeset for help on using the changeset viewer.