2016-02-08 19:57:48 +00:00
|
|
|
<?php
|
|
|
|
namespace WooCommerce\Tests\Payment_Tokens;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Payment_Tokens
|
|
|
|
* @package WooCommerce\Tests\Payment_Tokens
|
|
|
|
*/
|
|
|
|
class Payment_Tokens extends \WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
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() {
|
|
|
|
$order = \WC_Helper_Order::create_order();
|
|
|
|
$this->assertEmpty( \WC_Payment_Tokens::get_order_tokens( $order->id ) );
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
update_post_meta( $order->id, '_payment_tokens', array( $token->get_id() ) );
|
|
|
|
|
|
|
|
$this->assertCount( 1, \WC_Payment_Tokens::get_order_tokens( $order->id ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-02-08 19:57:48 +00:00
|
|
|
$this->assertEmpty( \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->save();
|
|
|
|
|
2016-03-01 17:09:29 +00:00
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
$this->assertEmpty( \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_gateway_id( 'simplify_commerce' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
$this->assertCount( 1, \WC_Payment_Tokens::get_customer_tokens( 1, 'simplify_commerce' ) );
|
|
|
|
|
|
|
|
foreach ( \WC_Payment_Tokens::get_customer_tokens( 1, 'simplify_commerce' ) as $simplify_token ) {
|
|
|
|
$this->assertEquals( 'simplify_commerce', $simplify_token->get_gateway_id() );
|
|
|
|
}
|
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() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_gateway_id( 'simplify_commerce' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_default( true );
|
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
|
|
|
|
$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
|
|
|
|
$this->assertEquals( 'paypal', $default_token->get_gateway_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a customers default token, when there is no default token.
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
function test_wc_get_customer_default_token_returns_null_when_no_default_token() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_gateway_id( 'simplify_commerce' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
|
|
|
|
|
|
|
|
$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
|
|
|
|
$this->assertNull( $default_token );
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id = $token->get_id();
|
|
|
|
$get_token = \WC_Payment_Tokens::get( $token_id );
|
|
|
|
$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() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id = $token->get_id();
|
|
|
|
|
|
|
|
\WC_Payment_Tokens::delete( $token_id );
|
|
|
|
|
|
|
|
$get_token = \WC_Payment_Tokens::get( $token_id );
|
|
|
|
$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() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id = $token->get_id();
|
|
|
|
$this->assertEquals( 'CC', \WC_Payment_Tokens::get_token_type_by_id( $token_id ) );
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
$token = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id = $token->get_id();
|
|
|
|
$token->set_user_id( 1 );
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
$token2 = \WC_Helper_Payment_Token::create_cc_token();
|
|
|
|
$token_id_2 = $token2->get_id();
|
|
|
|
$token2->set_user_id( 1 );
|
|
|
|
$token2->save();
|
|
|
|
|
|
|
|
$this->assertFalse( $token->is_default() );
|
|
|
|
$this->assertFalse( $token2->is_default() );
|
|
|
|
|
|
|
|
\WC_Payment_Tokens::set_users_default( 1, $token_id_2 );
|
|
|
|
$token->read( $token_id );
|
|
|
|
$token2->read( $token_id_2 );
|
|
|
|
$this->assertFalse( $token->is_default() );
|
|
|
|
$this->assertTrue( $token2->is_default() );
|
|
|
|
|
|
|
|
\WC_Payment_Tokens::set_users_default( 1, $token_id );
|
|
|
|
$token->read( $token_id );
|
|
|
|
$token2->read( $token_id_2 );
|
|
|
|
$this->assertTrue( $token->is_default() );
|
|
|
|
$this->assertFalse( $token2->is_default() );
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:57:48 +00:00
|
|
|
}
|