|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for Phone field format properties. |
| 4 | + * |
| 5 | + * @package WPGraphQL\GF\Tests\WPUnit |
| 6 | + */ |
| 7 | + |
| 8 | +use Tests\WPGraphQL\GF\TestCase\GFGraphQLTestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class PhoneFieldFormatTest |
| 12 | + */ |
| 13 | +class PhoneFieldFormatTest extends GFGraphQLTestCase { |
| 14 | + /** |
| 15 | + * The ID of the form created for testing. |
| 16 | + * |
| 17 | + * @var int |
| 18 | + */ |
| 19 | + private $form_id; |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritDoc} |
| 23 | + */ |
| 24 | + public function setUp(): void { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + wp_set_current_user( $this->admin->ID ); |
| 28 | + $this->clearSchema(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + public function tearDown(): void { |
| 35 | + $this->factory->form->delete( $this->form_id ); |
| 36 | + |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Helper method to create a form with a phone field with the given properties. |
| 42 | + * |
| 43 | + * @param array $props The properties for the phone field. |
| 44 | + * |
| 45 | + * @return int The ID of the created form. |
| 46 | + */ |
| 47 | + private function create_phone_form( array $props ): int { |
| 48 | + $field_helper = $this->tester->getPropertyHelper( 'PhoneField', $props ); |
| 49 | + $field = $this->factory->field->create( $field_helper->values ); |
| 50 | + |
| 51 | + return $this->factory->form->create( |
| 52 | + array_merge( |
| 53 | + [ 'fields' => [ $field ] ], |
| 54 | + $this->tester->getFormDefaultArgs() |
| 55 | + ) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test that the standard phone format returns all properties. |
| 61 | + */ |
| 62 | + public function testStandardPhoneFormat(): void { |
| 63 | + $this->form_id = $this->create_phone_form( [ 'phoneFormat' => 'standard' ] ); |
| 64 | + |
| 65 | + $query = ' |
| 66 | + query ($id: ID!) { |
| 67 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 68 | + formFields { |
| 69 | + nodes { |
| 70 | + ... on PhoneField { |
| 71 | + phoneFormatType |
| 72 | + _phoneFormatExperimental { |
| 73 | + label |
| 74 | + mask |
| 75 | + regex |
| 76 | + instruction |
| 77 | + type |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + '; |
| 85 | + |
| 86 | + $variables = [ 'id' => $this->form_id ]; |
| 87 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 88 | + |
| 89 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 90 | + $this->assertEquals( 'STANDARD', $response['data']['gfForm']['formFields']['nodes'][0]['phoneFormatType'] ); |
| 91 | + |
| 92 | + $properties = $response['data']['gfForm']['formFields']['nodes'][0]['_phoneFormatExperimental']; |
| 93 | + $this->assertNotNull( $properties ); |
| 94 | + $this->assertNotNull( $properties['label'] ); |
| 95 | + $this->assertNotNull( $properties['mask'] ); |
| 96 | + $this->assertNotNull( $properties['regex'] ); |
| 97 | + $this->assertNotNull( $properties['instruction'] ); |
| 98 | + $this->assertEquals( 'STANDARD', $properties['type'] ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test that the international phone format returns null for mask, regex, and instruction. |
| 103 | + */ |
| 104 | + public function testInternationalPhoneFormat(): void { |
| 105 | + $this->form_id = $this->create_phone_form( [ 'phoneFormat' => 'international' ] ); |
| 106 | + |
| 107 | + $query = ' |
| 108 | + query ($id: ID!) { |
| 109 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 110 | + formFields { |
| 111 | + nodes { |
| 112 | + ... on PhoneField { |
| 113 | + phoneFormatType |
| 114 | + _phoneFormatExperimental { |
| 115 | + label |
| 116 | + mask |
| 117 | + regex |
| 118 | + instruction |
| 119 | + type |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + '; |
| 127 | + |
| 128 | + $variables = [ 'id' => $this->form_id ]; |
| 129 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 130 | + |
| 131 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 132 | + $this->assertEquals( 'INTERNATIONAL', $response['data']['gfForm']['formFields']['nodes'][0]['phoneFormatType'] ); |
| 133 | + |
| 134 | + $properties = $response['data']['gfForm']['formFields']['nodes'][0]['_phoneFormatExperimental']; |
| 135 | + $this->assertNotNull( $properties ); |
| 136 | + $this->assertNotNull( $properties['label'] ); |
| 137 | + $this->assertNull( $properties['mask'] ); |
| 138 | + $this->assertNull( $properties['regex'] ); |
| 139 | + $this->assertNull( $properties['instruction'] ); |
| 140 | + $this->assertEquals( 'INTERNATIONAL', $properties['type'] ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test that a custom phone format added via filter is supported. |
| 145 | + */ |
| 146 | + public function testCustomPhoneFormat(): void { |
| 147 | + $custom_format = [ |
| 148 | + 'label' => 'UK Format', |
| 149 | + 'mask' => '+44 9999 999999', |
| 150 | + 'regex' => '/^\+44\s?\d{4}\s?\d{6}$/', |
| 151 | + 'instruction' => '+44 XXXX XXXXXX', |
| 152 | + ]; |
| 153 | + |
| 154 | + add_filter( |
| 155 | + 'gform_phone_formats', |
| 156 | + static function ( $formats ) use ( $custom_format ) { |
| 157 | + $formats['uk'] = $custom_format; |
| 158 | + return $formats; |
| 159 | + } |
| 160 | + ); |
| 161 | + |
| 162 | + // Use the property helper but override phoneFormat after creation. |
| 163 | + $field_helper = $this->tester->getPropertyHelper( 'PhoneField' ); |
| 164 | + $field = $this->factory->field->create( $field_helper->values ); |
| 165 | + $field->phoneFormat = 'uk'; |
| 166 | + |
| 167 | + $this->form_id = $this->factory->form->create( |
| 168 | + array_merge( |
| 169 | + [ 'fields' => [ $field ] ], |
| 170 | + $this->tester->getFormDefaultArgs() |
| 171 | + ) |
| 172 | + ); |
| 173 | + |
| 174 | + $query = ' |
| 175 | + query ($id: ID!) { |
| 176 | + gfForm(id: $id, idType: DATABASE_ID) { |
| 177 | + formFields { |
| 178 | + nodes { |
| 179 | + ... on PhoneField { |
| 180 | + _phoneFormatExperimental { |
| 181 | + label |
| 182 | + mask |
| 183 | + regex |
| 184 | + instruction |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + '; |
| 192 | + |
| 193 | + $variables = [ 'id' => $this->form_id ]; |
| 194 | + $response = $this->graphql( compact( 'query', 'variables' ) ); |
| 195 | + |
| 196 | + $this->assertArrayNotHasKey( 'errors', $response ); |
| 197 | + |
| 198 | + $properties = $response['data']['gfForm']['formFields']['nodes'][0]['_phoneFormatExperimental']; |
| 199 | + $this->assertNotNull( $properties ); |
| 200 | + $this->assertEquals( $custom_format['label'], $properties['label'] ); |
| 201 | + $this->assertEquals( $custom_format['mask'], $properties['mask'] ); |
| 202 | + $this->assertEquals( $custom_format['regex'], $properties['regex'] ); |
| 203 | + $this->assertEquals( $custom_format['instruction'], $properties['instruction'] ); |
| 204 | + |
| 205 | + remove_all_filters( 'gform_phone_formats' ); |
| 206 | + } |
| 207 | +} |
0 commit comments