Skip to content

Commit

Permalink
Merge pull request woocommerce#10533 from justinshreve/payment-token-…
Browse files Browse the repository at this point in the history
…fixes

Fix Payment Token 'set default' methods.
  • Loading branch information
justinshreve committed Mar 14, 2016
2 parents 3d3ce24 + ea26025 commit 353dc2d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
15 changes: 12 additions & 3 deletions includes/abstracts/abstract-wc-payment-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ public function update() {
update_metadata( 'payment_token', $this->get_id(), $meta_key, $meta_value );
}

// Make sure all other tokens are not set to default
if ( $this->is_default() && $this->get_user_id() > 0 ) {
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $this->get_id() );
}

do_action( 'woocommerce_payment_token_updated', $this->get_id() );
return true;
}
Expand All @@ -217,10 +222,9 @@ public function create() {
}

global $wpdb;

// Are there any other tokens? If not, set this token as default
if ( ! $this->is_default() && is_user_logged_in() ) {
$default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() );
if ( ! $this->is_default() && $this->get_user_id() > 0 ) {
$default_token = WC_Payment_Tokens::get_customer_default_token( $this->get_user_id() );
if ( is_null( $default_token ) ) {
$this->set_default( true );
}
Expand All @@ -232,6 +236,11 @@ public function create() {
add_metadata( 'payment_token', $token_id, $meta_key, $meta_value, true );
}

// Make sure all other tokens are not set to default
if ( $this->is_default() && $this->get_user_id() > 0 ) {
WC_Payment_Tokens::set_users_default( $this->get_user_id(), $token_id );
}

do_action( 'woocommerce_payment_token_created', $token_id );
return true;
}
Expand Down
12 changes: 11 additions & 1 deletion includes/class-wc-payment-tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,24 @@ public static function delete( $token_id ) {
* @param int $token_id The ID of the token that should be default
*/
public static function set_users_default( $user_id, $token_id ) {
global $wpdb; // DB queries so we avoid an infinite loop (update & create use this function)
$users_tokens = self::get_customer_tokens( $user_id );
foreach ( $users_tokens as $token ) {
if ( $token_id === $token->get_id() ) {
$token->set_default( true );
$wpdb->update(
$wpdb->prefix . 'woocommerce_payment_tokens',
array( 'is_default' => 1 ),
array( 'token_id' => $token->get_id(),
) );
} else {
$token->set_default( false );
$wpdb->update(
$wpdb->prefix . 'woocommerce_payment_tokens',
array( 'is_default' => 0 ),
array( 'token_id' => $token->get_id(),
) );
}
$token->update();
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/framework/helpers/class-wc-helper-payment-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class WC_Helper_Payment_Token {
* @since 2.6
* @return WC_Payment_Token_CC object
*/
public static function create_cc_token() {
public static function create_cc_token( $user_id = '' ) {
$token = new WC_Payment_Token_CC();
$token->set_last4( 1234 );
$token->set_expiry_month( '08' );
$token->set_expiry_year( '2016' );
$token->set_card_type( 'visa' );
$token->set_token( time() );
if ( ! empty( $user_id ) ) {
$token->set_user_id( $user_id );
}
$token->save();
return $token;
}
Expand Down Expand Up @@ -53,4 +56,3 @@ public static function create_stub_token( $extra ) {
}

}

65 changes: 35 additions & 30 deletions tests/unit-tests/payment-tokens/payment-tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
*/
class Payment_Tokens extends \WC_Unit_Test_Case {

public function setUp() {
parent::setUp();
$this->user_id = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
wp_set_current_user( $this->user_id );
}

/**
* Test getting tokens associated with an order.
* @since 2.6.0
Expand All @@ -27,40 +33,40 @@ function test_wc_payment_tokens_get_order_tokens() {
* @since 2.6.0
*/
function test_wc_payment_tokens_get_customer_tokens_no_gateway() {
$this->assertEmpty( \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$this->assertEmpty( \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$token->save();

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$token->save();

$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
}

/**
* 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 ) );
$this->assertEmpty( \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$token->set_gateway_id( 'simplify_commerce' );
$token->save();

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$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' ) );
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );
$this->assertCount( 1, \WC_Payment_Tokens::get_customer_tokens( $this->user_id, 'simplify_commerce' ) );

foreach ( \WC_Payment_Tokens::get_customer_tokens( 1, 'simplify_commerce' ) as $simplify_token ) {
foreach ( \WC_Payment_Tokens::get_customer_tokens( $this->user_id, 'simplify_commerce' ) as $simplify_token ) {
$this->assertEquals( 'simplify_commerce', $simplify_token->get_gateway_id() );
}
}
Expand All @@ -71,41 +77,42 @@ function test_wc_payment_tokens_get_customer_tokens_with_gateway() {
*/
function test_wc_get_customer_default_token() {
$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$token->set_gateway_id( 'simplify_commerce' );
$token->save();

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token->set_user_id( $this->user_id );
$token->set_default( true );
$token->set_gateway_id( 'paypal' );
$token->save();

$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );

$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
$default_token = \WC_Payment_Tokens::get_customer_default_token( $this->user_id );
$this->assertEquals( 'paypal', $default_token->get_gateway_id() );
}

/**
* Test getting a customers default token, when there is no default token.
* 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
* @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 );
function test_wc_get_customer_default_token_returns_first_created_when_no_default_token_set() {
$token = \WC_Helper_Payment_Token::create_cc_token( $this->user_id );
$token->set_gateway_id( 'simplify_commerce' );
$token->save();

$token = \WC_Helper_Payment_Token::create_cc_token();
$token->set_user_id( 1 );
$token = \WC_Helper_Payment_Token::create_cc_token( $this->user_id );
$token->set_gateway_id( 'paypal' );
$token->save();

$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( 1 ) );
$this->assertCount( 2, \WC_Payment_Tokens::get_customer_tokens( $this->user_id ) );

$default_token = \WC_Payment_Tokens::get_customer_default_token( 1 );
$this->assertNull( $default_token );
$default_token = \WC_Payment_Tokens::get_customer_default_token( $this->user_id );
$this->assertEquals( 'simplify_commerce', $default_token->get_gateway_id() );
}

/**
Expand Down Expand Up @@ -148,26 +155,24 @@ function test_wc_payment_tokens_get_type_by_id() {
* @since 2.6.0
*/
function test_wc_payment_tokens_set_users_default() {
$token = \WC_Helper_Payment_Token::create_cc_token();
$token = \WC_Helper_Payment_Token::create_cc_token( $this->user_id );
$token_id = $token->get_id();
$token->set_user_id( 1 );
$token->save();

$token2 = \WC_Helper_Payment_Token::create_cc_token();
$token2 = \WC_Helper_Payment_Token::create_cc_token( $this->user_id );
$token_id_2 = $token2->get_id();
$token2->set_user_id( 1 );
$token2->save();

$this->assertFalse( $token->is_default() );
$this->assertTrue( $token->is_default() ); // first created is default
$this->assertFalse( $token2->is_default() );

\WC_Payment_Tokens::set_users_default( 1, $token_id_2 );
\WC_Payment_Tokens::set_users_default( $this->user_id, $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 );
\WC_Payment_Tokens::set_users_default( $this->user_id, $token_id );
$token->read( $token_id );
$token2->read( $token_id_2 );
$this->assertTrue( $token->is_default() );
Expand Down

0 comments on commit 353dc2d

Please sign in to comment.