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
4 changes: 3 additions & 1 deletion inc/checkout/class-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2770,7 +2770,9 @@ public function get_validation_rules() {
'billing_zip_code' => '',
];

foreach ($this->step['fields'] as $field_key => $field) {
$billing_rule_fields = $this->checkout_form ? $this->checkout_form->get_all_fields() : $this->step['fields'];

foreach ($billing_rule_fields as $field_key => $field) {
if ( ! is_array($field)) {
continue;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/WP_Ultimo/Checkout/Checkout_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,67 @@
unset($_REQUEST['billing_country'], $_REQUEST['billing_zip_code'], $_REQUEST['user_id']);
}

/**
* Test optional billing fields on earlier checkout steps relax final validation.
*/
public function test_get_validation_rules_relaxes_optional_billing_address_fields_from_all_steps(): void {

$form = new \WP_Ultimo\Models\Checkout_Form([
'name' => 'Optional Billing Multi-step ' . time(),
'slug' => 'optional-billing-multi-step-' . time(),
'settings' => [
[
'id' => 'billing-step',
'name' => 'Billing Step',
'fields' => [
[
'id' => 'billing_country',
'type' => 'select',
],
[
'id' => 'billing_zip_code',
'type' => 'text',
],
],
],
[
'id' => 'final-step',
'name' => 'Final Step',
'fields' => [
[
'id' => 'site_title',
'type' => 'text',
],
],
],
],
]);

$checkout = Checkout::get_instance();
$checkout->checkout_form = $form;
$checkout->step = $form->get_step('final-step', true);
$checkout->steps = $form->get_steps_to_show();
$checkout->step_name = 'final-step';

$this->ensure_session($checkout);

unset($_REQUEST['pre-flight'], $_REQUEST['checkout_form']);

$_REQUEST['billing_country'] = 'US';
$_REQUEST['billing_zip_code'] = '';
$_REQUEST['user_id'] = self::$customer->get_user_id();

$rules = $checkout->get_validation_rules();

$this->assertSame('country', $rules['billing_country']);
$this->assertSame('', $rules['billing_zip_code']);
$this->assertTrue($checkout->validate($rules));

unset($_REQUEST['billing_country'], $_REQUEST['billing_zip_code'], $_REQUEST['user_id']);

$checkout->checkout_form = null;
}

/**
* Test get_validation_rules returns array.
*/
Expand Down Expand Up @@ -1733,7 +1794,7 @@
public function test_get_js_validation_rules_is_filterable(): void {

add_filter('wu_checkout_js_validation_rules', function ($rules) {
$rules['custom_js_field'] = [['rule' => 'required', 'param' => null]];

Check warning on line 1797 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

When a multi-item array uses associative keys, each value should start on a new line.
return $rules;
});

Expand Down Expand Up @@ -1887,9 +1948,9 @@
*/
public function test_setup_checkout_sets_already_setup_flag(): void {

$checkout = Checkout::get_instance();

Check warning on line 1951 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 4 spaces
$reflection = new \ReflectionClass($checkout);

Check warning on line 1952 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces
$setup_prop = $reflection->getProperty('already_setup');

Check warning on line 1953 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces

if (PHP_VERSION_ID < 80100) {
$setup_prop->setAccessible(true);
Expand Down Expand Up @@ -1938,10 +1999,10 @@
*/
public function test_setup_checkout_initialises_session(): void {

$checkout = Checkout::get_instance();

Check warning on line 2002 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 6 spaces
$reflection = new \ReflectionClass($checkout);

Check warning on line 2003 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 4 spaces
$session_prop = $this->get_session_prop($reflection);

Check warning on line 2004 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces
$setup_prop = $reflection->getProperty('already_setup');

Check warning on line 2005 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 4 spaces

if (PHP_VERSION_ID < 80100) {
$setup_prop->setAccessible(true);
Expand Down Expand Up @@ -1976,8 +2037,8 @@

$setup_prop->setValue($checkout, false);

$_REQUEST['pre-flight'] = '1';

Check warning on line 2040 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 5 spaces
$_REQUEST['checkout_form'] = 'some-form';

Check warning on line 2041 in tests/WP_Ultimo/Checkout/Checkout_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces
$_REQUEST['some_field'] = 'some_value';

$checkout->setup_checkout();
Expand Down
Loading