From fa7a0bf8581e7528f7a8a0b82293513fe534b046 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 23 Oct 2023 14:18:45 +0200 Subject: [PATCH 1/3] Add unit testing to duotone CSS declarations --- phpunit/class-wp-duotone-test.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/phpunit/class-wp-duotone-test.php b/phpunit/class-wp-duotone-test.php index 327e88f972861..111e12d1c81c8 100644 --- a/phpunit/class-wp-duotone-test.php +++ b/phpunit/class-wp-duotone-test.php @@ -45,6 +45,27 @@ public function test_gutenberg_render_duotone_support_custom() { $this->assertMatchesRegularExpression( $expected, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block ) ); } + + /** + * Tests whether the CSS declarations are generated even if the block content is + * empty. We need this to make the CSS output stable across paginations for + * features like the enhanced pagination of the Query block. + * + * @covers ::render_duotone_support + */ + public function test_css_declarations_are_generated_even_with_empty_block_content() { + $block = array( + 'blockName' => 'core/image', + 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ), + ); + $wp_block = new WP_Block( $block ); + $block_css_declarations_property = new ReflectionProperty( 'WP_Duotone_Gutenberg', 'block_css_declarations' ); + $block_css_declarations_property->setAccessible( true ); + $block_css_declarations_property->setValue( array() ); + WP_Duotone_Gutenberg::render_duotone_support( '', $block, $wp_block ); + $this->assertNotEmpty( $block_css_declarations_property->getValue() ); + } + public function data_get_slug_from_attribute() { return array( 'pipe-slug' => array( 'var:preset|duotone|blue-orange', 'blue-orange' ), From 14c1de0bace214f50a07f1a65676f012142e7136 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Mon, 23 Oct 2023 14:20:21 +0200 Subject: [PATCH 2/3] Rename test to include gutenberg prefix --- phpunit/class-wp-duotone-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-wp-duotone-test.php b/phpunit/class-wp-duotone-test.php index 111e12d1c81c8..ee35a50502ac8 100644 --- a/phpunit/class-wp-duotone-test.php +++ b/phpunit/class-wp-duotone-test.php @@ -53,7 +53,7 @@ public function test_gutenberg_render_duotone_support_custom() { * * @covers ::render_duotone_support */ - public function test_css_declarations_are_generated_even_with_empty_block_content() { + public function test_gutenberg_css_declarations_are_generated_even_with_empty_block_content() { $block = array( 'blockName' => 'core/image', 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ), From 59d098e3086023dc7b663440f545b6030b5c33d7 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 2 Nov 2023 11:26:57 +0100 Subject: [PATCH 3/3] Update tests to be like the ones in Core --- phpunit/class-wp-duotone-test.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/phpunit/class-wp-duotone-test.php b/phpunit/class-wp-duotone-test.php index ee35a50502ac8..57c5540b8a9f1 100644 --- a/phpunit/class-wp-duotone-test.php +++ b/phpunit/class-wp-duotone-test.php @@ -48,22 +48,39 @@ public function test_gutenberg_render_duotone_support_custom() { /** * Tests whether the CSS declarations are generated even if the block content is - * empty. We need this to make the CSS output stable across paginations for + * empty. This is needed to make the CSS output stable across paginations for * features like the enhanced pagination of the Query block. * * @covers ::render_duotone_support */ public function test_gutenberg_css_declarations_are_generated_even_with_empty_block_content() { - $block = array( + $block = array( 'blockName' => 'core/image', 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ), ); - $wp_block = new WP_Block( $block ); + $wp_block = new WP_Block( $block ); + + /* + * Handling to access the static WP_Duotone::$block_css_declarations property. + * + * Why is an instance needed? + * WP_Duotone is a static class by design, meaning it only contains static properties and methods. + * In production, it should not be instantiated. However, as of PHP 8.3, ReflectionProperty::setValue() + * needs an object. + */ + $wp_duotone = new WP_Duotone_Gutenberg(); $block_css_declarations_property = new ReflectionProperty( 'WP_Duotone_Gutenberg', 'block_css_declarations' ); $block_css_declarations_property->setAccessible( true ); - $block_css_declarations_property->setValue( array() ); + $previous_value = $block_css_declarations_property->getValue(); + $block_css_declarations_property->setValue( $wp_duotone, array() ); WP_Duotone_Gutenberg::render_duotone_support( '', $block, $wp_block ); - $this->assertNotEmpty( $block_css_declarations_property->getValue() ); + $actual = $block_css_declarations_property->getValue(); + + // Reset the property. + $block_css_declarations_property->setValue( $wp_duotone, $previous_value ); + $block_css_declarations_property->setAccessible( false ); + + $this->assertNotEmpty( $actual ); } public function data_get_slug_from_attribute() {