2016-02-08 19:57:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Payment_Tokens
|
|
|
|
* @package WooCommerce\Tests\Payment_Tokens
|
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Payment_Tokens extends WC_Unit_Test_Case {
|
2016-02-08 19:57:48 +00:00
|
|
|
|
2016-03-10 18:19:08 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->user_id = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
|
|
|
|
wp_set_current_user( $this->user_id );
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:57:48 +00:00
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test getting tokens associated with an order.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_get_order_tokens() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$order = WC_Helper_Order::create_order();
|
2016-06-22 11:29:23 +00:00
|
|
|
$this->assertEmpty( WC_Payment_Tokens::get_order_tokens( $order->get_id() ) );
|
2016-02-08 19:57:48 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-06-22 11:29:23 +00:00
|
|
|
update_post_meta( $order->get_id(), '_payment_tokens', array( $token->get_id() ) );
|
2016-02-08 19:57:48 +00:00
|
|
|
|
2016-06-22 11:29:23 +00:00
|
|
|
$this->assertCount( 1, WC_Payment_Tokens::get_order_tokens( $order->get_id() ) );
|
2016-02-08 19:57:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test getting tokens associated with a user and no gateway ID.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
2016-03-01 17:09:29 +00:00
|
|
|
function test_wc_payment_tokens_get_customer_tokens_no_gateway() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertEmpty( WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-02-08 19:57:48 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-03-01 17:09:29 +00:00
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertCount( 2, WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-03-01 17:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting tokens associated with a user and for a specific gateway.
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_get_customer_tokens_with_gateway() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertEmpty( WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-03-01 17:09:29 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-05-27 10:29:19 +00:00
|
|
|
$token->set_gateway_id( 'bacs' );
|
2016-03-01 17:09:29 +00:00
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-03-01 17:09:29 +00:00
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertCount( 2, WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-05-27 10:29:19 +00:00
|
|
|
$this->assertCount( 1, WC_Payment_Tokens::get_customer_tokens( $this->user_id, 'bacs' ) );
|
2016-03-01 17:09:29 +00:00
|
|
|
|
2016-05-27 10:29:19 +00:00
|
|
|
foreach ( WC_Payment_Tokens::get_customer_tokens( $this->user_id, 'bacs' ) as $gateway_token ) {
|
|
|
|
$this->assertEquals( 'bacs', $gateway_token->get_gateway_id() );
|
2016-03-01 17:09:29 +00:00
|
|
|
}
|
2016-02-08 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:37:06 +00:00
|
|
|
/**
|
|
|
|
* Test getting a customers default token.
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
function test_wc_get_customer_default_token() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-05-27 10:29:19 +00:00
|
|
|
$token->set_gateway_id( 'bacs' );
|
2016-03-01 19:37:06 +00:00
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-03-10 18:19:08 +00:00
|
|
|
$token->set_user_id( $this->user_id );
|
2016-03-01 19:37:06 +00:00
|
|
|
$token->set_default( true );
|
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertCount( 2, WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-03-01 19:37:06 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$default_token = WC_Payment_Tokens::get_customer_default_token( $this->user_id );
|
2016-03-01 19:37:06 +00:00
|
|
|
$this->assertEquals( 'paypal', $default_token->get_gateway_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-10 18:19:08 +00:00
|
|
|
* Test getting a customers default token, when there no token is expictly set.
|
|
|
|
* This should be the "first created".
|
|
|
|
* @see WC_Payment_Token::create()
|
|
|
|
* @group failing
|
2016-03-01 19:37:06 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
2016-03-10 18:19:08 +00:00
|
|
|
function test_wc_get_customer_default_token_returns_first_created_when_no_default_token_set() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token( $this->user_id );
|
2016-05-27 10:29:19 +00:00
|
|
|
$token->set_gateway_id( 'bacs' );
|
2016-03-01 19:37:06 +00:00
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token( $this->user_id );
|
2016-03-01 19:37:06 +00:00
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertCount( 2, WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
|
2016-03-01 19:37:06 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$default_token = WC_Payment_Tokens::get_customer_default_token( $this->user_id );
|
2016-05-27 10:29:19 +00:00
|
|
|
$this->assertEquals( 'bacs', $default_token->get_gateway_id() );
|
2016-03-01 19:37:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 19:57:48 +00:00
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test getting a token by ID.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_get() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id = $token->get_id();
|
2016-03-23 12:14:13 +00:00
|
|
|
$get_token = WC_Payment_Tokens::get( $token_id );
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertEquals( $token->get_token(), $get_token->get_token() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test deleting a token by ID.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_delete() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token_id = $token->get_id();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
WC_Payment_Tokens::delete( $token_id );
|
2016-02-08 19:57:48 +00:00
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$get_token = WC_Payment_Tokens::get( $token_id );
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertNull( $get_token );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test getting a token's type by ID.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_get_type_by_id() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token_id = $token->get_id();
|
2016-03-23 12:14:13 +00:00
|
|
|
$this->assertEquals( 'CC', WC_Payment_Tokens::get_token_type_by_id( $token_id ) );
|
2016-02-08 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 20:19:34 +00:00
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test setting a users default token.
|
|
|
|
* @since 2.6.0
|
2016-02-09 20:19:34 +00:00
|
|
|
*/
|
|
|
|
function test_wc_payment_tokens_set_users_default() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_cc_token( $this->user_id );
|
2016-02-09 20:19:34 +00:00
|
|
|
$token_id = $token->get_id();
|
|
|
|
$token->save();
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$token2 = WC_Helper_Payment_Token::create_cc_token( $this->user_id );
|
2016-02-09 20:19:34 +00:00
|
|
|
$token_id_2 = $token2->get_id();
|
|
|
|
$token2->save();
|
|
|
|
|
2016-03-10 18:19:08 +00:00
|
|
|
$this->assertTrue( $token->is_default() ); // first created is default
|
2016-02-09 20:19:34 +00:00
|
|
|
$this->assertFalse( $token2->is_default() );
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
WC_Payment_Tokens::set_users_default( $this->user_id, $token_id_2 );
|
2016-11-14 14:20:41 +00:00
|
|
|
$token = new WC_Payment_Token_CC( $token_id );
|
|
|
|
$token2 = new WC_Payment_Token_CC( $token_id_2 );
|
2016-02-09 20:19:34 +00:00
|
|
|
$this->assertFalse( $token->is_default() );
|
|
|
|
$this->assertTrue( $token2->is_default() );
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
WC_Payment_Tokens::set_users_default( $this->user_id, $token_id );
|
2016-11-14 14:20:41 +00:00
|
|
|
$token = new WC_Payment_Token_CC( $token_id );
|
|
|
|
$token2 = new WC_Payment_Token_CC( $token_id_2 );
|
2016-02-09 20:19:34 +00:00
|
|
|
$this->assertTrue( $token->is_default() );
|
|
|
|
$this->assertFalse( $token2->is_default() );
|
|
|
|
}
|
2016-02-08 19:57:48 +00:00
|
|
|
}
|