Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadow: add unit tests for shadow support #60063

Merged
merged 4 commits into from
May 22, 2024
Merged

Conversation

madhusudhand
Copy link
Contributor

@madhusudhand madhusudhand commented Mar 21, 2024

What?

This PR adds missing PHP unit tests for Shadow support.

It tests

  • shadow support enabled/disabled, and
  • serialization is skipped/not skipped.
if (
    ! $has_shadow_support ||
    wp_should_skip_block_supports_serialization( $block_type, 'shadow' )
) {
    return array();
}

Testing Instructions

Not applicable.

Related fix: #59887

@madhusudhand madhusudhand added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Mar 21, 2024
@madhusudhand madhusudhand self-assigned this Mar 21, 2024
Copy link

github-actions bot commented Mar 21, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: madhusudhand <madhudollu@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: vcanales <vcanales@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround and adding these tests @madhusudhand 🚀

They test well for me.

I appreciate these follow the existing approaches in some of our other block support tests. What do you think about using a data provider here instead?

It will make the tests a little more concise and readable as well as more aligned with how they end up being backported to core.

Diff to switch to a data provider
diff --git a/phpunit/block-supports/shadow-test.php b/phpunit/block-supports/shadow-test.php
index 89ede32621..bc0c1964e2 100644
--- a/phpunit/block-supports/shadow-test.php
+++ b/phpunit/block-supports/shadow-test.php
@@ -50,119 +50,58 @@ class WP_Block_Supports_Shadow_Test extends WP_UnitTestCase {
 		return $registry->get_registered( $this->test_block_name );
 	}
 
