-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Payment_Information tests * Add token request key variable to get_token_from_request * Add subscriptions sign up tests To avoid coupling the tests with WC Subscriptions code, this commit introduces a WCS_Mock helper that can be used to mock WCS global functions. It also uses regular orders, which should behave the same as a subscription one once WCS methods are mocked. * Add add_token_to_order tests * Add update_failing_payment_method tests * Use gateway add_token_to_order when updating failed payment methods * Add scheduled_subscription_payment tests * Use current year + 1 as the test tokens expiry date The test tokens are never really used, so we shoudln't have any issues if the token expiry date has passed. However, if WC starts validating the expiry date before creating an already expired token, they could start failing. This commit uses the next year for the token expiration, to avoid this if it ever happens.
- Loading branch information
Showing
12 changed files
with
865 additions
and
22 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
/** | ||
* Class Payment_Information_Test | ||
* | ||
* @package WooCommerce\Payments\Tests | ||
*/ | ||
|
||
use WCPay\DataTypes\Payment_Information; | ||
|
||
/** | ||
* Payment_Information unit tests. | ||
*/ | ||
class Payment_Information_Test extends WP_UnitTestCase { | ||
const PAYMENT_METHOD_REQUEST_KEY = 'wcpay-payment-method'; | ||
const PAYMENT_METHOD = 'pm_mock'; | ||
const TOKEN_REQUEST_KEY = 'wc-' . \WC_Payment_Gateway_WCPay::GATEWAY_ID . '-payment-token'; | ||
const TOKEN = 'pm_mock_token'; | ||
|
||
/** | ||
* WC token to be used in tests. | ||
* @var WC_Payment_Token_CC | ||
*/ | ||
private $token; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
$this->token = WC_Helper_Token::create_token( self::TOKEN ); | ||
} | ||
|
||
public function test_requires_payment_method_or_token() { | ||
$this->expectException( Exception::class ); | ||
$this->expectExceptionMessage( 'Invalid payment method. Please input a new card number.' ); | ||
|
||
$payment_information = new Payment_Information( '' ); | ||
} | ||
|
||
public function test_is_merchant_initiated_returns_off_session() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD, null, true ); | ||
$this->assertTrue( $payment_information->is_merchant_initiated() ); | ||
} | ||
|
||
public function test_get_payment_method_returns_payment_method() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD, null ); | ||
$this->assertEquals( self::PAYMENT_METHOD, $payment_information->get_payment_method() ); | ||
} | ||
|
||
public function test_get_payment_method_returns_token_if_present() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD, $this->token ); | ||
$this->assertEquals( self::TOKEN, $payment_information->get_payment_method() ); | ||
} | ||
|
||
public function test_get_payment_token_returns_token() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD, $this->token ); | ||
$this->assertEquals( $this->token, $payment_information->get_payment_token() ); | ||
} | ||
|
||
public function is_using_saved_payment_method_returns_true_if_token() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD, $this->token ); | ||
$this->assertTrue( $payment_information->is_using_saved_payment_method() ); | ||
} | ||
|
||
public function test_set_token_updates_token() { | ||
$payment_information = new Payment_Information( self::PAYMENT_METHOD ); | ||
$this->assertFalse( $payment_information->is_using_saved_payment_method() ); | ||
|
||
$payment_information->set_token( $this->token ); | ||
$this->assertEquals( $this->token, $payment_information->get_payment_token() ); | ||
$this->assertTrue( $payment_information->is_using_saved_payment_method() ); | ||
} | ||
|
||
public function test_get_payment_method_from_request() { | ||
$payment_method = Payment_Information::get_payment_method_from_request( | ||
[ self::PAYMENT_METHOD_REQUEST_KEY => self::PAYMENT_METHOD ] | ||
); | ||
$this->assertEquals( self::PAYMENT_METHOD, $payment_method ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_null_when_not_set() { | ||
$token = Payment_Information::get_token_from_request( [] ); | ||
$this->assertNull( $token ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_null_when_new() { | ||
$token = Payment_Information::get_token_from_request( | ||
[ self::TOKEN_REQUEST_KEY => 'new' ] | ||
); | ||
$this->assertNull( $token ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_null_when_invalid() { | ||
$token = Payment_Information::get_token_from_request( | ||
[ self::TOKEN_REQUEST_KEY => $this->token->get_id() + 1 ] | ||
); | ||
$this->assertNull( $token ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_null_when_wrong_gateway() { | ||
$this->token->set_gateway_id( 'wrong_gateway' ); | ||
$this->token->save(); | ||
$token = Payment_Information::get_token_from_request( | ||
[ self::TOKEN_REQUEST_KEY => $this->token->get_id() ] | ||
); | ||
$this->assertNull( $token ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_null_when_wrong_customer() { | ||
$this->token->set_user_id( get_current_user_id() + 1 ); | ||
$this->token->save(); | ||
$token = Payment_Information::get_token_from_request( | ||
[ self::TOKEN_REQUEST_KEY => $this->token->get_id() ] | ||
); | ||
$this->assertNull( $token ); | ||
} | ||
|
||
public function test_get_token_from_request_returns_token() { | ||
$token = Payment_Information::get_token_from_request( | ||
[ self::TOKEN_REQUEST_KEY => $this->token->get_id() ] | ||
); | ||
$this->assertEquals( $this->token, $token ); | ||
} | ||
|
||
public function test_from_payment_request_with_token() { | ||
$payment_information = Payment_Information::from_payment_request( | ||
[ | ||
self::PAYMENT_METHOD_REQUEST_KEY => self::PAYMENT_METHOD, | ||
self::TOKEN_REQUEST_KEY => $this->token->get_id(), | ||
], | ||
true | ||
); | ||
$this->assertEquals( self::TOKEN, $payment_information->get_payment_method() ); | ||
$this->assertTrue( $payment_information->is_using_saved_payment_method() ); | ||
$this->assertEquals( $this->token, $payment_information->get_payment_token() ); | ||
$this->assertTrue( $payment_information->is_merchant_initiated() ); | ||
} | ||
|
||
public function test_from_payment_request_without_token() { | ||
$payment_information = Payment_Information::from_payment_request( | ||
[ self::PAYMENT_METHOD_REQUEST_KEY => self::PAYMENT_METHOD ] | ||
); | ||
$this->assertEquals( self::PAYMENT_METHOD, $payment_information->get_payment_method() ); | ||
$this->assertFalse( $payment_information->is_using_saved_payment_method() ); | ||
$this->assertFalse( $payment_information->is_merchant_initiated() ); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Subscription helpers. | ||
* | ||
* @package WooCommerce\Payments\Tests | ||
*/ | ||
|
||
// Set up subscriptions mocks. | ||
function wcs_order_contains_subscription( $order ) { | ||
return call_user_func( WCS_Mock::$wcs_order_contains_subscription, $order ); | ||
} | ||
|
||
function wcs_get_subscriptions_for_order( $order ) { | ||
return call_user_func( WCS_Mock::$wcs_get_subscriptions_for_order, $order ); | ||
} | ||
|
||
/** | ||
* Class WCS_Mock. | ||
* | ||
* This helper class should ONLY be used for unit tests!. | ||
*/ | ||
class WCS_Mock { | ||
/** | ||
* wcs_order_contains_subscription mock. | ||
* | ||
* @var function | ||
*/ | ||
public static $wcs_order_contains_subscription = null; | ||
|
||
/** | ||
* wcs_get_subscriptions_for_order mock. | ||
* | ||
* @var function | ||
*/ | ||
public static $wcs_get_subscriptions_for_order = null; | ||
|
||
public static function set_wcs_order_contains_subscription( $function ) { | ||
self::$wcs_order_contains_subscription = $function; | ||
} | ||
|
||
public static function set_wcs_get_subscriptions_for_order( $function ) { | ||
self::$wcs_get_subscriptions_for_order = $function; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
/** | ||
* Token helpers. | ||
* | ||
* @package WooCommerce/Tests | ||
*/ | ||
|
||
/** | ||
* Class WC_Helper_Token. | ||
* | ||
* This helper class should ONLY be used for unit tests!. | ||
*/ | ||
class WC_Helper_Token { | ||
|
||
/** | ||
* Create a token. | ||
* | ||
* @param string $payment_method Token payment method. | ||
* @param int $user_id ID of the token's user, defaults to get_current_user_id(). | ||
* @param string $gateway Token's Gateway ID, default to WC_Payment_Gateway_WCPay::GATEWAY_ID | ||
*/ | ||
public static function create_token( $payment_method, $user_id = null, $gateway = WC_Payment_Gateway_WCPay::GATEWAY_ID ) { | ||
$token = new WC_Payment_Token_CC(); | ||
$token->set_token( $payment_method ); | ||
$token->set_gateway_id( $gateway ); | ||
$token->set_user_id( $user_id ?? get_current_user_id() ); | ||
$token->set_card_type( 'visa' ); | ||
$token->set_last4( '4242' ); | ||
$token->set_expiry_month( 6 ); | ||
$token->set_expiry_year( intval( gmdate( 'Y' ) ) + 1 ); | ||
$token->save(); | ||
|
||
return WC_Payment_Tokens::get( $token->get_id() ); | ||
} | ||
} |
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.