Skip to content

Commit e46f67b

Browse files
Migrate Search and Icon to the shadow helper and trim the tests
Search and Icon now get their shadow from the helper, like the other blocks in this PR, with the same output as trunk. Each gets one render test. The tests now cover the helper's own cases and one migration per block. Cases that re-tested the style engine are dropped.
1 parent 97d570d commit e46f67b

7 files changed

Lines changed: 112 additions & 72 deletions

File tree

‎packages/block-library/src/icon/index.php‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ function render_block_core_icon( $attributes ) {
8383
);
8484
}
8585

86-
// Shadow.
87-
$shadow_styles = $attributes['style']['shadow'] ?? null;
88-
8986
// Spacing (Padding).
9087
$spacing_styles = array();
9188
if ( isset( $attributes['style']['spacing']['padding'] ) ) {
@@ -101,15 +98,32 @@ function render_block_core_icon( $attributes ) {
10198
}
10299

103100
// Generate styles and classes.
104-
$styles = wp_style_engine_get_styles(
101+
$color_and_border_output = wp_style_engine_get_styles(
105102
array(
106103
'color' => $color_styles,
107104
'background' => $background_styles,
108105
'border' => $border_styles,
109-
'shadow' => $shadow_styles,
110-
'spacing' => $spacing_styles,
106+
)
107+
);
108+
$shadow_output = wp_get_shadow_classes_and_styles( $attributes );
109+
$dimensions_and_spacing_output = wp_style_engine_get_styles(
110+
array(
111111
'dimensions' => $dimensions_styles,
112+
'spacing' => $spacing_styles,
113+
)
114+
);
115+
$styles = array(
116+
'classnames' => implode(
117+
' ',
118+
array_filter(
119+
array(
120+
$color_and_border_output['classnames'] ?? '',
121+
$shadow_output['class'] ?? '',
122+
$dimensions_and_spacing_output['classnames'] ?? '',
123+
)
124+
)
112125
),
126+
'css' => ( $color_and_border_output['css'] ?? '' ) . ( $shadow_output['style'] ?? '' ) . ( $dimensions_and_spacing_output['css'] ?? '' ),
113127
);
114128

115129
$svg = wp_get_icon(

‎packages/block-library/src/search/index.php‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,13 @@ function styles_for_block_core_search( $attributes ) {
472472
}
473473

474474
// Shadow lands on the same elements as the border.
475-
if ( ! empty( $attributes['style']['shadow'] ) && is_string( $attributes['style']['shadow'] ) ) {
476-
$shadow_styles = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) );
477-
478-
if ( ! empty( $shadow_styles['css'] ) ) {
479-
if ( $is_button_inside ) {
480-
$wrapper_styles[] = $shadow_styles['css'];
481-
} else {
482-
$button_styles[] = $shadow_styles['css'];
483-
$input_styles[] = $shadow_styles['css'];
484-
}
475+
$shadow_attributes = wp_get_shadow_classes_and_styles( $attributes );
476+
if ( ! empty( $shadow_attributes['style'] ) ) {
477+
if ( $is_button_inside ) {
478+
$wrapper_styles[] = $shadow_attributes['style'];
479+
} else {
480+
$button_styles[] = $shadow_attributes['style'];
481+
$input_styles[] = $shadow_attributes['style'];
485482
}
486483
}
487484

‎phpunit/block-supports/shadow-test.php‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,14 @@ public function data_get_shadow_classes_and_styles() {
138138
'block_attributes' => array( 'style' => array( 'shadow' => 'var:preset|shadow|natural' ) ),
139139
'expected' => array( 'style' => 'box-shadow:var(--wp--preset--shadow--natural);' ),
140140
),
141-
'numeric shadow' => array(
142-
'block_attributes' => array( 'style' => array( 'shadow' => 5 ) ),
143-
'expected' => array(),
144-
),
145-
'zero shadow' => array(
146-
'block_attributes' => array( 'style' => array( 'shadow' => 0 ) ),
147-
'expected' => array(),
148-
),
149141
'empty shadow' => array(
150142
'block_attributes' => array( 'style' => array( 'shadow' => '' ) ),
151143
'expected' => array(),
152144
),
153-
'no style attribute' => array(
154-
'block_attributes' => array(),
155-
'expected' => array(),
156-
),
157145
'non-array style' => array(
158146
'block_attributes' => array( 'style' => 'box-shadow:1px 1px 1px #000' ),
159147
'expected' => array(),
160148
),
161-
'array shadow' => array(
162-
'block_attributes' => array( 'style' => array( 'shadow' => array( '1px 1px 1px #000' ) ) ),
163-
'expected' => array(),
164-
),
165149
'non-array attributes' => array(
166150
'block_attributes' => null,
167151
'expected' => array(),

‎phpunit/blocks/render-block-avatar-test.php‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ private function render_image_attributes( $style ) {
5050
);
5151
}
5252

53-
public function test_shadow_is_applied_to_the_image() {
54-
$actual = $this->render_image_attributes( array( 'shadow' => 'var:preset|shadow|natural' ) );
55-
56-
$this->assertSame( 'box-shadow:var(--wp--preset--shadow--natural);', $actual['style'] );
57-
}
58-
5953
public function test_shadow_follows_border_on_the_image() {
6054
$actual = $this->render_image_attributes(
6155
array(

‎phpunit/blocks/render-block-icon-test.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ public function test_preserves_intrinsic_svg_style_when_applying_rotation() {
8888
$this->assertLessThan( strpos( $style, 'rotate' ), strpos( $style, 'fill' ) );
8989
}
9090

91+
public function test_shadow_keeps_the_style_engine_order() {
92+
$output = gutenberg_render_block_core_icon(
93+
array(
94+
'icon' => 'core/info',
95+
'style' => array(
96+
'color' => array( 'text' => '#000' ),
97+
'border' => array( 'width' => '2px' ),
98+
'shadow' => 'var:preset|shadow|natural',
99+
'spacing' => array( 'padding' => '4px' ),
100+
'dimensions' => array( 'width' => '48px' ),
101+
),
102+
)
103+
);
104+
105+
$processor = new WP_HTML_Tag_Processor( $output );
106+
$this->assertTrue( $processor->next_tag( 'svg' ) );
107+
$this->assertSame( 'has-text-color', $processor->get_attribute( 'class' ) );
108+
$this->assertSame( 'fill: none; color:#000;border-width:2px;box-shadow:var(--wp--preset--shadow--natural);width:48px;padding:4px;', $processor->get_attribute( 'style' ) );
109+
}
110+
91111
public function test_renders_only_icons_in_public_collections() {
92112
// Renders public core icon.
93113
$processor = new WP_HTML_Tag_Processor( gutenberg_render_block_core_icon( array( 'icon' => 'core/caution' ) ) );

‎phpunit/blocks/render-block-post-featured-image-test.php‎

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public static function wpTearDownAfterClass() {
3636
}
3737

3838
/**
39-
* Renders a Post Featured Image block and returns the class and style
40-
* attributes of the image and the overlay.
39+
* Renders a Post Featured Image block and returns the image's class and style attributes.
4140
*
4241
* @param array $attributes Block attributes.
43-
* @return array Class and style of the image and the overlay.
42+
* @return array Class and style of the image.
4443
*/
45-
private function render_attributes( $attributes ) {
44+
private function render_image_attributes( $attributes ) {
4645
$block = new WP_Block(
4746
array(
4847
'blockName' => 'core/post-featured-image',
@@ -53,31 +52,17 @@ private function render_attributes( $attributes ) {
5352

5453
$processor = new WP_HTML_Tag_Processor( $block->render() );
5554
$this->assertTrue( $processor->next_tag( 'img' ) );
56-
$actual = array(
57-
'image_class' => $processor->get_attribute( 'class' ),
58-
'image_style' => $processor->get_attribute( 'style' ),
59-
);
60-
if ( $processor->next_tag( 'span' ) ) {
61-
$actual['overlay_class'] = $processor->get_attribute( 'class' );
62-
$actual['overlay_style'] = $processor->get_attribute( 'style' );
63-
}
64-
65-
return $actual;
66-
}
6755

68-
public function test_shadow_is_applied_to_the_image() {
69-
$actual = $this->render_attributes(
70-
array( 'style' => array( 'shadow' => 'var:preset|shadow|natural' ) )
56+
return array(
57+
'class' => $processor->get_attribute( 'class' ),
58+
'style' => $processor->get_attribute( 'style' ),
7159
);
72-
73-
$this->assertSame( 'object-fit:cover;box-shadow:var(--wp--preset--shadow--natural);', $actual['image_style'] );
7460
}
7561

76-
public function test_shadow_follows_border_and_dimensions_on_the_image() {
77-
$actual = $this->render_attributes(
62+
public function test_shadow_follows_border_on_the_image() {
63+
$actual = $this->render_image_attributes(
7864
array(
79-
'dimRatio' => 50,
80-
'style' => array(
65+
'style' => array(
8166
'border' => array(
8267
'radius' => '10px',
8368
'color' => '#ff0000',
@@ -87,15 +72,7 @@ public function test_shadow_follows_border_and_dimensions_on_the_image() {
8772
)
8873
);
8974

90-
$this->assertSame( 'has-border-color wp-post-image', $actual['image_class'] );
91-
$this->assertSame( 'border-color:#ff0000;border-radius:10px;object-fit:cover;box-shadow:1px 1px 1px #000;', $actual['image_style'] );
92-
$this->assertSame( 'wp-block-post-featured-image__overlay has-border-color has-background-dim has-background-dim-50', $actual['overlay_class'] );
93-
$this->assertSame( 'border-color:#ff0000;border-radius:10px', $actual['overlay_style'] );
94-
}
95-
96-
public function test_empty_shadow_is_ignored() {
97-
$actual = $this->render_attributes( array( 'style' => array( 'shadow' => '' ) ) );
98-
99-
$this->assertSame( 'object-fit:cover;', $actual['image_style'] );
75+
$this->assertSame( 'has-border-color wp-post-image', $actual['class'] );
76+
$this->assertSame( 'border-color:#ff0000;border-radius:10px;object-fit:cover;box-shadow:1px 1px 1px #000;', $actual['style'] );
10077
}
10178
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Tests for the core Search block renderer.
4+
*
5+
* @package gutenberg
6+
* @subpackage block-library
7+
*/
8+
9+
/**
10+
* @covers ::gutenberg_render_block_core_search
11+
*/
12+
class Render_Block_Search_Test extends WP_UnitTestCase {
13+
/**
14+
* @var array|null
15+
*/
16+
private $original_block_to_render;
17+
18+
public function set_up() {
19+
parent::set_up();
20+
21+
$this->original_block_to_render = WP_Block_Supports::$block_to_render;
22+
WP_Block_Supports::$block_to_render = array(
23+
'blockName' => 'core/search',
24+
'attrs' => array(),
25+
);
26+
}
27+
28+
public function tear_down() {
29+
WP_Block_Supports::$block_to_render = $this->original_block_to_render;
30+
31+
parent::tear_down();
32+
}
33+
34+
public function test_shadow_follows_border_on_the_input_and_button() {
35+
$output = gutenberg_render_block_core_search(
36+
array(
37+
'label' => 'Search',
38+
'buttonText' => 'Search',
39+
'buttonPosition' => 'button-outside',
40+
'placeholder' => '',
41+
'style' => array(
42+
'border' => array( 'width' => '2px' ),
43+
'shadow' => 'var:preset|shadow|natural',
44+
),
45+
)
46+
);
47+
48+
$processor = new WP_HTML_Tag_Processor( $output );
49+
$this->assertTrue( $processor->next_tag( 'input' ) );
50+
$this->assertSame( 'border-width: 2px;box-shadow:var(--wp--preset--shadow--natural)', $processor->get_attribute( 'style' ) );
51+
$this->assertTrue( $processor->next_tag( 'button' ) );
52+
$this->assertSame( 'border-width: 2px;box-shadow:var(--wp--preset--shadow--natural)', $processor->get_attribute( 'style' ) );
53+
}
54+
}

0 commit comments

Comments
 (0)