From 759c0294a5f788657902d02d20b2b43ee81fb05b Mon Sep 17 00:00:00 2001 From: mikeselander Date: Mon, 28 Nov 2016 14:51:47 +0100 Subject: [PATCH 1/2] Added several new tests to verify acrruate output and that saved values port through correctly --- tests/testDateField.php | 4 +-- tests/testField.php | 63 ++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/tests/testDateField.php b/tests/testDateField.php index 06904e96..12568df6 100644 --- a/tests/testDateField.php +++ b/tests/testDateField.php @@ -35,8 +35,8 @@ function testDateFieldAssets() { $this->assertContains( '/js/field.datetime.js', $scripts_output ); $this->assertContains( '/js/cmb.js', $scripts_output ); if ( version_compare( $wp_version, '4.1', '>=' ) ) { - $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/core.min.js', $scripts_output ); - $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/datepicker.min.js', $scripts_output ); + $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/core.js', $scripts_output ); + $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/datepicker.js', $scripts_output ); } else { $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/jquery.ui.core.min.js', $scripts_output ); $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/jquery.ui.datepicker.min.js', $scripts_output ); diff --git a/tests/testField.php b/tests/testField.php index 78b0b493..56b7c990 100644 --- a/tests/testField.php +++ b/tests/testField.php @@ -34,39 +34,49 @@ 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 ) ); + $field->save( $this->post->ID, $field_value ); - $meta = get_post_meta( $this->post->ID, 'foo', false ); - - $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 ); } @@ -120,4 +130,35 @@ function testNameAttrValue() { } + function testEmptyFieldOutput() { + $field = new CMB_Text_Field( 'foo', 'Title', array( 1 ) ); + + if ( ! $this->post ) { + $this->markTestSkipped( 'Post not found' ); + } + + // Test empty output + $this->expectOutputRegex( '/(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( '/(value=\"one\")/s' ); + + // Trigger output. + $field->html(); + } + } \ No newline at end of file From 7c8853c2f9a433dd91f258b72441ad429cc93384 Mon Sep 17 00:00:00 2001 From: mikeselander Date: Mon, 28 Nov 2016 15:48:46 +0100 Subject: [PATCH 2/2] Fixed js tests and made output test more robust --- tests/testDateField.php | 4 ++-- tests/testField.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testDateField.php b/tests/testDateField.php index 12568df6..06904e96 100644 --- a/tests/testDateField.php +++ b/tests/testDateField.php @@ -35,8 +35,8 @@ function testDateFieldAssets() { $this->assertContains( '/js/field.datetime.js', $scripts_output ); $this->assertContains( '/js/cmb.js', $scripts_output ); if ( version_compare( $wp_version, '4.1', '>=' ) ) { - $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/core.js', $scripts_output ); - $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/datepicker.js', $scripts_output ); + $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/core.min.js', $scripts_output ); + $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/datepicker.min.js', $scripts_output ); } else { $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/jquery.ui.core.min.js', $scripts_output ); $this->assertContains( site_url() . '/wp-includes/js/jquery/ui/jquery.ui.datepicker.min.js', $scripts_output ); diff --git a/tests/testField.php b/tests/testField.php index 56b7c990..d44feb1a 100644 --- a/tests/testField.php +++ b/tests/testField.php @@ -138,7 +138,7 @@ function testEmptyFieldOutput() { } // Test empty output - $this->expectOutputRegex( '/(value=\"1\")/s' ); + $this->expectOutputRegex( '/(type=\"text\".*?id=\"foo-cmb-field-0\".*?value=\"1\")/s' ); // Trigger output. $field->html(); @@ -155,7 +155,7 @@ function testSavedFieldOutput() { $field->save( $this->post->ID, $field_value ); - $this->expectOutputRegex( '/(value=\"one\")/s' ); + $this->expectOutputRegex( '/(type=\"text\".*?id=\"foo-cmb-field-0\".*?value=\"one\")/s' ); // Trigger output. $field->html();