From 2f42b0d196c71fd7888445ca3bb8896533c67c4b Mon Sep 17 00:00:00 2001 From: Miguel Lezama Date: Mon, 13 Jan 2025 13:02:07 -0300 Subject: [PATCH] add test --- .../contact-form/test-class.contact-form.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/projects/packages/forms/tests/php/contact-form/test-class.contact-form.php b/projects/packages/forms/tests/php/contact-form/test-class.contact-form.php index 51e65b7ebbbb9..6186698128a65 100644 --- a/projects/packages/forms/tests/php/contact-form/test-class.contact-form.php +++ b/projects/packages/forms/tests/php/contact-form/test-class.contact-form.php @@ -108,11 +108,17 @@ public function tear_down() { /** * Adds the field values to the global $_POST value. * - * @param array $values Array of field key value pairs. + * @param array $values Array of form fields and values. + * @param string $form_id Optional form ID. If not provided, will use $this->post->ID. */ - private function add_field_values( $values ) { + private function add_field_values( $values, $form_id = null ) { + $prefix = $form_id ? $form_id : 'g' . $this->post->ID; foreach ( $values as $key => $val ) { - $_POST[ 'g' . $this->post->ID . '-' . $key ] = $val; + if ( strpos( $key, 'contact-form' ) === 0 || strpos( $key, 'action' ) === 0 ) { + $_POST[ $key ] = $val; + } else { + $_POST[ $prefix . '-' . $key ] = $val; + } } } @@ -2072,4 +2078,28 @@ public function test_grunion_contact_form_apply_block_attribute() { Util::grunion_contact_form_apply_block_attribute( $original, array( 'foo' => 'bar' ) ) ); } + + /** + * Tests that multiple instances of the same form work correctly with unique IDs. + */ + public function test_multiple_form_instances_with_unique_ids() { + // Create two instances of the same form + $form1 = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Message' type='textarea' required='1'/]" ); + $form2 = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Message' type='textarea' required='1'/]" ); + + // Verify that the forms have different IDs + $this->assertNotEquals( $form1->get_attribute( 'id' ), $form2->get_attribute( 'id' ), 'Forms should have unique IDs' ); + + // Submit first form + $this->add_field_values( + array( + 'name' => 'First form name', + 'message' => 'First form message', + ), + $form1->get_attribute( 'id' ) + ); + $result1 = $form1->process_submission(); + $this->assertTrue( is_string( $result1 ), 'First form submission should be successful' ); + // TODO: Test that the both froms submissions are saved with the correct information. + } } // end class