Skip to content

Commit f82492a

Browse files
committed
Additional field type unit tests
1 parent 23004c7 commit f82492a

3 files changed

Lines changed: 405 additions & 53 deletions

File tree

dt-posts/dt-posts-hooks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public static function dt_ignore_duplicated_post_fields( $updated_fields, $field
239239
switch ( $field_type ) {
240240
case 'text':
241241
case 'textarea':
242+
case 'number':
242243
case 'boolean':
243244
if ( $field_value != $existing_fields[ $field_key ] ) {
244245
$updated_fields[ $field_key ] = $field_value;
@@ -254,6 +255,7 @@ public static function dt_ignore_duplicated_post_fields( $updated_fields, $field
254255
$updated_fields[ $field_key ] = $field_value;
255256
}
256257
break;
258+
case 'tags':
257259
case 'multi_select':
258260
$values = [];
259261
foreach ( $field_value['values'] ?? [] as $value ) {

tests/dt-posts/dt-posts/unit-test-create-post.php

Lines changed: 213 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DT_Posts_DT_Posts_Create_Post extends WP_UnitTestCase {
1717
'contact_phone' => [ 'values' => [ [ 'value' => '798456780' ] ] ],
1818
'contact_email' => [ 'values' => [ [ 'value' => 'bob@example.com' ] ] ],
1919
'tags' => [ 'values' => [ [ 'value' => 'tag1' ] ] ],
20+
'quick_button_contact_established' => '1'
2021
];
2122

2223
public $sample_group = [
@@ -176,12 +177,13 @@ public function test_custom_number_field_min_max_error() {
176177
*/
177178
public function test_do_not_overwrite_existing_fields_create() {
178179
// Create initial contact
179-
$initial_contact = DT_Posts::create_post('contacts', [
180-
'name' => 'John Doe',
181-
'contact_phone' => [ 'values' => [ [ 'value' => '123-456-7890' ] ] ],
182-
'overall_status' => 'active',
183-
'nickname' => 'Johnny'
184-
], true, false);
180+
$initial_fields = $this->sample_contact;
181+
$initial_fields['name'] = 'John Doe';
182+
$initial_fields['contact_phone'] = [ 'values' => [ [ 'value' => '123-456-7890' ] ] ];
183+
$initial_fields['overall_status'] = 'active';
184+
$initial_fields['nickname'] = 'Johnny';
185+
186+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
185187
$this->assertNotWPError( $initial_contact );
186188

187189
// Try to create duplicate with do_not_overwrite_existing_fields = true
@@ -205,20 +207,26 @@ public function test_do_not_overwrite_existing_fields_create() {
205207
$this->assertSame( 'paused', $result['overall_status']['key'] );
206208
$this->assertSame( 'John', $result['nickname'] );
207209
// Should add new fields.
208-
$this->assertSame( 'john@example.com', $result['contact_email'][0]['value'] );
210+
$this->assertSame( 'john@example.com', $result['contact_email'][1]['value'] );
211+
// Extra assertion sanity checks.
212+
$this->assertContains( 'tag1', $result['tags'] );
213+
$this->assertSame( 1, count( $result['location_grid'] ) );
214+
$this->assertSame( 2, count( $result['milestones'] ) );
215+
$this->assertSame( true, $result['requires_update'] );
209216
}
210217

211218
/**
212219
* @testdox do_not_overwrite_existing_fields: create with overwrite enabled
213220
*/
214221
public function test_overwrite_existing_fields_create() {
215222
// Create initial contact
216-
$initial_contact = DT_Posts::create_post('contacts', [
217-
'name' => 'Jane Doe',
218-
'contact_phone' => [ 'values' => [ [ 'value' => '987-654-3210' ] ] ],
219-
'overall_status' => 'active',
220-
'nickname' => 'Janie'
221-
], true, false);
223+
$initial_fields = $this->sample_contact;
224+
$initial_fields['name'] = 'John Doe';
225+
$initial_fields['contact_phone'] = [ 'values' => [ [ 'value' => '987-654-3210' ] ] ];
226+
$initial_fields['overall_status'] = 'active';
227+
$initial_fields['nickname'] = 'Janie';
228+
229+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
222230
$this->assertNotWPError( $initial_contact );
223231

224232
// Try to create duplicate with do_not_overwrite_existing_fields = false
@@ -227,7 +235,8 @@ public function test_overwrite_existing_fields_create() {
227235
'contact_phone' => [ 'values' => [ [ 'value' => '987-654-3210' ] ] ],
228236
'overall_status' => 'paused', // Different value
229237
'nickname' => 'Jane', // Different value
230-
'contact_email' => [ 'values' => [ [ 'value' => 'jane@example.com' ] ] ] // New field
238+
'contact_email' => [ 'values' => [ [ 'value' => 'jane@example.com' ] ] ], // New field
239+
'milestones' => [ 'values' => [ [ 'value' => 'milestone_has_bible' ], [ 'value' => 'mature_christian' ] ] ]
231240
];
232241

233242
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
@@ -240,26 +249,30 @@ public function test_overwrite_existing_fields_create() {
240249
$this->assertSame( 'paused', $result['overall_status']['key'] );
241250
$this->assertSame( 'Jane', $result['nickname'] );
242251
// Should add new fields
243-
$this->assertSame( 'jane@example.com', $result['contact_email'][0]['value'] );
252+
$this->assertSame( 'jane@example.com', $result['contact_email'][1]['value'] );
253+
// Extra assertion sanity checks.
254+
$this->assertSame( 1, $result['quick_button_contact_established'] );
244255
}
245256

246257
/**
247258
* @testdox do_not_overwrite_existing_fields: multi-select fields
248259
*/
249260
public function test_do_not_overwrite_multi_select_fields_create() {
250261
// Create initial contact with tags
251-
$initial_contact = DT_Posts::create_post('contacts', [
252-
'name' => 'Multi Test',
253-
'contact_phone' => [ 'values' => [ [ 'value' => '555-0001' ] ] ],
254-
'tags' => [ 'values' => [ [ 'value' => 'existing_tag' ] ] ]
255-
], true, false);
262+
$initial_fields = $this->sample_contact;
263+
$initial_fields['name'] = 'Multi Test';
264+
$initial_fields['contact_phone'] = [ 'values' => [ [ 'value' => '555-0001' ] ] ];
265+
$initial_fields['tags'] = [ 'values' => [ [ 'value' => 'existing_tag' ] ] ];
266+
267+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
256268
$this->assertNotWPError( $initial_contact );
257269

258270
// Try to create duplicate with additional tags
259271
$duplicate_fields = [
260272
'name' => 'Multi Test',
261273
'contact_phone' => [ 'values' => [ [ 'value' => '555-0001' ] ] ],
262-
'tags' => [ 'values' => [ [ 'value' => 'existing_tag' ], [ 'value' => 'new_tag' ] ] ]
274+
'tags' => [ 'values' => [ [ 'value' => 'existing_tag' ], [ 'value' => 'new_tag' ] ] ],
275+
'baptism_date' => '2025-01-01'
263276
];
264277

265278
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
@@ -272,5 +285,184 @@ public function test_do_not_overwrite_multi_select_fields_create() {
272285
$this->assertContains( 'existing_tag', $result['tags'] );
273286
$this->assertContains( 'new_tag', $result['tags'] );
274287
$this->assertSame( 2, count( $result['tags'] ) );
288+
// Extra assertion sanity checks.
289+
$this->assertSame( $duplicate_fields['baptism_date'], $result['baptism_date']['formatted'] );
290+
}
291+
292+
public function test_do_not_overwrite_text_fields_create() {
293+
$initial_fields = $this->sample_contact;
294+
$initial_fields['contact_phone'] = [ 'values' => [ [ 'value' => '123-456-7890' ] ] ];
295+
$initial_fields['nickname'] = 'Johnny';
296+
297+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
298+
$this->assertNotWPError( $initial_contact );
299+
300+
$duplicate_fields = [
301+
'name' => 'John Doe',
302+
'contact_phone' => [ 'values' => [ [ 'value' => '123-456-7890' ] ] ],
303+
'nickname' => 'John', // Different value
304+
];
305+
306+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
307+
'check_for_duplicates' => [ 'contact_phone' ],
308+
'do_not_overwrite_existing_fields' => true
309+
]);
310+
$this->assertNotWPError( $result );
311+
312+
$this->assertSame( 'John', $result['nickname'] );
313+
}
314+
315+
public function test_do_not_overwrite_number_fields_create() {
316+
$initial_fields = $this->sample_contact;
317+
318+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
319+
$this->assertNotWPError( $initial_contact );
320+
321+
$duplicate_fields = [
322+
'title' => $initial_fields['title'],
323+
'contact_phone' => $initial_fields['contact_phone'],
324+
'quick_button_contact_established' => 1
325+
];
326+
327+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
328+
'check_for_duplicates' => [ 'contact_phone' ],
329+
'do_not_overwrite_existing_fields' => true
330+
]);
331+
$this->assertNotWPError( $result );
332+
333+
$this->assertSame( $duplicate_fields['quick_button_contact_established'], $result['quick_button_contact_established'] );
334+
}
335+
336+
public function test_do_not_overwrite_boolean_fields_create() {
337+
$initial_fields = $this->sample_contact;
338+
$initial_fields['requires_update'] = true;
339+
340+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
341+
$this->assertNotWPError( $initial_contact );
342+
343+
$duplicate_fields = [
344+
'title' => $initial_fields['title'],
345+
'contact_phone' => $initial_fields['contact_phone'],
346+
'requires_update' => false
347+
];
348+
349+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
350+
'check_for_duplicates' => [ 'contact_phone' ],
351+
'do_not_overwrite_existing_fields' => false
352+
]);
353+
$this->assertNotWPError( $result );
354+
355+
$this->assertSame( true, empty( $result['requires_update'] ) );
356+
}
357+
358+
public function test_do_not_overwrite_date_fields_create() {
359+
$initial_fields = $this->sample_contact;
360+
361+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
362+
$this->assertNotWPError( $initial_contact );
363+
364+
$duplicate_fields = [
365+
'name' => 'Frank',
366+
'contact_phone' => $initial_fields['contact_phone'],
367+
'baptism_date' => '2025-01-01'
368+
];
369+
370+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
371+
'check_for_duplicates' => [ 'contact_phone' ],
372+
'do_not_overwrite_existing_fields' => true
373+
]);
374+
$this->assertNotWPError( $result );
375+
376+
$this->assertSame( $duplicate_fields['baptism_date'], $result['baptism_date']['formatted'] );
377+
}
378+
379+
public function test_do_not_overwrite_key_select_fields_create() {
380+
$initial_fields = $this->sample_contact;
381+
$initial_fields['overall_status'] = 'active';
382+
383+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
384+
$this->assertNotWPError( $initial_contact );
385+
386+
$duplicate_fields = [
387+
'name' => 'John Doe',
388+
'contact_phone' => $initial_fields['contact_phone'],
389+
'overall_status' => 'paused', // Different value
390+
];
391+
392+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
393+
'check_for_duplicates' => [ 'contact_phone' ],
394+
'do_not_overwrite_existing_fields' => true
395+
]);
396+
$this->assertNotWPError( $result );
397+
398+
$this->assertSame( 'paused', $result['overall_status']['key'] );
399+
}
400+
401+
public function test_do_not_overwrite_tags_fields_create() {
402+
$initial_fields = $this->sample_contact;
403+
$initial_fields['tags'] = [ 'values' => [ [ 'value' => 'existing_tag' ] ] ];
404+
405+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
406+
$this->assertNotWPError( $initial_contact );
407+
408+
$duplicate_fields = [
409+
'name' => 'Tags Test',
410+
'contact_phone' => $initial_fields['contact_phone'],
411+
'tags' => [ 'values' => [ [ 'value' => 'existing_tag' ], [ 'value' => 'new_tag' ] ] ]
412+
];
413+
414+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
415+
'check_for_duplicates' => [ 'contact_phone' ],
416+
'do_not_overwrite_existing_fields' => true
417+
]);
418+
$this->assertNotWPError( $result );
419+
420+
$this->assertSame( 2, count( $result['tags'] ) );
421+
$this->assertContains( 'existing_tag', $result['tags'] );
422+
$this->assertContains( 'new_tag', $result['tags'] );
423+
}
424+
425+
public function test_do_not_overwrite_location_fields_create() {
426+
$initial_fields = $this->sample_contact;
427+
428+
$contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
429+
$this->assertNotWPError( $contact );
430+
431+
$duplicate_fields = [
432+
'title' => $initial_fields['title'],
433+
'contact_phone' => $initial_fields['contact_phone'],
434+
'location_grid' => [ 'values' => [ [ 'value' => '100089589' ] ] ]
435+
];
436+
437+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
438+
'check_for_duplicates' => [ 'contact_phone' ],
439+
'do_not_overwrite_existing_fields' => false
440+
]);
441+
$this->assertNotWPError( $result );
442+
443+
$this->assertSame( 1, count( $result['location_grid'] ) );
444+
}
445+
446+
public function test_do_not_overwrite_comms_channel_fields_create() {
447+
$initial_fields = $this->sample_contact;
448+
$initial_fields['contact_phone'] = [ 'values' => [ [ 'value' => '123-456-7890' ] ] ];
449+
450+
$initial_contact = DT_Posts::create_post( 'contacts', $initial_fields, true, false );
451+
$this->assertNotWPError( $initial_contact );
452+
453+
$duplicate_fields = [
454+
'name' => 'John Doe',
455+
'contact_phone' => [ 'values' => [ [ 'value' => '123-456-7890' ] ] ],
456+
'contact_email' => [ 'values' => [ [ 'value' => 'john@example.com' ] ] ]
457+
];
458+
459+
$result = DT_Posts::create_post('contacts', $duplicate_fields, true, false, [
460+
'check_for_duplicates' => [ 'contact_phone' ],
461+
'do_not_overwrite_existing_fields' => true
462+
]);
463+
$this->assertNotWPError( $result );
464+
465+
$this->assertSame( 1, count( $result['contact_phone'] ) );
466+
$this->assertSame( 'john@example.com', $result['contact_email'][1]['value'] );
275467
}
276468
}

0 commit comments

Comments
 (0)