Skip to content

Commit

Permalink
Tests: Synchronize Theme.JSON unit test between Core and Gutenberg.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
youknowriad committed Feb 20, 2024
1 parent 977653e commit 18b21c3
Show file tree
Hide file tree
Showing 8 changed files with 1,029 additions and 835 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{
"slug": "dark",
"name": "Dark",
"color": "#010101"
}
]
}
}
}
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/data/themedir1/block-theme-child/theme.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 1,
"version": 2,
"settings": {
"color": {
"palette": [
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/styles/variation-a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 2,
"settings": {
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f2f2f2"
}
]
}
}
}
}
}
14 changes: 7 additions & 7 deletions tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 1,
"version": 2,
"title": "Block theme",
"settings": {
"color": {
Expand Down Expand Up @@ -42,11 +42,11 @@
}
],
"customFontSize": false,
"customLineHeight": true
"lineHeight": true
},
"spacing": {
"units": ["rem"],
"customPadding": true,
"units": [ "rem" ],
"padding": true,
"blockGap": true
},
"blocks": {
Expand All @@ -63,8 +63,8 @@
}
}
},
"styles" : {
"blocks" :{
"styles": {
"blocks": {
"core/post-featured-image": {
"shadow": "10px 10px 5px 0px rgba(0,0,0,0.66)",
"filter": {
Expand Down Expand Up @@ -122,7 +122,7 @@
{
"name": "custom-single-post-template",
"title": "Custom Single Post template",
"postTypes": ["post"]
"postTypes": [ "post" ]
}
],
"templateParts": [
Expand Down
236 changes: 122 additions & 114 deletions tests/phpunit/tests/rest-api/rest-global-styles-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -130,6 +119,89 @@ public function test_context_param() {
// 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
*/
Expand Down Expand Up @@ -411,7 +483,6 @@ public function test_update_item() {
$this->assertSame( 'My new global styles title', $data['title']['raw'] );
}


/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 54516
Expand Down Expand Up @@ -445,6 +516,45 @@ public function test_update_item_permission_check() {
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}

/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 57536
*/
public function test_update_item_valid_styles_css() {
wp_set_current_user( self::$admin_id );
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_body_params(
array(
'styles' => array( 'css' => 'body { color: red; }' ),
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'body { color: red; }', $data['styles']['css'] );
}

/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 57536
*/
public function test_update_item_invalid_styles_css() {
wp_set_current_user( self::$admin_id );
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_body_params(
array(
'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
}

/**
* @doesNotPerformAssertions
*/
Expand Down Expand Up @@ -475,69 +585,6 @@ public function test_get_item_schema() {
$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
*/
Expand All @@ -556,43 +603,4 @@ public function test_assign_edit_css_action_admin() {
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
}
}

/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 57536
*/
public function test_update_item_valid_styles_css() {
wp_set_current_user( self::$admin_id );
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_body_params(
array(
'styles' => array( 'css' => 'body { color: red; }' ),
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'body { color: red; }', $data['styles']['css'] );
}

/**
* @covers WP_REST_Global_Styles_Controller::update_item
* @ticket 57536
*/
public function test_update_item_invalid_styles_css() {
wp_set_current_user( self::$admin_id );
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_body_params(
array(
'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
}
}
Loading

0 comments on commit 18b21c3

Please sign in to comment.