Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
80 stars 29 forks source link

Added break statements in switch (#533) #561

Closed 10goC closed 5 months ago

10goC commented 5 months ago

Fixes #533

Description

Missing break statements inside the switch ... case block in WC_Subscription::validate_date_updates() are causing an unexpected behavior when validating dates while changing subscription status.

According to PHP documentation,

when a case statement is found whose expression (...) matches the value of the switch expression (...) PHP continues to execute the statements until the end of the switch block.

So for example, when the case matches 'end' then all the following lines inside the block will be executed as if the expression would match 'cancelled', 'next_payment' and 'trial_end'.

subscription-bug-533

In this screenshot we can see how the debugger stops in the wrong case.

How to test this PR

  1. Generate a subscription.
  2. After the subscription period, generate renewal.
  3. Move the subscription status to "On hold".
  4. Make a PUT request to /wc/v3/subscriptions/{ID} with {"status":"cancelled"}
  5. Before the fix, the request will throw an error: The next_payment date must occur after the start date. After the fix, the request should change the subscription status to cancelled without errors.

Product impact

lkraav commented 5 months ago

So what happened here, the discovery was not correct?