Skip to content

Commit

Permalink
Make show_option_none applicable for 'select', 'radio', and 'radio_in…
Browse files Browse the repository at this point in the history
…line' types, but defaulted to off
  • Loading branch information
jtsternberg committed Mar 16, 2015
1 parent 8136ce7 commit 833e2f2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 41 deletions.
11 changes: 8 additions & 3 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,17 @@ public function _set_field_defaults( $args ) {
$args['options']['textarea_name'] = $args['_name'];
}

$option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this );
$option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'select', 'radio', 'radio_inline', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this );

if ( in_array( $args['type'], $option_types, true ) ) {

$args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : __( 'None', 'cmb2' );
$args['show_option_all'] = isset( $args['show_option_all'] ) ? $args['show_option_all'] : __( 'All', 'cmb2' ); // @todo: implementation
$args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : false;
$args['show_option_none'] = true === $args['show_option_none'] ? __( 'None', 'cmb2' ) : $args['show_option_none'];

if ( ! $args['show_option_none'] ) {
$off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true );
$args['show_option_none'] = $off_by_default ? false : __( 'None', 'cmb2' );
}

}

Expand Down
8 changes: 7 additions & 1 deletion includes/CMB2_Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ public function concat_items( $args = array() ) {
: $this->field->args( 'default' );

$concatenated_items = ''; $i = 1;
foreach ( (array) $this->field->options() as $opt_value => $opt_label ) {

$options = array();
if ( $option_none = $this->field->args( 'show_option_none' ) ) {
$options[ '' ] = $option_none;
}
$options = $options + (array) $this->field->options();
foreach ( $options as $opt_value => $opt_label ) {

// Clone args & modify for just this item
$a = $args;
Expand Down
61 changes: 37 additions & 24 deletions tests/cmb-tests-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ public function tearDown() {
parent::tearDown();
}

public function normalize_string( $string ) {
return trim( preg_replace( array(
'/[\t\n\r]/', // Remove tabs and newlines
'/\s{2,}/', // Replace repeating spaces with one space
'/> </', // Remove spaces between carats
), array(
'',
' ',
'><',
), $string ) );
}

public function is_connected() {
$connected = @fsockopen( 'www.youtube.com', 80 );
if ( $connected ){
$is_conn = true;
fclose( $connected );
} else {
$is_conn = false; //action in connection failure
}

return $is_conn;
}

protected function capture_render( $cb ) {
ob_start();
call_user_func( $cb );
$output = ob_get_contents();
ob_end_clean();

return $output;
}

protected function render_field( $field ) {
return $this->capture_render( array( $field, 'render_field' ) );
}

public function assertHTMLstringsAreEqual( $expected_string, $string_to_test ) {
$expected_string = $this->normalize_string( $expected_string );
$string_to_test = $this->normalize_string( $string_to_test );
Expand Down Expand Up @@ -42,30 +79,6 @@ public function assertIsDefined( $definition ) {
return $this->assertTrue( defined( $definition ), "$definition is not defined." );
}

public function normalize_string( $string ) {
return trim( preg_replace( array(
'/[\t\n\r]/', // Remove tabs and newlines
'/\s{2,}/', // Replace repeating spaces with one space
'/> </', // Remove spaces between carats
), array(
'',
' ',
'><',
), $string ) );
}

public function is_connected() {
$connected = @fsockopen( 'www.youtube.com', 80 );
if ( $connected ){
$is_conn = true;
fclose( $connected );
} else {
$is_conn = false; //action in connection failure
}

return $is_conn;
}

/**
* Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2
*
Expand Down
50 changes: 50 additions & 0 deletions tests/test-cmb-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,56 @@ public function test_empty_field_with_empty_object_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 = new CMB2_Field( array(
'field_args' => $args,
) );

$this->assertFalse( $field->args( 'show_option_none' ) );

$this->assertHTMLstringsAreEqual(
'<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-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 = new CMB2_Field( array(
'field_args' => $args,
) );

$this->assertEquals( __( 'None', 'cmb2' ), $field->args( 'show_option_none' ) );

$this->assertHTMLstringsAreEqual(
'<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-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 = new CMB2_Field( array(
'field_args' => $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"><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 before_field_cb( $args ) {
echo 'before_field_cb_' . $args['id'];
}
Expand Down
13 changes: 0 additions & 13 deletions tests/test-cmb-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,6 @@ private function get_field_type_object( $args = '' ) {
return new CMB2_Types( $field );
}

private function capture_render( $cb ) {
ob_start();
call_user_func( $cb );
$output = ob_get_contents();
ob_end_clean();

return $output;
}

private function render_field( $field ) {
return $this->capture_render( array( $field, 'render_field' ) );
}

/**
* Test Callbacks
*/
Expand Down

0 comments on commit 833e2f2

Please sign in to comment.