forked from WordPress/wordpress-develop
/
custom-css-setting.php
385 lines (338 loc) · 12.4 KB
/
custom-css-setting.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Test Test_WP_Customize_Custom_CSS_Setting.
*
* Tests WP_Customize_Custom_CSS_Setting.
*
* @group customize
*/
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase {
/**
* Instance of WP_Customize_Manager which is reset for each test.
*
* @var WP_Customize_Manager
*/
public $wp_customize;
/**
* The Setting instance.
*
* @var WP_Customize_Custom_CSS_Setting
*/
public $setting;
/**
* Set up the test case.
*
* @see WP_UnitTestCase::setup()
*/
function setUp(): void {
parent::setUp();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$user_id = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
if ( is_multisite() ) {
grant_super_admin( $user_id );
}
wp_set_current_user( $user_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
$wp_customize = $this->wp_customize;
do_action( 'customize_register', $this->wp_customize );
$this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[' . get_stylesheet() . ']' );
$this->wp_customize->add_setting( $this->setting );
}
/**
* Tear down the test case.
*/
function tearDown(): void {
parent::tearDown();
$this->setting = null;
}
/**
* Delete the $wp_customize global when cleaning up scope.
*/
function clean_up_global_scope() {
global $wp_customize;
$wp_customize = null;
parent::clean_up_global_scope();
}
/**
* Test constructor.
*
* Mainly validates that the correct hooks exist.
*
* Also checks for the post type and the Setting Type.
*
* @covers WP_Customize_Custom_CSS_Setting::__construct()
*/
function test_construct() {
$this->assertTrue( post_type_exists( 'custom_css' ) );
$this->assertEquals( 'custom_css', $this->setting->type );
$this->assertEquals( get_stylesheet(), $this->setting->stylesheet );
$this->assertEquals( 'edit_css', $this->setting->capability );
$exception = null;
try {
$x = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'bad' );
unset( $x );
} catch ( Exception $e ) {
$exception = $e;
}
$this->assertInstanceOf( 'Exception', $exception );
$exception = null;
try {
$x = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css' );
unset( $x );
} catch ( Exception $e ) {
$exception = $e;
}
$this->assertInstanceOf( 'Exception', $exception );
}
/**
* Test crud methods on WP_Customize_Custom_CSS_Setting.
*
* @covers ::wp_get_custom_css
* @covers WP_Customize_Custom_CSS_Setting::value()
* @covers WP_Customize_Custom_CSS_Setting::preview()
* @covers WP_Customize_Custom_CSS_Setting::update()
*/
function test_crud() {
$this->setting->default = '/* Hello World */';
$this->assertEquals( $this->setting->default, $this->setting->value() );
$this->assertNull( wp_get_custom_css_post() );
$this->assertNull( wp_get_custom_css_post( $this->setting->stylesheet ) );
$this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
$original_css = 'body { color: black; }';
$post_id = $this->factory()->post->create(
array(
'post_title' => $this->setting->stylesheet,
'post_name' => $this->setting->stylesheet,
'post_content' => $original_css,
'post_status' => 'publish',
'post_type' => 'custom_css',
)
);
$twentyten_css = 'body { color: red; }';
$twentyten_post_id = $this->factory()->post->create(
array(
'post_title' => 'twentyten',
'post_name' => 'twentyten',
'post_content' => $twentyten_css,
'post_status' => 'publish',
'post_type' => 'custom_css',
)
);
$twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' );
remove_theme_mod( 'custom_css_post_id' );
$this->assertEquals( $post_id, wp_get_custom_css_post()->ID );
$this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID );
$this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID );
$this->assertEquals( $original_css, wp_get_custom_css( $this->setting->stylesheet ) );
$this->assertEquals( $original_css, $this->setting->value() );
$this->assertEquals( $twentyten_css, wp_get_custom_css( 'twentyten' ) );
$this->assertEquals( $twentyten_css, $twentyten_setting->value() );
$updated_css = 'body { color: blue; }';
$this->wp_customize->set_post_value( $this->setting->id, $updated_css );
$saved = $this->setting->save();
$this->assertNotFalse( $saved );
$this->assertEquals( $updated_css, $this->setting->value() );
$this->assertEquals( $updated_css, wp_get_custom_css( $this->setting->stylesheet ) );
$this->assertEquals( $updated_css, get_post( $post_id )->post_content );
$previewed_css = 'body { color: red; }';
$this->wp_customize->set_post_value( $this->setting->id, $previewed_css );
$this->setting->preview();
$this->assertEquals( $previewed_css, $this->setting->value() );
$this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
// Make sure that wp_update_custom_css_post() works as expected for updates.
$r = wp_update_custom_css_post(
'body { color:red; }',
array(
'stylesheet' => $this->setting->stylesheet,
'preprocessed' => "body\n\tcolor:red;",
)
);
$this->assertInstanceOf( 'WP_Post', $r );
$this->assertEquals( $post_id, $r->ID );
$this->assertEquals( 'body { color:red; }', get_post( $r )->post_content );
$this->assertEquals( "body\n\tcolor:red;", get_post( $r )->post_content_filtered );
$r = wp_update_custom_css_post( 'body { content: "\o/"; }' );
$this->assertEquals( $this->wp_customize->get_stylesheet(), get_post( $r )->post_name );
$this->assertEquals( 'body { content: "\o/"; }', get_post( $r )->post_content );
$this->assertEquals( '', get_post( $r )->post_content_filtered );
// Make sure that wp_update_custom_css_post() works as expected for insertion.
$r = wp_update_custom_css_post(
'body { background:black; }',
array(
'stylesheet' => 'other',
)
);
$this->assertInstanceOf( 'WP_Post', $r );
$this->assertEquals( 'other', get_post( $r )->post_name );
$this->assertEquals( 'body { background:black; }', get_post( $r )->post_content );
$this->assertEquals( 'publish', get_post( $r )->post_status );
// Test deletion.
wp_delete_post( $post_id );
$this->assertNull( wp_get_custom_css_post() );
$this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );
$this->assertEquals( $previewed_css, wp_get_custom_css( get_stylesheet() ), 'Previewed value remains in spite of deleted post.' );
wp_delete_post( $twentyten_post_id );
$this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
$this->assertEquals( '', wp_get_custom_css( 'twentyten' ) );
}
/**
* Test revision saving on initial save of Custom CSS.
*
* @ticket 39032
*/
function test_custom_css_revision_saved() {
$inserted_css = 'body { background: black; }';
$updated_css = 'body { background: red; }';
$post = wp_update_custom_css_post(
$inserted_css,
array(
'stylesheet' => 'testtheme',
)
);
$this->assertSame( $inserted_css, $post->post_content );
$revisions = array_values( wp_get_post_revisions( $post ) );
$this->assertCount( 1, $revisions );
$this->assertSame( $inserted_css, $revisions[0]->post_content );
wp_update_custom_css_post(
$updated_css,
array(
'stylesheet' => 'testtheme',
)
);
$revisions = array_values( wp_get_post_revisions( $post ) );
$this->assertCount( 2, $revisions );
$this->assertSame( $updated_css, $revisions[0]->post_content );
$this->assertSame( $inserted_css, $revisions[1]->post_content );
}
/**
* Test that wp_get_custom_css_post() doesn't query for a post after caching a failed lookup.
*
* @ticket 39259
*/
function test_get_custom_css_post_queries_after_failed_lookup() {
set_theme_mod( 'custom_css_post_id', -1 );
$queries_before = get_num_queries();
wp_get_custom_css_post();
$this->assertSame( get_num_queries(), $queries_before );
}
/**
* Test that wp_update_custom_css_post() updates the 'custom_css_post_id' theme mod.
*
* @ticket 39259
*/
function test_update_custom_css_updates_theme_mod() {
set_theme_mod( 'custom_css_post_id', -1 );
$post = wp_update_custom_css_post( 'body { background: blue; }' );
$this->assertSame( $post->ID, get_theme_mod( 'custom_css_post_id' ) );
}
/**
* Test crud methods on WP_Customize_Custom_CSS_Setting.
*
* @covers WP_Customize_Custom_CSS_Setting::value()
*/
function test_value_filter() {
add_filter( 'customize_value_custom_css', array( $this, 'filter_value' ), 10, 2 );
$this->setting->default = '/*default*/';
$this->assertEquals( '/*default*//*filtered*/', $this->setting->value() );
$this->factory()->post->create(
array(
'post_title' => $this->setting->stylesheet,
'post_name' => $this->setting->stylesheet,
'post_content' => '/*custom*/',
'post_status' => 'publish',
'post_type' => 'custom_css',
)
);
remove_theme_mod( 'custom_css_post_id' );
$this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() );
$this->wp_customize->set_post_value( $this->setting->id, '/*overridden*/' );
$this->setting->preview();
$this->assertEquals( '/*overridden*/', $this->setting->value(), 'Expected value to not be filtered since post value is present.' );
}
/**
* Filter value.
*
* @param string $value Value.
* @param WP_Customize_Setting $setting Setting.
* @return string
*/
function filter_value( $value, $setting ) {
$this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting );
$value .= '/*filtered*/';
return $value;
}
/**
* Test update filter on WP_Customize_Custom_CSS_Setting.
*
* @covers WP_Customize_Custom_CSS_Setting::update()
*/
function test_update_filter() {
$original_css = 'body { color:red; }';
$post_id = $this->factory()->post->create(
array(
'post_title' => $this->setting->stylesheet,
'post_name' => $this->setting->stylesheet,
'post_content' => $original_css,
'post_status' => 'publish',
'post_type' => 'custom_css',
)
);
$overridden_css = 'body { color:green; }';
$this->wp_customize->set_post_value( $this->setting->id, $overridden_css );
$post = get_post( $post_id );
$original_title = $post->post_title;
add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 3 );
$this->setting->save();
$post = get_post( $post_id );
$this->assertEquals( $original_title, $post->post_title );
$this->assertStringContainsString( $overridden_css, $post->post_content );
$this->assertStringContainsString( '/* filtered post_content */', $post->post_content );
$this->assertStringContainsString( '/* filtered post_content_filtered */', $post->post_content_filtered );
}
/**
* Filter `customize_update_custom_css_post_content_args`.
*
* @param array $data Data.
* @param string $args Args.
* @return array Data.
*/
function filter_update_custom_css_data( $data, $args ) {
$this->assertIsArray( $data );
$this->assertEqualSets( array( 'css', 'preprocessed' ), array_keys( $data ) );
$this->assertEquals( '', $data['preprocessed'] );
$this->assertIsArray( $args );
$this->assertEqualSets( array( 'css', 'preprocessed', 'stylesheet' ), array_keys( $args ) );
$this->assertEquals( $args['css'], $data['css'] );
$this->assertEquals( $args['preprocessed'], $data['preprocessed'] );
$data['css'] .= '/* filtered post_content */';
$data['preprocessed'] = '/* filtered post_content_filtered */';
$data['post_title'] = 'Ignored';
return $data;
}
/**
* Tests that validation errors are caught appropriately.
*
* Note that the $validity \WP_Error object must be reset each time
* as it picks up the Errors and passes them to the next assertion.
*
* @covers WP_Customize_Custom_CSS_Setting::validate()
*/
function test_validate() {
// Empty CSS throws no errors.
$result = $this->setting->validate( '' );
$this->assertTrue( $result );
// Basic, valid CSS throws no errors.
$basic_css = 'body { background: #f00; } h1.site-title { font-size: 36px; } a:hover { text-decoration: none; } input[type="text"] { padding: 1em; }';
$result = $this->setting->validate( $basic_css );
$this->assertTrue( $result );
// Check for markup.
$unclosed_comment = $basic_css . '</style>';
$result = $this->setting->validate( $unclosed_comment );
$this->assertTrue( array_key_exists( 'illegal_markup', $result->errors ) );
}
}