Skip to content

Commit

Permalink
Add unit tests to Checkout Additional Fields (#45499)
Browse files Browse the repository at this point in the history
* try running tests

* add additional fields unit tests

* add keys back to Cart tests

* add extra checks to unit testing

* Add changefile(s) from automation for the following project(s): woocommerce

* address feedback

* fix linting issues

* add docblock

* escape errors

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
senadir and github-actions committed Mar 15, 2024
1 parent 0000d75 commit 6016aad
Show file tree
Hide file tree
Showing 6 changed files with 1,800 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev
Comment: Unit test PR

34 changes: 27 additions & 7 deletions plugins/woocommerce/src/Blocks/Domain/Services/CheckoutFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ public function register_checkout_field( $options ) {
$this->fields_locations[ $field_data['location'] ][] = $field_data['id'];
}

/**
* Deregister a checkout field.
*
* @param string $field_id The field ID.
*
* @internal
*/
public function deregister_checkout_field( $field_id ) {
if ( empty( $this->additional_fields[ $field_id ] ) ) {
return;
}

$location = $this->get_field_location( $field_id );

// Remove the field from the fields_locations array.
$this->fields_locations[ $location ] = array_diff( $this->fields_locations[ $location ], array( $field_id ) );

// Remove the field from the additional_fields array.
unset( $this->additional_fields[ $field_id ] );
}
/**
* Validates the "base" options (id, label, location) and shows warnings if they're not supplied.
*
Expand Down Expand Up @@ -381,13 +401,6 @@ private function validate_options( $options ) {
return false;
}

// Hidden fields are not supported right now. They will be registered with hidden => false.
if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
$message = sprintf( 'Registering a field with hidden set to true is not supported. The field "%s" will be registered as visible.', $id );
_doing_it_wrong( '__experimental_woocommerce_blocks_register_checkout_field', esc_html( $message ), '8.6.0' );
// Don't return here unlike the other fields because this is not an issue that will prevent registration.
}

if ( ! empty( $options['type'] ) ) {
if ( ! in_array( $options['type'], $this->supported_field_types, true ) ) {
$message = sprintf(
Expand All @@ -413,6 +426,13 @@ private function validate_options( $options ) {
return false;
}

// Hidden fields are not supported right now. They will be registered with hidden => false.
if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
$message = sprintf( 'Registering a field with hidden set to true is not supported. The field "%s" will be registered as visible.', $id );
_doing_it_wrong( '__experimental_woocommerce_blocks_register_checkout_field', esc_html( $message ), '8.6.0' );
// Don't return here unlike the other fields because this is not an issue that will prevent registration.
}

return true;
}

Expand Down
22 changes: 20 additions & 2 deletions plugins/woocommerce/src/Blocks/Domain/Services/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function __experimental_woocommerce_blocks_register_checkout_field( $options ) {
if ( ! $woocommerce_blocks_loaded_ran ) {
add_action(
'woocommerce_blocks_loaded',
function() use ( $options ) {
function () use ( $options ) {
__experimental_woocommerce_blocks_register_checkout_field( $options );
}
);
Expand All @@ -27,7 +27,25 @@ function() use ( $options ) {
$checkout_fields = Package::container()->get( CheckoutFields::class );
$result = $checkout_fields->register_checkout_field( $options );
if ( is_wp_error( $result ) ) {
throw new \Exception( $result->get_error_message() );
throw new \Exception( esc_attr( $result->get_error_message() ) );
}
}
}


if ( ! function_exists( '__internal_woocommerce_blocks_deregister_checkout_field' ) ) {
/**
* Deregister a checkout field.
*
* @param string $field_id Field ID.
* @throws \Exception If field deregistration fails.
* @internal
*/
function __internal_woocommerce_blocks_deregister_checkout_field( $field_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
$checkout_fields = Package::container()->get( CheckoutFields::class );
$result = $checkout_fields->deregister_checkout_field( $field_id );
if ( is_wp_error( $result ) ) {
throw new \Exception( esc_attr( $result->get_error_message() ) );
}
}
}

0 comments on commit 6016aad

Please sign in to comment.