Skip to content

Commit da3d808

Browse files
committed
test: Add international phone format coverage
- Add dedicated PhoneFieldInternationalFormatTest - Normalize phone format properties for null/false values - Adjust phone format expectations for international mask/regex
1 parent c3b1066 commit da3d808

File tree

3 files changed

+328
-5
lines changed

3 files changed

+328
-5
lines changed

src/Type/WPInterface/FieldSetting/FieldWithPhoneFormat.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,32 @@ public static function get_fields(): array {
6969
]
7070
);
7171

72-
// Return the properties for the selected format.
73-
return $phone_formats[ $field->phoneFormat ] ?? null;
72+
$format = $phone_formats[ $field->phoneFormat ] ?? null;
73+
if ( ! is_array( $format ) ) {
74+
return null;
75+
}
76+
77+
$required_keys = [
78+
'label' => null,
79+
'mask' => null,
80+
'regex' => null,
81+
'instruction' => null,
82+
'type' => null,
83+
];
84+
85+
$normalized = [];
86+
foreach ( $required_keys as $key => $default ) {
87+
$value = $format[ $key ] ?? $default;
88+
if ( false === $value ) {
89+
$value = null;
90+
}
91+
if ( null !== $value && ! is_scalar( $value ) ) {
92+
$value = $default;
93+
}
94+
$normalized[ $key ] = $value;
95+
}
96+
97+
return $normalized;
7498
},
7599
],
76100
];

tests/_support/Helper/GFHelpers/ExpectedFormFields.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,18 @@ public function pen_size_setting( GF_Field $field, array &$properties ): void {
437437
public function phone_format_setting( GF_Field $field, array &$properties ): void {
438438
$properties[] = $this->expectedField( 'phoneFormat', ! empty( $field->phoneFormat ) ? GFHelpers::get_enum_for_value( Enum\PhoneFieldFormatEnum::$type, $field->phoneFormat ) : self::IS_NULL );
439439

440-
// Add phoneFormatProperties field
440+
// Add phoneFormatProperties field.
441441
if ( ! empty( $field->phoneFormat ) ) {
442442
$properties[] = $this->expectedField( 'phoneFormatProperties.label', self::NOT_NULL );
443-
$properties[] = $this->expectedField( 'phoneFormatProperties.mask', self::NOT_NULL );
444-
$properties[] = $this->expectedField( 'phoneFormatProperties.regex', self::NOT_NULL );
445443
$properties[] = $this->expectedField( 'phoneFormatProperties.instruction', self::NOT_NULL );
446444
$properties[] = $this->expectedField( 'phoneFormatProperties.type', self::NOT_NULL );
445+
if ( 'international' === $field->phoneFormat ) {
446+
$properties[] = $this->expectedField( 'phoneFormatProperties.mask', self::IS_NULL );
447+
$properties[] = $this->expectedField( 'phoneFormatProperties.regex', self::IS_NULL );
448+
} else {
449+
$properties[] = $this->expectedField( 'phoneFormatProperties.mask', self::NOT_NULL );
450+
$properties[] = $this->expectedField( 'phoneFormatProperties.regex', self::NOT_NULL );
451+
}
447452
} else {
448453
$properties[] = $this->expectedField( 'phoneFormatProperties', self::IS_NULL );
449454
}
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
<?php
2+
/**
3+
* Test Phone type with international format.
4+
*
5+
* @package Tests\WPGraphQL\GF
6+
*/
7+
8+
use Tests\WPGraphQL\GF\TestCase\FormFieldTestCase;
9+
use Tests\WPGraphQL\GF\TestCase\FormFieldTestCaseInterface;
10+
11+
/**
12+
* Class - PhoneFieldInternationalFormatTest.
13+
*/
14+
class PhoneFieldInternationalFormatTest extends FormFieldTestCase implements FormFieldTestCaseInterface {
15+
/**
16+
* Tests the field properties and values.
17+
*/
18+
public function testField(): void {
19+
$this->runTestField();
20+
}
21+
22+
/**
23+
* Tests submitting the field values as a draft entry with submitGfForm.
24+
*/
25+
public function testSubmitDraft(): void {
26+
$this->runTestSubmitDraft();
27+
}
28+
29+
/**
30+
* Tests submitting the field values as an entry with submitGfForm.
31+
*/
32+
public function testSubmitForm(): void {
33+
$this->runtestSubmitForm();
34+
}
35+
36+
/**
37+
* Tests updating the field value with updateGfEntry.
38+
*/
39+
public function testUpdateEntry(): void {
40+
$this->runtestUpdateEntry();
41+
}
42+
43+
/**
44+
* Tests updating the draft field value with updateGfEntry.
45+
*/
46+
public function testUpdateDraft(): void {
47+
$this->runTestUpdateDraft();
48+
}
49+
50+
/**
51+
* Sets the correct Field Helper.
52+
*/
53+
public function field_helper() {
54+
return $this->tester->getPropertyHelper( 'PhoneField', [ 'phoneFormat' => 'international' ] );
55+
}
56+
57+
/**
58+
* Generates the form fields from factory. Must be wrappend in an array.
59+
*/
60+
public function generate_fields(): array {
61+
return [ $this->factory->field->create( $this->property_helper->values ) ];
62+
}
63+
64+
/**
65+
* The value as expected in GraphQL.
66+
*/
67+
public function field_value() {
68+
return (string) $this->property_helper->dummy->telephone();
69+
}
70+
71+
/**
72+
* The value as expected in GraphQL when updating from field_value().
73+
*/
74+
public function updated_field_value() {
75+
return (string) $this->property_helper->dummy->telephone();
76+
}
77+
78+
/**
79+
* The value as expected by Gravity Forms.
80+
*/
81+
public function value() {
82+
return [ $this->fields[0]['id'] => $this->field_value ];
83+
}
84+
85+
/**
86+
* The GraphQL query string.
87+
*/
88+
public function field_query(): string {
89+
return '
90+
... on PhoneField {
91+
adminLabel
92+
autocompleteAttribute
93+
canPrepopulate
94+
conditionalLogic {
95+
actionType
96+
logicType
97+
rules {
98+
fieldId
99+
operator
100+
value
101+
}
102+
}
103+
cssClass
104+
defaultValue
105+
description
106+
descriptionPlacement
107+
errorMessage
108+
hasAutocomplete
109+
inputName
110+
isRequired
111+
label
112+
labelPlacement
113+
personalData {
114+
isIdentificationField
115+
shouldErase
116+
shouldExport
117+
}
118+
phoneFormat
119+
phoneFormatProperties {
120+
label
121+
mask
122+
regex
123+
instruction
124+
type
125+
}
126+
placeholder
127+
shouldAllowDuplicates
128+
size
129+
value
130+
}
131+
';
132+
}
133+
134+
/**
135+
* SubmitForm mutation string.
136+
*/
137+
public function submit_form_mutation(): string {
138+
return '
139+
mutation ($formId: ID!, $fieldId: Int!, $value: String!, $draft: Boolean) {
140+
submitGfForm( input: { id: $formId, saveAsDraft: $draft, fieldValues: {id: $fieldId, value: $value}}) {
141+
errors {
142+
id
143+
message
144+
connectedFormField {
145+
databaseId
146+
type
147+
}
148+
}
149+
entry {
150+
formFields {
151+
nodes {
152+
... on PhoneField {
153+
value
154+
}
155+
}
156+
}
157+
... on GfSubmittedEntry {
158+
databaseId
159+
}
160+
... on GfDraftEntry {
161+
resumeToken
162+
}
163+
}
164+
}
165+
}
166+
';
167+
}
168+
169+
/**
170+
* Returns the UpdateEntry mutation string.
171+
*/
172+
public function update_entry_mutation(): string {
173+
return '
174+
mutation updateGfEntry( $entryId: ID!, $fieldId: Int!, $value: String! ){
175+
updateGfEntry( input: { id: $entryId, shouldValidate: true, fieldValues: {id: $fieldId, value: $value} }) {
176+
errors {
177+
id
178+
message
179+
connectedFormField {
180+
databaseId
181+
type
182+
}
183+
}
184+
entry {
185+
formFields {
186+
nodes {
187+
... on PhoneField {
188+
value
189+
}
190+
}
191+
}
192+
}
193+
}
194+
}
195+
';
196+
}
197+
198+
/**
199+
* Returns the UpdateDraftEntry mutation string.
200+
*/
201+
public function update_draft_entry_mutation(): string {
202+
return '
203+
mutation updateGfDraftEntry( $resumeToken: ID!, $fieldId: Int!, $value: String! ){
204+
updateGfDraftEntry( input: {id: $resumeToken, idType: RESUME_TOKEN, shouldValidate: true, fieldValues: {id: $fieldId, value: $value} }) {
205+
errors {
206+
id
207+
message
208+
connectedFormField {
209+
databaseId
210+
type
211+
}
212+
}
213+
entry: draftEntry {
214+
formFields {
215+
nodes {
216+
... on PhoneField {
217+
value
218+
}
219+
}
220+
}
221+
}
222+
}
223+
}
224+
';
225+
}
226+
227+
/**
228+
* {@inheritDoc}
229+
*/
230+
public function expected_field_response( array $form ): array {
231+
$expected = $this->getExpectedFormFieldValues( $form['fields'][0] );
232+
$expected[] = $this->expected_field_value( 'value', $this->field_value );
233+
234+
return [
235+
$this->expectedObject(
236+
'gfEntry',
237+
[
238+
$this->expectedObject(
239+
'formFields',
240+
[
241+
$this->expectedNode(
242+
'nodes',
243+
$expected,
244+
0
245+
),
246+
]
247+
),
248+
]
249+
),
250+
];
251+
}
252+
253+
/**
254+
* The expected WPGraphQL mutation response.
255+
*
256+
* @param string $mutationName .
257+
* @param mixed $value .
258+
*/
259+
public function expected_mutation_response( string $mutationName, $value ): array {
260+
return [
261+
$this->expectedObject(
262+
$mutationName,
263+
[
264+
$this->expectedObject(
265+
'entry',
266+
[
267+
$this->expectedObject(
268+
'formFields',
269+
[
270+
$this->expectedNode(
271+
'nodes',
272+
[
273+
$this->expected_field_value( 'value', $value ),
274+
]
275+
),
276+
]
277+
),
278+
]
279+
),
280+
]
281+
),
282+
];
283+
}
284+
285+
/**
286+
* Checks if values submitted by GraphQL are the same as whats stored on the server.
287+
*
288+
* @param array $actual_entry .
289+
* @param array $form .
290+
*/
291+
public function check_saved_values( $actual_entry, $form ): void {
292+
$this->assertEquals( $this->field_value, $actual_entry[ $form['fields'][0]->id ], 'Submit mutation entry value not equal' );
293+
}
294+
}

0 commit comments

Comments
 (0)