Repository navigation
Expand file tree
/
Copy pathclass-wp-duotone-test.php
More file actions
184 lines (162 loc) · 7.89 KB
/
Copy pathclass-wp-duotone-test.php
File metadata and controls
184 lines (162 loc) · 7.89 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
<?php
/**
* Test the block WP_Duotone_Gutenberg class.
*
* @package gutenberg
*/
class WP_Duotone_Gutenberg_Test extends WP_UnitTestCase {
/**
* Cleans up CSS added to block-supports from duotone styles. We need to do this
* in order to avoid impacting other tests.
*/
public static function wpTearDownAfterClass() {
WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores();
}
/**
* Tests that unsupported duotone attribute types leave the block unchanged.
*
* @dataProvider data_render_duotone_support_invalid_attribute
* @covers WP_Duotone_Gutenberg::render_duotone_support
*
* @param mixed $duotone Invalid duotone attribute value.
*/
public function test_gutenberg_render_duotone_support_invalid_attribute( $duotone ): void {
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => $duotone ) ) ),
);
$wp_block = new WP_Block( $block );
$block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
$this->assertSame( $block_content, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block, $wp_block ) );
}
/**
* @return array<string, array{ 0: bool|int|float }>
*/
public function data_render_duotone_support_invalid_attribute(): array {
return array(
'false' => array( false ),
'true' => array( true ),
'integer' => array( 1 ),
'float' => array( 1.5 ),
);
}
public function test_gutenberg_render_duotone_support_preset() {
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
);
$wp_block = new WP_Block( $block );
$block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
$expected = '<figure class="wp-block-image size-full wp-duotone-blue-orange"><img src="/my-image.jpg" /></figure>';
$this->assertSame( $expected, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block, $wp_block ) );
}
public function test_gutenberg_render_duotone_support_css() {
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'unset' ) ) ),
);
$wp_block = new WP_Block( $block );
$block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
$expected = '/<figure class="wp-block-image size-full wp-duotone-unset-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
$this->assertMatchesRegularExpression( $expected, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block, $wp_block ) );
}
public function test_gutenberg_render_duotone_support_custom() {
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => array( '#FFFFFF', '#000000' ) ) ) ),
);
$wp_block = new WP_Block( $block );
$block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
$expected = '/<figure class="wp-block-image size-full wp-duotone-ffffff-000000-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
$this->assertMatchesRegularExpression( $expected, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block, $wp_block ) );
}
public function test_gutenberg_restore_image_outer_container_moves_duotone_class_to_wrapper_in_classic_theme() {
switch_theme( 'default' );
$block_content = '<div class="wp-block-image"><figure class="alignright wp-duotone-blue-orange size-full"><img src="/my-image.jpg"></figure></div>';
$expected = '<div class="wp-block-image wp-duotone-blue-orange"><figure class="alignright size-full"><img src="/my-image.jpg"></figure></div>';
$this->assertEqualHTML( $expected, WP_Duotone_Gutenberg::restore_image_outer_container( $block_content ) );
}
/**
* Tests whether the CSS declarations are generated even if the block content is
* 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(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
);
$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' );
if ( PHP_VERSION_ID < 80100 ) {
$block_css_declarations_property->setAccessible( true );
}
$previous_value = $block_css_declarations_property->getValue();
$block_css_declarations_property->setValue( $wp_duotone, array() );
WP_Duotone_Gutenberg::render_duotone_support( '', $block, $wp_block );
$actual = $block_css_declarations_property->getValue();
// Reset the property.
$block_css_declarations_property->setValue( $wp_duotone, $previous_value );
if ( PHP_VERSION_ID < 80100 ) {
$block_css_declarations_property->setAccessible( false );
}
$this->assertNotEmpty( $actual );
}
public function data_get_slug_from_attribute() {
return array(
'pipe-slug' => array( 'var:preset|duotone|blue-orange', 'blue-orange' ),
'css-var' => array( 'var(--wp--preset--duotone--blue-orange)', 'blue-orange' ),
'css-var-invalid-slug-chars' => array( 'var(--wp--preset--duotone--.)', '.' ),
'css-var-missing-end-parenthesis' => array( 'var(--wp--preset--duotone--blue-orange', '' ),
'invalid' => array( 'not a valid attribute', '' ),
'css-var-no-value' => array( 'var(--wp--preset--duotone--)', '' ),
'pipe-slug-no-value' => array( 'var:preset|duotone|', '' ),
'css-var-spaces' => array( 'var(--wp--preset--duotone-- ', '' ),
'pipe-slug-spaces' => array( 'var:preset|duotone| ', '' ),
'array-of-colors' => array( array( '#000000', '#ffffff' ), '' ),
'empty-array' => array( array(), '' ),
);
}
/**
* @dataProvider data_get_slug_from_attribute
*/
public function test_get_slug_from_attribute( $data_attr, $expected ) {
$reflection = new ReflectionMethod( 'WP_Duotone_Gutenberg', 'get_slug_from_attribute' );
if ( PHP_VERSION_ID < 80100 ) {
$reflection->setAccessible( true );
}
$this->assertSame( $expected, $reflection->invoke( null, $data_attr ) );
}
public function data_is_preset() {
return array(
'pipe-slug' => array( 'var:preset|duotone|blue-orange', true ),
'css-var' => array( 'var(--wp--preset--duotone--blue-orange)', true ),
'css-var-invalid-slug-chars' => array( 'var(--wp--preset--duotone--.)', false ),
'css-var-missing-end-parenthesis' => array( 'var(--wp--preset--duotone--blue-orange', false ),
'invalid' => array( 'not a valid attribute', false ),
'array-of-colors' => array( array( '#000000', '#ffffff' ), false ),
'empty-array' => array( array(), false ),
);
}
/**
* @dataProvider data_is_preset
*/
public function test_is_preset( $data_attr, $expected ) {
$reflection = new ReflectionMethod( 'WP_Duotone_Gutenberg', 'is_preset' );
if ( PHP_VERSION_ID < 80100 ) {
$reflection->setAccessible( true );
}
$this->assertSame( $expected, $reflection->invoke( null, $data_attr ) );
}
}