Skip to content

Commit 3b62898

Browse files
authored
Unit tests & Fixes (#123)
* chore: refactor field tests * chore: add checkbox field tests * chore: add Consent field test * fix: use DateInputProperty (with deprecated 'autocompleteAttribute'). * fix: EmailInputProperty description * feat: Add support for additional dateFormats. * chore: add dateField tests * chore: rename field tests * chore: add updateEntry and UpdateDraftEntry tests to existing fields * chore: update deps * chore: rebuild after deps
1 parent 36c0ce0 commit 3b62898

31 files changed

+5419
-1553
lines changed

src/Types/Enum/DateFieldFormatEnum.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ class DateFieldFormatEnum extends AbstractEnum {
2020
public static $type = 'DateFieldFormatEnum';
2121

2222
// Individual elements.
23-
const MDY = 'mdy';
24-
const DMY = 'dmy';
23+
const MDY = 'mdy';
24+
const DMY = 'dmy';
25+
const DMY_DASH = 'dmy_dash';
26+
const DMY_DOT = 'dmy_dot';
27+
const YMD_SLASH = 'ymd_slash';
28+
const YMD_DASH = 'ymd_dash';
29+
const YMD_DOT = 'ymd_dot';
2530

2631
/**
2732
* Sets the Enum type description.
@@ -39,14 +44,34 @@ public function get_type_description() : string {
3944
*/
4045
public function set_values() : array {
4146
return [
42-
'MDY' => [
43-
'description' => __( 'MDY format.', 'wp-graphql-gravity-forms' ),
47+
'MDY' => [
48+
'description' => __( 'mm/dd/yyyy format.', 'wp-graphql-gravity-forms' ),
4449
'value' => self::MDY,
4550
],
46-
'DMY' => [
47-
'description' => __( 'DMY format.', 'wp-graphql-gravity-forms' ),
51+
'DMY' => [
52+
'description' => __( 'dd/mm/yyyy format.', 'wp-graphql-gravity-forms' ),
4853
'value' => self::DMY,
4954
],
55+
'DMY_DASH' => [
56+
'description' => __( 'dd-mm-yyyy format.', 'wp-graphql-gravity-forms' ),
57+
'value' => self::DMY_DASH,
58+
],
59+
'DMY_DOT' => [
60+
'description' => __( 'dd.mm.yyyy format.', 'wp-graphql-gravity-forms' ),
61+
'value' => self::DMY_DOT,
62+
],
63+
'YMD_SLASH' => [
64+
'description' => __( 'yyyy/mm/dd format.', 'wp-graphql-gravity-forms' ),
65+
'value' => self::YMD_SLASH,
66+
],
67+
'YMD_DASH' => [
68+
'description' => __( 'yyyy/mm/dd format.', 'wp-graphql-gravity-forms' ),
69+
'value' => self::YMD_DASH,
70+
],
71+
'YMD_DOT' => [
72+
'description' => __( 'yyyy.mm.dd format.', 'wp-graphql-gravity-forms' ),
73+
'value' => self::YMD_DOT,
74+
],
5075
];
5176
}
5277
}

src/Types/Field/DateField.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ protected function get_properties() : array {
5858
FieldProperty\DescriptionProperty::get(),
5959
FieldProperty\ErrorMessageProperty::get(),
6060
FieldProperty\InputNameProperty::get(),
61-
FieldProperty\InputsProperty::get(),
6261
FieldProperty\IsRequiredProperty::get(),
6362
FieldProperty\LabelProperty::get(),
6463
FieldProperty\NoDuplicatesProperty::get(),
@@ -83,6 +82,10 @@ protected function get_properties() : array {
8382
'type' => DateTypeEnum::$type,
8483
'description' => __( 'The type of date field to display.', 'wp-graphql-gravity-forms' ),
8584
],
85+
'inputs' => [
86+
'type' => [ 'list_of' => FieldProperty\DateInputProperty::$type ],
87+
'description' => __( 'An array containing the the individual properties for each element of the date field.', 'wp-graphql-gravity-forms' ),
88+
],
8689
]
8790
);
8891
}

src/Types/Field/EmailField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function get_properties() : array {
7171
],
7272
'inputs' => [
7373
'type' => [ 'list_of' => FieldProperty\EmailInputProperty::$type ],
74-
'description' => __( 'An array containing the the individual properties for each element of the address field.', 'wp-graphql-gravity-forms' ),
74+
'description' => __( 'An array containing the the individual properties for each element of the email field.', 'wp-graphql-gravity-forms' ),
7575
],
7676
],
7777
);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* GraphQL Object Type - DateInputProperty
4+
* An individual input in the Email field 'inputs' property.
5+
*
6+
* @see https://docs.gravityforms.com/gf_field_email/
7+
*
8+
* @package WPGraphQLGravityForms\Types\Field\FieldProperty;
9+
* @since 0.6.0
10+
*/
11+
12+
namespace WPGraphQLGravityForms\Types\Field\FieldProperty;
13+
14+
use WPGraphQLGravityForms\Types\Field\FieldProperty\InputProperty;
15+
use WPGraphQLGravityForms\Utils\Utils;
16+
17+
/**
18+
* Class - DateInputProperty
19+
*/
20+
class DateInputProperty extends AbstractProperty {
21+
/**
22+
* Type registered in WPGraphQL.
23+
*
24+
* @var string
25+
*/
26+
public static $type = 'DateInputProperty';
27+
28+
/**
29+
* Sets the field type description.
30+
*/
31+
protected function get_type_description() : string {
32+
return __( 'An array containing the the individual properties for each element of the Email field.', 'wp-graphql-gravity-forms' );
33+
}
34+
35+
/**
36+
* Gets the properties for the Field.
37+
*
38+
* @return array
39+
*/
40+
protected function get_properties() : array {
41+
return array_merge(
42+
AutocompleteAttributeProperty::get(),
43+
DefaultValueProperty::get(),
44+
LabelProperty::get(),
45+
PlaceholderProperty::get(),
46+
InputProperty\InputCustomLabelProperty::get(),
47+
InputProperty\InputIdProperty::get(),
48+
/**
49+
* Deprecated field properties.
50+
*
51+
* @since 0.6.0
52+
*/
53+
// translators: Gravity Forms Field input property.
54+
Utils::deprecate_property( AutocompleteAttributeProperty::get(), sprintf( __( 'This property is not associated with the Gravity Forms %s type.', 'wp-graphql-gravity-forms' ), self::$type ) ),
55+
// @since 0.2.0 .
56+
// translators: Gravity Forms Field input property.
57+
Utils::deprecate_property( InputProperty\InputIsHiddenProperty::get(), sprintf( __( 'This property is not associated with the Gravity Forms %s type.', 'wp-graphql-gravity-forms' ), self::$type ) ),
58+
// translators: Gravity Forms Field input property.
59+
Utils::deprecate_property( InputProperty\InputKeyProperty::get(), sprintf( __( 'This property is not associated with the Gravity Forms %s type.', 'wp-graphql-gravity-forms' ), self::$type ) ),
60+
// translators: Gravity Forms Field input property.
61+
Utils::deprecate_property( InputProperty\InputNameProperty::get(), sprintf( __( 'This property is not associated with the Gravity Forms %s type.', 'wp-graphql-gravity-forms' ), self::$type ) ),
62+
);
63+
}
64+
}

