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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55572 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-03-21 13:52:29 +00:00
parent aacd572647
commit 95826a28f1
@@ -1,216 +1,264 @@
<?php
require_once __DIR__ . '/base.php';
/**
* Tests wp_get_global_stylesheet().
*
* @group themes
*
* @covers ::wp_get_global_stylesheet
*/
class Tests_Theme_wpGetGlobalStylesheet extends WP_UnitTestCase {
class Tests_Theme_WpGetGlobalStylesheet extends WP_Theme_UnitTestCase {
/**
* Theme root directory.
* Flag to indicate whether to remove 'editor-font-sizes' theme support at tear_down().
*
* @var string
* @var bool
*/
private $theme_root;
private $remove_theme_support_at_teardown = false;
/**
* Original theme directory.
* Flag to indicate whether to switch back to the default theme at tear down.
*
* @var string
* @var bool
*/
private $orig_theme_dir;
public function set_up() {
parent::set_up();
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$this->theme_root = realpath( DIR_TESTDATA . '/themedir1' );
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
// Set up the new root.
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}
private $switch_to_default_theme_at_teardown = false;
public function tear_down() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
// Reset the theme support.
if ( $this->remove_theme_support_at_teardown ) {
$this->remove_theme_support_at_teardown = false;
remove_theme_support( 'editor-font-sizes' );
}
// Clear up the filters to modify the theme root.
remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
if ( $this->switch_to_default_theme_at_teardown ) {
$this->switch_to_default_theme_at_teardown = false;
switch_theme( WP_DEFAULT_THEME );
}
parent::tear_down();
}
public function filter_set_theme_root() {
return $this->theme_root;
}
/**
* @ticket 54782
*
* @dataProvider data_should_conditionally_include_font_sizes
*
* @param array $expected Expected CSS for each font size.
* @param string $theme The theme to switch to / use.
* @param array $types Optional. Types of styles to load. Default empty array.
* @param bool $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
*/
public function test_should_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
$this->maybe_switch_theme( $theme );
$this->add_custom_font_sizes( $classic_has_presets );
public function test_block_theme_using_variables() {
switch_theme( 'block-theme' );
$styles = wp_get_global_stylesheet( $types );
$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
$this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is 100px' );
$this->assertStringContainsString( $expected['small'], $styles, 'The small font size should be included.' );
$this->assertStringContainsString( $expected['medium'], $styles, 'The medium font size should be included.' );
$this->assertStringContainsString( $expected['large'], $styles, 'The large font size should be included.' );
$this->assertStringContainsString( $expected['x-large'], $styles, 'The x-large font size should be included.' );
switch_theme( WP_DEFAULT_THEME );
}
public function test_block_theme_using_presets() {
switch_theme( 'block-theme' );
$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertStringNotContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is not present' );
switch_theme( WP_DEFAULT_THEME );
}
public function test_block_theme_using_defaults() {
switch_theme( 'block-theme' );
$styles = wp_get_global_stylesheet();
$this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
$this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $styles, 'custom font size is 100px' );
switch_theme( WP_DEFAULT_THEME );
}
public function test_variables_in_classic_theme_with_no_presets_using_variables() {
$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
}
public function test_variables_in_classic_theme_with_no_presets_using_presets() {
$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertStringNotContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
}
public function test_variables_in_classic_theme_with_no_presets_using_defaults() {
$styles = wp_get_global_stylesheet();
$this->assertStringContainsString( '--wp--preset--font-size--small: 13px', $styles, 'small font size is 13px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 36px', $styles, 'large font size is 36px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
}
public function test_variables_in_classic_theme_with_presets_using_variables() {
add_theme_support(
'editor-font-sizes',
array(
array(
'name' => 'Small',
'size' => 18,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => 26.25,
'slug' => 'large',
),
)
);
$styles = wp_get_global_stylesheet( array( 'variables' ) );
$this->assertStringContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is 18px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is 26.25px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is 42px' );
remove_theme_support( 'editor-font-sizes' );
}
public function test_variables_in_classic_theme_with_presets_using_presets() {
add_theme_support(
'editor-font-sizes',
array(
array(
'name' => 'Small',
'size' => 18,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => 26.25,
'slug' => 'large',
),
)
);
$styles = wp_get_global_stylesheet( array( 'presets' ) );
$this->assertStringNotContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is not present' );
$this->assertStringNotContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'x-large font size is not present' );
remove_theme_support( 'editor-font-sizes' );
}
public function test_variables_in_classic_theme_with_presets_using_defaults() {
add_theme_support(
'editor-font-sizes',
array(
array(
'name' => 'Small',
'size' => 18,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => 26.25,
'slug' => 'large',
),
)
);
$styles = wp_get_global_stylesheet();
$this->assertStringContainsString( '--wp--preset--font-size--small: 18px', $styles, 'small font size is 18px' );
$this->assertStringContainsString( '--wp--preset--font-size--medium: 20px', $styles, 'medium font size is 20px' );
$this->assertStringContainsString( '--wp--preset--font-size--large: 26.25px', $styles, 'large font size is 26.25px' );
$this->assertStringContainsString( '--wp--preset--font-size--x-large: 42px', $styles, 'small font size is 42px' );
remove_theme_support( 'editor-font-sizes' );
if ( 'default' !== $theme ) {
$this->assertStringContainsString( $expected['custom'], $styles, 'The custom font size should be included.' );
}
}
/**
* Tests that switching themes recalculates the stylesheet.
* Data provider.
*
* @return array[]
*/
public function data_should_conditionally_include_font_sizes() {
return array(
'block theme using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
),
'block theme using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
'types' => array( 'variables' ),
),
'classic theme without presets using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
),
'classic theme without presets using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'variables' ),
),
'classic theme with presets using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array(),
'classic_has_presets' => true,
),
'classic theme with presets using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'variables' ),
'classic_has_presets' => true,
),
);
}
/**
* @ticket 54782
*
* @dataProvider data_should_not_conditionally_include_font_sizes
*
* @param array $expected Expected CSS for each font size.
* @param string $theme The theme to switch to / use.
* @param array $types Optional. Types of styles to load. Default empty array.
* @param bool $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
*/
public function test_should_not_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
$this->maybe_switch_theme( $theme );
$this->add_custom_font_sizes( $classic_has_presets );
$styles = wp_get_global_stylesheet( $types );
$this->assertStringNotContainsString( $expected['small'], $styles, 'The small font size should not be included.' );
$this->assertStringNotContainsString( $expected['medium'], $styles, 'The medium font size should not be included.' );
$this->assertStringNotContainsString( $expected['large'], $styles, 'The large font size should not be included.' );
$this->assertStringNotContainsString( $expected['x-large'], $styles, 'The x-large font size should not be included.' );
if ( 'default' !== $theme ) {
$this->assertStringNotContainsString( $expected['custom'], $styles, 'The custom font size should not be included.' );
}
}
/**
* Data provider.
*
* @return array[]
*/
public function data_should_not_conditionally_include_font_sizes() {
return array(
'block theme using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
'types' => array( 'presets' ),
),
'classic theme without presets using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'presets' ),
),
'classic theme with presets using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'presets' ),
'classic_has_presets' => true,
),
);
}
/**
* @ticket 56970
*/
public function test_switching_themes_should_recalculate_stylesheet() {
$stylesheet_for_default_theme = wp_get_global_stylesheet();
switch_theme( 'block-theme' );
$stylesheet_for_block_theme = wp_get_global_stylesheet();
switch_theme( WP_DEFAULT_THEME );
$expected = '--wp--preset--font-size--custom: 100px;';
$this->assertStringNotContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_default_theme, 'custom font size (100px) not present for default theme' );
$this->assertStringContainsString( '--wp--preset--font-size--custom: 100px;', $stylesheet_for_block_theme, 'custom font size (100px) is present for block theme' );
$stylesheet_for_default_theme = wp_get_global_stylesheet();
$this->assertStringNotContainsString( $expected, $stylesheet_for_default_theme, 'Custom font size (100px) should not present for default theme' );
$this->maybe_switch_theme( 'block-theme' );
$stylesheet_for_block_theme = wp_get_global_stylesheet();
$this->assertStringContainsString( $expected, $stylesheet_for_block_theme, 'Custom font size (100px) should be present for block theme' );
}
/**
* Adds the 'editor-font-sizes' theme support with custom font sizes.
*
* @param bool $add_theme_support Whether to add the theme support.
* @param int $small Optional. Small font size in pixels. Default 18.
* @param int $large Optional. Large font size in pixels. Default 26.25.
*/
private function add_custom_font_sizes( $add_theme_support, $small = 18, $large = 26.25 ) {
if ( ! $add_theme_support ) {
return;
}
$args = array(
array(
'name' => 'Small',
'size' => $small,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => $large,
'slug' => 'large',
),
);
add_theme_support( 'editor-font-sizes', $args );
$this->remove_theme_support_at_teardown = true;
}
/**
* Switches the theme when not the 'default' theme.
*
* @param string $theme Theme name to switch to.
*/
private function maybe_switch_theme( $theme ) {
if ( 'default' === $theme ) {
return;
}
switch_theme( $theme );
$this->switch_to_default_theme_at_teardown = true;
}
}