-
Notifications
You must be signed in to change notification settings - Fork 887
Expand file tree
/
Copy pathtest_class.jetpack-sync-callables.php
More file actions
689 lines (531 loc) · 26.6 KB
/
Copy pathtest_class.jetpack-sync-callables.php
File metadata and controls
689 lines (531 loc) · 26.6 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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
require_once dirname( __FILE__ ) . '/../../../sync/class.jetpack-sync-functions.php';
require_once 'test_class.jetpack-sync-base.php';
function jetpack_foo_is_callable() {
return 'bar';
}
/**
* Testing Functions
*/
class WP_Test_Jetpack_Sync_Functions extends WP_Test_Jetpack_Sync_Base {
protected $post;
protected $callable_module;
public function setUp() {
parent::setUp();
$this->resetCallableAndConstantTimeouts();
$this->callable_module = Jetpack_Sync_Modules::get_module( "functions" );
set_current_screen( 'post-user' ); // this only works in is_admin()
}
function test_white_listed_function_is_synced() {
$this->callable_module->set_callable_whitelist( array( 'jetpack_foo' => 'jetpack_foo_is_callable' ) );
$this->sender->do_sync();
$synced_value = $this->server_replica_storage->get_callable( 'jetpack_foo' );
$this->assertEquals( jetpack_foo_is_callable(), $synced_value );
}
public function test_sync_jetpack_updates() {
$this->sender->do_sync();
$updates = $this->server_replica_storage->get_callable( 'updates' );
$this->assertEqualsObject( Jetpack::get_updates(), $updates );
}
function test_wp_version_is_synced() {
global $wp_version;
$this->sender->do_sync();
$synced_value = $this->server_replica_storage->get_callable( 'wp_version' );
$this->assertEquals( $synced_value, $wp_version );
}
public function test_sync_callable_whitelist() {
// $this->setSyncClientDefaults();
$callables = array(
'wp_max_upload_size' => wp_max_upload_size(),
'is_main_network' => Jetpack::is_multi_network(),
'is_multi_site' => is_multisite(),
'main_network_site' => Jetpack_Sync_Functions::main_network_site_url(),
'single_user_site' => Jetpack::is_single_user_site(),
'updates' => Jetpack::get_updates(),
'home_url' => Jetpack_Sync_Functions::home_url(),
'site_url' => Jetpack_Sync_Functions::site_url(),
'has_file_system_write_access' => Jetpack_Sync_Functions::file_system_write_access(),
'is_version_controlled' => Jetpack_Sync_Functions::is_version_controlled(),
'taxonomies' => Jetpack_Sync_Functions::get_taxonomies(),
'post_types' => Jetpack_Sync_Functions::get_post_types(),
'post_type_features' => Jetpack_Sync_Functions::get_post_type_features(),
'rest_api_allowed_post_types' => Jetpack_Sync_Functions::rest_api_allowed_post_types(),
'rest_api_allowed_public_metadata' => Jetpack_Sync_Functions::rest_api_allowed_public_metadata(),
'sso_is_two_step_required' => Jetpack_SSO_Helpers::is_two_step_required(),
'sso_should_hide_login_form' => Jetpack_SSO_Helpers::should_hide_login_form(),
'sso_match_by_email' => Jetpack_SSO_Helpers::match_by_email(),
'sso_new_user_override' => Jetpack_SSO_Helpers::new_user_override(),
'sso_bypass_default_login_form' => Jetpack_SSO_Helpers::bypass_login_forward_wpcom(),
'wp_version' => Jetpack_Sync_Functions::wp_version(),
'get_plugins' => Jetpack_Sync_Functions::get_plugins(),
'get_plugins_action_links' => Jetpack_Sync_functions::get_plugins_action_links(),
'active_modules' => Jetpack::get_active_modules(),
'hosting_provider' => Jetpack_Sync_Functions::get_hosting_provider(),
'locale' => get_locale(),
'site_icon_url' => Jetpack_Sync_Functions::site_icon_url(),
'shortcodes' => Jetpack_Sync_Functions::get_shortcodes(),
'roles' => Jetpack_Sync_Functions::roles(),
);
if ( function_exists( 'wp_cache_is_enabled' ) ) {
$callables['wp_super_cache_globals'] = Jetpack_Sync_Module_WP_Super_Cache::get_wp_super_cache_globals();
}
if ( is_multisite() ) {
$callables['network_name'] = Jetpack::network_name();
$callables['network_allow_new_registrations'] = Jetpack::network_allow_new_registrations();
$callables['network_add_new_users'] = Jetpack::network_add_new_users();
$callables['network_site_upload_space'] = Jetpack::network_site_upload_space();
$callables['network_upload_file_types'] = Jetpack::network_upload_file_types();
$callables['network_enable_administration_menus'] = Jetpack::network_enable_administration_menus();
}
$this->sender->do_sync();
foreach ( $callables as $name => $value ) {
// TODO: figure out why _sometimes_ the 'support' value of
// the post_types value is being removed from the output
if ( $name === 'post_types' ) {
continue;
}
$this->assertCallableIsSynced( $name, $value );
}
$whitelist_keys = array_keys( $this->callable_module->get_callable_whitelist() );
$callables_keys = array_keys( $callables );
// Are we testing all the callables in the defaults?
$whitelist_and_callable_keys_difference = array_diff( $whitelist_keys, $callables_keys );
$this->assertTrue( empty( $whitelist_and_callable_keys_difference ), 'Some whitelisted options don\'t have a test: ' . print_r( $whitelist_and_callable_keys_difference, 1 ) );
// Are there any duplicate keys?
$unique_whitelist = array_unique( $whitelist_keys );
$this->assertEquals( count( $unique_whitelist ), count( $whitelist_keys ), 'The duplicate keys are: ' . print_r( array_diff_key( $whitelist_keys, array_unique( $whitelist_keys ) ), 1 ) );
}
function assertCallableIsSynced( $name, $value ) {
$this->assertEqualsObject( $value, $this->server_replica_storage->get_callable( $name ), 'Function ' . $name . ' didn\'t have the expected value of ' . json_encode( $value ) );
}
function test_white_listed_callables_doesnt_get_synced_twice() {
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME );
$this->callable_module->set_callable_whitelist( array( 'jetpack_foo' => 'jetpack_foo_is_callable' ) );
$this->sender->do_sync();
$synced_value = $this->server_replica_storage->get_callable( 'jetpack_foo' );
$this->assertEquals( 'bar', $synced_value );
$this->server_replica_storage->reset();
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
$this->sender->do_sync();
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'jetpack_foo' ) );
}
function test_sync_always_sync_changes_to_modules_right_away() {
Jetpack::update_active_modules( array( 'stats' ) );
$this->sender->do_sync();
$synced_value = $this->server_replica_storage->get_callable( 'active_modules' );
$this->assertEquals( array( 'stats' ), $synced_value );
$this->server_replica_storage->reset();
Jetpack::update_active_modules( array( 'json-api' ) );
$this->sender->do_sync();
$synced_value = $this->server_replica_storage->get_callable( 'active_modules' );
$this->assertEquals( array( 'json-api' ), $synced_value );
}
function test_sync_always_sync_changes_to_home_siteurl_right_away() {
$original_home_option = get_option( 'home' );
$original_siteurl_option = get_option( 'siteurl' );
// Let's see if the original values get synced
$this->sender->do_sync();
$synced_home_url = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $this->server_replica_storage->get_callable( 'site_url' );
$this->assertEquals( $original_home_option, $synced_home_url );
$this->assertEquals( $original_siteurl_option, $synced_site_url );
$this->server_replica_storage->reset();
$updated_home_option = 'http://syncrocks.com';
$updated_siteurl_option = 'http://syncrocks.com';
update_option( 'home', $updated_home_option );
update_option( 'siteurl', $updated_siteurl_option );
$this->sender->do_sync();
$synced_home_url = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $this->server_replica_storage->get_callable( 'site_url' );
$this->assertEquals( $updated_home_option, $synced_home_url );
$this->assertEquals( $updated_siteurl_option, $synced_site_url );
// Cleanup
update_option( 'home', $original_home_option );
update_option( 'siteurl', $original_siteurl_option );
}
function test_sync_jetpack_sync_unlock_sync_callable_action_allows_syncing_siteurl_changes() {
$original_home_option = get_option( 'home' );
$original_siteurl_option = get_option( 'siteurl' );
// Let's see if the original values get synced. This will also set the await transient.
$this->sender->do_sync();
$synced_home_url = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $this->server_replica_storage->get_callable( 'site_url' );
$this->assertEquals( $original_home_option, $synced_home_url );
$this->assertEquals( $original_siteurl_option, $synced_site_url );
$this->server_replica_storage->reset();
update_option( 'home', $this->return_https_site_com_blog() );
update_option( 'siteurl', $this->return_https_site_com_blog() );
/**
* Used to signal that the callables await transient should be cleared. Clearing the await transient is useful
* in cases where we need to sync values to WordPress.com sooner than the default wait time.
*
* @since 4.4.0
*/
do_action( 'jetpack_sync_unlock_sync_callable' );
$_SERVER['HTTPS'] = 'on';
$this->sender->do_sync();
$synced_home_url = $this->server_replica_storage->get_callable( 'home_url' );
$synced_site_url = $this->server_replica_storage->get_callable( 'site_url' );
$this->assertEquals( $this->return_https_site_com_blog(), $synced_home_url );
$this->assertEquals( $this->return_https_site_com_blog(), $synced_site_url );
// Cleanup
unset( $_SERVER['HTTPS'] );
update_option( 'home', $original_home_option );
update_option( 'siteurl', $original_siteurl_option );
}
function test_home_site_urls_synced_while_migrate_for_idc_set() {
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME );
$home_option = get_option( 'home' );
$siteurl_option = get_option( 'siteurl' );
$main_network = network_site_url();
// First, let's see if the original values get synced
$this->sender->do_sync();
$this->assertEquals( $home_option, $this->server_replica_storage->get_callable( 'home_url' ) );
$this->assertEquals( $siteurl_option, $this->server_replica_storage->get_callable( 'site_url' ) );
$this->assertEquals( $main_network, $this->server_replica_storage->get_callable( 'main_network_site' ) );
// Second, let's make sure that values don't get synced again if the migrate_for_idc option is not set
$this->server_replica_storage->reset();
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
$this->sender->do_sync();
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'home_url' ) );
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'site_url' ) );
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'main_network_site' ) );
// Third, let's test that values get syncd with the option set
Jetpack_Options::update_option( 'migrate_for_idc', true );
$this->server_replica_storage->reset();
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
$this->sender->do_sync();
$this->assertEquals( $home_option, $this->server_replica_storage->get_callable( 'home_url' ) );
$this->assertEquals( $siteurl_option, $this->server_replica_storage->get_callable( 'site_url' ) );
$this->assertEquals( $main_network, $this->server_replica_storage->get_callable( 'main_network_site' ) );
Jetpack_Options::delete_option( 'migrate_for_idc' );
}
function return_example_com() {
return 'http://example.com';
}
function return_example_com_blog() {
return 'http://example.com/blog';
}
function return_https_example_com() {
return 'https://example.com';
}
function return_https_example_org() {
return 'https://example.org';
}
function return_site_com() {
return 'http://site.com';
}
function return_https_site_com() {
return 'https://site.com';
}
function return_https_site_com_blog() {
return 'https://site.com/blog';
}
function return_https_www_example_com() {
return 'https://www.example.com';
}
function return_https_foo_example_com() {
return 'https://foo.example.com';
}
function test_get_protocol_normalized_url_works_with_no_history() {
$callable_type = 'home_url';
$option_key = Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable_type;
delete_option( $option_key );
$this->assertStringStartsWith(
'http://',
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_example_com() )
);
delete_option( $option_key );
$this->assertStringStartsWith(
'https://',
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_https_example_com() )
);
$this->assertCount( 1, get_option( $option_key ) );
delete_option( $option_key );
}
function test_get_protocol_normalized_url_stores_max_history() {
$callable_type = 'home_url';
$option_key = Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable_type;
delete_option( $option_key );
for ( $i = 0; $i < 20; $i++ ) {
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_example_com() );
}
$this->assertCount( Jetpack_Sync_Functions::HTTPS_CHECK_HISTORY, get_option( $option_key ) );
delete_option( $option_key );
}
function test_get_protocol_normalized_url_returns_http_when_https_falls_off() {
$callable_type = 'home_url';
$option_key = Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable_type;
delete_option( $option_key );
// Start with one https scheme
$this->assertStringStartsWith(
'https://',
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_https_example_com() )
);
// Now add enough http schemes to fill up the history
for ( $i = 1; $i < Jetpack_Sync_Functions::HTTPS_CHECK_HISTORY; $i++ ) {
$this->assertStringStartsWith(
'https://',
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_example_com() )
);
}
// Now that the history is full, this one should cause the function to return false.
$this->assertStringStartsWith(
'http://',
Jetpack_Sync_Functions::get_protocol_normalized_url( $callable_type, $this->return_example_com() )
);
}
function test_get_protocol_normalized_url_returns_new_value_cannot_parse() {
$test_url = 'http:///example.com';
$this->assertEquals(
$test_url,
Jetpack_Sync_Functions::get_protocol_normalized_url( 'home_url', $test_url )
);
}
function test_get_protocol_normalized_url_cleared_on_reset_data() {
Jetpack_Sync_Functions::get_protocol_normalized_url( 'home_url', get_home_url() );
Jetpack_Sync_Functions::get_protocol_normalized_url( 'site_url', get_site_url() );
Jetpack_Sync_Functions::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' );
foreach( $url_callables as $callable ) {
$this->assertInternalType( 'array', get_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable) );
}
Jetpack_Sync_Sender::get_instance()->uninstall();
foreach( $url_callables as $callable ) {
$this->assertFalse( get_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable ) );
}
}
function test_subdomain_switching_to_www_does_not_cause_sync() {
// a lot of sites accept www.domain.com or just domain.com, and we want to prevent lots of
// switching back and forth, so we force the domain to be the one in the siteurl option
$this->setSyncClientDefaults();
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME );
$original_site_url = site_url();
// sync original value
$this->sender->do_sync();
$this->assertEquals( $original_site_url, $this->server_replica_storage->get_callable( 'site_url' ) );
add_filter( 'site_url', array( $this, 'add_www_subdomain_to_siteurl' ) );
delete_transient( Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME );
delete_option( Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME );
$this->sender->do_sync();
$this->assertEquals( $original_site_url, $this->server_replica_storage->get_callable( 'site_url' ) );
}
function test_only_syncs_if_is_admin_and_not_cron() {
// non-admin
set_current_screen( 'front' );
$this->sender->do_sync();
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'site_url' ) );
set_current_screen( 'post-user' );
// admin but in cron (for some reason)
Jetpack_Sync_Settings::set_doing_cron( true );
$this->sender->do_sync();
$this->assertEquals( null, $this->server_replica_storage->get_callable( 'site_url' ) );
Jetpack_Sync_Settings::set_doing_cron( false );
$this->sender->do_sync();
$this->assertEquals( site_url(), $this->server_replica_storage->get_callable( 'site_url' ) );
}
function test_site_icon_url_returns_false_when_no_site_icon() {
delete_option( 'jetpack_site_icon_url' );
$this->sender->do_sync();
$this->assertFalse( $this->server_replica_storage->get_callable( 'site_icon_url' ) );
}
function test_site_icon_url_returns_core_site_icon_url_when_set() {
$attachment_id = $this->factory->post->create( array(
'post_type' => 'attachment',
'post_mime_type' => 'image/png',
) );
add_post_meta( $attachment_id, '_wp_attached_file', '2016/09/core_site_icon_url.png' );
update_option( 'site_icon', $attachment_id );
update_option( 'jetpack_site_icon_url', 'http://website.com/wp-content/uploads/2016/09/jetpack_site_icon.png' );
$this->sender->do_sync();
$this->assertContains( 'core_site_icon_url', $this->server_replica_storage->get_callable( 'site_icon_url' ) );
delete_option( 'site_icon' );
}
function test_site_icon_url_fallback_to_jetpack_site_icon_url() {
delete_option( 'site_icon' );
update_option( 'jetpack_site_icon_url', 'http://website.com/wp-content/uploads/2016/09/jetpack_site_icon.png' );
$this->sender->do_sync();
$this->assertContains( 'jetpack_site_icon', $this->server_replica_storage->get_callable( 'site_icon_url' ) );
}
function test_calling_taxonomies_do_not_modify_global() {
global $wp_taxonomies;
// adds taxonomies.
$test = new ABC_FOO_TEST_Taxonomy_Example();
$this->setSyncClientDefaults();
$sync_callable_taxonomies = Jetpack_Sync_Functions::get_taxonomies();
$this->assertNull( $sync_callable_taxonomies['example']->update_count_callback );
$this->assertNull( $sync_callable_taxonomies['example']->meta_box_cb );
$this->assertNotNull( $wp_taxonomies['example']->update_count_callback );
$this->assertNotNull( $wp_taxonomies['example']->meta_box_cb );
}
function test_sanitize_sync_taxonomies_method() {
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'meta_box_cb' => 'post_tags_meta_box' ) );
$this->assertEquals( $sanitized->meta_box_cb, 'post_tags_meta_box' );
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'meta_box_cb' => 'post_categories_meta_box' ) );
$this->assertEquals( $sanitized->meta_box_cb, 'post_categories_meta_box' );
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'meta_box_cb' => 'banana' ) );
$this->assertEquals( $sanitized->meta_box_cb, null );
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'update_count_callback' => 'banana' ) );
$this->assertFalse( isset( $sanitized->update_count_callback ) );
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'rest_controller_class' => 'banana' ) );
$this->assertEquals( $sanitized->rest_controller_class, null );
$sanitized = Jetpack_Sync_Functions::sanitize_taxonomy( (object) array( 'rest_controller_class' => 'WP_REST_Terms_Controller' ) );
$this->assertEquals( $sanitized->rest_controller_class, 'WP_REST_Terms_Controller' );
}
function test_get_raw_url_by_option_bypasses_filters() {
add_filter( 'option_home', array( $this, '__return_filtered_url' ) );
$this->assertTrue( 'http://filteredurl.com' !== Jetpack_Sync_Functions::get_raw_url( 'home' ) );
remove_filter( 'option_home', array( $this, '__return_filtered_url' ) );
}
function test_get_raw_url_by_constant_bypasses_filters() {
Jetpack_Constants::set_constant( 'WP_HOME', 'http://constanturl.com' );
Jetpack_Constants::set_constant( 'WP_SITEURL', 'http://constanturl.com' );
add_filter( 'option_home', array( $this, '__return_filtered_url' ) );
add_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) );
$this->assertEquals( 'http://constanturl.com', Jetpack_Sync_Functions::get_raw_url( 'home' ) );
$this->assertEquals( 'http://constanturl.com', Jetpack_Sync_Functions::get_raw_url( 'siteurl' ) );
remove_filter( 'option_home', array( $this, '__return_filtered_url' ) );
remove_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) );
Jetpack_Constants::clear_constants();
}
function test_get_raw_url_returns_with_http_if_is_ssl() {
$home_option = get_option( 'home' );
// Test without https first
$this->assertEquals( $home_option, Jetpack_Sync_Functions::get_raw_url( 'home' ) );
// Now, with https
$_SERVER['HTTPS'] = 'on';
$this->assertEquals(
set_url_scheme( $home_option, 'http' ),
Jetpack_Sync_Functions::get_raw_url( 'home' )
);
unset( $_SERVER['HTTPS'] );
}
function test_raw_home_url_is_https_when_is_ssl() {
Jetpack_Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', true );
$home_option = get_option( 'home' );
// Test without https first
$this->assertEquals(
$home_option,
Jetpack_Sync_Functions::home_url()
);
// Now, with https
$_SERVER['HTTPS'] = 'on';
$this->assertEquals(
set_url_scheme( $home_option, 'https' ),
Jetpack_Sync_Functions::home_url()
);
unset( $_SERVER['HTTPS'] );
}
function test_user_can_stop_raw_urls() {
add_filter( 'option_home', array( $this, '__return_filtered_url' ) );
add_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) );
// Test with constant first
$this->assertTrue( 'http://filteredurl.com' !== Jetpack_Sync_Functions::home_url() );
// Now, without, which should return the filtered URL
Jetpack_Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', false );
$this->assertEquals( $this->__return_filtered_url(), Jetpack_Sync_Functions::home_url() );
Jetpack_Constants::clear_constants();
remove_filter( 'option_home', array( $this, '__return_filtered_url' ) );
remove_filter( 'option_siteurl', array( $this, '__return_filtered_url' ) );
}
function test_plugin_action_links_get_synced() {
$helper_all = new Jetpack_Sync_Test_Helper();
$helper_all->array_override = array( '<a href="fun.php">fun</a>' );
add_filter( 'plugin_action_links', array( $helper_all, 'filter_override_array' ), 10 );
$helper_jetpack = new Jetpack_Sync_Test_Helper();
$helper_jetpack->array_override = array( '<a href="settings.php">settings</a>', '<a href="https://jetpack.com/support">support</a>' );
add_filter( 'plugin_action_links_jetpack/jetpack.php', array( $helper_jetpack, 'filter_override_array' ), 10 );
$callables_module = new Jetpack_Sync_Module_Callables(); // Do the admin init here so that we calculate the plugin links
$callables_module->set_plugin_action_links();
// Let's see if the original values get synced
$this->sender->do_sync();
$plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' );
$expected_array = array(
'hello.php' => array(
'fun' => admin_url( 'fun.php' )
),
'jetpack/jetpack.php' => array(
'settings' => admin_url( 'settings.php' ),
'support' => 'https://jetpack.com/support'
)
);
$this->assertEquals( $expected_array, $plugins_action_links );
$helper_all->array_override = array( '<a href="not-fun.php">not fun</a>' );
$this->resetCallableAndConstantTimeouts();
$callables_module->set_plugin_action_links();
$this->sender->do_sync();
$plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' );
// Nothing should have changed since we cache the results.
$this->assertEquals( $plugins_action_links, $expected_array );
activate_plugin('hello.php', '', false, true );
$this->resetCallableAndConstantTimeouts();
$callables_module->set_plugin_action_links();
$this->sender->do_sync();
$plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' );
// Links should have changes now since we activated the plugin.
$expected_array['hello.php'] = array( 'not fun' => admin_url( 'not-fun.php' ) );
$this->assertEquals( $plugins_action_links, $expected_array );
}
function __return_filtered_url() {
return 'http://filteredurl.com';
}
function add_www_subdomain_to_siteurl( $url ) {
$parsed_url = parse_url( $url );
return "{$parsed_url['scheme']}://www.{$parsed_url['host']}";
}
function test_taxonomies_objects_do_not_have_meta_box_callback() {
new ABC_FOO_TEST_Taxonomy_Example();
$taxonomies = Jetpack_Sync_Functions::get_taxonomies();
$taxonomy = $taxonomies['example'];
$this->assertInternalType( 'object', $taxonomy );
// Did we get rid of the expected attributes?
$this->assertNull( $taxonomy->update_count_callback, "example has the update_count_callback attribute, which should be removed since it is a callback" );
$this->assertNull( $taxonomy->meta_box_cb, "example has the meta_box_cb attribute, which should be removed since it is a callback" );
$this->assertNull( $taxonomy->rest_controller_class );
// Did we preserve the expected attributes?
$check_object_vars = array(
'labels',
'description',
'public',
'publicly_queryable',
'hierarchical',
'show_ui',
'show_in_menu',
'show_in_nav_menus',
'show_tagcloud',
'show_in_quick_edit',
'show_admin_column',
'rewrite',
);
foreach ( $check_object_vars as $test ) {
$this->assertObjectHasAttribute( $test, $taxonomy, "Taxonomy does not have expected {$test} attribute." );
}
}
}
/* Example Test Taxonomy */
class ABC_FOO_TEST_Taxonomy_Example {
function __construct() {
register_taxonomy(
'example',
'posts',
array(
'meta_box_cb' => 'bob',
'update_count_callback' => array( $this, 'callback_update_count_callback_tags' ),
'rest_controller_class' => 'tom'
)
);
}
function callback_update_count_callback_tags() {
return 123;
}
// Prevent this class being used as part of a Serialization injection attack
public function __clone() {
wp_die( __( 'Cheatin’ uh?' ) );
}
public function __wakeup() {
wp_die( __( 'Cheatin’ uh?' ) );
}
}