-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(subscriptions): add cancellation reason metadata #3568
Merged
+211
−14
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cf11b4f
feat: add cancellation reason metadata
chickenn00dle 06b1135
feat: add cancellation reason to esp sync
chickenn00dle 51cf666
fix: save subscription after meta update
chickenn00dle 0d33c90
fix: conditionally add esp cancellation reason
chickenn00dle a80e024
fix: remove uneeded comment
chickenn00dle 0701a6d
fix: add phpunit tests
chickenn00dle 8d0ec4d
fix: move cancellation_reason in fields array
chickenn00dle 99dd7da
fix: add feature flag
chickenn00dle eda0e35
fix: remove ff constant
chickenn00dle b3b4024
fix: correctly sync esp data
chickenn00dle ba22e30
fix: update unit test
chickenn00dle e5689aa
fix: add check for existing cancellation reason
chickenn00dle 9bff2cd
fix: init on plugin_loaded
chickenn00dle f321f5f
fix: move active/enabled checks
chickenn00dle ffca1d4
fix: handle pending-cancel to cancel transition
chickenn00dle 558d515
fix: update unit tests
chickenn00dle f1c44e9
fix: avoid infinite loop
leogermani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
includes/plugins/woocommerce-subscriptions/class-subscriptions-meta.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* WooCommerce Subscriptions meta class. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Main class. | ||
*/ | ||
class Subscriptions_Meta { | ||
const CANCELLATION_REASON_META_KEY = 'newspack_subscriptions_cancellation_reason'; | ||
const CANCELLATION_REASON_ADMIN_CANCELLED = 'manually-cancelled'; | ||
const CANCELLATION_REASON_ADMIN_PENDING_CANCEL = 'manually-pending-cancel'; | ||
const CANCELLATION_REASON_USER_CANCELLED = 'user-cancelled'; | ||
const CANCELLATION_REASON_USER_PENDING_CANCEL = 'user-pending-cancel'; | ||
|
||
/** | ||
* Initialize hooks and filters. | ||
*/ | ||
public static function init() { | ||
if ( ! WooCommerce_Subscriptions::is_enabled() ) { | ||
return; | ||
} | ||
|
||
add_action( 'woocommerce_subscription_status_updated', array( __CLASS__, 'maybe_record_cancelled_subscription_meta' ), 10, 3 ); | ||
} | ||
|
||
/** | ||
* Record woo custom field for cancelled subscriptions. | ||
* | ||
* @param WC_Subscription $subscription The subscription object. | ||
* @param string $to_status The status the subscription is changing to. | ||
* @param string $from_status The status the subscription is changing from. | ||
*/ | ||
public static function maybe_record_cancelled_subscription_meta( $subscription, $to_status, $from_status ) { | ||
// We only care about active, cancelled, and pending statuses. | ||
if ( ! in_array( $to_status, [ 'active', 'cancelled', 'pending-cancel' ], true ) || in_array( $from_status, [ 'cancelled', 'expired' ], true ) ) { | ||
return; | ||
} | ||
$meta_value = $subscription->get_meta( self::CANCELLATION_REASON_META_KEY, true ); | ||
if ( 'active' === $to_status && $meta_value ) { | ||
$subscription->delete_meta_data( self::CANCELLATION_REASON_META_KEY ); | ||
$subscription->save(); | ||
} | ||
if ( 'cancelled' === $to_status ) { | ||
if ( self::CANCELLATION_REASON_USER_PENDING_CANCEL === $meta_value ) { | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, self::CANCELLATION_REASON_USER_CANCELLED ); | ||
} elseif ( self::CANCELLATION_REASON_ADMIN_PENDING_CANCEL === $meta_value ) { | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, self::CANCELLATION_REASON_ADMIN_CANCELLED ); | ||
} else { | ||
$meta_value = is_admin() ? self::CANCELLATION_REASON_ADMIN_CANCELLED : self::CANCELLATION_REASON_USER_CANCELLED; | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, $meta_value ); | ||
} | ||
$subscription->save(); | ||
} | ||
if ( 'pending-cancel' === $to_status ) { | ||
$meta_value = is_admin() ? self::CANCELLATION_REASON_ADMIN_PENDING_CANCEL : self::CANCELLATION_REASON_USER_PENDING_CANCEL; | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, $meta_value ); | ||
$subscription->save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/unit-tests/plugins/woocommerce-subscriptions/class-subscriptions-meta.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Tests the WooCommerce Subscriptions integration class. | ||
* | ||
* @package Newspack\Tests | ||
*/ | ||
|
||
use Newspack\Subscriptions_Meta; | ||
use Newspack\WooCommerce_Subscriptions; | ||
|
||
/** | ||
* Test WooCommerce Subscriptions integration functionality. | ||
* | ||
* @group WooCommerce_Subscriptions_Integration | ||
*/ | ||
class Newspack_Test_Subscriptions_Meta extends WP_UnitTestCase { | ||
/** | ||
* Setup for the tests. | ||
*/ | ||
public static function set_up_before_class() { | ||
if ( ! defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) ) { | ||
define( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION', true ); | ||
} | ||
} | ||
|
||
/** | ||
* Test Subscriptions_Meta::maybe_record_cancelled_subscription_meta. | ||
*/ | ||
public function test_maybe_record_cancelled_subscription_meta() { | ||
$subscription = wcs_create_subscription(); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be empty before any updates.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'pending-cancel', 'cancelled' ); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be empty when subscription from-status is cancelled.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'pending-cancel', 'active' ); | ||
$this->assertEquals( | ||
Subscriptions_Meta::CANCELLATION_REASON_USER_PENDING_CANCEL, | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be user-pending-cancel when subscription is updated to pending-cancel from active status.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'active', 'pending-cancel' ); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be reset when subscription is updated to active from pending-cancel status.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'cancelled', 'pending-cancel', $subscription ); | ||
$this->assertEquals( | ||
Subscriptions_Meta::CANCELLATION_REASON_USER_CANCELLED, | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be set to user-cancelled when subscription is cancelled from pending-cancel status.' | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/unit-tests/plugins/woocommerce-subscriptions/class-woocommerce-subscriptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Tests the WooCommerce Subscriptions integration class. | ||
* | ||
* @package Newspack\Tests | ||
*/ | ||
|
||
use Newspack\WooCommerce_Subscriptions; | ||
use Newspack\Reader_Activation; | ||
|
||
/** | ||
* Test WooCommerce Subscriptions integration functionality. | ||
* | ||
* @group WooCommerce_Subscriptions_Integration | ||
*/ | ||
class Newspack_Test_WooCommerce_Subscriptions extends WP_UnitTestCase { | ||
/** | ||
* Setup for the tests. | ||
*/ | ||
public static function set_up_before_class() { | ||
if ( ! defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) ) { | ||
define( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION', true ); | ||
} | ||
} | ||
|
||
/** | ||
* Test WooCommerce_Subscriptions::is_active. | ||
*/ | ||
public function test_is_active() { | ||
$is_active = WooCommerce_Subscriptions::is_active(); | ||
$this->assertTrue( $is_active, 'WooCommerce Subscriptions integration should be active when WC_Subscriptions class exists.' ); | ||
} | ||
|
||
/** | ||
* Test WooCommerce_Subscriptions::is_enabled. | ||
*/ | ||
public function test_is_enabled() { | ||
$is_enabled = WooCommerce_Subscriptions::is_enabled(); | ||
$this->assertTrue( $is_enabled, 'WooCommerce Subscriptions integration should be enabled when Feature Flag is present.' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to test the case where FF is not defined, as well as when Reader Activation is not enabled, but could not find a way to modify globally defined constants between test runs. I tried
@preserveGlobalState
paired with@runInSeparateProcess
, but this ended up removing other globals the tested method relied on.Would love any advice on how to approach this if possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no good way to test constants... The best we could do would be to change the approach we use for Feature Flags and use something else to handle these things