forked from WordPress/wordpress-develop
/
selective-refresh-ajax.php
519 lines (472 loc) · 19.1 KB
/
selective-refresh-ajax.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
/**
* WP_Customize_Selective_Refresh Ajax tests.
*
* @package WordPress
* @subpackage UnitTests
*/
/**
* Tests for the WP_Customize_Selective_Refresh class Ajax.
*
* Note that this is intentionally not extending WP_Ajax_UnitTestCase because it
* is not admin ajax.
*
* @since 4.5.0
* @group ajax
*/
class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase {
/**
* Manager.
*
* @var WP_Customize_Manager
*/
public $wp_customize;
/**
* Component.
*
* @var WP_Customize_Selective_Refresh
*/
public $selective_refresh;
/**
* Set up the test fixture.
*/
function setUp(): void {
parent::setUp();
// Define wp_doing_ajax so that wp_die() will be used instead of die().
add_filter( 'wp_doing_ajax', '__return_true' );
add_filter( 'wp_die_ajax_handler', array( $this, 'get_wp_die_handler' ), 1, 1 );
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->wp_customize = $GLOBALS['wp_customize'];
if ( isset( $this->wp_customize->selective_refresh ) ) {
$this->selective_refresh = $this->wp_customize->selective_refresh;
}
}
/**
* Do Customizer boot actions.
*/
function do_customize_boot_actions() {
$_SERVER['REQUEST_METHOD'] = 'POST';
do_action( 'setup_theme' );
do_action( 'after_setup_theme' );
do_action( 'init' );
do_action( 'customize_register', $this->wp_customize );
$this->wp_customize->customize_preview_init();
do_action( 'wp', $GLOBALS['wp'] );
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request().
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_for_unauthenticated_user() {
$_POST[ WP_Customize_Selective_Refresh::RENDER_QUERY_VAR ] = '1';
// Check current_user_cannot_customize.
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
unset( $e );
}
$output = json_decode( ob_get_clean(), true );
$this->assertFalse( $output['success'] );
$this->assertEquals( 'expected_customize_preview', $output['data'] );
// Check expected_customize_preview.
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() );
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
unset( $e );
}
$output = json_decode( ob_get_clean(), true );
$this->assertFalse( $output['success'] );
$this->assertEquals( 'expected_customize_preview', $output['data'] );
// Check missing_partials.
$this->do_customize_boot_actions();
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
unset( $e );
}
$output = json_decode( ob_get_clean(), true );
$this->assertFalse( $output['success'] );
$this->assertEquals( 'missing_partials', $output['data'] );
// Check missing_partials.
$_POST['partials'] = 'bad';
$this->do_customize_boot_actions();
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$output = json_decode( ob_get_clean(), true );
$this->assertFalse( $output['success'] );
$this->assertEquals( 'malformed_partials', $output['data'] );
}
/**
* Set the current user to be an admin, add the preview nonce, and set the query var.
*/
function setup_valid_render_partials_request_environment() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() );
$_POST[ WP_Customize_Selective_Refresh::RENDER_QUERY_VAR ] = '1';
$this->do_customize_boot_actions();
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() for an unrecognized partial.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_for_unrecognized_partial() {
$this->setup_valid_render_partials_request_environment();
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'foo' => $placements,
)
)
);
ob_start();
try {
$this->expected_partial_ids = array( 'foo' );
add_filter( 'customize_render_partials_response', array( $this, 'filter_customize_render_partials_response' ), 10, 3 );
add_action( 'customize_render_partials_before', array( $this, 'handle_action_customize_render_partials_before' ), 10, 2 );
add_action( 'customize_render_partials_after', array( $this, 'handle_action_customize_render_partials_after' ), 10, 2 );
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$output = json_decode( ob_get_clean(), true );
$this->assertTrue( $output['success'] );
$this->assertIsArray( $output['data'] );
$this->assertArrayHasKey( 'contents', $output['data'] );
$this->assertArrayHasKey( 'errors', $output['data'] );
$this->assertArrayHasKey( 'foo', $output['data']['contents'] );
$this->assertEquals( null, $output['data']['contents']['foo'] );
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() for a partial that does not render.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_for_non_rendering_partial() {
$this->setup_valid_render_partials_request_environment();
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->wp_customize->add_setting( 'home' );
$this->wp_customize->selective_refresh->add_partial( 'foo', array( 'settings' => array( 'home' ) ) );
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'foo' => $placements,
)
)
);
$count_customize_render_partials_before = has_action( 'customize_render_partials_before' );
$count_customize_render_partials_after = has_action( 'customize_render_partials_after' );
ob_start();
try {
$this->expected_partial_ids = array( 'foo' );
add_filter( 'customize_render_partials_response', array( $this, 'filter_customize_render_partials_response' ), 10, 3 );
add_action( 'customize_render_partials_before', array( $this, 'handle_action_customize_render_partials_before' ), 10, 2 );
add_action( 'customize_render_partials_after', array( $this, 'handle_action_customize_render_partials_after' ), 10, 2 );
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$this->assertEquals( $count_customize_render_partials_before + 1, has_action( 'customize_render_partials_before' ) );
$this->assertEquals( $count_customize_render_partials_after + 1, has_action( 'customize_render_partials_after' ) );
$output = json_decode( ob_get_clean(), true );
$this->assertEquals( array( false ), $output['data']['contents']['foo'] );
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() for a partial the user doesn't have the capability to edit.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_rendering_disallowed_partial() {
$this->setup_valid_render_partials_request_environment();
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->wp_customize->add_setting(
'secret_message',
array(
'capability' => 'top_secret_clearance',
)
);
$this->wp_customize->selective_refresh->add_partial( 'secret_message', array( 'settings' => 'secret_message' ) );
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'secret_message' => $placements,
)
)
);
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$output = json_decode( ob_get_clean(), true );
$this->assertNull( $output['data']['contents']['secret_message'] );
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() for a partial for which an associated setting does not exist.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_rendering_partial_with_missing_settings() {
$this->setup_valid_render_partials_request_environment();
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->wp_customize->selective_refresh->add_partial( 'bar', array( 'settings' => 'bar' ) );
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'bar' => $placements,
)
)
);
ob_start();
try {
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$output = json_decode( ob_get_clean(), true );
$this->assertNull( $output['data']['contents']['bar'] );
}
/**
* Get the rendered blogname.
*
* @param WP_Customize_Partial $partial Partial.
* @param array $context Context data.
* @return string
*/
function render_callback_blogname( $partial, $context ) {
$this->assertIsArray( $context );
$this->assertInstanceOf( 'WP_Customize_Partial', $partial );
return get_bloginfo( 'name', 'display' );
}
/**
* Get the rendered blogdescription.
*
* @param WP_Customize_Partial $partial Partial.
* @param array $context Context data.
* @return string
*/
function render_callback_blogdescription( $partial, $context ) {
$this->assertIsArray( $context );
$this->assertInstanceOf( 'WP_Customize_Partial', $partial );
$x = get_bloginfo( 'description', 'display' );
return $x;
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() for a partial that does render.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_with_single_valid_placement() {
$this->setup_valid_render_partials_request_environment();
$this->wp_customize->selective_refresh->add_partial(
'test_blogname',
array(
'settings' => array( 'blogname' ),
'render_callback' => array( $this, 'render_callback_blogname' ),
)
);
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'test_blogname' => $placements,
)
)
);
$count_customize_render_partials_before = has_action( 'customize_render_partials_before' );
$count_customize_render_partials_after = has_action( 'customize_render_partials_after' );
ob_start();
try {
$this->expected_partial_ids = array( 'test_blogname' );
add_filter( 'customize_render_partials_response', array( $this, 'filter_customize_render_partials_response' ), 10, 3 );
add_action( 'customize_render_partials_before', array( $this, 'handle_action_customize_render_partials_before' ), 10, 2 );
add_action( 'customize_render_partials_after', array( $this, 'handle_action_customize_render_partials_after' ), 10, 2 );
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$this->assertEquals( $count_customize_render_partials_before + 1, has_action( 'customize_render_partials_before' ) );
$this->assertEquals( $count_customize_render_partials_after + 1, has_action( 'customize_render_partials_after' ) );
$output = json_decode( ob_get_clean(), true );
$this->assertEquals( array( get_bloginfo( 'name', 'display' ) ), $output['data']['contents']['test_blogname'] );
$this->assertArrayHasKey( 'setting_validities', $output['data'] );
}
/**
* Filter customize_dynamic_partial_args.
*
* @param array $partial_args Partial args.
* @param string $partial_id Partial ID.
*
* @return array|false Args.
*/
function filter_customize_dynamic_partial_args( $partial_args, $partial_id ) {
if ( 'test_dynamic_blogname' === $partial_id ) {
$partial_args = array(
'settings' => array( 'blogname' ),
'render_callback' => array( $this, 'render_callback_blogname' ),
);
}
return $partial_args;
}
/**
* Filter customize_render_partials_response.
*
* @param array $response Response.
* @param WP_Customize_Selective_Refresh $component Selective refresh component.
* @param array $partial_placements Placements' context data for the partials rendered in the request.
* The array is keyed by partial ID, with each item being an array of
* the placements' context data.
* @return array Response.
*/
function filter_customize_render_partials_response( $response, $component, $partial_placements ) {
$this->assertIsArray( $response );
$this->assertInstanceOf( 'WP_Customize_Selective_Refresh', $component );
if ( isset( $this->expected_partial_ids ) ) {
$this->assertEqualSets( $this->expected_partial_ids, array_keys( $partial_placements ) );
}
return $response;
}
/**
* Expected partial IDs.
*
* @var array
*/
protected $expected_partial_ids;
/**
* Handle 'customize_render_partials_before' action.
*
* @param WP_Customize_Selective_Refresh $component Selective refresh component.
* @param array $partial_placements Partial IDs.
*/
function handle_action_customize_render_partials_after( $component, $partial_placements ) {
$this->assertInstanceOf( 'WP_Customize_Selective_Refresh', $component );
if ( isset( $this->expected_partial_ids ) ) {
$this->assertEqualSets( $this->expected_partial_ids, array_keys( $partial_placements ) );
}
}
/**
* Handle 'customize_render_partials_after' action.
*
* @param WP_Customize_Selective_Refresh $component Selective refresh component.
* @param array $partial_placements Partial IDs.
*/
function handle_action_customize_render_partials_before( $component, $partial_placements ) {
$this->assertInstanceOf( 'WP_Customize_Selective_Refresh', $component );
if ( isset( $this->expected_partial_ids ) ) {
$this->assertEqualSets( $this->expected_partial_ids, array_keys( $partial_placements ) );
}
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request()dynamic partials are recognized.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_for_dynamic_partial() {
$this->setup_valid_render_partials_request_environment();
add_filter( 'customize_dynamic_partial_args', array( $this, 'filter_customize_dynamic_partial_args' ), 10, 2 );
$context_data = array();
$placements = array( $context_data );
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'test_dynamic_blogname' => $placements,
)
)
);
$count_customize_render_partials_before = has_action( 'customize_render_partials_before' );
$count_customize_render_partials_after = has_action( 'customize_render_partials_after' );
ob_start();
try {
$this->expected_partial_ids = array( 'test_dynamic_blogname' );
add_filter( 'customize_render_partials_response', array( $this, 'filter_customize_render_partials_response' ), 10, 3 );
add_action( 'customize_render_partials_before', array( $this, 'handle_action_customize_render_partials_before' ), 10, 2 );
add_action( 'customize_render_partials_after', array( $this, 'handle_action_customize_render_partials_after' ), 10, 2 );
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$this->assertEquals( $count_customize_render_partials_before + 1, has_action( 'customize_render_partials_before' ) );
$this->assertEquals( $count_customize_render_partials_after + 1, has_action( 'customize_render_partials_after' ) );
$output = json_decode( ob_get_clean(), true );
$this->assertEquals( array( get_bloginfo( 'name', 'display' ) ), $output['data']['contents']['test_dynamic_blogname'] );
}
/**
* Test WP_Customize_Selective_Refresh::handle_render_partials_request() to multiple partials can be requested at once.
*
* @see WP_Customize_Selective_Refresh::handle_render_partials_request()
*/
function test_handle_render_partials_request_for_multiple_partials_placements() {
$this->setup_valid_render_partials_request_environment();
$this->wp_customize->selective_refresh->add_partial(
'test_blogname',
array(
'settings' => array( 'blogname' ),
'render_callback' => array( $this, 'render_callback_blogname' ),
)
);
$this->wp_customize->selective_refresh->add_partial(
'test_blogdescription',
array(
'settings' => array( 'blogdescription' ),
'render_callback' => array( $this, 'render_callback_blogdescription' ),
)
);
$placement_context_data = array();
$_POST['partials'] = wp_slash(
wp_json_encode(
array(
'test_blogname' => array( $placement_context_data ),
'test_blogdescription' => array( $placement_context_data, $placement_context_data ),
)
)
);
$count_customize_render_partials_before = has_action( 'customize_render_partials_before' );
$count_customize_render_partials_after = has_action( 'customize_render_partials_after' );
ob_start();
try {
$this->expected_partial_ids = array( 'test_blogname', 'test_blogdescription' );
add_filter( 'customize_render_partials_response', array( $this, 'filter_customize_render_partials_response' ), 10, 3 );
add_action( 'customize_render_partials_before', array( $this, 'handle_action_customize_render_partials_before' ), 10, 2 );
add_action( 'customize_render_partials_after', array( $this, 'handle_action_customize_render_partials_after' ), 10, 2 );
$this->selective_refresh->handle_render_partials_request();
} catch ( WPDieException $e ) {
$this->assertEquals( '', $e->getMessage() );
}
$this->assertEquals( $count_customize_render_partials_before + 1, has_action( 'customize_render_partials_before' ) );
$this->assertEquals( $count_customize_render_partials_after + 1, has_action( 'customize_render_partials_after' ) );
$output = json_decode( ob_get_clean(), true );
$this->assertEquals( array( get_bloginfo( 'name', 'display' ) ), $output['data']['contents']['test_blogname'] );
$this->assertEquals( array_fill( 0, 2, get_bloginfo( 'description', 'display' ) ), $output['data']['contents']['test_blogdescription'] );
}
/**
* Tear down.
*/
function tearDown(): void {
$this->expected_partial_ids = null;
$this->wp_customize = null;
unset( $GLOBALS['wp_customize'] );
unset( $GLOBALS['wp_scripts'] );
parent::tearDown();
}
}