@@ -39,6 +39,134 @@ class Contact_Form_Test extends BaseTestCase {
3939
4040 private $ plugin ;
4141
42+ /**
43+ * Test that form submissions are stored in database when saveResponses is 'yes' (default)
44+ */
45+ public function test_process_submission_stores_feedback_when_save_responses_yes () {
46+ // Fill field values
47+ $ this ->add_field_values (
48+ array (
49+ 'name ' => 'John Doe ' ,
50+ 51+ 'message ' => 'Test message ' ,
52+ )
53+ );
54+
55+ // Create form with saveResponses explicitly set to 'yes'
56+ $ form = new Contact_Form (
57+ array (
58+ 'saveResponses ' => 'yes ' ,
59+ ),
60+ "[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Message' type='textarea' required='1'/] "
61+ );
62+
63+ // Get initial post count
64+ $ initial_posts = Posts::init ()->posts ;
65+ $ initial_count = count ( $ initial_posts );
66+
67+ // Process the submission
68+ $ result = $ form ->process_submission ();
69+
70+ // Processing should be successful
71+ $ this ->assertTrue ( is_string ( $ result ), 'Form submission should be successful ' );
72+
73+ // Check that a new feedback post was created
74+ $ final_posts = Posts::init ()->posts ;
75+ $ final_count = count ( $ final_posts );
76+ $ this ->assertEquals ( $ initial_count + 1 , $ final_count , 'A new feedback post should be created when saveResponses is yes ' );
77+
78+ // Verify the feedback post was created with correct type
79+ $ feedback_id = end ( $ final_posts )->ID ;
80+ $ submission = get_post ( $ feedback_id );
81+ $ this ->assertEquals ( 'feedback ' , $ submission ->post_type , 'Post type should be feedback ' );
82+
83+ // Verify the form attribute is correctly set
84+ $ this ->assertEquals ( 'yes ' , $ form ->get_attribute ( 'saveResponses ' ), 'Form should have saveResponses set to yes ' );
85+ }
86+
87+ /**
88+ * Test that form submissions are NOT stored in database when saveResponses is 'no'
89+ */
90+ public function test_process_submission_does_not_store_feedback_when_save_responses_no () {
91+ // Fill field values
92+ $ this ->add_field_values (
93+ array (
94+ 'name ' => 'Jane Doe ' ,
95+ 96+ 'message ' => 'Test message for no save ' ,
97+ )
98+ );
99+
100+ // Create form with saveResponses set to 'no'
101+ $ form = new Contact_Form (
102+ array (
103+ 'saveResponses ' => 'no ' ,
104+ ),
105+ "[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Message' type='textarea' required='1'/] "
106+ );
107+
108+ // Get initial post count
109+ $ initial_posts = Posts::init ()->posts ;
110+ $ initial_count = count ( $ initial_posts );
111+
112+ // Process the submission
113+ $ result = $ form ->process_submission ();
114+
115+ // Processing should still be successful (email should still be sent)
116+ $ this ->assertTrue ( is_string ( $ result ), 'Form submission should be successful even when not saving responses ' );
117+
118+ // Check that NO new feedback post was created
119+ $ final_posts = Posts::init ()->posts ;
120+ $ final_count = count ( $ final_posts );
121+ $ this ->assertEquals ( $ initial_count , $ final_count , 'No new feedback post should be created when saveResponses is no ' );
122+
123+ // Verify the form attribute is correctly set
124+ $ this ->assertEquals ( 'no ' , $ form ->get_attribute ( 'saveResponses ' ), 'Form should have saveResponses set to no ' );
125+ }
126+
127+ /**
128+ * Test that form submissions are stored in database when saveResponses is not specified (defaults to 'yes')
129+ */
130+ public function test_process_submission_stores_feedback_when_save_responses_default () {
131+ // Fill field values
132+ $ this ->add_field_values (
133+ array (
134+ 'name ' => 'Default User ' ,
135+ 136+ 'message ' => 'Test message for default behavior ' ,
137+ )
138+ );
139+
140+ // Create form without specifying saveResponses (should default to 'yes')
141+ $ form = new Contact_Form (
142+ array (),
143+ "[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Message' type='textarea' required='1'/] "
144+ );
145+
146+ // Get initial post count
147+ $ initial_posts = Posts::init ()->posts ;
148+ $ initial_count = count ( $ initial_posts );
149+
150+ // Process the submission
151+ $ result = $ form ->process_submission ();
152+
153+ // Processing should be successful
154+ $ this ->assertTrue ( is_string ( $ result ), 'Form submission should be successful ' );
155+
156+ // Check that a new feedback post was created (default behavior)
157+ $ final_posts = Posts::init ()->posts ;
158+ $ final_count = count ( $ final_posts );
159+ $ this ->assertEquals ( $ initial_count + 1 , $ final_count , 'A new feedback post should be created by default ' );
160+
161+ // Verify the feedback post was created with correct type
162+ $ feedback_id = end ( $ final_posts )->ID ;
163+ $ submission = get_post ( $ feedback_id );
164+ $ this ->assertEquals ( 'feedback ' , $ submission ->post_type , 'Post type should be feedback ' );
165+
166+ // Verify the form attribute defaults to 'yes'
167+ $ this ->assertEquals ( 'yes ' , $ form ->get_attribute ( 'saveResponses ' ), 'Form should default saveResponses to yes ' );
168+ }
169+
42170 /**
43171 * Test the esc_shortcode_val method with various input types
44172 */
0 commit comments