forked from CMB2/CMB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cmb-field.php
507 lines (397 loc) · 18.3 KB
/
test-cmb-field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/**
* CMB2_Field tests
*
* @package Tests_CMB2
* @author WebDevStudios
* @license GPL-2.0+
* @link http://webdevstudios.com
*/
require_once( 'cmb-tests-base.php' );
class Test_CMB2_Field extends Test_CMB2 {
/**
* Set up the test fixture
*/
public function setUp() {
parent::setUp();
$this->post_id = $this->factory->post->create();
$this->field_args = array(
'name' => 'Name',
'id' => 'test_test',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'disabled' => 'disabled',
'data-test' => 'data-value',
'data-test' => json_encode( array(
'name' => 'Name',
'id' => 'test_test',
'type' => 'text',
) ),
),
'before_field' => array( $this, 'before_field_cb' ),
'after_field' => 'after_field_static',
'classes_cb' => array( $this, 'row_classes_array_cb' ),
'default' => array( $this, 'cb_to_set_default' ),
'render_row_cb' => array( $this, 'render_row_cb_test' ),
'label_cb' => array( $this, 'label_cb_test' ),
);
$this->object_id = $this->post_id;
$this->object_type = 'post';
$this->cmb_id = 'metabox';
$this->group = false;
$this->field = $this->new_field( $this->field_args );
}
public function tearDown() {
parent::tearDown();
}
public function test_cmb2_field_instance() {
$this->assertInstanceOf( 'CMB2_Field', $this->field );
}
public function test_cmb2_before_and_after_field_callbacks() {
ob_start();
$this->field->peform_param_callback( 'before_field' );
$this->field->peform_param_callback( 'after_field' );
// grab the data from the output buffer and add it to our $content variable
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals( 'before_field_cb_test_testafter_field_static', $content );
}
public function test_cmb2_render_row_cb_field_callback() {
ob_start();
$this->field->render_field();
$rendered = ob_get_clean();
$this->assertEquals( 'test render cb', $rendered );
}
public function test_cmb2_label_cb_field_callback() {
$label = $this->field->get_param_callback_result( 'label_cb' );
$this->assertEquals( 'test label cb', $label );
}
public function test_cmb2_row_classes_field_callback_with_array() {
// Add row classes dynamically with a callback that returns an array
$classes = $this->field->row_classes();
$this->assertHTMLstringsAreEqual( 'cmb-type-text cmb2-id-test-test table-layout type name desc before after options options_cb text text_cb attributes protocols default default_cb classes classes_cb select_all_button multiple repeatable inline on_front show_names save_field date_format time_format description preview_size render_row_cb display_cb label_cb column js_dependencies id before_field after_field _id _name has_supporting_data', $classes );
}
public function test_cmb2_default_field_callback_with_array() {
// Add row classes dynamically with a callback that returns an array
$default = $this->field->args( 'default' );
$this->assertHTMLstringsAreEqual( 'type, name, desc, before, after, options, options_cb, text, text_cb, attributes, protocols, default, default_cb, classes, classes_cb, select_all_button, multiple, repeatable, inline, on_front, show_names, save_field, date_format, time_format, description, preview_size, render_row_cb, display_cb, label_cb, column, js_dependencies, id, before_field, after_field, _id, _name, has_supporting_data', $default );
}
public function test_cmb2_row_classes_field_callback_with_string() {
// Test with string
$args = $this->field_args;
// Add row classes dynamically with a callback that returns a string
$args['row_classes'] = array( $this, 'row_classes_string_cb' );
$field = $this->new_field( $args );
$classes = $field->row_classes();
$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout callback with string', $classes );
}
public function test_cmb2_row_classes_string() {
// Test with string
$args = $this->field_args;
// Add row classes statically as a string
$args['row_classes'] = 'these are some classes';
$field = $this->new_field( $args );
$classes = $field->row_classes();
$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout these are some classes', $classes );
}
public function test_cmb2_should_show() {
// Test with string
$args = $this->field_args;
// Add row classes statically as a string
$args['show_on_cb'] = '__return_false';
$field = $this->new_field( $args );
$this->assertFalse( $field->should_show() );
$field->args['show_on_cb'] = '__return_true';
$this->assertTrue( $field->should_show() );
}
public function test_filtering_field_type_sanitization_filtering() {
$field = $this->new_field( $this->field_args );
add_filter( 'cmb2_sanitize_text', array( $this, '_return_different_value' ) );
$modified = $field->save_field( 'some value to be modified' );
$this->assertTrue( !! $modified );
remove_filter( 'cmb2_sanitize_text', array( $this, '_return_different_value' ) );
// $val = $field->get_data();
$val = get_post_meta( $this->object_id, 'test_test', 1 );
$this->assertEquals( 'modified string', $val );
$this->assertEquals( $val, $field->get_data() );
}
public function _return_different_value( $null ) {
return 'modified string';
}
/**
* @expectedException WPDieException
*/
public function test_cmb2_save_field_action() {
$field = $this->new_field( $this->field_args );
$this->hook_to_wp_die( 'cmb2_save_field' );
$modified = $field->save_field( 'some value to be modified' );
}
/**
* @expectedException WPDieException
*/
public function test_cmb2_save_field_field_id_action() {
$field = $this->new_field( $this->field_args );
$this->hook_to_wp_die( 'cmb2_save_field_test_test' );
$modified = $field->save_field( 'some value to be modified' );
}
public function test_empty_field_with_empty_object_id() {
$field = new CMB2_Field( array(
'field_args' => $this->field_args,
) );
// data should be empty since we have no object id
$this->assertEmpty( $field->get_data() );
// add some xss for good measure
$dirty_val = 'test<html><stuff><script>xss</script><a href="http://xssattackexamples.com/">Click to Download</a>';
$cleaned_val = sanitize_text_field( $dirty_val );
// Make sure it sanitizes as expected
$this->assertEquals( $cleaned_val, $field->sanitization_cb( $dirty_val ) );
// Sanitize/store the field
$this->assertTrue( $field->save_field( $dirty_val ) );
// Retrieve saved value(s)
$this->assertEquals( $cleaned_val, cmb2_options( 0 )->get( $field->id() ) );
$this->assertEquals( array( 'test_test' => $cleaned_val ), cmb2_options( 0 )->get_options() );
}
public function test_show_option_none() {
$args = array(
'name' => 'Test Radio inline',
'desc' => 'field description (optional)',
'id' => 'radio_inline',
'type' => 'radio_inline',
'options' => array(
'standard' => 'Option One',
'custom' => 'Option Two',
'none' => 'Option Three',
),
);
$field = $this->new_field( $args );
$this->assertFalse( $field->args( 'show_option_none' ) );
$this->assertHTMLstringsAreEqual(
'<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline" data-fieldtype="radio_inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="standard"/><label for="radio_inline1">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="custom"/><label for="radio_inline2">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="none"/><label for="radio_inline3">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
$this->render_field( $field )
);
$args['show_option_none'] = true;
$field = $this->new_field( $args );
$this->assertEquals( esc_html__( 'None', 'cmb2' ), $field->args( 'show_option_none' ) );
$this->assertHTMLstringsAreEqual(
'<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline" data-fieldtype="radio_inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="" checked="checked"/><label for="radio_inline1">None</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard"/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom"/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none"/><label for="radio_inline4">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
$this->render_field( $field )
);
$args['show_option_none'] = 'No Value';
$field = $this->new_field( $args );
$this->assertEquals( 'No Value', $field->args( 'show_option_none' ) );
$this->assertHTMLstringsAreEqual(
'<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline" data-fieldtype="radio_inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="" checked="checked"/><label for="radio_inline1">No Value</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard"/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom"/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none"/><label for="radio_inline4">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
$this->render_field( $field )
);
}
public function test_multiple_meta_rows() {
$prefix = 'testing';
$post_id = $this->post_id;
$array_val = array( 'check1', 'check2' );
$cmb_demo = cmb2_get_metabox( array(
'id' => $prefix . 'metabox',
'title' => esc_html__( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ), // Post type
'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // true to keep the metabox closed by default
), $post_id );
$field_id = $cmb_demo->add_field( array(
'name' => esc_html__( 'Test Multi Checkbox', 'cmb2' ),
'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'multicheckbox',
'type' => 'multicheck',
'multiple' => true, // Store values in individual rows
'options' => array(
'check1' => esc_html__( 'Check One', 'cmb2' ),
'check2' => esc_html__( 'Check Two', 'cmb2' ),
'check3' => esc_html__( 'Check Three', 'cmb2' ),
),
) );
$field = $cmb_demo->get_field( $field_id );
$saved = $field->save_field( $array_val );
$this->assertEquals( 2, $saved );
$this->assertEquals( $array_val, $field->get_data() );
$this->assertEquals( $array_val, get_post_meta( $post_id, $field_id ) );
$val = get_post_meta( $post_id, $field_id, 1 );
$this->assertEquals( reset( $array_val ), $val );
}
public function test_set_get_filters() {
// Value to set
$array_val = array( 'check1', 'check2' );
// Set the field value normally
$this->field->save_field( $array_val );
// Verify that field was saved succesfully to post-meta
$this->assertEquals( $array_val, get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
// Now delete the post-meta
delete_post_meta( $this->field->object_id, $this->field->_id() );
// Verify that the post-meta no longer exists
$this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
// Now filter the setting of the meta.. will use update_option instead
add_filter( "cmb2_override_{$this->field->_id()}_meta_save", array( $this, 'override_set' ), 10, 4 );
// Set the value
$this->field->save_field( $array_val );
// Verify that the post-meta is still empty
$this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
// Re-create the option key that we used to save the data
$opt_key = 'test-'. $this->field->object_id . '-' . $this->field->_id();
// And retrieve the option
$opt = get_option( $opt_key );
// Verify it is the value we set
$this->assertEquals( $array_val, $opt );
// Now retireve the value.. will default to getting from post-meta
$value = $this->field->get_data();
// Verify that there's still nothing there in post-meta
$this->assertFalse( !! $value );
// Now filter the getting of the meta, which will use get_option
add_filter( "cmb2_override_{$this->field->_id()}_meta_value", array( $this, 'override_get' ), 10, 4 );
// Get the value
$value = $this->field->get_data();
// Verify that the data we retrieved now matches the value we set
$this->assertEquals( $array_val, $value );
}
public function test_get_cmb() {
$cmb = new_cmb2_box( array(
'id' => 'metabox',
'object_types' => array( 'post' ),
) );
$field = $this->invokeMethod( $cmb, 'get_new_field', $this->field_args );
$this->assertEquals( $cmb, $field->get_cmb() );
}
public function test_get_field_clone() {
$field = $this->field->get_field_clone( array( 'id' => 'test_field_clone' ) );
foreach ( $this->field_args as $key => $arg ) {
if ( 'id' === $key || 'default' === $key ) {
continue;
}
$this->assertEquals( $arg, $field->args( $key ) );
}
$this->assertEquals( 'test_field_clone', $field->id() );
}
public function test_string() {
$field = $this->new_field( array(
'name' => 'Case Study',
'id' => 'prouct-case-study',
'type' => 'select',
'show_option_none' => true,
'repeatable' => true,
'options' => array(
'1' => 'Rad Case Study',
'2' => 'Wicked Case Study',
'3' => 'Cool Case Study',
),
'text' => array(
'add_row_text' => 'Add Case Study',
),
) );
ob_start();
$field->render_field();
$rendered = ob_get_clean();
$expected = '
<div class="cmb-row cmb-type-select cmb2-id-prouct-case-study cmb-repeat" data-fieldtype="select">
<div class="cmb-th">
<label for="prouct-case-study">Case Study</label>
</div>
<div class="cmb-td">
<div id="prouct-case-study_repeat" class="cmb-repeat-table cmb-nested">
<div class="cmb-tbody cmb-field-list">
<div class="cmb-row cmb-repeat-row">
<div class="cmb-td">
<select class="cmb2_select" name="prouct-case-study[0]" id="prouct-case-study_0" data-iterator="0"> <option value="" selected=\'selected\'>None</option>
<option value="1" >Rad Case Study</option>
<option value="2" >Wicked Case Study</option>
<option value="3" >Cool Case Study</option>
</select>
</div>
<div class="cmb-td cmb-remove-row">
<button type="button" class="button cmb-remove-row-button button-disabled">Remove</button>
</div>
</div>
<div class="cmb-row empty-row hidden">
<div class="cmb-td">
<select class="cmb2_select" name="prouct-case-study[1]" id="prouct-case-study_1" data-iterator="1"> <option value="" selected=\'selected\'>None</option>
<option value="1" >Rad Case Study</option>
<option value="2" >Wicked Case Study</option>
<option value="3" >Cool Case Study</option>
</select>
</div>
<div class="cmb-td cmb-remove-row">
<button type="button" class="button cmb-remove-row-button">Remove</button>
</div>
</div>
</div>
</div>
<p class="cmb-add-row">
<button type="button" data-selector="prouct-case-study_repeat" class="cmb-add-row-button button">Add Case Study</button>
</p>
</div>
</div>';
$this->assertHTMLstringsAreEqual( $expected, $rendered );
}
public function new_field( $field_args ) {
$args = array(
'field_args' => array(),
'group_field' => $this->group,
'object_id' => $this->object_id,
'object_type' => $this->object_type,
'cmb_id' => $this->cmb_id,
);
if ( isset( $field_args['field_args'] ) ) {
$args = wp_parse_args( $field_args, $args );
} else {
$args['field_args'] = $field_args;
}
return new CMB2_Field( $args );
}
public function before_field_cb( $args ) {
echo 'before_field_cb_' . $args['id'];
}
public function row_classes_array_cb( $args ) {
/**
* Side benefit: this will call out when default args change
*/
return array_keys( $args );
}
public function row_classes_string_cb( $args ) {
return 'callback with string';
}
public function render_row_cb_test( $field_args ) {
echo 'test render cb';
}
public function label_cb_test( $field_args ) {
echo 'test label cb';
}
public function cb_to_set_default( $args ) {
/**
* Side benefit: this will call out when default args change
*/
return implode( ', ', array_keys( $args ) );
}
public function override_set( $override, $args, $field_args, $field ) {
$opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
$updated = update_option( $opt_key, $args['value'] );
return true;
}
public function override_get( $override_val, $object_id, $args, $field ) {
$opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
return get_option( $opt_key );
}
public function test_cmb2_field_save_field_false() {
$args = $this->field_args;
$args['save_field'] = false;
$field = $this->new_field( $args );
$modified = $field->save_field( 'some value that should not be saved' );
$this->assertFalse( $modified );
}
public function test_cmb2_field_save_field_true() {
$args = $this->field_args;
$args['save_field'] = true;
$field = $this->new_field( $args );
$modified = $field->save_field( 'some value that should be saved' );
$this->assertNotFalse( $modified );
}
}