-	public function test_shadow_object_with_no_styles() {
+	/**
+	 * Tests the generation of shadow block support styles.
+	 *
+	 * @dataProvider data_generate_shadow_fixtures
+	 *
+	 * @param boolean|array $support Shadow block support configuration.
+	 * @param string        $value   Shadow style value for style attribute object.
+	 * @param array         $expected       Expected shadow block support styles.
+	 */
+	public function test_gutenberg_apply_shadow_support( $support, $value, $expected ) {
 		$block_type  = self::register_shadow_block_with_support(
-			'test/shadow-object-with-no-styles',
-			array(
-				'shadow' => true,
-			)
+			'test/shadow-block',
+			array( 'shadow' => $support )
 		);
-		$block_attrs = array( 'style' => array( 'shadow' => '' ) );
+		$block_attrs = array( 'style' => array( 'shadow' => $value ) );
 		$actual      = gutenberg_apply_shadow_support( $block_type, $block_attrs );
-		$expected    = array();
 
 		$this->assertSame( $expected, $actual );
 	}
 
-	public function test_shadow_without_support() {
-		$block_type = self::register_shadow_block_with_support(
-			'test/shadow-without-support',
-			array(
-				'shadow' => false,
-			)
-		);
-		$block_atts = array(
-			'style' => array(
-				'shadow' => '1px 1px 1px #000',
+	public function data_generate_shadow_fixtures() {
+		return array(
+			'with no styles'               => array(
+				'support'  => true,
+				'value'    => '',
+				'expected' => array(),
 			),
-		);
-
-		$actual   = gutenberg_apply_shadow_support( $block_type, $block_atts );
-		$expected = array();
-
-		$this->assertSame( $expected, $actual );
-	}
-
-	public function test_shadow_with_single_shadow() {
-		$block_type = self::register_shadow_block_with_support(
-			'test/shadow-with-single-shadow',
-			array(
-				'shadow' => true,
-			)
-		);
-		$block_atts = array(
-			'style' => array(
-				'shadow' => '1px 1px 1px #000',
+			'without support'              => array(
+				'support'  => false,
+				'value'    => '1px 1px 1px #000',
+				'expected' => array(),
 			),
-		);
-
-		$actual   = gutenberg_apply_shadow_support( $block_type, $block_atts );
-		$expected = array(
-			'style' => 'box-shadow:1px 1px 1px #000;',
-		);
-
-		$this->assertSame( $expected, $actual );
-	}
-
-	public function test_shadow_with_comma_separated_shadows() {
-		$block_type = self::register_shadow_block_with_support(
-			'test/shadow-with-comma-separated-shadows',
-			array(
-				'shadow' => true,
-			)
-		);
-		$block_atts = array(
-			'style' => array(
-				'shadow' => '1px 1px 1px #000, 2px 2px 2px #fff',
+			'with single shadow'           => array(
+				'support'  => true,
+				'value'    => '1px 1px 1px #000',
+				'expected' => array( 'style' => 'box-shadow:1px 1px 1px #000;' ),
 			),
-		);
-
-		$actual   = gutenberg_apply_shadow_support( $block_type, $block_atts );
-		$expected = array(
-			'style' => 'box-shadow:1px 1px 1px #000, 2px 2px 2px #fff;',
-		);
-
-		$this->assertSame( $expected, $actual );
-	}
-
-	public function test_shadow_with_named_shadow() {
-		$block_type  = self::register_shadow_block_with_support(
-			'test/shadow-with-named-shadow',
-			array(
-				'shadow' => true,
-			)
-		);
-		$block_attrs = array(
-			'style' => array(
-				'shadow' => 'var:preset|shadow|natural',
+			'with comma separated shadows' => array(
+				'support'  => true,
+				'value'    => '1px 1px 1px #000, 2px 2px 2px #fff',
+				'expected' => array( 'style' => 'box-shadow:1px 1px 1px #000, 2px 2px 2px #fff;' ),
 			),
-		);
-		$actual      = gutenberg_apply_shadow_support( $block_type, $block_attrs );
-		$expected    = array(
-			'style' => 'box-shadow:var(--wp--preset--shadow--natural);',
-		);
-
-		$this->assertSame( $expected, $actual );
-	}
-
-	public function test_shadow_with_skipped_serialization() {
-		$block_type = self::register_shadow_block_with_support(
-			'test/shadow-with-skipped-serialization',
-			array(
-				'shadow' => array(
-					'__experimentalSkipSerialization' => true,
-				),
-			)
-		);
-		$block_atts = array(
-			'style' => array(
-				'shadow' => '1px 1px 1px #000',
+			'with preset shadow'           => array(
+				'support'  => true,
+				'value'    => 'var:preset|shadow|natural',
+				'expected' => array( 'style' => 'box-shadow:var(--wp--preset--shadow--natural);' ),
+			),
+			'with serialization skipped'   => array(
+				'support'  => array( '__experimentalSkipSerialization' => true ),
+				'value'    => '1px 1px 1px #000',
+				'expected' => array(),
 			),
 		);
-
-		$actual   = gutenberg_apply_shadow_support( $block_type, $block_atts );
-		$expected = array();
-
-		$this->assertSame( $expected, $actual );
 	}
 }
@madhusudhand
Copy link
Contributor Author

Thanks @aaronrobertshaw. Applied the suggested patch. It now looks neat.

Copy link

github-actions bot commented Mar 22, 2024

Flaky tests detected in a6ae8d7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9190853846
📝 Reported issues:

Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me; I believe it's covering the logic added in #59887 and it tests well 👍

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Screenshot 2024-03-25 at 11 00 13 AM
phpunit/block-supports/shadow-test.php Show resolved Hide resolved
@vcanales
Copy link
Member

@madhusudhand Is this good to merge?

Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ phpunit/block-supports/shadow-test.php
@madhusudhand madhusudhand added the Backported to WP Core Pull request that has been successfully merged into WP Core label May 22, 2024
@madhusudhand
Copy link
Contributor Author

madhusudhand and others added 4 commits May 22, 2024 13:51
Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
@madhusudhand madhusudhand enabled auto-merge (squash) May 22, 2024 12:12
@madhusudhand madhusudhand merged commit 66d35c1 into trunk May 22, 2024
62 checks passed
@madhusudhand madhusudhand deleted the shadow-support-unit-tests branch May 22, 2024 12:34
@github-actions github-actions bot added this to the Gutenberg 18.5 milestone May 22, 2024
@madhusudhand madhusudhand removed the Backported to WP Core Pull request that has been successfully merged into WP Core label May 22, 2024
carstingaxion pushed a commit to carstingaxion/gutenberg that referenced this pull request Jun 4, 2024
* add unit tests for shadow support

* refactor tests

* Update method doc block

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>

* add core pr reference

---------

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>

Co-authored-by: madhusudhand <madhudollu@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: vcanales <vcanales@git.wordpress.org>
patil-vipul pushed a commit to patil-vipul/gutenberg that referenced this pull request Jun 17, 2024
* add unit tests for shadow support

* refactor tests

* Update method doc block

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>

* add core pr reference

---------

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>

Co-authored-by: madhusudhand <madhudollu@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: vcanales <vcanales@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
4 participants