forked from froger-me/wp-packages-update-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.php
360 lines (308 loc) · 17.4 KB
/
tests.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
<?php
require_once ABSPATH . 'wp-admin/includes/file.php';
global $wppus_functions_to_test_params, $wppus_functions_to_test, $wppus_actions_to_test, $wppus_filters_to_test, $wppus_output_log, $wppus_tests_show_queries_details, $wppus_tests_show_scripts_details;
// $wppus_output_log = 'serverlog';
// $wppus_output_log = 'filelog';
// $wppus_tests_show_queries_details = true;
// $wppus_tests_show_scripts_details = true;
$wppus_functions_to_test_params = array(
'test_plugin_slug' => 'dummy-plugin',
'test_theme_slug' => 'dummy-theme',
'test_package_slug' => 'dummy-plugin',
'test_package_path' => null,
'test_browse_licenses_payload' => array(
'relationship' => 'AND',
'limit' => 10,
'offset' => 0,
'order_by' => 'date_created',
'criteria' => array(
array(
'field' => 'email',
'value' => 'test@%',
'operator' => 'LIKE',
),
array(
'field' => 'license_key',
'value' => 'test-license',
'operator' => '=',
),
),
),
'test_add_license_payload' => array(
'license_key' => 'test-license',
'status' => 'blocked',
'max_allowed_domains' => '3',
'allowed_domains' => array(
'test.test.com',
'test2.test.com',
),
'owner_name' => 'Test Owner',
'email' => '[email protected]',
'company_name' => 'Test Company',
'txn_id' => '1111-test-license',
'date_created' => '2018-07-09',
'date_expiry' => '2099-07-09',
'date_renewed' => '2098-07-09',
'package_slug' => 'test-license-create',
'package_type' => 'plugin',
),
'test_read_license_payload' => array( 'license_key' => 'test-license' ),
'test_edit_license_payload' => array(
'license_key' => 'test-license',
'status' => 'pending',
),
'test_check_license_payload' => array( 'license_key' => 'test-license' ),
'test_activate_license_payload' => array(
'license_key' => 'test-license',
'allowed_domains' => 'test3.test.com',
),
'test_deactivate_license_payload' => array(
'license_key' => 'test-license',
'allowed_domains' => 'test3.test.com',
),
'test_delete_license_payload' => array( 'license_key' => 'test-license' ),
);
$wppus_functions_to_test = array(
// /** Functions **/ /** Parameters **/
// /** Plugin Data functions **/
// 'wppus_get_root_data_dir' => array(),
// 'wppus_get_packages_data_dir' => array(),
// 'wppus_get_logs_data_dir' => array(),
// 'wppus_force_cleanup_cache' => array(),
// 'wppus_force_cleanup_logs' => array(),
// 'wppus_force_cleanup_tmp' => array(),
// /** Update Server functions **/
// 'wppus_is_doing_update_api_request' => array(),
// 'wppus_check_remote_theme_update' => array( $wppus_functions_to_test_params['test_theme_slug'] ),
// 'wppus_download_remote_theme_to_local' => array( $wppus_functions_to_test_params['test_theme_slug'] ),
// 'wppus_check_remote_plugin_update' => array( $wppus_functions_to_test_params['test_plugin_slug'] ),
// 'wppus_download_remote_plugin_to_local' => array( $wppus_functions_to_test_params['test_plugin_slug'] ),
// 'wppus_get_local_package_path' => array( $wppus_functions_to_test_params['test_package_slug'] ),
// 'wppus_download_local_package' => array( $wppus_functions_to_test_params['test_package_slug'], $wppus_functions_to_test_params['test_package_path'] ),
// /** Licenses functions **/
// 'wppus_is_doing_license_api_request' => array(),
// 'wppus_add_license' => array( $wppus_functions_to_test_params['test_add_license_payload'] ),
// 'wppus_check_license' => array( $wppus_functions_to_test_params['test_check_license_payload'] ),
// 'wppus_read_license' => array( $wppus_functions_to_test_params['test_read_license_payload'] ),
// 'wppus_edit_license' => array( $wppus_functions_to_test_params['test_edit_license_payload'] ),
// 'wppus_browse_licenses' => array( $wppus_functions_to_test_params['test_browse_licenses_payload'] ),
// 'wppus_activate_license' => array( $wppus_functions_to_test_params['test_activate_license_payload'] ),
// 'wppus_deactivate_license' => array( $wppus_functions_to_test_params['test_deactivate_license_payload'] ),
// 'wppus_delete_license' => array( $wppus_functions_to_test_params['test_delete_license_payload'] ),
);
$wppus_actions_to_test = array(
// /** Actions **/ /** Parameters **/
// /** Plugin Data actions **/
// 'wppus_scheduled_cleanup_event' => 6, // bool $result, string $type, int $timestamp, string $frequency, string $hook, array $params
// 'wppus_registered_cleanup_schedule' => 2, // string $type, array $params
// 'wppus_cleared_cleanup_schedule' => 2, // string $type, array $params
// 'wppus_did_cleanup' => 4, // bool $result, string $type, int $size, bool $force
// /** Update Server actions **/
// 'wppus_primed_package_from_remote' => 2, // bool $result, string $slug
// 'wppus_scheduled_check_remote_event' => 6, // bool $result, string $slug, int $timestamp, string $frequency, string $hook, array $params
// 'wppus_registered_check_remote_schedule' => 3, // string $slug, string $scheduled_hook, string $action_hook
// 'wppus_cleared_check_remote_schedule' => 3, // string $slug, string $scheduled_hook, array $params
// 'wppus_downloaded_remote_package' => 3, // mixed $package, string $type, string $slug
// 'wppus_saved_remote_package_to_local' => 3, // bool $result, string $type, string $slug
// 'wppus_checked_remote_package_update' => 3, // bool $has_update, string $type, string $slug
// 'wppus_did_manual_upload_package' => 3, // bool $result, string $type, string $slug
// 'wppus_before_packages_download' => 3, // string $archive_name, string $archive_path, array $package_slugs
// 'wppus_triggered_package_download' => 2, // string $archive_name, string $archive_path
// 'wppus_before_handle_update_request' => 1, // array $request_params
// 'wppus_deleted_package' => 3, // bool $result, string $type, string $slug
// 'wppus_before_remote_package_zip' => 3, // string $package_slug, string $files_path, string $archive_path
// /** Licenses actions **/
// 'wppus_added_license_check' => 1, // string $package_slug
// 'wppus_removed_license_check' => 1, // string $package_slug
// 'wppus_registered_license_schedule' => 1, // array $scheduled_hook
// 'wppus_cleared_license_schedule' => 1, // array $scheduled_hook
// 'wppus_scheduled_license_event' => 4, // bool $result, int $timestamp, string $frequency, string $hook
// 'wppus_browse_licenses' => 1, // array $payload
// 'wppus_did_browse_licenses' => 1, // stdClass $license
// 'wppus_did_read_license' => 1, // stdClass $license
// 'wppus_did_edit_license' => 1, // stdClass $license
// 'wppus_did_add_license' => 1, // stdClass $license
// 'wppus_did_delete_license' => 1, // stdClass $license
// 'wppus_did_check_license' => 1, // mixed $result
// 'wppus_did_activate_license' => 1, // mixed $result
// 'wppus_did_deactivate_license' => 1, // mixed $result
);
$wppus_filters_to_test = array(
// /** Filters **/ /** Parameters **/
// /** Plugin Data filters **/
// 'wppus_submitted_data_config' => 1, // array $config
// 'wppus_schedule_cleanup_frequency' => 2, // string $frequency, string $type
// /** Update Server filters **/
// 'wppus_update_server' => 4, // mixed $update_server, array $config, string $slug, bool $use_license
// 'wppus_handle_update_request_params' => 1, // array $params
// 'wppus_update_checker' => 8, // mixed $update_checker, string $slug, string $type, string $package_file_name, string $repository_service_url, string $repository_branch, mixed $repository_credentials, bool $repository_service_self_hosted
// 'wppus_update_api_config' => 1, // array $config
// 'wppus_submitted_remote_sources_config' => 1, // array $config
// 'wppus_check_remote_frequency' => 2, // string $frequency, string $slug
// /** Licenses filters **/
// 'wppus_license_valid' => 2, // bool $isValid, mixed $license, string $license_signature
// 'wppus_license_server' => 1, // mixed $license_server
// 'wppus_license_api_config' => 1, // array $config
// 'wppus_licensed_package_slugs' => 1, // array $package_slugs
// 'wppus_submitted_licenses_config' => 1, // array $config
// 'wppus_check_license_result' => 2, // mixed $result, array $license_data
// 'wppus_activate_license_result' => 3, // mixed $result, array $license_data, mixed $license
// 'wppus_deactivate_license_result' => 3, // mixed $result, array $license_data, mixed $license
// 'wppus_activate_license_dirty_payload' => 1, // array $dirty_payload
// 'wppus_deactivate_license_dirty_payload' => 1, // array $dirty_payload
// 'wppus_browse_licenses_payload' => 1, // array $payload
// 'wppus_read_license_payload' => 1, // array $payload
// 'wppus_edit_license_payload' => 1, // array $payload
// 'wppus_add_license_payload' => 1, // array $payload
// 'wppus_delete_license_payload' => 1, // array $payload
// 'wppus_check_license_dirty_payload' => 1, // array $payload
// 'wppus_activate_license_payload' => 1, // array $payload
// 'wppus_deactivate_license_payload' => 1, // array $payload
);
if ( ! empty( $wppus_functions_to_test ) && ! has_action( 'plugins_loaded', 'wppus_ready_for_function_tests' ) ) {
function wppus_ready_for_function_tests() {
wppus_run_tests( 'functions' );
}
add_action( 'plugins_loaded', 'wppus_ready_for_function_tests', 6 );
}
if ( ! empty( $wppus_actions_to_test ) && ! has_action( 'init', 'wppus_ready_for_action_tests' ) ) {
function wppus_ready_for_action_tests() {
wppus_run_tests( 'actions' );
}
add_action( 'init', 'wppus_ready_for_action_tests', 10 );
}
if ( ! empty( $wppus_filters_to_test ) && ! has_filter( 'init', 'wppus_ready_for_filter_tests' ) ) {
function wppus_ready_for_filter_tests() {
wppus_run_tests( 'filters' );
}
add_action( 'init', 'wppus_ready_for_filter_tests', 10 );
}
function wppus_run_tests( $test ) {
if ( wp_doing_ajax() ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once WPPUS_PLUGIN_PATH . 'functions.php';
global $wppus_functions_to_test_params, $wppus_functions_to_test, $wppus_actions_to_test, $wppus_filters_to_test;
if ( 'functions' === $test ) {
wppus_functions_test_func( $wppus_functions_to_test, $wppus_functions_to_test_params );
}
if ( 'actions' === $test ) {
if ( ! empty( $wppus_actions_to_test ) ) {
foreach ( $wppus_actions_to_test as $action => $num_params ) {
add_action( $action, 'wppus_action_test_hook', 10, $num_params );
}
}
}
if ( 'filters' === $test ) {
if ( ! empty( $wppus_filters_to_test ) ) {
foreach ( $wppus_filters_to_test as $filter => $num_params ) {
add_action( $filter, 'wppus_filter_test_hook', 10, $num_params );
}
}
}
}
function wppus_tests_log( $message, $array_or_object = null ) {
global $wppus_output_log;
date_default_timezone_set( @date_default_timezone_get() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set, WordPress.PHP.NoSilencedErrors.Discouraged
$line = date( '[Y-m-d H:i:s O]' ) . ' ' . $message; // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
if ( 'serverlog' === $wppus_output_log ) {
error_log( $line ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
if ( null !== $array_or_object ) {
error_log( print_r( $array_or_object, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r, WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
}
if ( 'filelog' === $wppus_output_log ) {
$log_file = WPPUS_Data_Manager::get_data_dir( 'logs' ) . 'tests.log';
$handle = fopen( $log_file, 'a' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
if ( $handle && flock( $handle, LOCK_EX ) ) {
$line .= "\n";
fwrite( $handle, $line ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
if ( null !== $array_or_object ) {
fwrite( $handle, print_r( $array_or_object, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r, WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
}
flock( $handle, LOCK_UN );
}
if ( $handle ) {
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
}
}
}
function wppus_functions_test_func( $functions, $test_params ) {
$start_message = '========================================================';
$header_delimiter_message = '--------------------------------------------------------';
$delimiter_message = '------';
if ( ! empty( $functions ) ) {
wppus_tests_log( $start_message );
wppus_tests_log( 'Start functions test with the following parameters: ', $test_params );
wppus_tests_log( $header_delimiter_message );
foreach ( $functions as $function_name => $params ) {
$message = $function_name . ' called with params: ';
wppus_tests_log( $message, $params );
$result = call_user_func_array( $function_name, $params );
if ( ! is_array( $result ) ) {
$result = array( $result );
}
$message = 'Result: ';
wppus_tests_log( $message, $result );
wppus_tests_log( $delimiter_message );
}
wppus_tests_log( '--- End functions test ---' );
}
}
function wppus_action_test_hook() {
$start_message = '========================================================';
$message = current_filter() . ' called with params: ';
wppus_tests_log( $start_message );
wppus_tests_log( '--- Start ' . current_filter() . ' action test ---' );
wppus_tests_log( $message, func_get_args() );
wppus_tests_log( '--- End ' . current_filter() . ' action test ---' );
}
function wppus_filter_test_hook() {
$start_message = '========================================================';
$message = current_filter() . ' called with params: ';
$params = func_get_args();
wppus_tests_log( $start_message );
wppus_tests_log( '--- Start ' . current_filter() . ' filter test ---' );
wppus_tests_log( $message, $params );
wppus_tests_log( '--- End ' . current_filter() . ' filter test ---' );
return reset( $params );
}
function wppus_get_formatted_memory( $bytes, $precision = 2 ) {
$units = array( 'B', 'K', 'M', 'G', 'T' );
$bytes = max( $bytes, 0 );
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min( $pow, count( $units ) - 1 );
$bytes /= ( 1 << ( 10 * $pow ) );
return round( $bytes, $precision ) . ' ' . $units[ $pow ];
}
function wppus_performance_stats_log() {
global $wpdb, $wppus_mem_before, $wppus_scripts_before, $wppus_queries_before, $wppus_tests_show_queries_details, $wppus_tests_show_scripts_details;
$mem_after = memory_get_peak_usage();
$query_list = array();
$scripts_after = get_included_files();
$scripts = array_diff( $scripts_after, $wppus_scripts_before );
$query_stats = 'Number of queries executed by the plugin: ' . ( count( $wpdb->queries ) - count( $wppus_queries_before ) );
$scripts_stats = 'Number of included/required scripts by the plugin: ' . count( $scripts );
$mem_stats = 'Server memory used to run the plugin: ' . wppus_get_formatted_memory( $mem_after - $wppus_mem_before ) . ' / ' . ini_get( 'memory_limit' );
foreach ( $wpdb->queries as $query ) {
$query_list[] = reset( $query );
}
wppus_tests_log( '========================================================' );
wppus_tests_log( '--- Start load tests ---' );
wppus_tests_log( 'Time elapsed: ' . timer_stop() );
wppus_tests_log( 'Total server memory used: ' . wppus_get_formatted_memory( $mem_after ) . ' / ' . ini_get( 'memory_limit' ) );
wppus_tests_log( 'Total number of queries: ' . count( $wpdb->queries ) );
wppus_tests_log( 'Total number of scripts: ' . count( $scripts_after ) );
wppus_tests_log( $mem_stats );
wppus_tests_log( $query_stats );
wppus_tests_log( $scripts_stats );
if ( $wppus_tests_show_queries_details ) {
wppus_tests_log( 'Queries: ', $query_list );
}
if ( $wppus_tests_show_scripts_details ) {
wppus_tests_log( 'Scripts: ', $scripts );
}
wppus_tests_log( '--- End load tests ---' );
}
add_action( 'shutdown', 'wppus_performance_stats_log' );