Skip to content

Commit 48b7a29

Browse files
authored
Tests: Add more tests for Field classes (#344)
1 parent 016e0ff commit 48b7a29

9 files changed

+1949
-10
lines changed

tests/php/includes/fields/test-class-acf-field-checkbox.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,150 @@ public function test_all_choices_selected() {
227227
$this->assertIsArray( $result );
228228
$this->assertCount( 3, $result );
229229
}
230+
231+
/**
232+
* Test validate_value rejects empty custom values.
233+
*
234+
* Tests the validate_value custom value check:
235+
* `if ( empty( $value ) && $value !== '0' ) { return __( 'Checkbox custom values cannot be empty...' ); }`
236+
*/
237+
public function test_validate_value_rejects_empty_custom_value() {
238+
$field = $this->get_field( array( 'allow_custom' => 1 ) );
239+
240+
// With allow_custom enabled, empty values in array should fail.
241+
$valid = $this->field_instance->validate_value( true, array( 'red', '' ), $field, 'acf[field_checkbox_test]' );
242+
243+
$this->assertIsString( $valid ); // Returns error message string.
244+
$this->assertStringContainsString( 'cannot be empty', $valid );
245+
}
246+
247+
/**
248+
* Test validate_value allows '0' as custom value.
249+
*
250+
* Tests the validate_value '0' exception:
251+
* `if ( empty( $value ) && $value !== '0' )`
252+
*/
253+
public function test_validate_value_allows_zero_string_custom() {
254+
$field = $this->get_field( array( 'allow_custom' => 1 ) );
255+
256+
// '0' should be allowed even though empty() returns true for it.
257+
$valid = $this->field_instance->validate_value( true, array( 'red', '0' ), $field, 'acf[field_checkbox_test]' );
258+
259+
$this->assertTrue( $valid );
260+
}
261+
262+
/**
263+
* Test validate_value skips validation without allow_custom.
264+
*
265+
* Tests the early return:
266+
* `if ( ! is_array( $value ) || empty( $field['allow_custom'] ) ) { return $valid; }`
267+
*/
268+
public function test_validate_value_skips_without_allow_custom() {
269+
$field = $this->get_field( array( 'allow_custom' => 0 ) );
270+
271+
// Without allow_custom, validation returns passed $valid unchanged.
272+
$valid = $this->field_instance->validate_value( true, array( 'red', '' ), $field, 'acf[field_checkbox_test]' );
273+
274+
$this->assertTrue( $valid );
275+
}
276+
277+
/**
278+
* Test validate_value skips validation for non-array.
279+
*/
280+
public function test_validate_value_skips_for_non_array() {
281+
$field = $this->get_field( array( 'allow_custom' => 1 ) );
282+
283+
// Non-array values return $valid unchanged.
284+
$valid = $this->field_instance->validate_value( true, 'red', $field, 'acf[field_checkbox_test]' );
285+
286+
$this->assertTrue( $valid );
287+
}
288+
289+
/**
290+
* Test format_value returns empty array for null.
291+
*
292+
* Tests the acf_is_empty check:
293+
* `if ( acf_is_empty( $value ) ) { return array(); }`
294+
*/
295+
public function test_format_value_null_returns_empty_array() {
296+
$field = $this->get_field();
297+
298+
$result = $this->field_instance->format_value( null, $this->post_id, $field );
299+
300+
$this->assertIsArray( $result );
301+
$this->assertEmpty( $result );
302+
}
303+
304+
/**
305+
* Test format_value returns empty array for empty string.
306+
*/
307+
public function test_format_value_empty_string_returns_empty_array() {
308+
$field = $this->get_field();
309+
310+
$result = $this->field_instance->format_value( '', $this->post_id, $field );
311+
312+
$this->assertIsArray( $result );
313+
$this->assertEmpty( $result );
314+
}
315+
316+
/**
317+
* Test format_value converts single value to array.
318+
*
319+
* Tests the acf_array conversion:
320+
* `$value = acf_array( $value );`
321+
*/
322+
public function test_format_value_converts_string_to_array() {
323+
$field = $this->get_field( array( 'return_format' => 'value' ) );
324+
325+
// Single string value should be converted to array.
326+
$result = $this->field_instance->format_value( 'red', $this->post_id, $field );
327+
328+
$this->assertIsArray( $result );
329+
$this->assertContains( 'red', $result );
330+
}
331+
332+
/**
333+
* Test get_rest_schema includes enum without allow_custom.
334+
*
335+
* Tests the enum generation branch:
336+
* `if ( ! empty( $field['allow_custom'] ) ) { return $schema; }`
337+
*/
338+
public function test_get_rest_schema_includes_enum() {
339+
$field = $this->get_field( array( 'allow_custom' => 0 ) );
340+
341+
$schema = $this->field_instance->get_rest_schema( $field );
342+
343+
$this->assertArrayHasKey( 'items', $schema );
344+
$this->assertArrayHasKey( 'enum', $schema['items'] );
345+
}
346+
347+
/**
348+
* Test get_rest_schema excludes enum with allow_custom.
349+
*
350+
* Tests the early return when allow_custom is enabled.
351+
*/
352+
public function test_get_rest_schema_no_enum_with_allow_custom() {
353+
$field = $this->get_field( array( 'allow_custom' => 1 ) );
354+
355+
$schema = $this->field_instance->get_rest_schema( $field );
356+
357+
// With allow_custom enabled, enum should NOT be set in items.
358+
$this->assertArrayHasKey( 'items', $schema );
359+
$this->assertArrayNotHasKey( 'enum', $schema['items'] );
360+
}
361+
362+
/**
363+
* Test update_value returns empty array unchanged.
364+
*
365+
* Tests the early return:
366+
* `if ( empty( $value ) ) { return $value; }`
367+
*/
368+
public function test_update_value_empty_array_returns_early() {
369+
$field = $this->get_field();
370+
371+
$result = $this->field_instance->update_value( array(), $this->post_id, $field );
372+
373+
$this->assertIsArray( $result );
374+
$this->assertEmpty( $result );
375+
}
230376
}

tests/php/includes/fields/test-class-acf-field-link.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)