diff --git a/tests/testField.php b/tests/testField.php index 6a0905ad..fc5be6a0 100644 --- a/tests/testField.php +++ b/tests/testField.php @@ -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 ); } @@ -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(); + } + }