|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit Tests for Automattic\Jetpack\Forms\Contact_Form. |
| 4 | + * |
| 5 | + * @package automattic/jetpack-forms |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Automattic\Jetpack\Forms\ContactForm; |
| 9 | + |
| 10 | +use WorDBless\BaseTestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test class for Contact_Form |
| 14 | + * |
| 15 | + * @covers Automattic\Jetpack\Forms\ContactForm\Contact_Form |
| 16 | + */ |
| 17 | +class WP_Test_Contact_Form_Field extends BaseTestCase { |
| 18 | + |
| 19 | + protected function setUp(): void { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + // Mock global variables |
| 23 | + global $user_identity; |
| 24 | + |
| 25 | + $user_id = wp_insert_user( |
| 26 | + array( |
| 27 | + 'user_login' => 'admin', |
| 28 | + 'user_pass' => 'pass', |
| 29 | + 'user_email' => '[email protected]', |
| 30 | + 'role' => 'reader', |
| 31 | + 'user_url' => 'https://example.com', |
| 32 | + ) |
| 33 | + ); |
| 34 | + |
| 35 | + // Simulate a logged-in user |
| 36 | + wp_set_current_user( $user_id ); |
| 37 | + $user_identity = 'Test User'; |
| 38 | + } |
| 39 | + |
| 40 | + protected function tearDown(): void { |
| 41 | + parent::tearDown(); |
| 42 | + global $current_user, $user_identity; |
| 43 | + |
| 44 | + // Clean up globals |
| 45 | + unset( $_POST, $_GET, $current_user, $user_identity ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Helper function to invoke the function from the class. |
| 50 | + */ |
| 51 | + private function invoke_get_computed_field_value( $field_type, $field_id ) { |
| 52 | + $field = $this->get_new_field_instance( |
| 53 | + array( |
| 54 | + 'type' => $field_type, |
| 55 | + 'id' => $field_id, |
| 56 | + ) |
| 57 | + ); |
| 58 | + return $field->get_computed_field_value( $field_type, $field_id ); |
| 59 | + } |
| 60 | + |
| 61 | + private function get_new_field_instance( $attributes ) { |
| 62 | + $defaults = array( |
| 63 | + 'type' => 'text', |
| 64 | + 'id' => 'id', |
| 65 | + 'default' => 'default', |
| 66 | + ); |
| 67 | + |
| 68 | + return new Contact_Form_Field( wp_parse_args( $attributes, $defaults ) ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test handling $_POST single value |
| 73 | + */ |
| 74 | + public function test_handles_post_single_value() { |
| 75 | + $_POST['test_field'] = 'Post Value'; |
| 76 | + |
| 77 | + $result = $this->invoke_get_computed_field_value( 'text', 'test_field' ); |
| 78 | + |
| 79 | + $this->assertEquals( 'Post Value', $result ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test handling $_POST array value |
| 84 | + */ |
| 85 | + public function test_handles_post_array_value() { |
| 86 | + $_POST['test_field'] = array( 'value1', 'value2' ); |
| 87 | + |
| 88 | + $result = $this->invoke_get_computed_field_value( 'text', 'test_field' ); |
| 89 | + |
| 90 | + $this->assertEquals( array( 'value1', 'value2' ), $result ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test handling $_GET single value |
| 95 | + */ |
| 96 | + public function test_handles_get_single_value() { |
| 97 | + $_GET['test_field'] = 'Get Value'; |
| 98 | + |
| 99 | + $result = $this->invoke_get_computed_field_value( 'text', 'test_field' ); |
| 100 | + |
| 101 | + $this->assertEquals( 'Get Value', $result ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test handling $_GET array value |
| 106 | + */ |
| 107 | + public function test_handles_get_array_value() { |
| 108 | + $_GET['test_field'] = array( 'value1', 'value2' ); |
| 109 | + |
| 110 | + $result = $this->invoke_get_computed_field_value( 'text', 'test_field' ); |
| 111 | + |
| 112 | + $this->assertEquals( array( 'value1', 'value2' ), $result ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Test logged-in user email return |
| 117 | + */ |
| 118 | + public function test_returns_logged_in_user_email() { |
| 119 | + add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 120 | + $result = $this->invoke_get_computed_field_value( 'email', 'test_field' ); |
| 121 | + remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 122 | + |
| 123 | + $this-> assertEquals( '[email protected]', $result ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Test logged-in user name return |
| 128 | + */ |
| 129 | + public function test_returns_logged_in_user_name() { |
| 130 | + add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 131 | + $result = $this->invoke_get_computed_field_value( 'name', 'test_field' ); |
| 132 | + remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 133 | + |
| 134 | + $this->assertEquals( 'Test User', $result ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test logged-in user URL return |
| 139 | + */ |
| 140 | + public function test_returns_logged_in_user_url() { |
| 141 | + add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 142 | + $result = $this->invoke_get_computed_field_value( 'url', 'test_field' ); |
| 143 | + remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 144 | + |
| 145 | + $this->assertEquals( 'https://example.com', $result ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test logged-in user URL return |
| 150 | + */ |
| 151 | + public function test_returns_logged_out_user_url() { |
| 152 | + global $current_user; |
| 153 | + unset( $current_user ); |
| 154 | + wp_set_current_user( 0 ); |
| 155 | + |
| 156 | + add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 157 | + $result = $this->invoke_get_computed_field_value( 'url', 'test_field' ); |
| 158 | + remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' ); |
| 159 | + |
| 160 | + $this->assertEquals( 'default', $result ); |
| 161 | + } |
| 162 | +} // end class |
0 commit comments