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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/6279.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/6279

* https://github.com/WordPress/gutenberg/pull/60063
112 changes: 112 additions & 0 deletions phpunit/block-supports/shadow-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* Test the shadow block supports.
*
* @package Gutenberg
*/

class WP_Block_Supports_Shadow_Test extends WP_UnitTestCase {
/**
* @var string|null
*/
private $test_block_name;

public function set_up() {
parent::set_up();
$this->test_block_name = null;
}

public function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tear_down();
}

/**
* Registers a new block for testing shadow support.
*
* @param string $block_name Name for the test block.
* @param array $supports Array defining block support configuration.
*
* @return WP_Block_Type The block type for the newly registered test block.
*/
private function register_shadow_block_with_support( $block_name, $supports = array() ) {
$this->test_block_name = $block_name;
register_block_type(
$this->test_block_name,
array(
'api_version' => 3,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => $supports,
)
);
$registry = WP_Block_Type_Registry::get_instance();

return $registry->get_registered( $this->test_block_name );
}

/**
* 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-block',
array( 'shadow' => $support )
);
$block_attrs = array( 'style' => array( 'shadow' => $value ) );
$actual = gutenberg_apply_shadow_support( $block_type, $block_attrs );

$this->assertSame( $expected, $actual );
}

/**
* Data provider.
*
* @return array
*/
public function data_generate_shadow_fixtures() {
madhusudhand marked this conversation as resolved.
Show resolved Hide resolved
return array(
'with no styles' => array(
'support' => true,
'value' => '',
'expected' => array(),
),
'without support' => array(
'support' => false,
'value' => '1px 1px 1px #000',
'expected' => array(),
),
'with single shadow' => array(
'support' => true,
'value' => '1px 1px 1px #000',
'expected' => array( 'style' => 'box-shadow:1px 1px 1px #000;' ),
),
'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;' ),
),
'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(),
),
);
}
}
Loading