src/WPGraphQLGravityForms.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private function create_instances() : void {
8181
$this->instances['chained_select_input_property'] = new FieldProperty\ChainedSelectInputProperty();
8282
$this->instances['checkbox_input_property'] = new FieldProperty\CheckboxInputProperty();
8383
$this->instances['choice_property'] = new FieldProperty\ChoiceProperty();
84+
$this->instances['date_input_property'] = new FieldProperty\DateInputProperty();
8485
$this->instances['email_input_property'] = new FieldProperty\EmailInputProperty();
8586
$this->instances['input_property'] = new FieldProperty\InputProperty();
8687
$this->instances['list_choice_property'] = new FieldProperty\ListChoiceProperty();

tests/_support/Factories/DraftEntry.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ public function create_object( $args ) : string {
5454
$args['entry'] ?? []
5555
);
5656

57-
$resume_token = GFFormsModel::save_draft_submission(
58-
$form,
59-
$entry,
60-
$args['field_values'] ?? null,
61-
$args['page_number'],
62-
[],
63-
GFFormsModel::$unique_ids[ $args['form_id'] ],
64-
$args['source_url'],
65-
$args['resume_token'],
66-
);
57+
$resume_token = 0;
58+
do {
59+
$resume_token = GFFormsModel::save_draft_submission(
60+
$form,
61+
$entry,
62+
$args['field_values'] ?? null,
63+
$args['page_number'],
64+
[],
65+
GFFormsModel::$unique_ids[ $args['form_id'] ],
66+
$args['source_url'],
67+
$args['resume_token'],
68+
);
69+
} while ( empty( $resume_token ) );
6770

6871
return $resume_token;
6972
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
/**
3+
* Abstract class - GFHelpers.
4+
*
5+
* @package Helper\GFHelpers
6+
*/
7+
8+
namespace Helper\GFHelpers;
9+
10+
use Dummy;
11+
use WPGraphQLGravityForms\Types\Enum;
12+
13+
/**
14+
* Abstract Class - GFHelpers.
15+
*/
16+
abstract class GFHelpers {
17+
/**
18+
* Provides access to dummy functions.
19+
*
20+
* @var Dummy
21+
*/
22+
public $dummy;
23+
24+
/**
25+
* The list of field keys.
26+
*
27+
* @var array
28+
*/
29+
public $keys;
30+
/**
31+
* The generated list of values.
32+
*
33+
* @var array
34+
*/
35+
public $values;
36+
37+
/**
38+
* Constructor
39+
*
40+
* @param array $keys default properties to include.
41+
*/
42+
public function __construct( array $keys ) {
43+
require_once __DIR__ . '/inc/Dummy.php';
44+
$this->dummy = new Dummy();
45+
46+
$this->keys = $this->get_keys( $keys );
47+
$this->values = $this->getAll( $keys );
48+
}
49+
50+
/**
51+
* Flattens key => value pairs to grab all the keys.
52+
*
53+
* @param array $keys
54+
*/
55+
public function get_keys( array $keys ) {
56+
$return_values = [];
57+
58+
foreach ( $keys as $key ) {
59+
if ( is_array( $key ) ) {
60+
$return_values[] = array_key_first( $key );
61+
continue;
62+
}
63+
$return_values[] = $key;
64+
}
65+
return $return_values;
66+
}
67+
68+
/**
69+
* Gets the key => value pair for the given key name.
70+
*
71+
* @param string $key
72+
* @param [type] $value
73+
*/
74+
public function get( string $key, $value = null ) {
75+
return [ $key => call_user_func( [ $this, $key ], $value ) ];
76+
}
77+
78+
/**
79+
* Gets all key => value pairs for the defined keys.
80+
*
81+
* @param array $keys
82+
*/
83+
public function getAll( array $keys ) {
84+
$return_values = [];
85+
86+
foreach ( $keys as $key ) {
87+
if ( 'formId' === $key ) {
88+
continue;
89+
}
90+
91+
if ( is_array( $key ) ) {
92+
$k = array_key_first( $key );
93+
94+
if ( 'inputs' === $k && isset( $key['inputs']['fieldId'] ) ) {
95+
$new_value = [ $k => $this->getFieldInputs( $key[ $k ]['fieldId'], $key[ $k ]['count'], $key[ $k ]['keys'] ) ];
96+
$return_values += $new_value;
97+
continue;
98+
}
99+
$return_values += $this->get( $k, $key[ $k ] );
100+
continue;
101+
}
102+
103+
$return_values += $this->get( $key );
104+
}
105+
106+
return $return_values;
107+
}
108+
109+
/**
110+
* Gets the actual object values for the provided key.
111+
*
112+
* @param string $key .
113+
* @param mixed $object .
114+
*/
115+
public function getActualValue( string $key, $object ) {
116+
switch ( $key ) {
117+
case 'addressType':
118+
case 'captchaType':
119+
case 'captchaTheme':
120+
case 'calendarIconType':
121+
case 'dateType':
122+
$string = ucfirst( $key ) . 'Enum';
123+
$value = $this->get_enum_for_value( $string, $object->$key );
124+
break;
125+
case 'labelPlacement':
126+
case 'descriptionPlacement':
127+
case 'size':
128+
case 'visibility':
129+
// @todo: rename classes.
130+
$string = ucfirst( $key ) . 'PropertyEnum';
131+
$value = $this->get_enum_for_value( $string, $object->$key );
132+
break;
133+
case 'dateFormat':
134+
// @todo: rename classes.
135+
$string = 'DateFieldFormatEnum';
136+
$value = $this->get_enum_for_value( $string, $object->$key );
137+
break;
138+
case 'copyValuesOptionDefault':
139+
case 'displayOnly':
140+
case 'enableCopyValuesOption':
141+
case 'enablePasswordInput':
142+
$value = (bool) $object->$key;
143+
break;
144+
case 'maxLength':
145+
$value = (int) $object->$key;
146+
break;
147+
default:
148+
$value = isset( $object->$key ) ? $object->$key : null;
149+
break;
150+
}
151+
return [ $key => $value ];
152+
}
153+
154+
/**
155+
* Gets all actual object values.
156+
*
157+
* @param mixed $object
158+
*/
159+
public function getAllActualValues( $object, array $exclude = null ) {
160+
$return_values = [];
161+
162+
foreach ( $this->keys as $key ) {
163+
if ( ! empty( $exclude ) && in_array( $key, $exclude, true ) ) {
164+
continue;
165+
}
166+
$return_values += $this->getActualValue( $key, $object );
167+
}
168+
return $return_values;
169+
}
170+
171+
/**
172+
* Converts a string value to its Enum equivalent
173+
*
174+
* @param string $enumName Name of the Enum registered in GraphQL.
175+
* @param string $value .
176+
* @return string
177+
*/
178+
public function get_enum_for_value( string $enumName, string $value ) : string {
179+
$typeRegistry = \WPGraphQL::get_type_registry();
180+
return $typeRegistry->get_type( $enumName )->serialize( $value );
181+
}
182+
183+
public function get_field_values( $values ) {
184+
return array_map(
185+
function( $key, $value ) {
186+
return [ 'input_' . $key => $value ];
187+
},
188+
array_keys( $values ),
189+
$values
190+
);
191+
}
192+
193+
public function getFieldInputs( int $fieldId, int $count, array $keys ) {
194+
$return_values = [];
195+
for ( $i = 0; $i < $count; $i++ ) {
196+
$input_keys = array_merge(
197+
$keys,
198+
[ [ 'id' => (string) $fieldId . '.' . ( $i + 1 ) ] ]
199+
);
200+
$return_values[ $i ] = $this->getAll( $input_keys );
201+
}
202+
return $return_values;
203+
}
204+
}

0 commit comments

Comments
 (0)