Build/Test Tools: Add tests for theme-related body classes.

This changeset adds new unit test cases following [59698].

Props sukhendu2002.
Fixes #19736.



git-svn-id: https://develop.svn.wordpress.org/trunk@59869 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2025-02-25 22:05:12 +00:00
parent 1af6d01b03
commit 0735741b77
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
/**
* @group admin
*/
class Tests_Admin_Theme_Body_Class extends WP_UnitTestCase {
protected static $admin_user;
protected $original_theme;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_user = $factory->user->create_and_get( array( 'role' => 'administrator' ) );
}
public function set_up() {
parent::set_up();
wp_set_current_user( self::$admin_user->ID );
set_current_screen( 'edit.php' );
$GLOBALS['admin_body_class'] = '';
$this->original_theme = wp_get_theme();
}
public function tear_down() {
$GLOBALS['admin_body_class'] = '';
switch_theme( $this->original_theme->get_stylesheet() );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
parent::tear_down();
}
/**
* Test theme-related admin body classes.
*
* @ticket 19736
*/
public function test_theme_admin_body_classes() {
global $admin_body_class;
switch_theme( 'block-theme' );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
$this->assertStringContainsString( 'wp-theme-block-theme', $admin_body_class, 'Parent theme admin body class not found' );
$admin_body_class = '';
switch_theme( 'block-theme-child' );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
if ( is_child_theme() ) {
$admin_body_class .= ' wp-child-theme-' . sanitize_html_class( get_stylesheet() );
}
$this->assertStringContainsString( 'wp-theme-block-theme', $admin_body_class, 'Parent theme admin body class not found in child theme context' );
$this->assertStringContainsString( 'wp-child-theme-block-theme-child', $admin_body_class, 'Child theme admin body class not found' );
}
}

View File

@ -258,4 +258,32 @@ class Tests_Post_GetBodyClass extends WP_UnitTestCase {
$this->assertContains( 'page', $class );
$this->assertContains( "page-id-{$page_id}", $class );
}
/**
* Test theme-related body classes.
*
* @ticket 19736
*/
public function test_theme_body_classes() {
$original_theme = wp_get_theme();
switch_theme( 'block-theme' );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
$classes = get_body_class();
$this->assertContains( 'wp-theme-block-theme', $classes, 'Parent theme body class not found' );
switch_theme( 'block-theme-child' );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
$classes = get_body_class();
$this->assertContains( 'wp-theme-block-theme', $classes, 'Parent theme body class not found in child theme context' );
$this->assertContains( 'wp-child-theme-block-theme-child', $classes, 'Child theme body class not found' );
switch_theme( $original_theme->get_stylesheet() );
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
}
}