Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions tests/php/includes/fields/test-class-acf-field-checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,150 @@ public function test_all_choices_selected() {
$this->assertIsArray( $result );
$this->assertCount( 3, $result );
}

/**
* Test validate_value rejects empty custom values.
*
* Tests the validate_value custom value check:
* `if ( empty( $value ) && $value !== '0' ) { return __( 'Checkbox custom values cannot be empty...' ); }`
*/
public function test_validate_value_rejects_empty_custom_value() {
$field = $this->get_field( array( 'allow_custom' => 1 ) );

// With allow_custom enabled, empty values in array should fail.
$valid = $this->field_instance->validate_value( true, array( 'red', '' ), $field, 'acf[field_checkbox_test]' );

$this->assertIsString( $valid ); // Returns error message string.
$this->assertStringContainsString( 'cannot be empty', $valid );
}

/**
* Test validate_value allows '0' as custom value.
*
* Tests the validate_value '0' exception:
* `if ( empty( $value ) && $value !== '0' )`
*/
public function test_validate_value_allows_zero_string_custom() {
$field = $this->get_field( array( 'allow_custom' => 1 ) );

// '0' should be allowed even though empty() returns true for it.
$valid = $this->field_instance->validate_value( true, array( 'red', '0' ), $field, 'acf[field_checkbox_test]' );

$this->assertTrue( $valid );
}

/**
* Test validate_value skips validation without allow_custom.
*
* Tests the early return:
* `if ( ! is_array( $value ) || empty( $field['allow_custom'] ) ) { return $valid; }`
*/
public function test_validate_value_skips_without_allow_custom() {
$field = $this->get_field( array( 'allow_custom' => 0 ) );

// Without allow_custom, validation returns passed $valid unchanged.
$valid = $this->field_instance->validate_value( true, array( 'red', '' ), $field, 'acf[field_checkbox_test]' );

$this->assertTrue( $valid );
}

/**
* Test validate_value skips validation for non-array.
*/
public function test_validate_value_skips_for_non_array() {
$field = $this->get_field( array( 'allow_custom' => 1 ) );

// Non-array values return $valid unchanged.
$valid = $this->field_instance->validate_value( true, 'red', $field, 'acf[field_checkbox_test]' );

$this->assertTrue( $valid );
}

