Tests: Synchronize Theme.JSON unit test between Core and Gutenberg.

Merges the changes from Core and Gutenberg for the following tests:

- WP_REST_Global_Styles_Controller_Test
- Tests_Theme_wpThemeJsonResolver
- Tests_Theme_wpThemeJsonSchema
- Tests_Theme_wpThemeJson

This will help ensure the stability of the theme.json style generation.

Props ajlende, scruffian, aaronrobertshaw, get_dave, youknowriad.
Fixes #60387.

git-svn-id: https://develop.svn.wordpress.org/trunk@57662 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Riad Benguella 2024-02-20 09:53:11 +00:00
parent 977653e0f9
commit 18b21c34c8
8 changed files with 1025 additions and 831 deletions

View File

@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{
"slug": "dark",
"name": "Dark",
"color": "#010101"
}
]
}
}
}
}
}

View File

@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 1,
"version": 2,
"settings": {
"color": {
"palette": [

View File

@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f2f2f2"
}
]
}
}
}
}
}

View File

@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 1,
"version": 2,
"title": "Block theme",
"settings": {
"color": {
@ -42,11 +42,11 @@
}
],
"customFontSize": false,
"customLineHeight": true
"lineHeight": true
},
"spacing": {
"units": ["rem"],
"customPadding": true,
"units": [ "rem" ],
"padding": true,
"blockGap": true
},
"blocks": {
@ -63,8 +63,8 @@
}
}
},
"styles" : {
"blocks" :{
"styles": {
"blocks": {
"core/post-featured-image": {
"shadow": "10px 10px 5px 0px rgba(0,0,0,0.66)",
"filter": {
@ -122,7 +122,7 @@
{
"name": "custom-single-post-template",
"title": "Custom Single Post template",
"postTypes": ["post"]
"postTypes": [ "post" ]
}
],
"templateParts": [

View File

@ -31,17 +31,6 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
*/
protected static $post_id;
private function find_and_normalize_global_styles_by_id( $global_styles, $id ) {
foreach ( $global_styles as $style ) {
if ( $style['id'] === $id ) {
unset( $style['_links'] );
return $style;
}
}
return null;
}
public function set_up() {
parent::set_up();
switch_theme( 'tt1-blocks' );
@ -130,6 +119,89 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
// Controller does not use get_context_param().
}
public function test_get_theme_items() {
wp_set_current_user( self::$admin_id );
switch_theme( 'block-theme' );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/block-theme/variations' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$expected = array(
array(
'version' => 2,
'title' => 'variation-a',
'settings' => array(
'blocks' => array(
'core/paragraph' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f2f2f2',
),
),
),
),
),
),
),
),
array(
'version' => 2,
'title' => 'variation-b',
'settings' => array(
'blocks' => array(
'core/post-title' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f1f1f1',
),
),
),
),
),
),
),
),
array(
'version' => 2,
'title' => 'Block theme variation',
'settings' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'foreground',
'color' => '#3F67C6',
'name' => 'Foreground',
),
),
),
),
),
'styles' => array(
'blocks' => array(
'core/post-title' => array(
'typography' => array(
'fontWeight' => '700',
),
),
),
),
),
);
wp_recursive_ksort( $data );
wp_recursive_ksort( $expected );
$this->assertSameSets( $expected, $data );
}
/**
* @doesNotPerformAssertions
*/
@ -411,7 +483,6 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
$this->assertSame( 'My new global styles title', $data['title']['raw'] );
}
/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 54516
@ -445,118 +516,6 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not implement delete_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_prepare_item() {
// Controller does not implement prepare_item().
}
/**
* @covers WP_REST_Global_Styles_Controller::get_item_schema
* @ticket 54516
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 4, $properties, 'Schema properties array does not have exactly 4 elements' );
$this->assertArrayHasKey( 'id', $properties, 'Schema properties array does not have "id" key' );
$this->assertArrayHasKey( 'styles', $properties, 'Schema properties array does not have "styles" key' );
$this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
$this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
}
public function test_get_theme_items() {
wp_set_current_user( self::$admin_id );
switch_theme( 'block-theme' );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/block-theme/variations' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$expected = array(
array(
'version' => 2,
'title' => 'variation-b',
'settings' => array(
'blocks' => array(
'core/post-title' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'light',
'name' => 'Light',
'color' => '#f1f1f1',
),
),
),
),
),
),
),
),
array(
'version' => 2,
'title' => 'Block theme variation',
'settings' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'foreground',
'color' => '#3F67C6',
'name' => 'Foreground',
),
),
),
),
),
'styles' => array(
'blocks' => array(
'core/post-title' => array(
'typography' => array(
'fontWeight' => '700',
),
),
),
),
),
);
wp_recursive_ksort( $data );
wp_recursive_ksort( $expected );
$this->assertSameSets( $data, $expected );
}
/**
* @covers WP_REST_Global_Styles_Controller::get_available_actions
*/
public function test_assign_edit_css_action_admin() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
$links = $response->get_links();
// Admins can only edit css on single site.
if ( is_multisite() ) {
$this->assertArrayNotHasKey( 'https://api.w.org/action-edit-css', $links );
} else {
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
}
}
/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 57536
@ -595,4 +554,53 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
}
/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not implement delete_item().
}
/**
* @doesNotPerformAssertions
*/
public function test_prepare_item() {
// Controller does not implement prepare_item().
}
/**
* @covers WP_REST_Global_Styles_Controller::get_item_schema
* @ticket 54516
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 4, $properties, 'Schema properties array does not have exactly 4 elements' );
$this->assertArrayHasKey( 'id', $properties, 'Schema properties array does not have "id" key' );
$this->assertArrayHasKey( 'styles', $properties, 'Schema properties array does not have "styles" key' );
$this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
$this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
}
/**
* @covers WP_REST_Global_Styles_Controller::get_available_actions
*/
public function test_assign_edit_css_action_admin() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
$links = $response->get_links();
// Admins can only edit css on single site.
if ( is_multisite() ) {
$this->assertArrayNotHasKey( 'https://api.w.org/action-edit-css', $links );
} else {
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -19,20 +19,6 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
*/
protected static $administrator_id;
/**
* Theme root directory.
*
* @var string
*/
private $theme_root;
/**
* Original theme directory.
*
* @var string
*/
private $orig_theme_dir;
/**
* WP_Theme_JSON_Resolver::$blocks_cache property.
*
@ -61,6 +47,25 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
*/
private static $property_core_orig_value;
/**
* Theme root directory.
*
* @var string|null
*/
private $theme_root;
/**
* Original theme directory.
*
* @var array|null
*/
private $orig_theme_dir;
/**
* @var array|null
*/
private $queries;
public static function set_up_before_class() {
parent::set_up_before_class();
@ -98,7 +103,7 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
$this->queries = array();
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
@ -140,7 +145,7 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
$this->assertSame( 'block-theme', wp_get_theme()->get( 'TextDomain' ) );
$this->assertSame( 'Motyw blokowy', $theme_data->get_data()['title'] );
$this->assertSameSets(
$this->assertSame(
array(
'color' => array(
'custom' => false,
@ -218,12 +223,13 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
$custom_templates = $theme_data->get_custom_templates();
$this->assertArrayHasKey( 'page-home', $custom_templates );
$this->assertSame(
$custom_templates['page-home'],
array(
'title' => 'Szablon strony głównej',
'postTypes' => array( 'page' ),
)
),
$custom_templates['page-home']
);
$this->assertSameSets(
array(
'small-header' => array(
@ -233,9 +239,10 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
),
$theme_data->get_template_parts()
);
$this->assertSame(
'Wariant motywu blokowego',
$style_variations[1]['title']
$style_variations[2]['title']
);
}
@ -257,8 +264,8 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
}
/**
* Tests when WP_Theme_JSON_Resolver::$blocks_cache is empty or does not match
* the all registered blocks.
* Tests when WP_Theme_JSON_Resolver::$blocks_cache is empty or
* does not match the all registered blocks.
*
* Though this is a non-public method, it is vital to other functionality.
* Therefore, tests are provided to validate it functions as expected.
@ -331,8 +338,8 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
}
/**
* Tests when WP_Theme_JSON_Resolver::$blocks_cache is empty or does not match
* the all registered blocks.
* Tests when WP_Theme_JSON_Resolver::$blocks_cache is empty or
* does not match the all registered blocks.
*
* Though this is a non-public method, it is vital to other functionality.
* Therefore, tests are provided to validate it functions as expected.
@ -596,7 +603,6 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
);
$this->assertSame(
WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates(),
array(
'page-home' => array(
'title' => 'Homepage',
@ -606,7 +612,8 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
'title' => 'Custom Single Post template',
'postTypes' => array( 'post' ),
),
)
),
WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates()
);
}
@ -749,6 +756,8 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
* @covers WP_Theme_JSON_Resolver::get_theme_data
*/
public function test_get_theme_data_theme_supports_overrides_theme_json() {
switch_theme( 'default' );
// Test that get_theme_data() returns a WP_Theme_JSON object.
$theme_json_resolver = new WP_Theme_JSON_Resolver();
$theme_json_resolver->get_merged_data();
@ -762,10 +771,12 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
$previous_settings = $theme_data->get_settings();
$previous_line_height = $previous_settings['typography']['lineHeight'];
$this->assertFalse( $previous_line_height, 'lineHeight setting from theme.json should be false.' );
add_theme_support( 'custom-line-height' );
$current_settings = $theme_json_resolver->get_theme_data()->get_settings();
$line_height = $current_settings['typography']['lineHeight'];
$this->assertTrue( $line_height, 'lineHeight setting after add_theme_support() should be true.' );
remove_theme_support( 'custom-line-height' );
}
/**
@ -809,15 +820,15 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
* @param string $block_styles_text Message.
* @param bool $theme_palette Whether the theme palette is present.
* @param string $theme_palette_text Message.
* @param bool $user_palette Whether the user palette is present.
* @param string $user_palette_text Message.
* @param bool $user_palette Whether the user palette is present.
* @param string $user_palette_text Message.
*/
public function test_get_merged_data_returns_origin( $origin, $core_palette, $core_palette_text, $block_styles, $block_styles_text, $theme_palette, $theme_palette_text, $user_palette, $user_palette_text ) {
// Make sure there is data from the blocks origin.
register_block_type(
'my/block-with-styles',
array(
'api_version' => 2,
'api_version' => 3,
'attributes' => array(
'borderColor' => array(
'type' => 'string',
@ -877,7 +888,6 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
* @ticket 57824
*
* @covers WP_Theme_JSON_Resolver::get_merged_data
*
*/
public function test_get_merged_data_returns_origin_proper() {
// Make sure the theme has a theme.json
@ -914,7 +924,7 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
}
/**
* Data provider.
* Data provider for test_get_merged_data_returns_origin.
*
* @return array[]
*/
@ -983,7 +993,28 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
$actual_settings = WP_Theme_JSON_Resolver::get_style_variations();
$expected_settings = array(
array(
'version' => 2,
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => 'variation-a',
'settings' => array(
'blocks' => array(
'core/paragraph' => array(
'color' => array(
'palette' => array(
'theme' => array(
array(
'slug' => 'dark',
'name' => 'Dark',
'color' => '#010101',
),
),
),
),
),
),
),
),
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => 'variation-b',
'settings' => array(
'blocks' => array(
@ -1004,7 +1035,7 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
),
),
array(
'version' => 2,
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => 'Block theme variation',
'settings' => array(
'color' => array(

View File

@ -11,15 +11,10 @@
* @group themes
*/
class Tests_Theme_wpThemeJsonSchema extends WP_UnitTestCase {
/**
* The current theme.json schema version.
*/
const LATEST_SCHEMA_VERSION = WP_Theme_JSON::LATEST_SCHEMA;
/**
* @ticket 54336
*/
public function test_migrate_v1_to_v2() {
public function test_migrate_v1_to_latest() {
$theme_json_v1 = array(
'version' => 1,
'settings' => array(
@ -106,7 +101,7 @@ class Tests_Theme_wpThemeJsonSchema extends WP_UnitTestCase {
$actual = WP_Theme_JSON_Schema::migrate( $theme_json_v1 );
$expected = array(
'version' => self::LATEST_SCHEMA_VERSION,
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'palette' => array(