wordpress-develop/tests/phpunit/tests/theme/wpThemeHasThemeJson.php
Tonya Mork 6a9e92dc76 Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.

For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).

Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.

References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.

Follow-up to [54493], [53282], [52744], [52049], [50959].

Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.

git-svn-id: https://develop.svn.wordpress.org/trunk@55086 602fd350-edb4-49c9-b593-d223f7449a82
2023-01-18 11:38:16 +00:00

73 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/base.php';
/**
* Tests wp_theme_has_theme_json().
*
* @group theme_json
*
* @covers ::wp_theme_has_theme_json
*/
class Tests_Theme_WpThemeHasThemeJson extends WP_Theme_UnitTestCase {
/**
* @ticket 56975
*
* @dataProvider data_theme_has_theme_json_reports_correctly
*
* @param string $theme The slug of the theme to switch to.
* @param bool $expected The expected result.
*/
public function test_theme_has_theme_json_reports_correctly( $theme, $expected ) {
switch_theme( $theme );
$this->assertSame( $expected, wp_theme_has_theme_json() );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_theme_has_theme_json_reports_correctly() {
return array(
'a theme with theme.json' => array(
'theme' => 'block-theme',
'expected' => true,
),
'a theme without theme.json' => array(
'theme' => 'default',
'expected' => false,
),
'a child theme with theme.json' => array(
'theme' => 'block-theme-child',
'expected' => true,
),
'a child theme without theme.json and parent theme with theme.json' => array(
'theme' => 'block-theme-child-no-theme-json',
'expected' => true,
),
'a child theme without theme.json and parent theme without theme.json' => array(
'theme' => 'default-child-no-theme-json',
'expected' => false,
),
);
}
/**
* @ticket 52991
*/
public function test_switching_themes_recalculates_support() {
// The "default" theme doesn't have theme.json support.
switch_theme( 'default' );
$default = wp_theme_has_theme_json();
// Switch to a theme that does have support.
switch_theme( 'block-theme' );
$block_theme = wp_theme_has_theme_json();
$this->assertFalse( $default, 'The "default" theme should not report theme.json support.' );
$this->assertTrue( $block_theme, 'The block theme should report theme.json support.' );
}
}