Skip to content
Merged
8 changes: 4 additions & 4 deletions classes/helpers/FrmEntriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class FrmEntriesHelper {
public static function setup_new_vars( $fields, $form = '', $reset = false, $args = array() ) {
remove_action( 'media_buttons', 'FrmFormsController::insert_form_button' );

$values = array(
$posted_key = FrmAppHelper::get_post_param( 'item_key', '', 'sanitize_title' );
$values = array(
'name' => '',
'description' => '',
'item_key' => '',
'item_key' => ! $reset && is_string( $posted_key ) ? $posted_key : '',
'fields' => array(),
);

$values['fields'] = array();

if ( ! $fields ) {
return apply_filters( 'frm_setup_new_entry', $values );
}
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/entries/test_FrmEntriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#[\PHPUnit\Framework\Attributes\CoversClass( FrmEntriesHelper::class )]
class test_FrmEntriesHelper extends FrmUnitTest {

public function tearDown(): void {
unset( $_POST['item_key'] );
parent::tearDown();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined static method FrmUnitTest::tearDown()


Invalid call to a static method. This would lead to a run time error.

}

public function test_display_value() {
$value = serialize( array( 'Option 1', 'Option 2' ) );
$field = $this->factory->field->create_and_get(
Expand All @@ -22,4 +27,33 @@ public function test_display_value() {
$this->assertIsString( $display_value );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertIsString()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertSame( 'Option 1, Option 2', $display_value );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

}

/**
* The Entry Key typed on one page of a multi-page form must survive to the next page.
*/
public function test_setup_new_vars_keeps_posted_item_key() {
$form_id = $this->factory->form->create();
$this->factory->field->create(
array(
'form_id' => $form_id,
'type' => 'text',
)
);
$fields = FrmField::get_all_for_form( $form_id );
$_POST['item_key'] = 'My Typed Key';
$values = FrmEntriesHelper::setup_new_vars( $fields );
$this->assertSame( 'my-typed-key', $values['item_key'] );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.


$values = FrmEntriesHelper::setup_new_vars( $fields, '', true );
$this->assertSame( '', $values['item_key'], 'A reset form must not carry the posted key.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: unset( $_POST['item_key'] ) only runs if both earlier assertSame calls pass. A failing assertion leaves $_POST['item_key'] set for later tests in the same process. Move the cleanup into tear_down() (unset( $_POST['item_key'] );) or wrap the assertions in try/finally.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 609ba61. The cleanup moved to tearDown().

$_POST['item_key'] = array( 'x' );
$values = FrmEntriesHelper::setup_new_vars( $fields );
$this->assertSame( '', $values['item_key'], 'An array posted as the key must not reach the view.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.


unset( $_POST['item_key'] );

$values = FrmEntriesHelper::setup_new_vars( $fields );
$this->assertSame( '', $values['item_key'] );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmEntriesHelper::assertSame()


The method you are trying to call is not defined, which can result in a fatal error.

}
}
Loading