forked from WordPress/wordpress-develop
/
wpGetGlobalStylesheet.php
290 lines (260 loc) · 10.3 KB
/
wpGetGlobalStylesheet.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
require_once __DIR__ . '/base.php';
/**
* Tests wp_get_global_stylesheet().
*
* @group themes
*
* @covers ::wp_get_global_stylesheet
*/
class Tests_Theme_WpGetGlobalStylesheet extends WP_Theme_UnitTestCase {
/**
* Flag to indicate whether to remove 'editor-font-sizes' theme support at tear_down().
*
* @var bool
*/
private $remove_theme_support_at_teardown = false;
/**
* Flag to indicate whether to switch back to the default theme at tear down.
*
* @var bool
*/
private $switch_to_default_theme_at_teardown = false;
public function tear_down() {
// Reset development mode after each test.
unset( $GLOBALS['_wp_tests_development_mode'] );
// Reset the theme support.
if ( $this->remove_theme_support_at_teardown ) {
$this->remove_theme_support_at_teardown = false;
remove_theme_support( 'editor-font-sizes' );
}
if ( $this->switch_to_default_theme_at_teardown ) {
$this->switch_to_default_theme_at_teardown = false;
switch_theme( WP_DEFAULT_THEME );
}
parent::tear_down();
}
/**
* @ticket 54782
*
* @dataProvider data_should_conditionally_include_font_sizes
*
* @param array $expected Expected CSS for each font size.
* @param string $theme The theme to switch to / use.
* @param array $types Optional. Types of styles to load. Default empty array.
* @param bool $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
*/
public function test_should_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
$this->maybe_switch_theme( $theme );
$this->add_custom_font_sizes( $classic_has_presets );
$styles = wp_get_global_stylesheet( $types );
$this->assertStringContainsString( $expected['small'], $styles, 'The small font size should be included.' );
$this->assertStringContainsString( $expected['medium'], $styles, 'The medium font size should be included.' );
$this->assertStringContainsString( $expected['large'], $styles, 'The large font size should be included.' );
$this->assertStringContainsString( $expected['x-large'], $styles, 'The x-large font size should be included.' );
if ( 'default' !== $theme ) {
$this->assertStringContainsString( $expected['custom'], $styles, 'The custom font size should be included.' );
}
}
/**
* Data provider.
*
* @return array[]
*/
public function data_should_conditionally_include_font_sizes() {
return array(
'block theme using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
),
'block theme using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
'types' => array( 'variables' ),
),
'classic theme without presets using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
),
'classic theme without presets using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'variables' ),
),
'classic theme with presets using defaults' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array(),
'classic_has_presets' => true,
),
'classic theme with presets using variables' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'variables' ),
'classic_has_presets' => true,
),
);
}
/**
* @ticket 54782
*
* @dataProvider data_should_not_conditionally_include_font_sizes
*
* @param array $expected Expected CSS for each font size.
* @param string $theme The theme to switch to / use.
* @param array $types Optional. Types of styles to load. Default empty array.
* @param bool $classic_has_presets Optional. Whether to apply presets for classic theme tests. Default false.
*/
public function test_should_not_conditionally_include_font_sizes( array $expected, $theme, array $types = array(), $classic_has_presets = false ) {
$this->maybe_switch_theme( $theme );
$this->add_custom_font_sizes( $classic_has_presets );
$styles = wp_get_global_stylesheet( $types );
$this->assertStringNotContainsString( $expected['small'], $styles, 'The small font size should not be included.' );
$this->assertStringNotContainsString( $expected['medium'], $styles, 'The medium font size should not be included.' );
$this->assertStringNotContainsString( $expected['large'], $styles, 'The large font size should not be included.' );
$this->assertStringNotContainsString( $expected['x-large'], $styles, 'The x-large font size should not be included.' );
if ( 'default' !== $theme ) {
$this->assertStringNotContainsString( $expected['custom'], $styles, 'The custom font size should not be included.' );
}
}
/**
* Data provider.
*
* @return array[]
*/
public function data_should_not_conditionally_include_font_sizes() {
return array(
'block theme using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
'custom' => '--wp--preset--font-size--custom: 100px;',
),
'theme' => 'block-theme',
'types' => array( 'presets' ),
),
'classic theme without presets using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 13px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 36px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'presets' ),
),
'classic theme with presets using presets' => array(
'expected' => array(
'small' => '--wp--preset--font-size--small: 18px',
'medium' => '--wp--preset--font-size--medium: 20px',
'large' => '--wp--preset--font-size--large: 26.25px',
'x-large' => '--wp--preset--font-size--x-large: 42px',
),
'theme' => 'default',
'types' => array( 'presets' ),
'classic_has_presets' => true,
),
);
}
/**
* @ticket 56970
*/
public function test_switching_themes_should_recalculate_stylesheet() {
$expected = '--wp--preset--font-size--custom: 100px;';
$stylesheet_for_default_theme = wp_get_global_stylesheet();
$this->assertStringNotContainsString( $expected, $stylesheet_for_default_theme, 'Custom font size (100px) should not present for default theme' );
$this->maybe_switch_theme( 'block-theme' );
$stylesheet_for_block_theme = wp_get_global_stylesheet();
$this->assertStringContainsString( $expected, $stylesheet_for_block_theme, 'Custom font size (100px) should be present for block theme' );
}
/**
* Tests that the function relies on the development mode for whether to use caching.
*
* @ticket 57487
*/
public function test_caching_is_used_when_developing_theme() {
global $_wp_tests_development_mode;
$this->maybe_switch_theme( 'block-theme' );
// Store CSS in cache.
$css = '.my-class { display: block; }';
wp_cache_set( 'wp_get_global_stylesheet', $css, 'theme_json' );
// By default, caching should be used, so the above value will be returned.
$_wp_tests_development_mode = '';
$this->assertSame( $css, wp_get_global_stylesheet(), 'Caching was not used despite development mode disabled' );
// When the development mode is set to 'theme', caching should not be used.
$_wp_tests_development_mode = 'theme';
$this->assertNotSame( $css, wp_get_global_stylesheet(), 'Caching was used despite theme development mode' );
}
/**
* Adds the 'editor-font-sizes' theme support with custom font sizes.
*
* @param bool $add_theme_support Whether to add the theme support.
* @param int $small Optional. Small font size in pixels. Default 18.
* @param int $large Optional. Large font size in pixels. Default 26.25.
*/
private function add_custom_font_sizes( $add_theme_support, $small = 18, $large = 26.25 ) {
if ( ! $add_theme_support ) {
return;
}
$args = array(
array(
'name' => 'Small',
'size' => $small,
'slug' => 'small',
),
array(
'name' => 'Large',
'size' => $large,
'slug' => 'large',
),
);
add_theme_support( 'editor-font-sizes', $args );
$this->remove_theme_support_at_teardown = true;
}
/**
* Switches the theme when not the 'default' theme.
*
* @param string $theme Theme name to switch to.
*/
private function maybe_switch_theme( $theme ) {
if ( 'default' === $theme ) {
return;
}
switch_theme( $theme );
$this->switch_to_default_theme_at_teardown = true;
}
}