Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Test_Jetpack_Sync_Functions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_Test_Jetpack_Sync_Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class WP_Test_Jetpack_Sync_Functions extends WP_Test_Jetpack_Sync_Base { |
||
36 | protected $post; |
||
37 | protected $callable_module; |
||
38 | |||
39 | protected static $admin_id; // used in mock_xml_rpc_request |
||
40 | |||
41 | public function setUp() { |
||
42 | parent::setUp(); |
||
43 | |||
44 | $this->resetCallableAndConstantTimeouts(); |
||
45 | |||
46 | $this->callable_module = Modules::get_module( "functions" ); |
||
47 | set_current_screen( 'post-user' ); // this only works in is_admin() |
||
48 | } |
||
49 | |||
50 | View Code Duplication | function test_white_listed_function_is_synced() { |
|
51 | $this->callable_module->set_callable_whitelist( array( 'jetpack_foo' => 'jetpack_foo_is_callable' ) ); |
||
|
|||
52 | |||
53 | $this->sender->do_sync(); |
||
54 | |||
55 | $synced_value = $this->server_replica_storage->get_callable( 'jetpack_foo' ); |
||
56 | $this->assertEquals( jetpack_foo_is_callable(), $synced_value ); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Verify that when a callable returns an anonymous function we don't fatal. |
||
61 | */ |
||
62 | View Code Duplication | public function test_anonymous_function_callable() { |
|
63 | $this->callable_module->set_callable_whitelist( array( 'jetpack_foo_anon' => 'jetpack_foo_is_anon_callable' ) ); |
||
64 | |||
65 | $this->sender->do_sync(); |
||
66 | |||
67 | $synced_value = $this->server_replica_storage->get_callable( 'jetpack_foo_anon' ); |
||
68 | $this->assertEquals( null, $synced_value ); |
||
69 | } |
||
70 | |||
71 | public function test_sync_jetpack_updates() { |
||
72 | $this->sender->do_sync(); |
||
73 | $updates = $this->server_replica_storage->get_callable( 'updates' ); |
||
74 | $this->assertEqualsObject( Jetpack::get_updates(), $updates, 'The updates object should match' ); |
||
75 | } |
||
76 | |||
77 | |||
78 | function test_wp_version_is_synced() { |
||
79 | global $wp_version; |
||
80 | $this->sender->do_sync(); |
||
81 | $synced_value = $this->server_replica_storage->get_callable( 'wp_version' ); |
||
82 | $this->assertEquals( $synced_value, $wp_version ); |
||
83 | } |
||
84 | |||
85 | public function test_sync_callable_whitelist() { |
||
86 | // $this->setSyncClientDefaults(); |
||
87 | |||
88 | add_filter( 'jetpack_set_available_extensions', array( $this, 'add_test_block' ) ); |
||
89 | Jetpack_Gutenberg::init(); |
||
90 | Blocks::jetpack_register_block( 'jetpack/test' ); |
||
91 | |||
92 | $callables = array( |
||
93 | 'wp_max_upload_size' => wp_max_upload_size(), |
||
94 | 'is_main_network' => Jetpack::is_multi_network(), |
||
95 | 'is_multi_site' => is_multisite(), |
||
96 | 'main_network_site' => Urls::main_network_site_url(), |
||
97 | 'single_user_site' => Jetpack::is_single_user_site(), |
||
98 | 'updates' => Jetpack::get_updates(), |
||
99 | 'home_url' => Urls::home_url(), |
||
100 | 'site_url' => Urls::site_url(), |
||
101 | 'has_file_system_write_access' => Functions::file_system_write_access(), |
||
102 | 'is_version_controlled' => Functions::is_version_controlled(), |
||
103 | 'taxonomies' => Functions::get_taxonomies(), |
||
104 | 'post_types' => Functions::get_post_types(), |
||
105 | 'post_type_features' => Functions::get_post_type_features(), |
||
106 | 'rest_api_allowed_post_types' => Functions::rest_api_allowed_post_types(), |
||
107 | 'rest_api_allowed_public_metadata' => Functions::rest_api_allowed_public_metadata(), |
||
108 | 'sso_is_two_step_required' => Jetpack_SSO_Helpers::is_two_step_required(), |
||
109 | 'sso_should_hide_login_form' => Jetpack_SSO_Helpers::should_hide_login_form(), |
||
110 | 'sso_match_by_email' => Jetpack_SSO_Helpers::match_by_email(), |
||
111 | 'sso_new_user_override' => Jetpack_SSO_Helpers::new_user_override(), |
||
112 | 'sso_bypass_default_login_form' => Jetpack_SSO_Helpers::bypass_login_forward_wpcom(), |
||
113 | 'wp_version' => Functions::wp_version(), |
||
114 | 'get_plugins' => Functions::get_plugins(), |
||
115 | 'get_plugins_action_links' => Functions::get_plugins_action_links(), |
||
116 | 'active_modules' => Jetpack::get_active_modules(), |
||
117 | 'hosting_provider' => Functions::get_hosting_provider(), |
||
118 | 'locale' => get_locale(), |
||
119 | 'site_icon_url' => Functions::site_icon_url(), |
||
120 | 'shortcodes' => Functions::get_shortcodes(), |
||
121 | 'roles' => Functions::roles(), |
||
122 | 'timezone' => Functions::get_timezone(), |
||
123 | 'available_jetpack_blocks' => Jetpack_Gutenberg::get_availability(), |
||
124 | 'paused_themes' => Functions::get_paused_themes(), |
||
125 | 'paused_plugins' => Functions::get_paused_plugins(), |
||
126 | 'main_network_site_wpcom_id' => Functions::main_network_site_wpcom_id(), |
||
127 | 'theme_support' => Functions::get_theme_support(), |
||
128 | 'wp_get_environment_type' => wp_get_environment_type(), |
||
129 | ); |
||
130 | |||
131 | if ( function_exists( 'wp_cache_is_enabled' ) ) { |
||
132 | $callables['wp_super_cache_globals'] = WP_Super_Cache::get_wp_super_cache_globals(); |
||
133 | } |
||
134 | |||
135 | if ( is_multisite() ) { |
||
136 | $callables['network_name'] = Jetpack::network_name(); |
||
137 | $callables['network_allow_new_registrations'] = Jetpack::network_allow_new_registrations(); |
||
138 | $callables['network_add_new_users'] = Jetpack::network_add_new_users(); |
||
139 | $callables['network_site_upload_space'] = Jetpack::network_site_upload_space(); |
||
140 | $callables['network_upload_file_types'] = Jetpack::network_upload_file_types(); |
||
141 | $callables['network_enable_administration_menus'] = Jetpack::network_enable_administration_menus(); |
||
142 | } |
||
143 | |||
144 | $this->sender->do_sync(); |
||
145 | |||
146 | foreach ( $callables as $name => $value ) { |
||
147 | // TODO: figure out why _sometimes_ the 'support' value of |
||
148 | // the post_types value is being removed from the output |
||
149 | if ( $name === 'post_types' ) { |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | $this->assertCallableIsSynced( $name, $value ); |
||
154 | } |
||
155 | |||
156 | $whitelist_keys = array_keys( $this->callable_module->get_callable_whitelist() ); |
||
157 | $callables_keys = array_keys( $callables ); |
||
158 | |||
159 | // Are we testing all the callables in the defaults? |
||
160 | $whitelist_and_callable_keys_difference = array_diff( $whitelist_keys, $callables_keys ); |
||
161 | $this->assertTrue( empty( $whitelist_and_callable_keys_difference ), 'Some whitelisted options don\'t have a test: ' . print_r( $whitelist_and_callable_keys_difference, 1 ) ); |
||
162 | |||
163 | // Are there any duplicate keys? |
||
164 | $unique_whitelist = array_unique( $whitelist_keys ); |
||
165 | $this->assertEquals( count( $unique_whitelist ), count( $whitelist_keys ), 'The duplicate keys are: ' . print_r( array_diff_key( $whitelist_keys, array_unique( $whitelist_keys ) ), 1 ) ); |
||
166 | |||
167 | remove_filter( 'jetpack_set_available_extensions', array( $this, 'add_test_block' ) ); |
||
168 | Jetpack_Gutenberg::reset(); |
||
169 | } |
||
170 | |||
171 | public function add_test_block() { |
||
174 | |||
175 | function assertCallableIsSynced( $name, $value ) { |
||
178 | |||
179 | function test_white_listed_callables_doesnt_get_synced_twice() { |
||
195 | |||
196 | /** |
||
197 | * Tests that calling unlock_sync_callable_next_tick works as expected. |
||
198 | * |
||
199 | * Return null |
||
200 | */ |
||
201 | public function test_white_listed_callable_sync_on_next_tick() { |
||
218 | |||
219 | /** |
||
220 | * Tests that updating the theme should result in the no callabled transient being set. |
||
221 | * |
||
222 | * Return null |
||
223 | */ |
||
224 | public function test_updating_stylesheet_sends_the_theme_data() { |
||
236 | |||
237 | function test_sync_always_sync_changes_to_modules_right_away() { |
||
253 | |||
254 | function test_sync_always_sync_changes_to_home_siteurl_right_away() { |
||
286 | |||
287 | function test_sync_jetpack_sync_unlock_sync_callable_action_allows_syncing_siteurl_changes() { |
||
328 | |||
329 | function test_home_site_urls_synced_while_migrate_for_idc_set() { |
||
366 | |||
367 | function return_example_com() { |
||
370 | |||
371 | function return_example_com_blog() { |
||
374 | |||
375 | function return_https_example_com() { |
||
378 | |||
379 | function return_https_example_org() { |
||
382 | |||
383 | function return_site_com() { |
||
386 | |||
387 | function return_https_site_com() { |
||
390 | |||
391 | function return_https_site_com_blog() { |
||
394 | |||
395 | function return_https_www_example_com() { |
||
398 | |||
399 | function return_https_foo_example_com() { |
||
402 | |||
403 | function test_get_protocol_normalized_url_works_with_no_history() { |
||
424 | |||
425 | function test_get_protocol_normalized_url_stores_max_history() { |
||
436 | |||
437 | function test_get_protocol_normalized_url_returns_http_when_https_falls_off() { |
||
462 | |||
463 | function test_get_protocol_normalized_url_returns_new_value_cannot_parse() { |
||
470 | |||
471 | function test_get_protocol_normalized_url_cleared_on_reset_data() { |
||
487 | |||
488 | function test_subdomain_switching_to_www_does_not_cause_sync() { |
||
510 | |||
511 | View Code Duplication | function test_sync_limited_set_of_callables_if_cron() { |
|
538 | |||
539 | View Code Duplication | function test_sync_limited_set_of_callables_if_wp_cli() { |
|
566 | |||
567 | function test_site_icon_url_returns_false_when_no_site_icon() { |
||
572 | |||
573 | function test_site_icon_url_returns_core_site_icon_url_when_set() { |
||
588 | |||
589 | function test_site_icon_url_fallback_to_jetpack_site_icon_url() { |
||
596 | |||
597 | function test_calling_taxonomies_do_not_modify_global() { |
||
611 | |||
612 | function test_sanitize_sync_taxonomies_method() { |
||
633 | |||
634 | View Code Duplication | function test_sanitize_sync_post_type_method_default() { |
|
647 | |||
648 | View Code Duplication | function test_sanitize_sync_post_type_method_remove_unknown_values_set() { |
|
660 | |||
661 | function assert_sanitized_post_type_default( $sanitized, $label ) { |
||
691 | |||
692 | function test_sanitize_sync_post_type_method_all_values_set() { |
||
737 | |||
738 | function test_get_post_types_method() { |
||
739 | global $wp_post_types; |
||
740 | $synced = Functions::get_post_types(); |
||
741 | foreach( $wp_post_types as $post_type => $post_type_object ) { |
||
742 | $post_type_object->rest_controller_class = false; |
||
743 | $post_type_object->rest_controller = null; |
||
744 | if ( ! isset( $post_type_object->supports ) ) { |
||
745 | $post_type_object->supports = array(); |
||
746 | } |
||
747 | $synced_post_type = Functions::expand_synced_post_type( $synced[ $post_type ], $post_type ); |
||
748 | $this->assertEqualsObject( $post_type_object, $synced_post_type, 'POST TYPE :'. $post_type . ' not equal' ); |
||
749 | } |
||
750 | } |
||
751 | |||
752 | function test_register_post_types_callback_error() { |
||
753 | register_post_type( 'testing', array( 'register_meta_box_cb' => function() {} ) ); |
||
754 | $this->sender->do_sync(); |
||
755 | |||
756 | $post_types = $this->server_replica_storage->get_callable( 'post_types' ); |
||
757 | $this->assertTrue( isset( $post_types['testing'] ) ); |
||
758 | } |
||
759 | |||
760 | function test_get_raw_url_by_option_bypasses_filters() { |
||
761 | add_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
762 | $this->assertTrue( 'http://filteredurl.com' !== Urls::get_raw_url( 'home' ) ); |
||
763 | remove_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
764 | } |
||
765 | |||
766 | function test_get_raw_url_by_constant_bypasses_filters() { |
||
767 | Constants::set_constant( 'WP_HOME', 'http://constanturl.com' ); |
||
768 | Constants::set_constant( 'WP_SITEURL', 'http://constanturl.com' ); |
||
769 | add_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
770 | add_filter( 'option_siteurl', array( $this, 'return_filtered_url' ) ); |
||
771 | |||
772 | if ( is_multisite() ) { |
||
773 | $this->assertTrue( $this->return_filtered_url() !== Urls::get_raw_url( 'home' ) ); |
||
774 | $this->assertTrue( $this->return_filtered_url() !== Urls::get_raw_url( 'siteurl' ) ); |
||
775 | } else { |
||
776 | $this->assertEquals( 'http://constanturl.com', Urls::get_raw_url( 'home' ) ); |
||
777 | $this->assertEquals( 'http://constanturl.com', Urls::get_raw_url( 'siteurl' ) ); |
||
778 | } |
||
779 | |||
780 | remove_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
781 | remove_filter( 'option_siteurl', array( $this, 'return_filtered_url' ) ); |
||
782 | Constants::clear_constants(); |
||
783 | } |
||
784 | |||
785 | View Code Duplication | function test_get_raw_url_returns_with_http_if_is_ssl() { |
|
786 | $home_option = get_option( 'home' ); |
||
787 | |||
788 | // Test without https first |
||
789 | $this->assertEquals( $home_option, Urls::get_raw_url( 'home' ) ); |
||
790 | |||
791 | // Now, with https |
||
792 | $_SERVER['HTTPS'] = 'on'; |
||
793 | $this->assertEquals( |
||
794 | set_url_scheme( $home_option, 'http' ), |
||
795 | Urls::get_raw_url( 'home' ) |
||
796 | ); |
||
797 | unset( $_SERVER['HTTPS'] ); |
||
798 | } |
||
799 | |||
800 | View Code Duplication | function test_raw_home_url_is_https_when_is_ssl() { |
|
801 | Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', true ); |
||
802 | |||
803 | $home_option = get_option( 'home' ); |
||
804 | |||
805 | // Test without https first |
||
806 | $this->assertEquals( |
||
807 | $home_option, |
||
808 | Urls::home_url() |
||
809 | ); |
||
810 | |||
811 | // Now, with https |
||
812 | $_SERVER['HTTPS'] = 'on'; |
||
813 | $this->assertEquals( |
||
814 | set_url_scheme( $home_option, 'https' ), |
||
815 | Urls::home_url() |
||
816 | ); |
||
817 | unset( $_SERVER['HTTPS'] ); |
||
818 | } |
||
819 | |||
820 | function test_user_can_stop_raw_urls() { |
||
821 | add_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
822 | add_filter( 'option_siteurl', array( $this, 'return_filtered_url' ) ); |
||
823 | |||
824 | // Test with constant first |
||
825 | $this->assertTrue( 'http://filteredurl.com' !== Urls::home_url() ); |
||
826 | |||
827 | // Now, without, which should return the filtered URL |
||
828 | Constants::set_constant( 'JETPACK_SYNC_USE_RAW_URL', false ); |
||
829 | $this->assertEquals( $this->return_filtered_url(), Urls::home_url() ); |
||
830 | Constants::clear_constants(); |
||
831 | |||
832 | remove_filter( 'option_home', array( $this, 'return_filtered_url' ) ); |
||
833 | remove_filter( 'option_siteurl', array( $this, 'return_filtered_url' ) ); |
||
834 | } |
||
835 | |||
836 | function test_plugin_action_links_get_synced() { |
||
837 | // Makes sure that we start fresh |
||
838 | delete_transient( 'jetpack_plugin_api_action_links_refresh' ); |
||
839 | $helper_all = new Jetpack_Sync_Test_Helper(); |
||
840 | |||
841 | $helper_all->array_override = array( '<a href="fun.php">fun</a>' ); |
||
842 | add_filter( 'plugin_action_links', array( $helper_all, 'filter_override_array' ), 10 ); |
||
843 | |||
844 | $helper_jetpack = new Jetpack_Sync_Test_Helper(); |
||
845 | $helper_jetpack->array_override = array( '<a href="settings.php">settings</a>', '<a href="https://jetpack.com/support">support</a>' ); |
||
846 | add_filter( 'plugin_action_links_jetpack/jetpack.php', array( $helper_jetpack, 'filter_override_array' ), 10 ); |
||
847 | |||
848 | set_current_screen( 'banana' ); |
||
849 | // Let's see if the original values get synced |
||
850 | $this->sender->do_sync(); |
||
851 | |||
852 | $plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' ); |
||
853 | |||
854 | $expected_array = array( |
||
855 | 'hello.php' => array( |
||
856 | 'fun' => admin_url( 'fun.php' ) |
||
857 | ), |
||
858 | 'jetpack/jetpack.php' => array( |
||
859 | 'settings' => admin_url( 'settings.php' ), |
||
860 | 'support' => 'https://jetpack.com/support' |
||
861 | ) |
||
862 | ); |
||
863 | |||
864 | $this->assertEquals( $expected_array, $this->extract_plugins_we_are_testing( $plugins_action_links ) ); |
||
865 | |||
866 | $helper_all->array_override = array( '<a href="not-fun.php">not fun</a>' ); |
||
867 | |||
868 | $this->resetCallableAndConstantTimeouts(); |
||
869 | |||
870 | set_current_screen( 'banana' ); |
||
871 | $this->sender->do_sync(); |
||
872 | |||
873 | $plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' ); |
||
874 | |||
875 | // Nothing should have changed since we cache the results. |
||
876 | $this->assertEquals( $this->extract_plugins_we_are_testing( $plugins_action_links ), $expected_array ); |
||
877 | |||
878 | if ( file_exists( WP_PLUGIN_DIR . '/hello.php' ) ) { |
||
879 | activate_plugin('hello.php', '', false, true ); |
||
880 | } |
||
881 | if ( file_exists( WP_PLUGIN_DIR . '/hello-dolly/hello.php' ) ) { |
||
882 | activate_plugin('hello-dolly/hello.php', '', false, true ); |
||
883 | } |
||
884 | |||
885 | $this->resetCallableAndConstantTimeouts(); |
||
886 | set_current_screen( 'banana' ); |
||
887 | $this->sender->do_sync(); |
||
888 | |||
889 | $plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' ); |
||
890 | |||
891 | // Links should have changes now since we activated the plugin. |
||
892 | $expected_array['hello.php'] = array( 'not fun' => admin_url( 'not-fun.php' ) ); |
||
893 | $this->assertEquals( $this->extract_plugins_we_are_testing( $plugins_action_links ), $expected_array, 'Array was not updated to the new value as expected' ); |
||
894 | } |
||
895 | |||
896 | function extract_plugins_we_are_testing( $plugins_action_links ) { |
||
897 | $only_plugins_we_care_about = array(); |
||
898 | if ( isset( $plugins_action_links['hello.php'] ) ) { |
||
899 | $only_plugins_we_care_about['hello.php'] = isset( $plugins_action_links['hello.php'] ) ? $plugins_action_links['hello.php'] : ''; |
||
900 | } else { |
||
901 | $only_plugins_we_care_about['hello.php'] = isset( $plugins_action_links['hello-dolly/hello.php'] ) ? $plugins_action_links['hello-dolly/hello.php'] : ''; |
||
902 | } |
||
903 | |||
904 | $only_plugins_we_care_about['jetpack/jetpack.php'] = isset( $plugins_action_links['jetpack/jetpack.php'] ) ? $plugins_action_links['jetpack/jetpack.php'] : ''; |
||
905 | return $only_plugins_we_care_about; |
||
906 | } |
||
907 | |||
908 | function cause_fatal_error( $actions ) { |
||
909 | unset( $actions['activate'] ); |
||
910 | $actions[] = '<a href="/hello">world</a>'; |
||
911 | return $actions; |
||
912 | } |
||
913 | |||
914 | function test_fixes_fatal_error( ) { |
||
915 | |||
916 | delete_transient( 'jetpack_plugin_api_action_links_refresh' ); |
||
917 | add_filter( 'plugin_action_links', array( $this, 'cause_fatal_error' ) ); |
||
918 | |||
919 | set_current_screen( 'plugins' ); |
||
920 | |||
921 | $this->resetCallableAndConstantTimeouts(); |
||
922 | set_current_screen( 'plugins' ); |
||
923 | $this->sender->do_sync(); |
||
924 | $plugins_action_links = $this->server_replica_storage->get_callable( 'get_plugins_action_links' ); |
||
925 | $plugins_action_links = $this->extract_plugins_we_are_testing( $plugins_action_links ); |
||
926 | $this->assertTrue( isset( $plugins_action_links['hello.php']['world'] ), 'World is not set' ); |
||
927 | } |
||
928 | |||
929 | /** |
||
930 | * Return "http://filteredurl.com". |
||
931 | * |
||
932 | * @return string |
||
933 | */ |
||
934 | public function return_filtered_url() { |
||
935 | return 'http://filteredurl.com'; |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Add a "www" subdomain to a URL. |
||
940 | * |
||
941 | * @param string $url URL. |
||
942 | * @return string |
||
943 | */ |
||
944 | public function add_www_subdomain_to_siteurl( $url ) { |
||
945 | $parsed_url = wp_parse_url( $url ); |
||
946 | |||
947 | return "{$parsed_url['scheme']}://www.{$parsed_url['host']}"; |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * Test "taxonomies_objects_do_not_have_meta_box_callback". |
||
952 | */ |
||
953 | public function test_taxonomies_objects_do_not_have_meta_box_callback() { |
||
954 | |||
955 | new ABC_FOO_TEST_Taxonomy_Example(); |
||
956 | $taxonomies = Functions::get_taxonomies(); |
||
957 | $taxonomy = $taxonomies['example']; |
||
958 | |||
959 | $this->assertInternalType( 'object', $taxonomy ); |
||
960 | // Did we get rid of the expected attributes? |
||
961 | $this->assertNull( $taxonomy->update_count_callback, 'example has the update_count_callback attribute, which should be removed since it is a callback' ); |
||
962 | $this->assertNull( $taxonomy->meta_box_cb, 'example has the meta_box_cb attribute, which should be removed since it is a callback' ); |
||
963 | $this->assertNull( $taxonomy->rest_controller_class ); |
||
964 | // Did we preserve the expected attributes? |
||
965 | $check_object_vars = array( |
||
966 | 'labels', |
||
967 | 'description', |
||
968 | 'public', |
||
969 | 'publicly_queryable', |
||
970 | 'hierarchical', |
||
971 | 'show_ui', |
||
972 | 'show_in_menu', |
||
973 | 'show_in_nav_menus', |
||
974 | 'show_tagcloud', |
||
975 | 'show_in_quick_edit', |
||
976 | 'show_admin_column', |
||
977 | 'rewrite', |
||
978 | ); |
||
979 | foreach ( $check_object_vars as $test ) { |
||
980 | $this->assertObjectHasAttribute( $test, $taxonomy, "Taxonomy does not have expected {$test} attribute." ); |
||
981 | } |
||
982 | } |
||
983 | |||
984 | /** |
||
985 | * Test "force_sync_callable_on_plugin_update". |
||
986 | */ |
||
987 | public function test_force_sync_callable_on_plugin_update() { |
||
988 | // fake the cron so that we really prevent the callables from being called. |
||
989 | Settings::$is_doing_cron = true; |
||
990 | |||
991 | $this->callable_module->set_callable_whitelist( array( 'jetpack_foo' => 'jetpack_foo_is_callable_random' ) ); |
||
992 | $this->sender->do_sync(); |
||
993 | $this->server_replica_storage->get_callable( 'jetpack_foo' ); |
||
994 | |||
995 | $this->server_replica_storage->reset(); |
||
996 | |||
997 | $synced_value2 = $this->server_replica_storage->get_callable( 'jetpack_foo' ); |
||
998 | $this->assertEmpty( $synced_value2 ); |
||
999 | |||
1000 | $upgrader = (object) array( |
||
1001 | 'skin' => (object) array( |
||
1002 | 'result' => new WP_Error( 'fail', 'Fail' ), |
||
1003 | ), |
||
1004 | ); |
||
1005 | |||
1006 | do_action( |
||
1007 | 'upgrader_process_complete', |
||
1008 | $upgrader, |
||
1009 | array( |
||
1010 | 'action' => 'update', |
||
1011 | 'type' => 'plugin', |
||
1012 | 'bulk' => true, |
||
1013 | 'plugins' => array( 'the/the.php' ), |
||
1014 | ) |
||
1015 | ); |
||
1016 | |||
1017 | $this->sender->do_sync(); |
||
1018 | $synced_value3 = $this->server_replica_storage->get_callable( 'jetpack_foo' ); |
||
1019 | Settings::$is_doing_cron = false; |
||
1020 | $this->assertNotEmpty( $synced_value3, 'value is empty!' ); |
||
1021 | |||
1022 | } |
||
1023 | |||
1024 | /** |
||
1025 | * Test "xml_rpc_request_callables_has_actor". |
||
1026 | */ |
||
1027 | public function test_xml_rpc_request_callables_has_actor() { |
||
1028 | $this->server_event_storage->reset(); |
||
1029 | $user = wp_get_current_user(); |
||
1030 | wp_set_current_user( 0 ); // |
||
1031 | $this->sender->do_sync(); |
||
1032 | $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_callable' ); |
||
1033 | $this->assertEquals( $event->user_id, 0, ' Callables user_id is null' ); |
||
1034 | |||
1035 | $this->resetCallableAndConstantTimeouts(); |
||
1036 | $this->mock_authenticated_xml_rpc(); // mock requet |
||
1037 | $this->sender->do_sync(); |
||
1038 | |||
1039 | $event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_callable' ); |
||
1040 | // clean up by unsetting globals, etc. set previously by $this->mock_authenticated_xml_rpc() |
||
1041 | $this->mock_authenticated_xml_rpc_cleanup( $user->ID ); |
||
1042 | |||
1043 | $this->assertEquals( $event->user_id, self::$admin_id, ' Callables XMLRPC_Reqeust not equal to event user_id' ); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * Mock authenticated XML RPC. |
||
1048 | */ |
||
1049 | public function mock_authenticated_xml_rpc() { |
||
1050 | self::$admin_id = $this->factory->user->create( array( |
||
1051 | 'role' => 'administrator', |
||
1052 | ) ); |
||
1053 | |||
1054 | add_filter( 'pre_option_jetpack_private_options', array( $this, 'mock_jetpack_private_options' ), 10, 2 ); |
||
1055 | $_GET['token'] = 'pretend_this_is_valid:1:' . self::$admin_id; |
||
1056 | $_GET['timestamp'] = (string) time(); |
||
1057 | $_GET['nonce'] = 'testing123'; |
||
1058 | |||
1059 | $_SERVER['REQUEST_URI'] = '/xmlrpc.php'; |
||
1060 | $_GET['body'] = 'abc'; |
||
1061 | $_GET['body-hash'] = base64_encode( sha1( 'abc', true ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
1062 | $GLOBALS['HTTP_RAW_POST_DATA'] = 'abc'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
1063 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
1064 | |||
1065 | $normalized_request_pieces = array( |
||
1066 | $_GET['token'], |
||
1067 | $_GET['timestamp'], |
||
1068 | $_GET['nonce'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
1069 | $_GET['body-hash'], |
||
1070 | 'POST', |
||
1071 | 'example.org', |
||
1072 | '80', |
||
1073 | '/xmlrpc.php', |
||
1074 | ); |
||
1075 | $normalize = join( "\n", $normalized_request_pieces ) . "\n"; |
||
1076 | |||
1077 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
1078 | $_GET['signature'] = base64_encode( hash_hmac( 'sha1', $normalize, 'secret', true ) ); |
||
1079 | |||
1080 | // call one of the authenticated endpoints |
||
1081 | Constants::set_constant( 'XMLRPC_REQUEST', true ); |
||
1082 | Jetpack::init(); |
||
1083 | $connection = Jetpack::connection(); |
||
1084 | $connection->xmlrpc_methods( array() ); |
||
1085 | $connection->require_jetpack_authentication(); |
||
1086 | $connection->verify_xml_rpc_signature(); |
||
1087 | } |
||
1088 | |||
1089 | /** |
||
1090 | * Mock authenticated XML RPC cleanup. |
||
1091 | * |
||
1092 | * @param int $user_id User ID. |
||
1093 | */ |
||
1094 | public function mock_authenticated_xml_rpc_cleanup( $user_id ) { |
||
1095 | Constants::clear_constants(); |
||
1096 | remove_filter( 'pre_option_jetpack_private_options', array( $this, 'mock_jetpack_private_options' ), 10 ); |
||
1097 | |||
1098 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
1099 | unset( $_GET['token'] ); |
||
1100 | unset( $_GET['timestamp'] ); |
||
1101 | unset( $_GET['nonce'] ); |
||
1102 | $_SERVER['REQUEST_URI'] = ''; |
||
1103 | unset( $_GET['body'] ); |
||
1104 | unset( $_GET['body-hash'] ); |
||
1105 | unset( $GLOBALS['HTTP_RAW_POST_DATA'] ); |
||
1106 | unset( $_SERVER['REQUEST_METHOD'] ); |
||
1107 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
||
1108 | |||
1109 | Connection_Rest_Authentication::init()->reset_saved_auth_state(); |
||
1110 | Jetpack::connection()->reset_raw_post_data(); |
||
1111 | wp_set_current_user( $user_id ); |
||
1112 | self::$admin_id = null; |
||
1113 | } |
||
1114 | |||
1115 | /** |
||
1116 | * Mock Jetpack private options. |
||
1117 | */ |
||
1118 | View Code Duplication | public function mock_jetpack_private_options() { |
|
1119 | $user_tokens = array(); |
||
1120 | $user_tokens[ self::$admin_id ] = 'pretend_this_is_valid.secret.' . self::$admin_id; |
||
1121 | return array( |
||
1122 | 'user_tokens' => $user_tokens, |
||
1123 | ); |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * Test "get_timezone_from_timezone_string". |
||
1128 | */ |
||
1129 | public function test_get_timezone_from_timezone_string() { |
||
1130 | update_option( 'timezone_string', 'America/Rankin_Inlet' ); |
||
1131 | update_option( 'gmt_offset', '' ); |
||
1132 | $this->assertEquals( 'America/Rankin Inlet', Functions::get_timezone() ); |
||
1133 | } |
||
1134 | |||
1135 | /** |
||
1136 | * Test "get_timezone_from_gmt_offset_zero". |
||
1137 | */ |
||
1138 | public function test_get_timezone_from_gmt_offset_zero() { |
||
1139 | update_option( 'timezone_string', '' ); |
||
1140 | update_option( 'gmt_offset', '0' ); |
||
1141 | $this->assertEquals( 'UTC+0', Functions::get_timezone() ); |
||
1142 | } |
||
1143 | |||
1144 | /** |
||
1145 | * Test "get_timezone_from_gmt_offset_plus". |
||
1146 | */ |
||
1147 | public function test_get_timezone_from_gmt_offset_plus() { |
||
1148 | update_option( 'timezone_string', '' ); |
||
1149 | update_option( 'gmt_offset', '1' ); |
||
1150 | $this->assertEquals( 'UTC+1', Functions::get_timezone() ); |
||
1151 | } |
||
1152 | |||
1153 | /** |
||
1154 | * Test "get_timezone_from_gmt_offset_fractions". |
||
1155 | */ |
||
1156 | public function test_get_timezone_from_gmt_offset_fractions() { |
||
1157 | update_option( 'timezone_string', '' ); |
||
1158 | update_option( 'gmt_offset', '5.5' ); |
||
1159 | $this->assertEquals( 'UTC+5:30', Functions::get_timezone() ); |
||
1160 | } |
||
1161 | |||
1162 | /** |
||
1163 | * Test "get_timezone_from_gmt_offset_minus". |
||
1164 | */ |
||
1165 | public function test_get_timezone_from_gmt_offset_minus() { |
||
1166 | update_option( 'timezone_string', '' ); |
||
1167 | update_option( 'gmt_offset', '-1' ); |
||
1168 | $this->assertEquals( 'UTC-1', Functions::get_timezone() ); |
||
1169 | } |
||
1170 | |||
1171 | /** |
||
1172 | * Test "sync_callable_recursive_gets_checksum". |
||
1173 | */ |
||
1174 | View Code Duplication | public function test_sync_callable_recursive_gets_checksum() { |
|
1175 | |||
1176 | $this->callable_module->set_callable_whitelist( array( 'jetpack_banana' => 'jetpack_recursive_banana' ) ); |
||
1177 | $this->sender->do_sync(); |
||
1178 | $synced_value = $this->server_replica_storage->get_callable( 'jetpack_banana' ); |
||
1179 | $this->assertTrue( ! empty( $synced_value ), 'We couldn\'t synced a value!' ); |
||
1180 | } |
||
1181 | |||
1182 | /** |
||
1183 | * Test get_hosting_provider() callable to ensure that known hosts have the |
||
1184 | * right hosting provider returned. |
||
1185 | * |
||
1186 | * @return void |
||
1187 | */ |
||
1188 | public function test_get_hosting_provider_callable_with_unknown_host() { |
||
1189 | $this->assertEquals( Functions::get_hosting_provider(), 'unknown' ); |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * Test getting a hosting provider by a known constant |
||
1194 | * |
||
1195 | * @return void |
||
1196 | */ |
||
1197 | public function test_get_hosting_provider_by_known_constant() { |
||
1198 | $functions = new Functions(); |
||
1199 | Constants::set_constant( 'GD_SYSTEM_PLUGIN_DIR', 'set' ); |
||
1200 | $this->assertEquals( $functions->get_hosting_provider_by_known_constant(), 'gd-managed-wp' ); |
||
1201 | Constants::clear_constants(); |
||
1202 | |||
1203 | Constants::set_constant( 'UNKNOWN', 'set' ); |
||
1204 | $this->assertFalse( $functions->get_hosting_provider_by_known_constant() ); |
||
1205 | Constants::clear_constants(); |
||
1206 | } |
||
1207 | |||
1208 | /** |
||
1209 | * Test getting a hosting provider by a known class |
||
1210 | * |
||
1211 | * @return void |
||
1212 | */ |
||
1213 | public function test_get_hosting_provider_by_known_class() { |
||
1214 | $functions = new Functions(); |
||
1215 | |||
1216 | $this->assertFalse( $functions->get_hosting_provider_by_known_class() ); |
||
1217 | |||
1218 | $class_mock = $this->getMockBuilder( '\\WPaaS\\Plugin' ) |
||
1219 | ->getMock(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
1220 | |||
1221 | $this->assertEquals( $functions->get_hosting_provider_by_known_class(), 'gd-managed-wp' ); |
||
1222 | |||
1223 | } |
||
1224 | |||
1225 | /** |
||
1226 | * Test getting a hosting provider by a known function |
||
1227 | * |
||
1228 | * @return bool |
||
1229 | */ |
||
1230 | public function test_get_hosting_provider_by_known_function() { |
||
1231 | |||
1232 | /** |
||
1233 | * Stub is_wpe for testing function exists |
||
1234 | * |
||
1235 | * @return boolean |
||
1236 | */ |
||
1237 | function is_wpe() { // phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction |
||
1240 | |||
1241 | $functions = new Functions(); |
||
1242 | |||
1243 | // Get hosting provider by known function. |
||
1244 | $this->assertEquals( $functions->get_hosting_provider_by_known_function(), 'wpe' ); |
||
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * Test getting the main network site wpcom ID in multisite installs |
||
1249 | * |
||
1250 | * @return void |
||
1251 | */ |
||
1252 | public function test_get_main_network_site_wpcom_id_multisite() { |
||
1253 | if ( ! is_multisite() ) { |
||
1254 | $this->markTestSkipped( 'Only used on multisite' ); |
||
1255 | } |
||
1256 | |||
1257 | // set the Jetpack ID for this site. |
||
1258 | $main_network_wpcom_id = 12345; |
||
1259 | \Jetpack_Options::update_option( 'id', $main_network_wpcom_id ); |
||
1260 | |||
1261 | $user_id = $this->factory->user->create(); |
||
1262 | |||
1263 | // NOTE this is necessary because WPMU causes certain assumptions about transients. |
||
1264 | // to be wrong, and tests to explode. @see: https://github.com/sheabunge/WordPress/commit/ff4f1bb17095c6af8a0f35ac304f79074f3c3ff6 . |
||
1265 | global $wpdb; |
||
1266 | |||
1267 | $suppress = $wpdb->suppress_errors(); |
||
1268 | $other_blog_id = wpmu_create_blog( 'foo.com', '', 'My Blog', $user_id ); |
||
1269 | $wpdb->suppress_errors( $suppress ); |
||
1278 | |||
1279 | /** |
||
1280 | * Verify get_check_sum is consistent for differently ordered arrays. |
||
1281 | */ |
||
1282 | View Code Duplication | public function test_sync_does_not_send_updates_if_array_order_is_only_change() { |
|
1299 | |||
1300 | /** |
||
1301 | * Verify that all options are returned by get_objects_by_id |
||
1302 | */ |
||
1303 | public function test_get_objects_by_id_all() { |
||
1308 | |||
1309 | /** |
||
1310 | * Verify that get_object_by_id returns a allowed option |
||
1311 | */ |
||
1312 | View Code Duplication | public function test_get_objects_by_id_singular() { |
|
1313 | $module = Modules::get_module( 'functions' ); |
||
1314 | $callables = $module->get_all_callables(); |
||
1315 | $get_callable = $module->get_objects_by_id( 'callable', array( 'has_file_system_write_access' ) ); |
||
1316 | $this->assertEquals( $callables['has_file_system_write_access'], $get_callable['has_file_system_write_access'] ); |
||
1317 | } |
||
1318 | |||
1319 | /** |
||
1320 | * Reorder the get_plugins array keys. |
||
1321 | * |
||
1322 | * @param array $plugins array of plugins. |
||
1323 | * |
||
1324 | * @return array |
||
1325 | */ |
||
1326 | public function reorder_array_keys( $plugins ) { |
||
1336 | |||
1337 | /** |
||
1338 | * Test getting the main network site wpcom ID in single site installs |
||
1339 | * |
||
1340 | * @return void |
||
1341 | */ |
||
1342 | public function test_get_main_network_site_wpcom_id_single() { |
||
1350 | |||
1351 | } |
||
1352 | |||
1429 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: