Skip to content

Commit 0735741

Browse files
committed
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
1 parent 1af6d01 commit 0735741

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* @group admin
5+
*/
6+
class Tests_Admin_Theme_Body_Class extends WP_UnitTestCase {
7+
protected static $admin_user;
8+
protected $original_theme;
9+
10+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
11+
self::$admin_user = $factory->user->create_and_get( array( 'role' => 'administrator' ) );
12+
}
13+
14+
public function set_up() {
15+
parent::set_up();
16+
wp_set_current_user( self::$admin_user->ID );
17+
set_current_screen( 'edit.php' );
18+
$GLOBALS['admin_body_class'] = '';
19+
$this->original_theme = wp_get_theme();
20+
}
21+
22+
public function tear_down() {
23+
$GLOBALS['admin_body_class'] = '';
24+
switch_theme( $this->original_theme->get_stylesheet() );
25+
do_action( 'setup_theme' );
26+
do_action( 'after_setup_theme' );
27+
parent::tear_down();
28+
}
29+
30+
/**
31+
* Test theme-related admin body classes.
32+
*
33+
* @ticket 19736
34+
*/
35+
public function test_theme_admin_body_classes() {
36+
global $admin_body_class;
37+
38+
switch_theme( 'block-theme' );
39+
do_action( 'setup_theme' );
40+
do_action( 'after_setup_theme' );
41+
42+
$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
43+
$this->assertStringContainsString( 'wp-theme-block-theme', $admin_body_class, 'Parent theme admin body class not found' );
44+
45+
$admin_body_class = '';
46+
switch_theme( 'block-theme-child' );
47+
do_action( 'setup_theme' );
48+
do_action( 'after_setup_theme' );
49+
50+
$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
51+
if ( is_child_theme() ) {
52+
$admin_body_class .= ' wp-child-theme-' . sanitize_html_class( get_stylesheet() );
53+
}
54+
55+
$this->assertStringContainsString( 'wp-theme-block-theme', $admin_body_class, 'Parent theme admin body class not found in child theme context' );
56+
$this->assertStringContainsString( 'wp-child-theme-block-theme-child', $admin_body_class, 'Child theme admin body class not found' );
57+
}
58+
}

tests/phpunit/tests/post/getBodyClass.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,32 @@ public function test_privacy_policy_body_class() {
258258
$this->assertContains( 'page', $class );
259259
$this->assertContains( "page-id-{$page_id}", $class );
260260
}
261+
262+
/**
263+
* Test theme-related body classes.
264+
*
265+
* @ticket 19736
266+
*/
267+
public function test_theme_body_classes() {
268+
$original_theme = wp_get_theme();
269+
270+
switch_theme( 'block-theme' );
271+
do_action( 'setup_theme' );
272+
do_action( 'after_setup_theme' );
273+
274+
$classes = get_body_class();
275+
$this->assertContains( 'wp-theme-block-theme', $classes, 'Parent theme body class not found' );
276+
277+
switch_theme( 'block-theme-child' );
278+
do_action( 'setup_theme' );
279+
do_action( 'after_setup_theme' );
280+
281+
$classes = get_body_class();
282+
$this->assertContains( 'wp-theme-block-theme', $classes, 'Parent theme body class not found in child theme context' );
283+
$this->assertContains( 'wp-child-theme-block-theme-child', $classes, 'Child theme body class not found' );
284+
285+
switch_theme( $original_theme->get_stylesheet() );
286+
do_action( 'setup_theme' );
287+
do_action( 'after_setup_theme' );
288+
}
261289
}

0 commit comments

Comments
 (0)