76 lines
3.0 KiB
PHP
76 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* Tests for the early theme preference bootstrap.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/** Verify no-flash bootstrap and toggle block contracts. */
|
|
class ThemePreferenceTest extends WP_UnitTestCase {
|
|
/** Bootstrap runs at the earliest wp_head priority. */
|
|
public function test_bootstrap_is_registered_at_priority_zero() {
|
|
$this->assertSame( 0, has_action( 'wp_head', 'chrnx_print_theme_preference_bootstrap' ) );
|
|
}
|
|
|
|
/** Bootstrap applies theme before enqueued stylesheet markup. */
|
|
public function test_bootstrap_precedes_styles_and_uses_only_local_preference_inputs() {
|
|
ob_start();
|
|
do_action( 'wp_head' );
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertStringContainsString( 'chrnx-theme', $output );
|
|
$this->assertStringContainsString( 'prefers-color-scheme: dark', $output );
|
|
$this->assertStringContainsString( 'dataset.theme', $output );
|
|
$this->assertStringContainsString( 'colorScheme', $output );
|
|
$this->assertLessThan( strpos( $output, '<link' ), strpos( $output, 'dataset.theme' ) );
|
|
$this->assertStringNotContainsString( 'cookie', strtolower( $output ) );
|
|
$this->assertStringNotContainsString( 'fetch(', $output );
|
|
}
|
|
|
|
/** Standard inline-script filters can add CSP attributes without replacing the bootstrap. */
|
|
public function test_bootstrap_uses_filterable_wordpress_inline_script_tag() {
|
|
$filter = static function ( $attributes ) {
|
|
$attributes['nonce'] = 'test-nonce';
|
|
$attributes['data-test-policy'] = 'allowed';
|
|
return $attributes;
|
|
};
|
|
add_filter( 'wp_inline_script_attributes', $filter );
|
|
try {
|
|
ob_start();
|
|
chrnx_print_theme_preference_bootstrap();
|
|
$output = ob_get_clean();
|
|
} finally {
|
|
remove_filter( 'wp_inline_script_attributes', $filter );
|
|
}
|
|
|
|
$this->assertStringContainsString( 'nonce="test-nonce"', $output );
|
|
$this->assertStringContainsString( 'data-test-policy="allowed"', $output );
|
|
$this->assertStringContainsString( 'document.documentElement.dataset.theme=t', $output );
|
|
}
|
|
|
|
/** Toggle uses a labeled native radio group with a live status. */
|
|
public function test_toggle_renders_accessible_native_controls() {
|
|
do_action( 'init' );
|
|
$output = render_block(
|
|
array(
|
|
'blockName' => 'chrnx/theme-toggle',
|
|
'attrs' => array(),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
|
|
$this->assertStringContainsString( '<fieldset', $output );
|
|
$this->assertStringContainsString( '<legend', $output );
|
|
$this->assertSame( 3, substr_count( $output, 'type="radio"' ) );
|
|
$this->assertStringContainsString( 'value="light"', $output );
|
|
$this->assertStringContainsString( 'value="dark"', $output );
|
|
$this->assertStringContainsString( 'value="system"', $output );
|
|
$this->assertStringContainsString( 'aria-live="polite"', $output );
|
|
$this->assertStringContainsString( 'data-status-system-light="Light theme active, following system preference"', $output );
|
|
$this->assertStringContainsString( 'data-status-system-dark="Dark theme active, following system preference"', $output );
|
|
$this->assertStringContainsString( '<noscript>', $output );
|
|
}
|
|
}
|