Skip to content

Commit

Permalink
Input and Select phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtashjian committed Aug 31, 2023
1 parent 92c6988 commit 78637a4
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
16 changes: 11 additions & 5 deletions includes/BlockLibrary/Blocks/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* The Input block class.
*/
class Input extends BaseControlBlock {
const FORMAT_DATE = 'Y-m-d';
const FORMAT_TIME = 'h:i:00';
const FORMAT_MONTH = 'Y-m';
const FORMAT_WEEK = 'Y-\WW';
const FORMAT_DATETIME_LOCAL = 'Y-m-d H:i:00';

/**
* Renders the form control.
*
Expand Down Expand Up @@ -61,15 +67,15 @@ public function get_control_value() {
return $this->get_field_label();
// Date and time inputs need a default vaule to display properly on iOS.
case 'date':
return gmdate( 'Y-m-d' );
return gmdate( self::FORMAT_DATE );
case 'time':
return gmdate( 'h:i:00' );
return gmdate( self::FORMAT_TIME );
case 'month':
return gmdate( 'Y-m' );
return gmdate( self::FORMAT_MONTH );
case 'week':
return gmdate( 'Y-\WW' );
return gmdate( self::FORMAT_WEEK );
case 'datetime-local':
return gmdate( 'Y-m-d H:i:00' );
return gmdate( self::FORMAT_DATETIME_LOCAL );
default:
return '';
}
Expand Down
76 changes: 76 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
* Tests the Input class.
*/
class InputTest extends FormBlockTestCase {
/**
* The block instance to test against.
*
* @var \OmniForm\BlockLibrary\Blocks\Input
*/
protected $block_instance;

/**
* Register the block to test against.
*/
Expand All @@ -29,6 +36,75 @@ public function test_does_not_render_without_field_label() {
$this->apply_block_context( 'omniform/fieldLabel', 'field label' );
$this->assertNotEmpty( $this->render_block_with_attributes() );
}

/**
* Default values should be returned unless the block has a fieldValue attribute.
*/
public function test_get_control_value() {
$field_label = 'field label';
$field_value = 'field value';

$this->apply_block_context( 'omniform/fieldLabel', $field_label );

foreach ( array( 'checkbox', 'radio' ) as $field_type ) {
$this->render_block_with_attributes( array( 'fieldType' => $field_type ) );
$this->assertEquals( $this->block_instance->get_control_value(), $field_label );

$this->render_block_with_attributes(
array(
'fieldType' => $field_type,
'fieldValue' => $field_value,
)
);
$this->assertEquals( $this->block_instance->get_control_value(), $field_value );
}

$this->render_block_with_attributes( array( 'fieldType' => 'date' ) );
$this->assertEquals( $this->block_instance->get_control_value(), gmdate( $this->block_instance::FORMAT_DATE ) );

$this->render_block_with_attributes( array( 'fieldType' => 'time' ) );
$this->assertEquals( $this->block_instance->get_control_value(), gmdate( $this->block_instance::FORMAT_TIME ) );

$this->render_block_with_attributes( array( 'fieldType' => 'month' ) );
$this->assertEquals( $this->block_instance->get_control_value(), gmdate( $this->block_instance::FORMAT_MONTH ) );

$this->render_block_with_attributes( array( 'fieldType' => 'week' ) );
$this->assertEquals( $this->block_instance->get_control_value(), gmdate( $this->block_instance::FORMAT_WEEK ) );

$this->render_block_with_attributes( array( 'fieldType' => 'datetime-local' ) );
$this->assertEquals( $this->block_instance->get_control_value(), gmdate( $this->block_instance::FORMAT_DATETIME_LOCAL ) );
}

/**
* Ensure fieldLabel and fieldGroupLabel are returned in the control name parts.
* Checkbox and radio fields should not return fieldLabel when fieldGroupLabel is present.
*/
public function test_get_control_name_parts() {
$this->apply_block_context( 'omniform/fieldLabel', 'field label' );

$this->render_block_with_attributes( array( 'fieldType' => 'text' ) );
$this->assertTrue( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );

$this->render_block_with_attributes( array( 'fieldType' => 'checkbox' ) );
$this->assertTrue( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );

$this->render_block_with_attributes( array( 'fieldType' => 'radio' ) );
$this->assertTrue( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );

$this->apply_block_context( 'omniform/fieldGroupLabel', 'field group label' );

$this->render_block_with_attributes( array( 'fieldType' => 'text' ) );
$this->assertTrue( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );
$this->assertTrue( in_array( 'field-group-label', $this->block_instance->get_control_name_parts(), true ) );

$this->render_block_with_attributes( array( 'fieldType' => 'checkbox' ) );
$this->assertFalse( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );
$this->assertTrue( in_array( 'field-group-label', $this->block_instance->get_control_name_parts(), true ) );

$this->render_block_with_attributes( array( 'fieldType' => 'radio' ) );
$this->assertFalse( in_array( 'field-label', $this->block_instance->get_control_name_parts(), true ) );
$this->assertTrue( in_array( 'field-group-label', $this->block_instance->get_control_name_parts(), true ) );
}
}

// phpcs:disable
Expand Down
30 changes: 30 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
* Tests the Select class.
*/
class SelectTest extends FormBlockTestCase {
/**
* The block instance to test against.
*
* @var \OmniForm\BlockLibrary\Blocks\Select
*/
protected $block_instance;

/**
* Register the block to test against.
*/
Expand All @@ -29,6 +36,29 @@ public function test_does_not_render_without_field_label() {
$this->apply_block_context( 'omniform/fieldLabel', 'field label' );
$this->assertNotEmpty( $this->render_block_with_attributes() );
}

/**
* Test rendering the select block with a placeholder.
*/
public function test_render_with_placeholder() {
$this->apply_block_context( 'omniform/fieldLabel', 'field label' );

$this->assertFalse( strpos( $this->render_block_with_attributes(), '<option value="">' ) );
$this->assertNotFalse( strpos( $this->render_block_with_attributes( array( 'fieldPlaceholder' => 'field placeholder' ) ), '<option value="">field placeholder</option>' ) );
}

/**
* Test the get_control_name method of the Select block.
*/
public function test_get_control_name() {
$this->apply_block_context( 'omniform/fieldLabel', 'field label' );

$this->render_block_with_attributes();
$this->assertEquals( $this->block_instance->get_control_name(), 'field-label' );

$this->render_block_with_attributes( array( 'isMultiple' => true ) );
$this->assertEquals( $this->block_instance->get_control_name(), 'field-label[]' );
}
}

// phpcs:disable
Expand Down

0 comments on commit 78637a4

Please sign in to comment.