Skip to content

Commit

Permalink
Merge pull request #375 from humanmade/unit-test-repeateble-fields
Browse files Browse the repository at this point in the history
Strengthening Base Field Unit Tests
  • Loading branch information
mikeselander authored Nov 30, 2016
2 parents 06953e6 + e350549 commit 6d4201f
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions tests/testField.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,51 @@ function testGetValues() {
$field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) );
$this->assertEquals( $field->get_values(), array( 1 ) );

// // Multiple Values - eg repeatable.
// Single, saved value.
$field_value = array( 'one' );
$field->save( $this->post->ID, $field_value );
$this->assertEquals( $field->get_values(), $field_value );

// Multiple Values - eg repeatable.
$field = new CMB_Text_Field( 'foo', 'Title', array( 1, 2 ), array( 'repeatable' => true ) );
$this->assertEquals( $field->get_values(), array( 1, 2 ) );

// Multiple, saved values.
$repeat_value = array( 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero' );
$field->save( $this->post->ID, $repeat_value );
$this->assertEquals( $field->get_values(), $repeat_value );

}

function testSaveValues() {

$field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) );
$field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) );
$field_value = array( 'one' );

if ( ! $this->post ) {
$this->markTestSkipped( 'Post not found' );
}

$field->save( $this->post->ID, array( 1 ) );

$meta = get_post_meta( $this->post->ID, 'foo', false );
$field->save( $this->post->ID, $field_value );

$this->assertEquals( $meta, array( 1 ) );
// Verify single value is properly saved.
$this->assertEquals( get_post_meta( $this->post->ID, 'foo', false ), $field_value );

}

function testSaveValuesOnRepeatable() {

$field = new CMB_Text_Field( 'foo', 'Title', array( 1, 2 ), array( 'repeatable' => true ) );
$field = new CMB_Text_Field( 'foo', 'Title', array( 1, 2 ), array( 'repeatable' => true ) );
$repeat_value = array( 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero' );

if ( ! $this->post ) {
$this->markTestSkipped( 'Post not found' );
}

$field->save( $this->post->ID, array( 1, 2 ) );

$meta = get_post_meta( $this->post->ID, 'foo', false );
$field->save( $this->post->ID, $repeat_value );

$this->assertEquals( $meta, array( 1, 2 ) );
// Test that the repeatable field is saved properly.
$this->assertEquals( get_post_meta( $this->post->ID, 'foo', false ), $repeat_value );

}

Expand Down Expand Up @@ -121,4 +131,36 @@ function testNameAttrValue() {
$this->assertEquals( $id_attr, 'foo[cmb-field-12]' );

}

function testEmptyFieldOutput() {
$field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) );

if ( ! $this->post ) {
$this->markTestSkipped( 'Post not found' );
}

// Test empty output
$this->expectOutputRegex( '/(type=\"text\".*?id=\"foo-cmb-field-0\".*?value=\"1\")/s' );

// Trigger output.
$field->html();

}

function testSavedFieldOutput() {
$field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) );
$field_value = array( 'one' );

if ( ! $this->post ) {
$this->markTestSkipped( 'Post not found' );
}

$field->save( $this->post->ID, $field_value );

$this->expectOutputRegex( '/(type=\"text\".*?id=\"foo-cmb-field-0\".*?value=\"one\")/s' );

// Trigger output.
$field->html();
}

}

0 comments on commit 6d4201f

Please sign in to comment.