Repository navigation
Expand file tree
/
Copy pathrender-block-button-test.php
More file actions
202 lines (174 loc) · 6.21 KB
/
Copy pathrender-block-button-test.php
File metadata and controls
202 lines (174 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Button block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/
/**
* Tests for the Button block.
*
* @group blocks
*/
class Render_Block_Button_Test extends WP_UnitTestCase {
/**
* Button content with a link element.
*
* @var string
*/
private static $button_content = '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Click me</a></div>';
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_empty_button_renders_nothing() {
$content = '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button"></a></div>';
$result = gutenberg_render_block_core_button( array(), $content );
$this->assertEmpty( $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_button_without_width_returns_content_unchanged() {
$result = gutenberg_render_block_core_button( array(), self::$button_content );
$this->assertStringNotContainsString( 'has-custom-width', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_non_percentage_fixed_width_applies_inline_style() {
$attributes = array(
'style' => array(
'dimensions' => array(
'width' => '200px',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$this->assertStringContainsString( 'has-custom-width', $result );
$this->assertStringContainsString( 'width: 200px;', $result );
$this->assertStringNotContainsString( 'wp-block-button__width', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_non_percentage_preset_width_applies_css_var() {
$attributes = array(
'style' => array(
'dimensions' => array(
'width' => 'var:preset|dimension|custom-width',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$this->assertStringContainsString( 'has-custom-width', $result );
$this->assertStringContainsString( 'width: var(--wp--preset--dimension--custom-width);', $result );
$this->assertStringNotContainsString( 'wp-block-button__width', $result );
$this->assertStringNotContainsString( 'var:preset|dimension|custom-width', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_percentage_width_applies_custom_property_and_classes() {
$attributes = array(
'style' => array(
'dimensions' => array(
'width' => '50%',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$this->assertStringContainsString( 'has-custom-width', $result );
$this->assertStringContainsString( 'wp-block-button__width', $result );
$this->assertStringContainsString( 'wp-block-button__width-50', $result );
$this->assertStringContainsString( '--wp--block-button--width: 50;', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_percentage_preset_width_applies_custom_property_and_classes() {
// Use the default '50' dimension preset which resolves to '50%'.
$attributes = array(
'style' => array(
'dimensions' => array(
'width' => 'var:preset|dimension|50',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$this->assertStringContainsString( 'has-custom-width', $result );
$this->assertStringContainsString( 'wp-block-button__width', $result );
$this->assertStringContainsString( 'wp-block-button__width-50', $result );
$this->assertStringContainsString( '--wp--block-button--width: 50;', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_custom_percentage_width_adds_generic_class_without_legacy() {
$attributes = array(
'style' => array(
'dimensions' => array(
'width' => '33%',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$this->assertStringContainsString( 'has-custom-width', $result );
$this->assertStringContainsString( 'wp-block-button__width', $result );
$this->assertStringNotContainsString( 'wp-block-button__width-33', $result );
$this->assertStringContainsString( '--wp--block-button--width: 33;', $result );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_background_image_applies_to_link() {
$attributes = array(
'style' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'https://example.com/image.jpg',
),
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$processor = new WP_HTML_Tag_Processor( $result );
$processor->next_tag( 'a' );
$this->assertSame( "background-image:url('https://example.com/image.jpg');background-size:cover;", $processor->get_attribute( 'style' ) );
$this->assertTrue( $processor->has_class( 'has-background' ) );
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_background_gradient_appends_to_existing_link_style() {
$content = '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" style="color:#fff">Click me</a></div>';
$attributes = array(
'style' => array(
'background' => array(
'gradient' => 'linear-gradient(red, blue)',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, $content );
$this->assertSame(
'<div class="wp-block-button"><a class="wp-block-button__link wp-element-button has-background" style="color:#fff;background-image:linear-gradient(red, blue);">Click me</a></div>',
$result
);
}
/**
* @covers ::gutenberg_render_block_core_button
*/
public function test_background_gradient_preset_applies_to_link() {
$attributes = array(
'style' => array(
'background' => array(
'gradient' => 'var:preset|gradient|purple-to-yellow',
),
),
);
$result = gutenberg_render_block_core_button( $attributes, self::$button_content );
$processor = new WP_HTML_Tag_Processor( $result );
$processor->next_tag( 'a' );
$this->assertSame( 'background-image:var(--wp--preset--gradient--purple-to-yellow);', $processor->get_attribute( 'style' ) );
$this->assertTrue( $processor->has_class( 'has-background' ) );
}
}