/**
* Test format_value returns empty array for null.
*
* Tests the acf_is_empty check:
* `if ( acf_is_empty( $value ) ) { return array(); }`
*/
public function test_format_value_null_returns_empty_array() {
$field = $this->get_field();

$result = $this->field_instance->format_value( null, $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertEmpty( $result );
}

/**
* Test format_value returns empty array for empty string.
*/
public function test_format_value_empty_string_returns_empty_array() {
$field = $this->get_field();

$result = $this->field_instance->format_value( '', $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertEmpty( $result );
}

/**
* Test format_value converts single value to array.
*
* Tests the acf_array conversion:
* `$value = acf_array( $value );`
*/
public function test_format_value_converts_string_to_array() {
$field = $this->get_field( array( 'return_format' => 'value' ) );

// Single string value should be converted to array.
$result = $this->field_instance->format_value( 'red', $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertContains( 'red', $result );
}

/**
* Test get_rest_schema includes enum without allow_custom.
*
* Tests the enum generation branch:
* `if ( ! empty( $field['allow_custom'] ) ) { return $schema; }`
*/
public function test_get_rest_schema_includes_enum() {
$field = $this->get_field( array( 'allow_custom' => 0 ) );

$schema = $this->field_instance->get_rest_schema( $field );

$this->assertArrayHasKey( 'items', $schema );
$this->assertArrayHasKey( 'enum', $schema['items'] );
}

/**
* Test get_rest_schema excludes enum with allow_custom.
*
* Tests the early return when allow_custom is enabled.
*/
public function test_get_rest_schema_no_enum_with_allow_custom() {
$field = $this->get_field( array( 'allow_custom' => 1 ) );

$schema = $this->field_instance->get_rest_schema( $field );

// With allow_custom enabled, enum should NOT be set in items.
$this->assertArrayHasKey( 'items', $schema );
$this->assertArrayNotHasKey( 'enum', $schema['items'] );
}

/**
* Test update_value returns empty array unchanged.
*
* Tests the early return:
* `if ( empty( $value ) ) { return $value; }`
*/
public function test_update_value_empty_array_returns_early() {
$field = $this->get_field();

$result = $this->field_instance->update_value( array(), $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertEmpty( $result );
}
}
175 changes: 175 additions & 0 deletions tests/php/includes/fields/test-class-acf-field-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,179 @@ public function test_internal_url() {

$this->assertEquals( '/about/', $result['url'] );
}

/**
* Test get_link with array value (ACF 5.6.0+).
*
* Tests the array merge branch:
* `if ( is_array( $value ) ) { $link = array_merge( $link, $value ); }`
*/
public function test_get_link_array_format() {
$input = array(
'title' => 'Custom Title',
'url' => 'https://custom.com',
'target' => '_blank',
);

$result = $this->field_instance->get_link( $input );

$this->assertEquals( 'Custom Title', $result['title'] );
$this->assertEquals( 'https://custom.com', $result['url'] );
$this->assertEquals( '_blank', $result['target'] );
}

/**
* Test get_link with string value (legacy ACF < 5.6.0).
*
* Tests the string branch:
* `elseif ( is_string( $value ) ) { $link['url'] = $value; }`
*/
public function test_get_link_string_format() {
$result = $this->field_instance->get_link( 'https://example.com/page' );

$this->assertEquals( '', $result['title'] );
$this->assertEquals( 'https://example.com/page', $result['url'] );
$this->assertEquals( '', $result['target'] );
}

/**
* Test get_link with empty string returns defaults.
*/
public function test_get_link_empty_string() {
$result = $this->field_instance->get_link( '' );

$this->assertEquals( '', $result['title'] );
$this->assertEquals( '', $result['url'] );
$this->assertEquals( '', $result['target'] );
}

/**
* Test get_link with partial array fills defaults.
*/
public function test_get_link_partial_array() {
$result = $this->field_instance->get_link( array( 'url' => 'https://partial.com' ) );

$this->assertEquals( '', $result['title'] );
$this->assertEquals( 'https://partial.com', $result['url'] );
$this->assertEquals( '', $result['target'] );
}

/**
* Test validate_value returns early when not required.
*
* Tests the early return:
* `if ( ! $field['required'] ) { return $valid; }`
*/
public function test_validate_value_not_required_returns_valid() {
$field = $this->get_field( array( 'required' => 0 ) );

// Even with empty value, should return passed $valid when not required.
$valid = $this->field_instance->validate_value( true, '', $field, 'acf[field_link_test]' );

$this->assertTrue( $valid );
}

/**
* Test validate_value with empty URL when required fails.
*
* Tests the URL check:
* `if ( empty( $value ) || empty( $value['url'] ) ) { return false; }`
*/
public function test_validate_value_empty_url_required_fails() {
$field = $this->get_field( array( 'required' => 1 ) );
$link = array(
'title' => 'Has title',
'url' => '', // Empty URL.
'target' => '',
);

$valid = $this->field_instance->validate_value( true, $link, $field, 'acf[field_link_test]' );

$this->assertFalse( $valid );
}

/**
* Test update_value converts empty URL array to empty string.
*
* Tests the empty check:
* `if ( empty( $value ) || empty( $value['url'] ) ) { $value = ''; }`
*/
public function test_update_value_empty_url_converts_to_empty_string() {
$field = $this->get_field();
$link = array(
'title' => 'Has title',
'url' => '',
'target' => '_blank',
);

$result = $this->field_instance->update_value( $link, $this->post_id, $field );

$this->assertEquals( '', $result );
}

/**
* Test update_value with null converts to empty string.
*/
public function test_update_value_null_converts_to_empty_string() {
$field = $this->get_field();

$result = $this->field_instance->update_value( null, $this->post_id, $field );

$this->assertEquals( '', $result );
}

/**
* Test update_value preserves valid link data.
*/
public function test_update_value_preserves_valid_link() {
$field = $this->get_field();
$link = array(
'title' => 'Title',
'url' => 'https://valid.com',
'target' => '_blank',
);

$result = $this->field_instance->update_value( $link, $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertEquals( 'https://valid.com', $result['url'] );
}

/**
* Test format_value with URL return_format.
*
* Tests the return_format branch:
* `if ( $field['return_format'] == 'url' ) { return $link['url']; }`
*/
public function test_format_value_url_return_format() {
$field = $this->get_field( array( 'return_format' => 'url' ) );
$link = array(
'title' => 'Title',
'url' => 'https://url-only.com',
'target' => '',
);

$result = $this->field_instance->format_value( $link, $this->post_id, $field );

$this->assertEquals( 'https://url-only.com', $result );
}

/**
* Test format_value with array return_format returns full link.
*/
public function test_format_value_array_return_format() {
$field = $this->get_field( array( 'return_format' => 'array' ) );
$link = array(
'title' => 'Full Title',
'url' => 'https://array.com',
'target' => '_self',
);

$result = $this->field_instance->format_value( $link, $this->post_id, $field );

$this->assertIsArray( $result );
$this->assertEquals( 'Full Title', $result['title'] );
$this->assertEquals( 'https://array.com', $result['url'] );
$this->assertEquals( '_self', $result['target'] );
}
}
Loading
Loading