Make WordPress Core

Changeset 52383


Ignore:
Timestamp:
12/16/2021 02:45:32 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add unit tests for theme features that block themes should support by default.

By default, block themes should have a few theme supports enabled:

  • post-thumbnails
  • responsive-embeds
  • editor-styles
  • html5 for comment-form, comment-list, style, script
  • automatic-feed-links

They should also load core block assets only when the blocks are rendered. This commit adds the associated tests.

Follow-up to [52369].

Props costdev.
See #54597.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme.php

    r52369 r52383  
    41694169 * Adds default theme supports for block themes when the 'setup_theme' action fires.
    41704170 *
     4171 * See {@see 'setup_theme'}.
     4172 *
    41714173 * @since 5.9.0
    41724174 * @access private
    41734175 */
    41744176function _add_default_theme_supports() {
    4175         if ( wp_is_block_theme() ) {
    4176                 add_theme_support( 'post-thumbnails' );
    4177                 add_theme_support( 'responsive-embeds' );
    4178                 add_theme_support( 'editor-styles' );
    4179                 add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'style', 'script' ) );
    4180                 add_theme_support( 'automatic-feed-links' );
    4181                 add_filter( 'should_load_separate_core_block_assets', '__return_true' );
    4182         }
    4183 }
     4177        if ( ! wp_is_block_theme() ) {
     4178                return;
     4179        }
     4180
     4181        add_theme_support( 'post-thumbnails' );
     4182        add_theme_support( 'responsive-embeds' );
     4183        add_theme_support( 'editor-styles' );
     4184        add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'style', 'script' ) );
     4185        add_theme_support( 'automatic-feed-links' );
     4186
     4187        add_filter( 'should_load_separate_core_block_assets', '__return_true' );
     4188}
  • trunk/tests/phpunit/tests/theme/wpTheme.php

    r52330 r52383  
    354354                );
    355355        }
     356
     357        /**
     358         * Tests that block themes support a feature by default.
     359         *
     360         * @ticket 54597
     361         * @dataProvider data_block_theme_has_default_support
     362         *
     363         * @covers ::_add_default_theme_supports
     364         *
     365         * @param array $support {
     366         *     The feature to check.
     367         *
     368         *     @type string $feature     The feature to check.
     369         *     @type string $sub_feature Optional. The sub-feature to check.
     370         * }
     371         */
     372        public function test_block_theme_has_default_support( $support ) {
     373                $this->helper_requires_block_theme();
     374
     375                $support_data     = array_values( $support );
     376                $support_data_str = implode( ': ', $support_data );
     377
     378                // Remove existing support.
     379                if ( current_theme_supports( ...$support_data ) ) {
     380                        remove_theme_support( ...$support_data );
     381                }
     382
     383                $this->assertFalse(
     384                        current_theme_supports( ...$support_data ),
     385                        "Could not remove support for $support_data_str."
     386                );
     387
     388                do_action( 'setup_theme' );
     389
     390                $this->assertTrue(
     391                        current_theme_supports( ...$support_data ),
     392                        "Does not have default support for $support_data_str."
     393                );
     394        }
     395
     396        /**
     397         * Data provider.
     398         *
     399         * @return array
     400         */
     401        public function data_block_theme_has_default_support() {
     402                return array(
     403                        'post-thumbnails'      => array(
     404                                'support' => array(
     405                                        'feature' => 'post-thumbnails',
     406                                ),
     407                        ),
     408                        'responsive-embeds'    => array(
     409                                'support' => array(
     410                                        'feature' => 'responsive-embeds',
     411                                ),
     412                        ),
     413                        'editor-styles'        => array(
     414                                'support' => array(
     415                                        'feature' => 'editor-styles',
     416                                ),
     417                        ),
     418                        'html5: style'         => array(
     419                                'support' => array(
     420                                        'feature'     => 'html5',
     421                                        'sub_feature' => 'style',
     422                                ),
     423                        ),
     424                        'html5: script'        => array(
     425                                'support' => array(
     426                                        'feature'     => 'html5',
     427                                        'sub_feature' => 'script',
     428                                ),
     429                        ),
     430                        'automatic-feed-links' => array(
     431                                'support' => array(
     432                                        'feature' => 'automatic-feed-links',
     433                                ),
     434                        ),
     435                );
     436        }
     437
     438        /**
     439         * Tests that block themes load separate core block assets by default.
     440         *
     441         * @ticket 54597
     442         *
     443         * @covers ::wp_should_load_separate_core_block_assets
     444         */
     445        public function test_block_theme_should_load_separate_core_block_assets_by_default() {
     446                $this->helper_requires_block_theme();
     447
     448                add_filter( 'should_load_separate_core_block_assets', '__return_false' );
     449
     450                $this->assertFalse(
     451                        wp_should_load_separate_core_block_assets(),
     452                        'Could not disable loading separate core block assets.'
     453                );
     454
     455                do_action( 'setup_theme' );
     456
     457                $this->assertTrue(
     458                        wp_should_load_separate_core_block_assets(),
     459                        'Block themes do not load separate core block assets by default.'
     460                );
     461        }
     462
     463        /**
     464         * Helper function to ensure that a block theme is available and active.
     465         */
     466        private function helper_requires_block_theme() {
     467                // No need to switch if we're already on a block theme.
     468                if ( wp_is_block_theme() ) {
     469                        return;
     470                }
     471
     472                $block_theme        = 'twentytwentytwo';
     473                $block_theme_object = new WP_Theme( $block_theme, WP_CONTENT_DIR . '/themes' );
     474
     475                // Skip if the block theme is not available.
     476                if ( ! $block_theme_object->exists() ) {
     477                        $this->markTestSkipped( "$block_theme must be available." );
     478                }
     479
     480                switch_theme( $block_theme );
     481
     482                // Skip if we could not switch to the block theme.
     483                if ( wp_get_theme()->stylesheet !== $block_theme ) {
     484                        $this->markTestSkipped( "Could not switch to $block_theme." );
     485                }
     486        }
    356487}
Note: See TracChangeset for help on using the changeset viewer.