From 0f1ea45a8d982309f0ad3dc8d31aa4eefed0005c Mon Sep 17 00:00:00 2001 From: ramonjd Date: Thu, 24 Feb 2022 12:29:20 +1100 Subject: [PATCH] Adding block supports tests Adding skip serialization tests --- phpunit/block-supports/border-test.php | 143 ++++++++++++++++++++ phpunit/block-supports/colors-test.php | 80 +++++++++++ phpunit/block-supports/spacing-test.php | 148 +++++++++++++++++++++ phpunit/block-supports/typography-test.php | 74 +++++++++++ 4 files changed, 445 insertions(+) create mode 100644 phpunit/block-supports/border-test.php create mode 100644 phpunit/block-supports/spacing-test.php diff --git a/phpunit/block-supports/border-test.php b/phpunit/block-supports/border-test.php new file mode 100644 index 00000000000000..bf09bc39ee0876 --- /dev/null +++ b/phpunit/block-supports/border-test.php @@ -0,0 +1,143 @@ + 2, + 'attributes' => array( + 'borderColor' => array( + 'type' => 'string', + ), + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + '__experimentalBorder' => array( + 'color' => true, + 'radius' => true, + 'width' => true, + 'style' => true, + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'borderColor' => 'red', + 'style' => array( + 'border' => array( + 'radius' => '10px', + 'width' => '1px', + 'style' => 'dashed', + ), + ), + ); + + $actual = gutenberg_apply_border_support( $block_type, $block_atts ); + $expected = array( + 'class' => 'has-border-color has-red-border-color', + 'style' => 'border-radius: 10px; border-style: dashed; border-width: 1px;', + ); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_border_with_skipped_serialization_block_supports() { + $block_name = 'test/border-with-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + '__experimentalBorder' => array( + 'color' => true, + 'radius' => true, + 'width' => true, + 'style' => true, + '__experimentalSkipSerialization' => true, + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'border' => array( + 'color' => '#eeeeee', + 'width' => '1px', + 'style' => 'dotted', + 'radius' => '10px', + ), + ), + ); + + $actual = gutenberg_apply_border_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_radius_with_individual_skipped_serialization_block_supports() { + $block_name = 'test/radius-with-individual-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + '__experimentalBorder' => array( + 'color' => true, + 'radius' => true, + 'width' => true, + 'style' => true, + '__experimentalSkipSerialization' => array( 'radius', 'color' ), + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'border' => array( + 'color' => '#eeeeee', + 'width' => '1px', + 'style' => 'dotted', + 'radius' => '10px', + ), + ), + ); + + $actual = gutenberg_apply_border_support( $block_type, $block_atts ); + $expected = array( + 'style' => 'border-style: dotted; border-width: 1px;', + ); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } +} diff --git a/phpunit/block-supports/colors-test.php b/phpunit/block-supports/colors-test.php index 9548b83dde743d..88e3994eb226b2 100644 --- a/phpunit/block-supports/colors-test.php +++ b/phpunit/block-supports/colors-test.php @@ -48,4 +48,84 @@ function test_color_slugs_with_numbers_are_kebab_cased_properly() { $this->assertSame( $expected, $actual ); unregister_block_type( 'test/color-slug-with-numbers' ); } + + function test_color_with_skipped_serialization_block_supports() { + $block_name = 'test/color-with-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'color' => array( + 'text' => true, + 'gradients' => true, + '__experimentalSkipSerialization' => true, + ), + ), + ) + ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'color' => array( + 'text' => '#d92828', + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(223,13,13) 46%,rgb(155,81,224) 100%)', + ), + ), + ); + + $actual = gutenberg_apply_colors_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_gradient_with_individual_skipped_serialization_block_supports() { + $block_name = 'test/gradient-with-individual-skipped-serialization-block-support'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'color' => array( + 'text' => true, + 'gradients' => true, + '__experimentalSkipSerialization' => array( 'gradients' ), + ), + ), + ) + ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'color' => array( + 'text' => '#d92828', + ), + ), + ); + + $actual = gutenberg_apply_colors_support( $block_type, $block_atts ); + $expected = array( + 'class' => 'has-text-color', + 'style' => 'color: #d92828;', + ); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } } diff --git a/phpunit/block-supports/spacing-test.php b/phpunit/block-supports/spacing-test.php new file mode 100644 index 00000000000000..270b16bbba132d --- /dev/null +++ b/phpunit/block-supports/spacing-test.php @@ -0,0 +1,148 @@ + 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'spacing' => array( + 'margin' => true, + 'padding' => true, + 'blockGap' => true, + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'spacing' => array( + 'margin' => array( + 'top' => '1px', + 'right' => '2px', + 'bottom' => '3px', + 'left' => '4px', + ), + 'padding' => '111px', + 'blockGap' => '2em', + ), + ), + ); + + $actual = gutenberg_apply_spacing_support( $block_type, $block_atts ); + $expected = array( + 'style' => 'padding: 111px; margin-top: 1px; margin-right: 2px; margin-bottom: 3px; margin-left: 4px;', + ); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_spacing_with_skipped_serialization_block_supports() { + $block_name = 'test/spacing-with-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'spacing' => array( + 'margin' => true, + 'padding' => true, + 'blockGap' => true, + '__experimentalSkipSerialization' => true, + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'spacing' => array( + 'margin' => array( + 'top' => '1px', + 'right' => '2px', + 'bottom' => '3px', + 'left' => '4px', + ), + 'padding' => '111px', + 'blockGap' => '2em', + ), + ), + ); + + $actual = gutenberg_apply_spacing_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_margin_with_individual_skipped_serialization_block_supports() { + $block_name = 'test/margin-with-individual-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'spacing' => array( + 'margin' => true, + 'padding' => true, + 'blockGap' => true, + '__experimentalSkipSerialization' => array( 'margin' ), + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'spacing' => array( + 'padding' => array( + 'top' => '1px', + 'right' => '2px', + 'bottom' => '3px', + 'left' => '4px', + ), + 'margin' => '111px', + 'blockGap' => '2em', + ), + ), + ); + + $actual = gutenberg_apply_spacing_support( $block_type, $block_atts ); + $expected = array( + 'style' => 'padding-top: 1px; padding-right: 2px; padding-bottom: 3px; padding-left: 4px;', + ); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } +} diff --git a/phpunit/block-supports/typography-test.php b/phpunit/block-supports/typography-test.php index dbca89457a2093..312b86f6a0b389 100644 --- a/phpunit/block-supports/typography-test.php +++ b/phpunit/block-supports/typography-test.php @@ -66,6 +66,80 @@ function test_font_family_with_legacy_inline_styles_using_a_value() { unregister_block_type( $block_name ); } + function test_typography_with_skipped_serialization_block_supports() { + $block_name = 'test/typography-with-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'typography' => array( + 'fontSize' => true, + 'lineHeight' => true, + '__experimentalFontFamily' => true, + '__experimentalLetterSpacing' => true, + '__experimentalSkipSerialization' => true, + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( + 'style' => array( + 'typography' => array( + 'fontSize' => 'serif', + 'lineHeight' => 'serif', + 'fontFamily' => '22px', + 'letterSpacing' => '22px', + ), + ), + ); + + $actual = gutenberg_apply_typography_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + + function test_letter_spacing_with_individual_skipped_serialization_block_supports() { + $block_name = 'test/letter-spacing-with-individua-skipped-serialization-block-supports'; + register_block_type( + $block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'typography' => array( + '__experimentalLetterSpacing' => true, + '__experimentalSkipSerialization' => array( + 'letterSpacing', + ), + ), + ), + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $block_name ); + $block_atts = array( 'style' => array( 'typography' => array( 'letterSpacing' => '22px' ) ) ); + + $actual = gutenberg_apply_typography_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + unregister_block_type( $block_name ); + } + function test_font_family_with_legacy_inline_styles_using_a_css_var() { $block_name = 'test/font-family-with-inline-styles-using-css-var'; register_block_type(