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
<?php
require_once( 'cmb-tests-base.php' );
class Test_CMB2_Field extends Test_CMB2 {
public function set_up() {
parent::set_up();
$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( __CLASS__, 'before_field_cb' ),
'after_field' => 'after_field_static',
'classes_cb' => array( __CLASS__, 'row_classes_array_cb' ),
'default_cb' => array( __CLASS__, 'cb_to_set_default' ),
'render_row_cb' => array( __CLASS__, 'render_row_cb_test' ),
'label_cb' => array( __CLASS__, '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 tear_down() {
parent::tear_down();
}
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' );
$content = ob_get_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() {
$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 show_in_rest char_counter char_max char_max_enforce id before_field after_field _id _name has_supporting_data', $classes );
}
public function test_cmb2_default_field_callback_with_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, show_in_rest, char_counter, char_max, char_max_enforce, id, before_field, after_field, _id, _name, has_supporting_data', $default );
}
public function test_cmb2_classes_field_callback_with_string() {
$args = $this->field_args;
$args['row_classes'] = array( __CLASS__, '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_classes_string() {
$args = $this->field_args;
$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() {
$args = $this->field_args;
$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( __CLASS__, '_return_different_value' ) );
$modified = $field->save_field( 'some value to be modified' );
$this->assertTrue( ! ! $modified );
remove_filter( 'cmb2_sanitize_text', array( __CLASS__, '_return_different_value' ) );
$val = get_post_meta( $this->object_id, 'test_test', 1 );
$this->assertEquals( 'modified string', $val );
$this->assertEquals( $val, $field->get_data() );
}
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' );
}
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,
) );
$this->assertEmpty( $field->get_data() );
$dirty_val = 'test<html><stuff><script>xss</script><a href="http://xssattackexamples.com/">Click to Download</a>';
$cleaned_val = sanitize_text_field( $dirty_val );
$this->assertEquals( $cleaned_val, $field->sanitization_cb( $dirty_val ) );
$this->assertTrue( $field->save_field( $dirty_val ) );
$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" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline1">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="custom" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline2">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="none" data-hash=\'6p2q27ncjf10\'/><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" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline1">None</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none" data-hash=\'6p2q27ncjf10\'/><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" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline1">No Value</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom" data-hash=\'6p2q27ncjf10\'/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none" data-hash=\'6p2q27ncjf10\'/><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' ),
'show_on_cb' => 'yourprefix_show_if_front_page',
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
), $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,
'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() {
$array_val = array( 'check1', 'check2' );
$this->field->save_field( $array_val );
$this->assertEquals( $array_val, get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
delete_post_meta( $this->field->object_id, $this->field->_id() );
$this->assertFalse( ! ! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
add_filter( "cmb2_override_{$this->field->_id()}_meta_save", array( __CLASS__, 'override_set' ), 10, 4 );
$this->field->save_field( $array_val );
$this->assertFalse( ! ! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
$opt_key = 'test-' . $this->field->object_id . '-' . $this->field->_id();
$opt = get_option( $opt_key );
$this->assertEquals( $array_val, $opt );
$value = $this->field->get_data();
$this->assertFalse( ! ! $value );
add_filter( "cmb2_override_{$this->field->_id()}_meta_value", array( __CLASS__, 'override_get' ), 10, 4 );
$value = $this->field->get_data();
$this->assertEquals( $array_val, $value );
}
public function test_get_cmb() {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
$this->assertTrue( true );
return;
}
$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 || 'default_cb' === $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" data-hash=\'6ll8fi7o2du0\'> <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-secondary cmb-remove-row-button" title="Remove Row">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" data-hash=\'6ll8fi7o2du0\'> <option value="" >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-secondary cmb-remove-row-button" title="Remove Row">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-secondary">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 static function before_field_cb( $args ) {
echo 'before_field_cb_' . $args['id'];
}
public static function row_classes_array_cb( $args ) {
return array_keys( $args );
}
public static function row_classes_string_cb( $args ) {
return 'callback with string';
}
public static function render_row_cb_test( $field_args ) {
echo 'test render cb';
}
public static function label_cb_test( $field_args ) {
echo 'test label cb';
}
public static function cb_to_set_default( $args ) {
return implode( ', ', array_keys( $args ) );
}
public static function _return_different_value( $null ) {
return 'modified string';
}
public static 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 static 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 );
}
public function test_cmb2_field_save_slashed() {
$args = $this->field_args;
$args['save_field'] = true;
$field = $this->new_field( $args );
$tests = array(
'[{\"content\":\"This is a \\\"some\\\" content\"}]',
'¯\\\_(ツ)_/¯',
);
foreach ( $tests as $test_val ) {
$unslashed_val = wp_unslash( $test_val );
$field->save_field( $test_val );
$this->assertSame( CMB2_Utils::wp_at_least( '3.8.1' ) ? $unslashed_val : $test_val, $field->get_data() );
$field->save_field( $unslashed_val );
$this->assertSame( $unslashed_val, $field->get_data() );
}
}
public function test_cmb2_money_field_save_slashed() {
$args = $this->field_args;
$args['save_field'] = true;
$args['type'] = 'text_money';
$field = $this->new_field( $args );
$this->cmb2_money_field_save_slashed( $field, '¯\\\_(ツ)_/¯', '0.00' );
$this->cmb2_money_field_save_slashed( $field, '5,000.23', '5,000.23' );
$this->cmb2_money_field_save_slashed( $field, '444446000.23', '444,446,000.23' );
global $wp_locale;
$thousands_sep = $wp_locale->number_format['thousands_sep'];
$wp_locale->number_format['thousands_sep'] = "'";
$this->cmb2_money_field_save_slashed( $field, "2\'180.00", "2'180.00" );
$this->cmb2_money_field_save_slashed( $field, "444\'446\'000.23", "444'446'000.23" );
$this->cmb2_money_field_save_slashed( $field, "444'446'000.23", "444'446'000.23" );
$this->cmb2_money_field_save_slashed( $field, '444446000.23', "444'446'000.23" );
$wp_locale->number_format['thousands_sep'] = $thousands_sep;
}
protected function cmb2_money_field_save_slashed( $field, $test_val, $expected ) {
$field->save_field( $test_val );
$this->assertSame( $expected, $field->get_data() );
$field->save_field( $expected );
$this->assertSame( $expected, $field->get_data() );
}
}