|
| 1 | +<?php |
| 2 | +require __DIR__ . '/../../../modules/contact-form/grunion-contact-form.php'; |
| 3 | + |
| 4 | +class WP_Test_Grunion_Contact_Form extends WP_UnitTestCase { |
| 5 | + |
| 6 | + public static function setUpBeforeClass() { |
| 7 | + parent::setUpBeforeClass(); |
| 8 | + define( 'DOING_AJAX', true ); // Defined so that 'exit' is not called in process_submission |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Inserts globals needed to process contact form submits |
| 13 | + */ |
| 14 | + private function set_globals() { |
| 15 | + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
| 16 | + $_SERVER['HTTP_USER_AGENT'] = 'unit-test'; |
| 17 | + $_SERVER['HTTP_REFERER'] = 'test'; |
| 18 | + } |
| 19 | + |
| 20 | + public function setUp() { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->set_globals(); |
| 24 | + |
| 25 | + $author_id = $this->factory->user->create( array( |
| 26 | + 'user_email' => '[email protected]' |
| 27 | + ) ); |
| 28 | + |
| 29 | + $post_id = $this->factory->post->create( array( |
| 30 | + 'post_status' => 'draft', |
| 31 | + 'post_author' => strval( $author_id ) |
| 32 | + ) ); |
| 33 | + |
| 34 | + global $post; |
| 35 | + $post = get_post( $post_id ); |
| 36 | + |
| 37 | + // Place post_id to contact form id to make the form processable |
| 38 | + $_POST['contact-form-id'] = $post_id; |
| 39 | + |
| 40 | + // Make the global post (used by contact forms) accessbile to tests |
| 41 | + $this->post = $post; |
| 42 | + } |
| 43 | + |
| 44 | + private function add_field_values( $values ) { |
| 45 | + foreach( $values as $key => $val ) { |
| 46 | + $_POST['g' . $this->post->ID . '-' . $key] = $val; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @author tonykova |
| 52 | + * @covers Grunion_Contact_Form::process_submission |
| 53 | + * |
| 54 | + * Tests that the submission as a whole will produce something in the |
| 55 | + * database when required information is provided |
| 56 | + */ |
| 57 | + public function test_process_submission_will_store_a_feedback_correctly_with_default_form() { |
| 58 | + $form = new Grunion_Contact_Form( array() ); |
| 59 | + $result = $form->process_submission(); |
| 60 | + |
| 61 | + // Processing should be successful and produce the success message |
| 62 | + $this->assertTrue( is_string( $result ) ); |
| 63 | + |
| 64 | + $feedback = get_posts( array( 'post_type' => 'feedback' ) ); |
| 65 | + $this->assertEquals( 1, count( $feedback ), 'There should be one feedback after process_submission' ); |
| 66 | + |
| 67 | + // Default metadata should be saved |
| 68 | + $submission = $feedback[0]; |
| 69 | + $email = get_post_meta( $submission->ID, '_feedback_email', true ); |
| 70 | + $this-> assertEquals( '[email protected]', $email[ 'to'][ 0] ); |
| 71 | + $this->assertContains( 'IP Address: 127.0.0.1', $email['message'] ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @author tonykova |
| 76 | + * @covers Grunion_Contact_Form::process_submission |
| 77 | + * |
| 78 | + * Tests that the submission as a whole will produce something in the |
| 79 | + * database when some labels are provided |
| 80 | + */ |
| 81 | + public function test_process_submission_will_store_extra_field_metadata() { |
| 82 | + // Fill field values |
| 83 | + $this->add_field_values( array( |
| 84 | + 'name' => 'John Doe', |
| 85 | + 'dropdown' => 'First option', |
| 86 | + 'radio' =>'Second option', |
| 87 | + 'text' =>'Texty text' |
| 88 | + ) ); |
| 89 | + |
| 90 | + // Initialize a form with name, dropdown and radiobutton (first, second |
| 91 | + // and third option), text field |
| 92 | + $form = new Grunion_Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" ); |
| 93 | + $result = $form->process_submission(); |
| 94 | + |
| 95 | + // Processing should be successful and produce the success message |
| 96 | + $this->assertTrue( is_string( $result ) ); |
| 97 | + |
| 98 | + $feedback = get_posts( array( 'post_type' => 'feedback' ) ); |
| 99 | + $this->assertEquals( 1, count( $feedback ), 'There should be one feedback after process_submission' ); |
| 100 | + |
| 101 | + // Default metadata should be saved |
| 102 | + $submission = $feedback[0]; |
| 103 | + $extra_fields = get_post_meta( $submission->ID, '_feedback_extra_fields', true ); |
| 104 | + |
| 105 | + $this->assertEquals( 3, count( $extra_fields ), 'There should be exactly three extra fields when one of the fields is name, and the others are an extra dropdown, radio button field and text field' ); |
| 106 | + $this->assertEquals( $extra_fields['Dropdown'], 'First option', 'When the first option of a dropdown field with label Dropdown is selected, there should be metadata with that key and value' ); |
| 107 | + $this->assertEquals( $extra_fields['Radio'], 'Second option', 'When the first option of a radio button field with label Radio is selected, there should be metadata with that key and value' ); |
| 108 | + $this->assertEquals( $extra_fields['Text'], 'Texty text', 'When the text field with label Text is filled with the text \'Texty text\', there should be metadata with that key and value' ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @author tonykova |
| 113 | + * @covers Grunion_Contact_Form::process_submission |
| 114 | + * |
| 115 | + * Tests that the submission will store the subject when specified |
| 116 | + */ |
| 117 | + public function test_process_submission_will_store_subject_when_specified() { |
| 118 | + // Initialize a form with name, dropdown and radiobutton (first, second |
| 119 | + // and third option), text field |
| 120 | + $form = new Grunion_Contact_Form( array( 'subject' => 'I\'m sorry, but the party\'s over') ); // Default form |
| 121 | + $result = $form->process_submission(); |
| 122 | + |
| 123 | + // Processing should be successful and produce the success message |
| 124 | + $this->assertTrue( is_string( $result ) ); |
| 125 | + |
| 126 | + $feedback = get_posts( array( 'post_type' => 'feedback' ) ); |
| 127 | + $this->assertEquals( 1, count( $feedback ), 'There should be one feedback after process_submission' ); |
| 128 | + |
| 129 | + // Default metadata should be saved |
| 130 | + $submission = $feedback[0]; |
| 131 | + |
| 132 | + $this->assertContains( 'SUBJECT: I\'m sorry, but the party\'s over', $submission->post_content, 'The stored subject didn\'t match the given' ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @author tonykova |
| 137 | + * @covers Grunion_Contact_Form::process_submission |
| 138 | + */ |
| 139 | + public function test_process_submission_will_store_fields_and_their_values_to_post_content() { |
| 140 | + // Fill field values |
| 141 | + $this->add_field_values( array( |
| 142 | + 'name' => 'John Doe', |
| 143 | + 'dropdown' => 'First option', |
| 144 | + 'radio' =>'Second option', |
| 145 | + 'text' =>'Texty text' |
| 146 | + ) ); |
| 147 | + |
| 148 | + // Initialize a form with name, dropdown and radiobutton (first, second |
| 149 | + // and third option), text field |
| 150 | + $form = new Grunion_Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" ); |
| 151 | + $result = $form->process_submission(); |
| 152 | + |
| 153 | + // Processing should be successful and produce the success message |
| 154 | + $this->assertTrue( is_string( $result ) ); |
| 155 | + |
| 156 | + $feedback = get_posts( array( 'post_type' => 'feedback' ) ); |
| 157 | + $this->assertEquals( 1, count( $feedback ), 'There should be one feedback after process_submission' ); |
| 158 | + |
| 159 | + // Default metadata should be saved |
| 160 | + $submission = $feedback[0]; |
| 161 | + |
| 162 | + $this->assertContains( '[Name] => John Doe', $submission->post_content, 'Post content did not contain the name label and/or value' ); |
| 163 | + $this->assertContains( '[Dropdown] => First option', $submission->post_content, 'Post content did not contain the dropdown label and/or value' ); |
| 164 | + $this->assertContains( '[Radio] => Second option', $submission->post_content, 'Post content did not contain the radio button label and/or value' ); |
| 165 | + $this->assertContains( '[Text] => Texty text', $submission->post_content, 'Post content did not contain the text field label and/or value' ); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @author tonykova |
| 170 | + * @covers Grunion_Contact_Form::process_submission |
| 171 | + */ |
| 172 | + public function test_process_submission_will_store_fields_and_their_values_to_email_meta() { |
| 173 | + // Fill field values |
| 174 | + $this->add_field_values( array( |
| 175 | + 'name' => 'John Doe', |
| 176 | + 'dropdown' => 'First option', |
| 177 | + 'radio' =>'Second option', |
| 178 | + 'text' =>'Texty text' |
| 179 | + ) ); |
| 180 | + |
| 181 | + // Initialize a form with name, dropdown and radiobutton (first, second |
| 182 | + // and third option), text field |
| 183 | + $form = new Grunion_Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" ); |
| 184 | + $result = $form->process_submission(); |
| 185 | + |
| 186 | + // Processing should be successful and produce the success message |
| 187 | + $this->assertTrue( is_string( $result ) ); |
| 188 | + |
| 189 | + $feedback = get_posts( array( 'post_type' => 'feedback' ) ); |
| 190 | + $this->assertEquals( 1, count( $feedback ), 'There should be one feedback after process_submission' ); |
| 191 | + |
| 192 | + // Default metadata should be saved |
| 193 | + $submission = $feedback[0]; |
| 194 | + $email = get_post_meta( $submission->ID, '_feedback_email', true ); |
| 195 | + |
| 196 | + $expected = 'Name: John Doe' . PHP_EOL; |
| 197 | + $expected .= 'Dropdown: First option' . PHP_EOL; |
| 198 | + $expected .= 'Radio: Second option' . PHP_EOL; |
| 199 | + $expected .= 'Text: Texty text'; |
| 200 | + |
| 201 | + $email_body = explode( PHP_EOL . PHP_EOL, $email['message'] )[0]; |
| 202 | + |
| 203 | + $this->assertEquals( $expected, $email_body ); |
| 204 | + } |
| 205 | + |
| 206 | + public function test_process_submission_sends_correct_email() { |
| 207 | + // Fill field values |
| 208 | + $this->add_field_values( array( |
| 209 | + 'name' => 'John Doe', |
| 210 | + 'dropdown' => 'First option', |
| 211 | + 'radio' =>'Second option', |
| 212 | + 'text' =>'Texty text' |
| 213 | + ) ); |
| 214 | + |
| 215 | + add_filter( 'wp_mail', function( $args ) { |
| 216 | + $this-> assertContains( '[email protected]', $args[ 'to'] ); |
| 217 | + $this->assertEquals( 'Hello there!', $args['subject'] ); |
| 218 | + |
| 219 | + $expected = 'Name: John Doe' . PHP_EOL; |
| 220 | + $expected .= 'Dropdown: First option' . PHP_EOL; |
| 221 | + $expected .= 'Radio: Second option' . PHP_EOL; |
| 222 | + $expected .= 'Text: Texty text'; |
| 223 | + |
| 224 | + $email_body = explode( PHP_EOL . PHP_EOL, $args['message'] )[0]; |
| 225 | + |
| 226 | + $this->assertEquals( $expected, $email_body ); |
| 227 | + } ); |
| 228 | + |
| 229 | + // Initialize a form with name, dropdown and radiobutton (first, second |
| 230 | + // and third option), text field |
| 231 | + $form = new Grunion_Contact_Form( array( 'to' => '[email protected]', 'subject' => 'Hello there!' ), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" ); |
| 232 | + $form->process_submission(); |
| 233 | + } |
| 234 | +} // end class |
0 commit comments