diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 8fef9c29b861c..0b54d99cbaa72 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -3093,9 +3093,9 @@ public function test_get_property_value_self() { * @param array $layout_definitions Layout definitions as stored in core theme.json. */ public function test_get_stylesheet_generates_layout_styles( $layout_definitions ) { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'layout' => array( 'definitions' => $layout_definitions, @@ -3128,9 +3128,9 @@ public function test_get_stylesheet_generates_layout_styles( $layout_definitions * @param array $layout_definitions Layout definitions as stored in core theme.json. */ public function test_get_stylesheet_generates_layout_styles_with_spacing_presets( $layout_definitions ) { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'layout' => array( 'definitions' => $layout_definitions, @@ -3163,9 +3163,9 @@ public function test_get_stylesheet_generates_layout_styles_with_spacing_presets * @param array $layout_definitions Layout definitions as stored in core theme.json. */ public function test_get_stylesheet_generates_fallback_gap_layout_styles( $layout_definitions ) { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'layout' => array( 'definitions' => $layout_definitions, @@ -3199,9 +3199,9 @@ public function test_get_stylesheet_generates_fallback_gap_layout_styles( $layou * @param array $layout_definitions Layout definitions as stored in core theme.json. */ public function test_get_stylesheet_generates_base_fallback_gap_layout_styles( $layout_definitions ) { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'layout' => array( 'definitions' => $layout_definitions, @@ -3231,9 +3231,9 @@ public function test_get_stylesheet_generates_base_fallback_gap_layout_styles( $ */ public function test_get_stylesheet_skips_layout_styles( $layout_definitions ) { add_theme_support( 'disable-layout-styles' ); - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'layout' => array( 'definitions' => $layout_definitions, @@ -3340,11 +3340,346 @@ public function data_get_layout_definitions() { ); } + /** + * Tests generating the spacing presets array based on the spacing scale provided. + * + * @ticket 56467 + * + * @dataProvider data_generate_spacing_scale_fixtures + */ + function test_set_spacing_sizes( $spacing_scale, $expected_output ) { + $theme_json = new WP_Theme_JSON( + array( + 'version' => 2, + 'settings' => array( + 'spacing' => array( + 'spacingScale' => $spacing_scale, + ), + ), + ) + ); + + $theme_json->set_spacing_sizes(); + $this->assertSame( $expected_output, _wp_array_get( $theme_json->get_raw_data(), array( 'settings', 'spacing', 'spacingSizes', 'default' ) ) ); + } + + /** + * Data provider for spacing scale tests. + * + * @ticket 56467 + * + * @return array + */ + function data_generate_spacing_scale_fixtures() { + return array( + 'one_step_spacing_scale' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 1, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '4rem', + ), + ), + ), + + 'two_step_spacing_scale_should_add_step_above_medium' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 2, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '4rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '5.5rem', + ), + ), + ), + + 'three_step_spacing_scale_should_add_step_above_and_below_medium' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 3, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'Small', + 'slug' => '40', + 'size' => '2.5rem', + ), + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '4rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '5.5rem', + ), + ), + ), + + 'even_step_spacing_scale_steps_should_add_extra_step_above_medium' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 4, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'Small', + 'slug' => '40', + 'size' => '2.5rem', + ), + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '4rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '5.5rem', + ), + array( + 'name' => 'X-Large', + 'slug' => '70', + 'size' => '7rem', + ), + ), + ), + + 'if_bottom_end_will_go_below_zero_should_add_extra_steps_above_medium_instead' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 2.5, + 'steps' => 5, + 'mediumStep' => 5, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'Small', + 'slug' => '40', + 'size' => '2.5rem', + ), + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '5rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '7.5rem', + ), + array( + 'name' => 'X-Large', + 'slug' => '70', + 'size' => '10rem', + ), + array( + 'name' => '2X-Large', + 'slug' => '80', + 'size' => '12.5rem', + ), + ), + ), + + 'multiplier_should_correctly_calculate_above_and_below_medium' => array( + 'spacingScale' => array( + 'operator' => '*', + 'increment' => 1.5, + 'steps' => 5, + 'mediumStep' => 1.5, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'X-Small', + 'slug' => '30', + 'size' => '0.67rem', + ), + array( + 'name' => 'Small', + 'slug' => '40', + 'size' => '1rem', + ), + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '1.5rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '2.25rem', + ), + array( + 'name' => 'X-Large', + 'slug' => '70', + 'size' => '3.38rem', + ), + ), + ), + + 'increment_<_1_combined_with_*_operator_should_act_as_divisor_to_calculate_above_and_below_medium' => array( + 'spacingScale' => array( + 'operator' => '*', + 'increment' => 0.25, + 'steps' => 5, + 'mediumStep' => 1.5, + 'unit' => 'rem', + ), + 'expected_output' => array( + array( + 'name' => 'X-Small', + 'slug' => '30', + 'size' => '0.09rem', + ), + array( + 'name' => 'Small', + 'slug' => '40', + 'size' => '0.38rem', + ), + array( + 'name' => 'Medium', + 'slug' => '50', + 'size' => '1.5rem', + ), + array( + 'name' => 'Large', + 'slug' => '60', + 'size' => '6rem', + ), + array( + 'name' => 'X-Large', + 'slug' => '70', + 'size' => '24rem', + ), + ), + ), + ); + } + + /** + * Tests generating the spacing presets array based on the spacing scale provided. + * + * @ticket 56467 + * + * @dataProvider data_set_spacing_sizes_when_invalid + */ + public function test_set_spacing_sizes_when_invalid( $spacing_scale, $expected_output ) { + $this->expectNotice(); + $this->expectNoticeMessage( 'Some of the theme.json settings.spacing.spacingScale values are invalid' ); + + $theme_json = new WP_Theme_JSON( + array( + 'version' => 2, + 'settings' => array( + 'spacing' => array( + 'spacingScale' => $spacing_scale, + ), + ), + ) + ); + + $theme_json->set_spacing_sizes(); + $this->assertSame( $expected_output, _wp_array_get( $theme_json->get_raw_data(), array( 'settings', 'spacing', 'spacingSizes', 'default' ) ) ); + } + + /** + * Data provider for spacing scale tests. + * + * @ticket 56467 + * + * @return array + */ + function data_set_spacing_sizes_when_invalid() { + return array( + + 'invalid_spacing_scale_values_missing_operator' => array( + 'spacingScale' => array( + 'operator' => '', + 'increment' => 1.5, + 'steps' => 1, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_increment' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 'add two to previous value', + 'steps' => 1, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_steps' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 'spiral staircase preferred', + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_medium_step' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 5, + 'mediumStep' => 'That which is just right', + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_missing_unit' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 5, + 'mediumStep' => 4, + ), + 'expected_output' => null, + ), + ); + } + /** * @ticket 56467 */ function test_get_styles_for_block_with_padding_aware_alignments() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( 'version' => 2, 'styles' => array( @@ -3378,7 +3713,7 @@ function test_get_styles_for_block_with_padding_aware_alignments() { * @ticket 56467 */ function test_get_styles_for_block_without_padding_aware_alignments() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( 'version' => 2, 'styles' => array( @@ -3409,7 +3744,7 @@ function test_get_styles_for_block_without_padding_aware_alignments() { * @ticket 56467 */ function test_get_styles_for_block_with_content_width() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $theme_json = new WP_Theme_JSON( array( 'version' => 2, 'settings' => array(