-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpest.php
More file actions
459 lines (380 loc) · 11.6 KB
/
pest.php
File metadata and controls
459 lines (380 loc) · 11.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
<?php
declare(strict_types=1);
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit\Framework\TestCase class.
| By default, that will be PestPHP\Pest\TestCase. Of course, you may need to bind a different class for certain
| file or directories. You can do so here.
|
*/
use PHPUnit\Framework\TestCase;
uses(TestCase::class)->in('tests');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of expectations that you can apply to your values.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| Feel free to use this space to define helper functions that you find yourself
| regularly needing within your tests. They're shared across every test file in your
| application, giving you a convenient place to reference them.
|
*/
function createField(array $config = []): array
{
return array_merge([
'id' => 'test_field',
'type' => 'text',
'label' => 'Test Field',
], $config);
}
function createWPField(array $config = [], string $storage_type = 'options'): WP_Field
{
return new WP_Field(createField($config), $storage_type);
}
/*
|--------------------------------------------------------------------------
| WordPress Mock Functions for Testing
|--------------------------------------------------------------------------
*/
// Global metadata storage for all meta functions
global $wp_test_meta_storage;
$wp_test_meta_storage = [
'post' => [],
'term' => [],
'user' => [],
'comment' => [],
'options' => [],
];
if (! function_exists('plugin_dir_url')) {
function plugin_dir_url($file)
{
return 'http://example.com/wp-content/plugins/woo2iiko/lib/wp-field/';
}
}
if (! function_exists('wp_kses_post')) {
function wp_kses_post($data)
{
return $data;
}
}
if (! function_exists('esc_attr')) {
function esc_attr($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
}
if (! function_exists('esc_attr__')) {
function esc_attr__($text, $domain = 'default')
{
return esc_attr(__($text, $domain));
}
}
if (! function_exists('esc_html')) {
function esc_html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
}
if (! function_exists('__')) {
function __($text, $domain = 'default')
{
return $text;
}
}
if (! function_exists('esc_html__')) {
function esc_html__($text, $domain = 'default')
{
return esc_html(__($text, $domain));
}
}
if (! function_exists('esc_textarea')) {
function esc_textarea($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
}
if (! function_exists('esc_url')) {
function esc_url($url)
{
return htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
}
}
if (! function_exists('tag_escape')) {
function tag_escape($tag)
{
return htmlspecialchars($tag, ENT_QUOTES, 'UTF-8');
}
}
if (! function_exists('absint')) {
function absint($maybeint)
{
return abs((int) $maybeint);
}
}
if (! function_exists('get_option')) {
function get_option($option, $default = false)
{
global $wp_test_meta_storage;
return $wp_test_meta_storage['options'][$option] ?? $default;
}
}
if (! function_exists('get_post_meta')) {
function get_post_meta($post_id, $key, $single = false)
{
global $wp_test_meta_storage;
if (! isset($wp_test_meta_storage['post'][$post_id][$key])) {
return $single ? '' : [];
}
return $single ? $wp_test_meta_storage['post'][$post_id][$key] : [$wp_test_meta_storage['post'][$post_id][$key]];
}
}
if (! function_exists('get_term_meta')) {
function get_term_meta($term_id, $key, $single = false)
{
global $wp_test_meta_storage;
if (! isset($wp_test_meta_storage['term'][$term_id][$key])) {
return $single ? '' : [];
}
return $single ? $wp_test_meta_storage['term'][$term_id][$key] : [$wp_test_meta_storage['term'][$term_id][$key]];
}
}
if (! function_exists('get_user_meta')) {
function get_user_meta($user_id, $key, $single = false)
{
global $wp_test_meta_storage;
if (! isset($wp_test_meta_storage['user'][$user_id][$key])) {
return $single ? '' : [];
}
return $single ? $wp_test_meta_storage['user'][$user_id][$key] : [$wp_test_meta_storage['user'][$user_id][$key]];
}
}
if (! function_exists('get_comment_meta')) {
function get_comment_meta($comment_id, $key, $single = false): void {}
}
if (! function_exists('is_admin')) {
function is_admin()
{
return false;
}
}
if (! function_exists('wp_enqueue_script')) {
function wp_enqueue_script($handle, $src = '', $deps = [], $ver = false, $in_footer = false)
{
return true;
}
}
if (! function_exists('wp_enqueue_style')) {
function wp_enqueue_style($handle, $src = '', $deps = [], $ver = false, $media = 'all')
{
return true;
}
}
if (! function_exists('apply_filters')) {
function apply_filters($tag, $value, ...$args)
{
return $value;
}
}
if (! function_exists('get_the_ID')) {
function get_the_ID()
{
return 1;
}
}
if (! function_exists('sanitize_text_field')) {
function sanitize_text_field($str)
{
return strip_tags($str);
}
}
if (! function_exists('is_email')) {
function is_email($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
}
if (! function_exists('update_post_meta')) {
function update_post_meta($post_id, $meta_key, $meta_value)
{
global $wp_test_meta_storage;
$wp_test_meta_storage['post'][$post_id][$meta_key] = $meta_value;
return true;
}
}
if (! function_exists('delete_post_meta')) {
function delete_post_meta($post_id, $meta_key)
{
global $wp_test_meta_storage;
unset($wp_test_meta_storage['post'][$post_id][$meta_key]);
return true;
}
}
if (! function_exists('metadata_exists')) {
function metadata_exists($meta_type, $object_id, $meta_key)
{
global $wp_test_meta_storage;
return isset($wp_test_meta_storage[$meta_type][$object_id][$meta_key]);
}
}
if (! function_exists('update_term_meta')) {
function update_term_meta($term_id, $meta_key, $meta_value)
{
global $wp_test_meta_storage;
$wp_test_meta_storage['term'][$term_id][$meta_key] = $meta_value;
return true;
}
}
if (! function_exists('delete_term_meta')) {
function delete_term_meta($term_id, $meta_key)
{
global $wp_test_meta_storage;
unset($wp_test_meta_storage['term'][$term_id][$meta_key]);
return true;
}
}
if (! function_exists('update_user_meta')) {
function update_user_meta($user_id, $meta_key, $meta_value)
{
global $wp_test_meta_storage;
$wp_test_meta_storage['user'][$user_id][$meta_key] = $meta_value;
return true;
}
}
if (! function_exists('delete_user_meta')) {
function delete_user_meta($user_id, $meta_key)
{
global $wp_test_meta_storage;
unset($wp_test_meta_storage['user'][$user_id][$meta_key]);
return true;
}
}
if (! function_exists('update_option')) {
function update_option($option, $value)
{
global $wp_test_meta_storage;
$wp_test_meta_storage['options'][$option] = $value;
return true;
}
}
if (! function_exists('delete_option')) {
function delete_option($option)
{
return true;
}
}
if (! function_exists('add_action')) {
function add_action($hook, $callback, $priority = 10, $accepted_args = 1)
{
$GLOBALS['wp_test_actions'] ??= [];
$GLOBALS['wp_test_actions'][] = ['hook' => $hook, 'callback' => $callback];
}
}
if (! function_exists('add_meta_box')) {
function add_meta_box($id, $title, $callback, $screen, $context = 'advanced', $priority = 'default', $callback_args = null)
{
$GLOBALS['wp_test_meta_boxes'] ??= [];
$GLOBALS['wp_test_meta_boxes'][] = compact('id', 'title', 'callback', 'screen', 'context', 'priority', 'callback_args');
}
}
if (! function_exists('wp_nonce_field')) {
function wp_nonce_field($action = -1, $name = '_wpnonce', $referer = true, $echo = true)
{
$nonce = 'test_nonce';
if ($echo) {
echo '<input type="hidden" name="'.$name.'" value="'.$nonce.'" />';
}
return $nonce;
}
}
if (! function_exists('wp_verify_nonce')) {
function wp_verify_nonce($nonce, $action = -1)
{
return $GLOBALS['wp_test_verify_nonce'] ?? true;
}
}
if (! function_exists('current_user_can')) {
function current_user_can($capability, ...$args)
{
$can = $GLOBALS['wp_test_current_user_can'] ?? true;
if ($capability === 'edit_post' && isset($args[0]) && $args[0] === 999999) {
return false;
}
return $can;
}
}
if (! function_exists('add_menu_page')) {
function add_menu_page($page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon = '', $position = null)
{
$GLOBALS['wp_test_menu_pages'] ??= [];
$GLOBALS['wp_test_menu_pages'][] = compact('page_title', 'menu_title', 'capability', 'menu_slug', 'icon', 'position');
return $menu_slug;
}
}
if (! function_exists('add_submenu_page')) {
function add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '')
{
$GLOBALS['wp_test_submenu_pages'] ??= [];
$GLOBALS['wp_test_submenu_pages'][] = compact('parent_slug', 'page_title', 'menu_title', 'capability', 'menu_slug');
return $menu_slug;
}
}
if (! function_exists('register_setting')) {
function register_setting($option_group, $option_name, $args = [])
{
$GLOBALS['wp_test_registered_settings'] ??= [];
$GLOBALS['wp_test_registered_settings'][] = compact('option_group', 'option_name', 'args');
}
}
if (! function_exists('settings_fields')) {
function settings_fields($option_group)
{
echo '<input type="hidden" name="option_page" value="'.$option_group.'" />';
}
}
if (! function_exists('submit_button')) {
function submit_button($text = null, $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = [])
{
echo '<button type="'.$type.'" name="'.$name.'">'.($text ?? 'Save Changes').'</button>';
}
}
if (! class_exists('WP_Term')) {
class WP_Term
{
public $term_id;
public $name;
public $slug;
public $term_group;
public $term_taxonomy_id;
public $taxonomy;
public $description;
public $parent;
public $count;
public function __construct($term_id)
{
$this->term_id = $term_id;
$this->name = 'Test Term';
$this->slug = 'test-term';
$this->term_group = 0;
$this->term_taxonomy_id = $term_id;
$this->taxonomy = 'category';
$this->description = '';
$this->parent = 0;
$this->count = 0;
}
}
}