@@ -218,4 +218,179 @@ public function test_internal_url() {
218218
219219 $ this ->assertEquals ( '/about/ ' , $ result ['url ' ] );
220220 }
221+
222+ /**
223+ * Test get_link with array value (ACF 5.6.0+).
224+ *
225+ * Tests the array merge branch:
226+ * `if ( is_array( $value ) ) { $link = array_merge( $link, $value ); }`
227+ */
228+ public function test_get_link_array_format () {
229+ $ input = array (
230+ 'title ' => 'Custom Title ' ,
231+ 'url ' => 'https://custom.com ' ,
232+ 'target ' => '_blank ' ,
233+ );
234+
235+ $ result = $ this ->field_instance ->get_link ( $ input );
236+
237+ $ this ->assertEquals ( 'Custom Title ' , $ result ['title ' ] );
238+ $ this ->assertEquals ( 'https://custom.com ' , $ result ['url ' ] );
239+ $ this ->assertEquals ( '_blank ' , $ result ['target ' ] );
240+ }
241+
242+ /**
243+ * Test get_link with string value (legacy ACF < 5.6.0).
244+ *
245+ * Tests the string branch:
246+ * `elseif ( is_string( $value ) ) { $link['url'] = $value; }`
247+ */
248+ public function test_get_link_string_format () {
249+ $ result = $ this ->field_instance ->get_link ( 'https://example.com/page ' );
250+
251+ $ this ->assertEquals ( '' , $ result ['title ' ] );
252+ $ this ->assertEquals ( 'https://example.com/page ' , $ result ['url ' ] );
253+ $ this ->assertEquals ( '' , $ result ['target ' ] );
254+ }
255+
256+ /**
257+ * Test get_link with empty string returns defaults.
258+ */
259+ public function test_get_link_empty_string () {
260+ $ result = $ this ->field_instance ->get_link ( '' );
261+
262+ $ this ->assertEquals ( '' , $ result ['title ' ] );
263+ $ this ->assertEquals ( '' , $ result ['url ' ] );
264+ $ this ->assertEquals ( '' , $ result ['target ' ] );
265+ }
266+
267+ /**
268+ * Test get_link with partial array fills defaults.
269+ */
270+ public function test_get_link_partial_array () {
271+ $ result = $ this ->field_instance ->get_link ( array ( 'url ' => 'https://partial.com ' ) );
272+
273+ $ this ->assertEquals ( '' , $ result ['title ' ] );
274+ $ this ->assertEquals ( 'https://partial.com ' , $ result ['url ' ] );
275+ $ this ->assertEquals ( '' , $ result ['target ' ] );
276+ }
277+
278+ /**
279+ * Test validate_value returns early when not required.
280+ *
281+ * Tests the early return:
282+ * `if ( ! $field['required'] ) { return $valid; }`
283+ */
284+ public function test_validate_value_not_required_returns_valid () {
285+ $ field = $ this ->get_field ( array ( 'required ' => 0 ) );
286+
287+ // Even with empty value, should return passed $valid when not required.
288+ $ valid = $ this ->field_instance ->validate_value ( true , '' , $ field , 'acf[field_link_test] ' );
289+
290+ $ this ->assertTrue ( $ valid );
291+ }
292+
293+ /**
294+ * Test validate_value with empty URL when required fails.
295+ *
296+ * Tests the URL check:
297+ * `if ( empty( $value ) || empty( $value['url'] ) ) { return false; }`
298+ */
299+ public function test_validate_value_empty_url_required_fails () {
300+ $ field = $ this ->get_field ( array ( 'required ' => 1 ) );
301+ $ link = array (
302+ 'title ' => 'Has title ' ,
303+ 'url ' => '' , // Empty URL.
304+ 'target ' => '' ,
305+ );
306+
307+ $ valid = $ this ->field_instance ->validate_value ( true , $ link , $ field , 'acf[field_link_test] ' );
308+
309+ $ this ->assertFalse ( $ valid );
310+ }
311+
312+ /**
313+ * Test update_value converts empty URL array to empty string.
314+ *
315+ * Tests the empty check:
316+ * `if ( empty( $value ) || empty( $value['url'] ) ) { $value = ''; }`
317+ */
318+ public function test_update_value_empty_url_converts_to_empty_string () {
319+ $ field = $ this ->get_field ();
320+ $ link = array (
321+ 'title ' => 'Has title ' ,
322+ 'url ' => '' ,
323+ 'target ' => '_blank ' ,
324+ );
325+
326+ $ result = $ this ->field_instance ->update_value ( $ link , $ this ->post_id , $ field );
327+
328+ $ this ->assertEquals ( '' , $ result );
329+ }
330+
331+ /**
332+ * Test update_value with null converts to empty string.
333+ */
334+ public function test_update_value_null_converts_to_empty_string () {
335+ $ field = $ this ->get_field ();
336+
337+ $ result = $ this ->field_instance ->update_value ( null , $ this ->post_id , $ field );
338+
339+ $ this ->assertEquals ( '' , $ result );
340+ }
341+
342+ /**
343+ * Test update_value preserves valid link data.
344+ */
345+ public function test_update_value_preserves_valid_link () {
346+ $ field = $ this ->get_field ();
347+ $ link = array (
348+ 'title ' => 'Title ' ,
349+ 'url ' => 'https://valid.com ' ,
350+ 'target ' => '_blank ' ,
351+ );
352+
353+ $ result = $ this ->field_instance ->update_value ( $ link , $ this ->post_id , $ field );
354+
355+ $ this ->assertIsArray ( $ result );
356+ $ this ->assertEquals ( 'https://valid.com ' , $ result ['url ' ] );
357+ }
358+
359+ /**
360+ * Test format_value with URL return_format.
361+ *
362+ * Tests the return_format branch:
363+ * `if ( $field['return_format'] == 'url' ) { return $link['url']; }`
364+ */
365+ public function test_format_value_url_return_format () {
366+ $ field = $ this ->get_field ( array ( 'return_format ' => 'url ' ) );
367+ $ link = array (
368+ 'title ' => 'Title ' ,
369+ 'url ' => 'https://url-only.com ' ,
370+ 'target ' => '' ,
371+ );
372+
373+ $ result = $ this ->field_instance ->format_value ( $ link , $ this ->post_id , $ field );
374+
375+ $ this ->assertEquals ( 'https://url-only.com ' , $ result );
376+ }
377+
378+ /**
379+ * Test format_value with array return_format returns full link.
380+ */
381+ public function test_format_value_array_return_format () {
382+ $ field = $ this ->get_field ( array ( 'return_format ' => 'array ' ) );
383+ $ link = array (
384+ 'title ' => 'Full Title ' ,
385+ 'url ' => 'https://array.com ' ,
386+ 'target ' => '_self ' ,
387+ );
388+
389+ $ result = $ this ->field_instance ->format_value ( $ link , $ this ->post_id , $ field );
390+
391+ $ this ->assertIsArray ( $ result );
392+ $ this ->assertEquals ( 'Full Title ' , $ result ['title ' ] );
393+ $ this ->assertEquals ( 'https://array.com ' , $ result ['url ' ] );
394+ $ this ->assertEquals ( '_self ' , $ result ['target ' ] );
395+ }
221396}
0 commit comments