Make WordPress Core

Changeset 55572 for trunk


Ignore:
Timestamp:
03/21/2023 01:52:29 PM (4 years ago)
Author:
hellofromTonya
Message:

Tests: Rename test class and improve tests for wp_get_global_stylesheet().

Changes include:

  • Renames the test class to be compliant with test coding standards.
  • Converts the test class to extend WP_Theme_UnitTestCase to reuse test fixtures.
  • Implements data providers to encapsulate datasets and reduce code repetition.
  • Adds a @covers annotation.
  • Improves assertion messages to help with diagnosing failed tests.

Follow-up to [55567], [55148], [52682], [53916], [52675-52677].

Props costdev, hellofromTonya.
See #57841, #57958.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php

    r55567 r55572  
    11<?php
     2
     3require_once __DIR__ . '/base.php';
    24
    35/**
    … …  
    57 *
    68 * @group themes
     9 *
     10 * @covers ::wp_get_global_stylesheet
    711 */
    8 class Tests_Theme_wpGetGlobalStylesheet extends WP_UnitTestCase {
    9 
    10         /**
    11          * Theme root directory.
    12          *
    13          * @var string
    14          */
    15         private $theme_root;
    16 
    17         /**
    18          * Original theme directory.
    19          *
    20          * @var string
    21          */
    22         private $orig_theme_dir;
    23 
    24         public function set_up() {
    25                 parent::set_up();
    26 
    27                 $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
    28                 $this->theme_root     = realpath( DIR_TESTDATA . '/themedir1' );
    29 
    30                 // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
    31                 $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
    32 
    33                 // Set up the new root.
    34                 add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
    35                 add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
    36                 add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
    37 
    38                 // Clear caches.
    39                 wp_clean_themes_cache();
    40                 unset( $GLOBALS['wp_themes'] );
    41         }
     12class Tests_Theme_WpGetGlobalStylesheet extends WP_Theme_UnitTestCase {
     13
     14        /**
     15         * Flag to indicate whether to remove 'editor-font-sizes' theme support at tear_down().
     16         *
     17         * @var bool
     18         */
     19        private $remove_theme_support_at_teardown = false;
     20
     21        /**
     22         * Flag to indicate whether to switch back to the default theme at tear down.
     23         *
     24         * @var bool
     25         */
     26        private $switch_to_default_theme_at_teardown = false;
    4227
    4328        public function tear_down() {
    44                 $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
    45 
    46                 // Clear up the filters to modify the theme root.
    47                 remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
    48                 remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
    49                 remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
    50 
    51                 wp_clean_themes_cache();
    52                 unset( $GLOBALS['wp_themes'] );
     29                // Reset the theme support.
     30                if ( $this->remove_theme_support_at_teardown ) {
     31                        $this->remove_theme_support_at_teardown = false;
     32                        remove_theme_support( 'editor-font-sizes' );
     33                }
     34
     35                if ( $this->switch_to_default_theme_at_teardown ) {
     36                        $this->switch_to_default_theme_at_teardown = false;
     37                        switch_theme( WP_DEFAULT_THEME );
     38                }
    5339
    5440                parent::tear_down();
    5541        }
    5642
    57         public function filter_set_theme_root() {
    58                 return $this->theme_root;
    59         }
    60 
    61         public function test_block_theme_using_variables() {
    62                 switch_theme( 'block-theme' );
    63 
    64                 $styles = wp_get_global_stylesheet( array( 'variables' ) );
    65                 $this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
    66                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    67                 $this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
    68                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
    69                 $this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is 100px' );
    70 
    71                 switch_theme( WP_DEFAULT_THEME );
    72         }
    73 
    74         public function test_block_theme_using_presets() {
    75                 switch_theme( 'block-theme' );
    76 
    77                 $styles = wp_get_global_stylesheet( array( 'presets' ) );
    78                 $this->assertStringNotContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is not present' );
    79                 $this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
    80                 $this->assertStringNotContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is not present' );
    81                 $this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
    82                 $this->assertStringNotContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is not present' );
    83 
    84                 switch_theme( WP_DEFAULT_THEME );
    85         }
    86 
    87         public function test_block_theme_using_defaults() {
    88                 switch_theme( 'block-theme' );
    89 
    90                 $styles = wp_get_global_stylesheet();
    91                 $this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
    92                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    93                 $this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
    94                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
    95                 $this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is 100px' );
    96 
    97                 switch_theme( WP_DEFAULT_THEME );
    98         }
    99 
    100         public function test_variables_in_classic_theme_with_no_presets_using_variables() {
    101                 $styles = wp_get_global_stylesheet( array( 'variables' ) );
    102                 $this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
    103                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    104                 $this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
    105                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
    106         }
    107 
    108         public function test_variables_in_classic_theme_with_no_presets_using_presets() {
    109                 $styles = wp_get_global_stylesheet( array( 'presets' ) );
    110                 $this->assertStringNotContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is not present' );
    111                 $this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
    112                 $this->assertStringNotContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is not present' );
    113                 $this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
    114         }
    115 
    116         public function test_variables_in_classic_theme_with_no_presets_using_defaults() {
    117                 $styles = wp_get_global_stylesheet();
    118                 $this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
    119                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    120                 $this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
    121                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
    122         }
    123 
    124         public function test_variables_in_classic_theme_with_presets_using_variables() {
    125                 add_theme_support(
    126                         'editor-font-sizes',
     43        /**
     44         * @ticket 54782
     45         *
     46         * @dataProvider data_should_conditionally_include_font_sizes
     47         *
     48         * @param array  $expected            Expected CSS for each font size.
     49         * @param string $theme               The theme to switch to / use.
     50         * @param array  $types               Optional. Types of styles to load. Default empty array.
     51         * @param bool   $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
     52         */
     53        public function test_should_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
     54                $this->maybe_switch_theme( $theme );
     55                $this->add_custom_font_sizes( $classic_has_presets );
     56
     57                $styles = wp_get_global_stylesheet( $types );
     58
     59                $this->assertStringContainsString( $expected['small'], $styles, 'The small font size should be included.' );
     60                $this->assertStringContainsString( $expected['medium'], $styles, 'The medium font size should be included.' );
     61                $this->assertStringContainsString( $expected['large'], $styles, 'The large font size should be included.' );
     62                $this->assertStringContainsString( $expected['x-large'], $styles, 'The x-large font size should be included.' );
     63
     64                if ( 'default' !== $theme ) {
     65                        $this->assertStringContainsString( $expected['custom'], $styles, 'The custom font size should be included.' );
     66                }
     67        }
     68
     69        /**
     70         * Data provider.
     71         *
     72         * @return array[]
     73         */
     74        public function data_should_conditionally_include_font_sizes() {
     75                return array(
     76                        'block theme using defaults'                   => array(
     77                                'expected' => array(
     78                                        'small'   => '--wp--preset--font-size--small: 13px',
     79                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     80                                        'large'   => '--wp--preset--font-size--large: 36px',
     81                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     82                                        'custom'  => '--wp--preset--font-size--custom: 100px;',
     83                                ),
     84                                'theme'    => 'block-theme',
     85                        ),
     86                        'block theme using variables'                  => array(
     87                                'expected' => array(
     88                                        'small'   => '--wp--preset--font-size--small: 13px',
     89                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     90                                        'large'   => '--wp--preset--font-size--large: 36px',
     91                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     92                                        'custom'  => '--wp--preset--font-size--custom: 100px;',
     93                                ),
     94                                'theme'    => 'block-theme',
     95                                'types'    => array( 'variables' ),
     96                        ),
     97                        'classic theme without presets using defaults' => array(
     98                                'expected' => array(
     99                                        'small'   => '--wp--preset--font-size--small: 13px',
     100                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     101                                        'large'   => '--wp--preset--font-size--large: 36px',
     102                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     103                                ),
     104                                'theme'    => 'default',
     105                        ),
     106                        'classic theme without presets using variables' => array(
     107                                'expected' => array(
     108                                        'small'   => '--wp--preset--font-size--small: 13px',
     109                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     110                                        'large'   => '--wp--preset--font-size--large: 36px',
     111                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     112                                ),
     113                                'theme'    => 'default',
     114                                'types'    => array( 'variables' ),
     115                        ),
     116                        'classic theme with presets using defaults'    => array(
     117                                'expected'            => array(
     118                                        'small'   => '--wp--preset--font-size--small: 18px',
     119                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     120                                        'large'   => '--wp--preset--font-size--large: 26.25px',
     121                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     122                                ),
     123                                'theme'               => 'default',
     124                                'types'               => array(),
     125                                'classic_has_presets' => true,
     126                        ),
     127                        'classic theme with presets using variables'   => array(
     128                                'expected'            => array(
     129                                        'small'   => '--wp--preset--font-size--small: 18px',
     130                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     131                                        'large'   => '--wp--preset--font-size--large: 26.25px',
     132                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     133                                ),
     134                                'theme'               => 'default',
     135                                'types'               => array( 'variables' ),
     136                                'classic_has_presets' => true,
     137                        ),
     138                );
     139        }
     140
     141        /**
     142         * @ticket 54782
     143         *
     144         * @dataProvider data_should_not_conditionally_include_font_sizes
     145         *
     146         * @param array  $expected            Expected CSS for each font size.
     147         * @param string $theme               The theme to switch to / use.
     148         * @param array  $types               Optional. Types of styles to load. Default empty array.
     149         * @param bool   $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
     150         */
     151        public function test_should_not_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
     152                $this->maybe_switch_theme( $theme );
     153                $this->add_custom_font_sizes( $classic_has_presets );
     154
     155                $styles = wp_get_global_stylesheet( $types );
     156
     157                $this->assertStringNotContainsString( $expected['small'], $styles, 'The small font size should not be included.' );
     158                $this->assertStringNotContainsString( $expected['medium'], $styles, 'The medium font size should not be included.' );
     159                $this->assertStringNotContainsString( $expected['large'], $styles, 'The large font size should not be included.' );
     160                $this->assertStringNotContainsString( $expected['x-large'], $styles, 'The x-large font size should not be included.' );
     161
     162                if ( 'default' !== $theme ) {
     163                        $this->assertStringNotContainsString( $expected['custom'], $styles, 'The custom font size should not be included.' );
     164                }
     165        }
     166
     167        /**
     168         * Data provider.
     169         *
     170         * @return array[]
     171         */
     172        public function data_should_not_conditionally_include_font_sizes() {
     173                return array(
     174                        'block theme using presets'                   => array(
     175                                'expected' => array(
     176                                        'small'   => '--wp--preset--font-size--small: 13px',
     177                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     178                                        'large'   => '--wp--preset--font-size--large: 36px',
     179                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     180                                        'custom'  => '--wp--preset--font-size--custom: 100px;',
     181                                ),
     182                                'theme'    => 'block-theme',
     183                                'types'    => array( 'presets' ),
     184                        ),
     185                        'classic theme without presets using presets' => array(
     186                                'expected' => array(
     187                                        'small'   => '--wp--preset--font-size--small: 13px',
     188                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     189                                        'large'   => '--wp--preset--font-size--large: 36px',
     190                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     191                                ),
     192                                'theme'    => 'default',
     193                                'types'    => array( 'presets' ),
     194                        ),
     195                        'classic theme with presets using presets'    => array(
     196                                'expected'            => array(
     197                                        'small'   => '--wp--preset--font-size--small: 18px',
     198                                        'medium'  => '--wp--preset--font-size--medium: 20px',
     199                                        'large'   => '--wp--preset--font-size--large: 26.25px',
     200                                        'x-large' => '--wp--preset--font-size--x-large: 42px',
     201                                ),
     202                                'theme'               => 'default',
     203                                'types'               => array( 'presets' ),
     204                                'classic_has_presets' => true,
     205                        ),
     206                );
     207        }
     208
     209        /**
     210         * @ticket 56970
     211         */
     212        public function test_switching_themes_should_recalculate_stylesheet() {
     213                $expected = '--wp--preset--font-size--custom: 100px;';
     214
     215                $stylesheet_for_default_theme = wp_get_global_stylesheet();
     216                $this->assertStringNotContainsString( $expected, $stylesheet_for_default_theme, 'Custom font size (100px) should not present for default theme' );
     217
     218                $this->maybe_switch_theme( 'block-theme' );
     219                $stylesheet_for_block_theme = wp_get_global_stylesheet();
     220                $this->assertStringContainsString( $expected, $stylesheet_for_block_theme, 'Custom font size (100px) should be present for block theme' );
     221        }
     222
     223        /**
     224         * Adds the 'editor-font-sizes' theme support with custom font sizes.
     225         *
     226         * @param bool $add_theme_support Whether to add the theme support.
     227         * @param int  $small             Optional. Small font size in pixels. Default 18.
     228         * @param int  $large             Optional. Large font size in pixels. Default 26.25.
     229         */
     230        private function add_custom_font_sizes( $add_theme_support, $small = 18, $large = 26.25 ) {
     231                if ( ! $add_theme_support ) {
     232                        return;
     233                }
     234
     235                $args = array(
    127236                        array(
    128                                 array(
    129                                         'name' => 'Small',
    130                                         'size' => 18,
    131                                         'slug' => 'small',
    132                                 ),
    133                                 array(
    134                                         'name' => 'Large',
    135                                         'size' => 26.25,
    136                                         'slug' => 'large',
    137                                 ),
    138                         )
     237                                'name' => 'Small',
     238                                'size' => $small,
     239                                'slug' => 'small',
     240                        ),
     241                        array(
     242                                'name' => 'Large',
     243                                'size' => $large,
     244                                'slug' => 'large',
     245                        ),
    139246                );
    140 
    141                 $styles = wp_get_global_stylesheet( array( 'variables' ) );
    142                 $this->assertStringContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is 18px' );
    143                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    144                 $this->assertStringContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is 26.25px' );
    145                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
    146 
    147                 remove_theme_support( 'editor-font-sizes' );
    148         }
    149 
    150         public function test_variables_in_classic_theme_with_presets_using_presets() {
    151                 add_theme_support(
    152                         'editor-font-sizes',
    153                         array(
    154                                 array(
    155                                         'name' => 'Small',
    156                                         'size' => 18,
    157                                         'slug' => 'small',
    158                                 ),
    159                                 array(
    160                                         'name' => 'Large',
    161                                         'size' => 26.25,
    162                                         'slug' => 'large',
    163                                 ),
    164                         )
    165                 );
    166 
    167                 $styles = wp_get_global_stylesheet( array( 'presets' ) );
    168                 $this->assertStringNotContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is not present' );
    169                 $this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
    170                 $this->assertStringNotContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is not present' );
    171                 $this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
    172 
    173                 remove_theme_support( 'editor-font-sizes' );
    174         }
    175 
    176         public function test_variables_in_classic_theme_with_presets_using_defaults() {
    177                 add_theme_support(
    178                         'editor-font-sizes',
    179                         array(
    180                                 array(
    181                                         'name' => 'Small',
    182                                         'size' => 18,
    183                                         'slug' => 'small',
    184                                 ),
    185                                 array(
    186                                         'name' => 'Large',
    187                                         'size' => 26.25,
    188                                         'slug' => 'large',
    189                                 ),
    190                         )
    191                 );
    192 
    193                 $styles = wp_get_global_stylesheet();
    194                 $this->assertStringContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is 18px' );
    195                 $this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
    196                 $this->assertStringContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is 26.25px' );
    197                 $this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'small font size is 42px' );
    198 
    199                 remove_theme_support( 'editor-font-sizes' );
    200         }
    201 
    202         /**
    203          * Tests that switching themes recalculates the stylesheet.
    204          *
    205          * @ticket 56970
    206          */
    207         public function test_switching_themes_should_recalculate_stylesheet() {
    208                 $stylesheet_for_default_theme = wp_get_global_stylesheet();
    209                 switch_theme( 'block-theme' );
    210                 $stylesheet_for_block_theme = wp_get_global_stylesheet();
    211                 switch_theme( WP_DEFAULT_THEME );
    212 
    213                 $this->assertStringNotContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_default_theme, 'custom font size (100px) not present for default theme' );
    214                 $this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_block_theme, 'custom font size (100px) is present for block theme' );
     247                add_theme_support( 'editor-font-sizes', $args );
     248                $this->remove_theme_support_at_teardown = true;
     249        }
     250
     251        /**
     252         * Switches the theme when not the 'default' theme.
     253         *
     254         * @param string $theme Theme name to switch to.
     255         */
     256        private function maybe_switch_theme( $theme ) {
     257                if ( 'default' === $theme ) {
     258                        return;
     259                }
     260
     261                switch_theme( $theme );
     262                $this->switch_to_default_theme_at_teardown = true;
    215263        }
    216264}
Note: See TracChangeset for help on using the changeset viewer.