@@ -192,4 +192,153 @@ public function test_dt_private_fields(){
192192 $ this ->assertArrayNotHasKey ( 'tags_test_private ' , $ contact2 );
193193 $ this ->assertArrayNotHasKey ( 'number_test_private ' , $ contact2 );
194194 }
195+
196+ /**
197+ * @testdox do_not_overwrite_existing_fields: update with protection enabled
198+ */
199+ public function test_do_not_overwrite_existing_fields_update () {
200+ // Create a contact with initial values
201+ $ contact = DT_Posts::create_post ('contacts ' , [
202+ 'name ' => 'Update Test ' ,
203+ 'overall_status ' => 'active ' ,
204+ 'nickname ' => 'Original Nick ' ,
205+ 'contact_phone ' => [ 'values ' => [ [ 'value ' => '111-222-3333 ' ] ] ]
206+ ], true , false );
207+ $ this ->assertNotWPError ( $ contact );
208+
209+ // Update with do_not_overwrite_existing_fields = true
210+ $ update_fields = [
211+ 'overall_status ' => 'paused ' , // Try to change existing field
212+ 'nickname ' => 'New Nick ' , // Try to change existing field
213+ 'contact_email ' => [ 'values ' => [ [ 'value ' => 'test@example.com ' ] ] ] // Add new field
214+ ];
215+
216+ $ result = DT_Posts::update_post ('contacts ' , $ contact ['ID ' ], $ update_fields , true , false , [
217+ 'do_not_overwrite_existing_fields ' => true
218+ ]);
219+
220+ $ this ->assertNotWPError ( $ result );
221+ // Should still only have one contact_phone entry.
222+ $ this ->assertSame ( 1 , count ( $ result ['contact_phone ' ] ) );
223+ // Should update to new values, as they are different.
224+ $ this ->assertSame ( 'paused ' , $ result ['overall_status ' ]['key ' ] );
225+ $ this ->assertSame ( 'New Nick ' , $ result ['nickname ' ] );
226+ // Should add new fields
227+ $ this ->assertSame ( 'test@example.com ' , $ result ['contact_email ' ][0 ]['value ' ] );
228+ }
229+
230+ /**
231+ * @testdox do_not_overwrite_existing_fields: update with protection disabled
232+ */
233+ public function test_overwrite_existing_fields_update () {
234+ // Create a contact with initial values
235+ $ contact = DT_Posts::create_post ('contacts ' , [
236+ 'name ' => 'Overwrite Test ' ,
237+ 'overall_status ' => 'active ' ,
238+ 'nickname ' => 'Original Nick '
239+ ], true , false );
240+ $ this ->assertNotWPError ( $ contact );
241+
242+ // Update with do_not_overwrite_existing_fields = false (default behavior)
243+ $ update_fields = [
244+ 'overall_status ' => 'paused ' ,
245+ 'nickname ' => 'New Nick ' ,
246+ 'contact_email ' => [ 'values ' => [ [ 'value ' => 'overwrite@example.com ' ] ] ]
247+ ];
248+
249+ $ result = DT_Posts::update_post ('contacts ' , $ contact ['ID ' ], $ update_fields , true , false , [
250+ 'do_not_overwrite_existing_fields ' => false
251+ ]);
252+
253+ $ this ->assertNotWPError ( $ result );
254+ // Should update existing fields with new values
255+ $ this ->assertSame ( 'paused ' , $ result ['overall_status ' ]['key ' ] );
256+ $ this ->assertSame ( 'New Nick ' , $ result ['nickname ' ] );
257+ // Should add new fields
258+ $ this ->assertSame ( 'overwrite@example.com ' , $ result ['contact_email ' ][0 ]['value ' ] );
259+ }
260+
261+ /**
262+ * @testdox do_not_overwrite_existing_fields: empty vs non-empty fields
263+ */
264+ public function test_do_not_overwrite_empty_fields_update () {
265+ // Create a contact with some empty fields
266+ $ contact = DT_Posts::create_post ('contacts ' , [
267+ 'name ' => 'Empty Field Test ' ,
268+ 'overall_status ' => 'active '
269+ // nickname is not set (empty)
270+ ], true , false );
271+ $ this ->assertNotWPError ( $ contact );
272+
273+ // Update with do_not_overwrite_existing_fields = true
274+ $ update_fields = [
275+ 'overall_status ' => 'paused ' , // Try to change existing field
276+ 'nickname ' => 'Should Be Added ' // Add to empty field
277+ ];
278+
279+ $ result = DT_Posts::update_post ('contacts ' , $ contact ['ID ' ], $ update_fields , true , false , [
280+ 'do_not_overwrite_existing_fields ' => true
281+ ]);
282+
283+ $ this ->assertNotWPError ( $ result );
284+ // Should update to new values, as they are different and not duplicates.
285+ $ this ->assertSame ( 'paused ' , $ result ['overall_status ' ]['key ' ] );
286+ // Should add value to empty field
287+ $ this ->assertSame ( 'Should Be Added ' , $ result ['nickname ' ] );
288+ }
289+
290+ /**
291+ * @testdox do_not_overwrite_existing_fields: communication channel fields
292+ */
293+ public function test_do_not_overwrite_communication_channels_update () {
294+ // Create a contact with phone number
295+ $ contact = DT_Posts::create_post ('contacts ' , [
296+ 'name ' => 'Communication Test ' ,
297+ 'contact_phone ' => [ 'values ' => [ [ 'value ' => '444-555-6666 ' ] ] ]
298+ ], true , false );
299+ $ this ->assertNotWPError ( $ contact );
300+
301+ // Update with additional phone and email
302+ $ update_fields = [
303+ 'contact_phone ' => [ 'values ' => [ [ 'value ' => '444-555-6666 ' ], [ 'value ' => '777-888-9999 ' ] ] ],
304+ 'contact_email ' => [ 'values ' => [ [ 'value ' => 'comm@example.com ' ] ] ]
305+ ];
306+
307+ $ result = DT_Posts::update_post ('contacts ' , $ contact ['ID ' ], $ update_fields , true , false , [
308+ 'do_not_overwrite_existing_fields ' => true
309+ ]);
310+
311+ $ this ->assertNotWPError ( $ result );
312+ // Should keep existing phone and add new one
313+ $ phone_values = array_column ( $ result ['contact_phone ' ], 'value ' );
314+ $ this ->assertContains ( '444-555-6666 ' , $ phone_values );
315+ $ this ->assertContains ( '777-888-9999 ' , $ phone_values );
316+ // Should add new email
317+ $ this ->assertSame ( 'comm@example.com ' , $ result ['contact_email ' ][0 ]['value ' ] );
318+ }
319+
320+ /**
321+ * @testdox do_not_overwrite_existing_fields: date fields
322+ */
323+ public function test_do_not_overwrite_date_fields_update () {
324+ // Create a contact with baptism date
325+ $ contact = DT_Posts::create_post ('contacts ' , [
326+ 'name ' => 'Date Test ' ,
327+ 'baptism_date ' => '2020-01-01 '
328+ ], true , false );
329+ $ this ->assertNotWPError ( $ contact );
330+
331+ // Try to update baptism date
332+ $ update_fields = [
333+ 'baptism_date ' => '2023-12-25 '
334+ ];
335+
336+ $ result = DT_Posts::update_post ('contacts ' , $ contact ['ID ' ], $ update_fields , true , false , [
337+ 'do_not_overwrite_existing_fields ' => true
338+ ]);
339+
340+ $ this ->assertNotWPError ( $ result );
341+ // Should update to new values, as they are different and not duplicates.
342+ $ this ->assertSame ( '2023-12-25 ' , $ result ['baptism_date ' ]['formatted ' ] );
343+ }
195344}
0 commit comments