From 0735741b77b82e6283209a9c443413a96a8f4cb5 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Tue, 25 Feb 2025 22:05:12 +0000 Subject: [PATCH] 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 --- tests/phpunit/tests/admin/themeBodyClass.php | 58 ++++++++++++++++++++ tests/phpunit/tests/post/getBodyClass.php | 28 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/phpunit/tests/admin/themeBodyClass.php diff --git a/tests/phpunit/tests/admin/themeBodyClass.php b/tests/phpunit/tests/admin/themeBodyClass.php new file mode 100644 index 0000000000..4a4073521f --- /dev/null +++ b/tests/phpunit/tests/admin/themeBodyClass.php @@ -0,0 +1,58 @@ +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' ); + } +} diff --git a/tests/phpunit/tests/post/getBodyClass.php b/tests/phpunit/tests/post/getBodyClass.php index 6027fc3a07..77c1833d13 100644 --- a/tests/phpunit/tests/post/getBodyClass.php +++ b/tests/phpunit/tests/post/getBodyClass.php @@ -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' ); + } }