2016-02-08 19:57:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Payment_Token
|
|
|
|
* @package WooCommerce\Tests\Payment_Tokens
|
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Payment_Token extends WC_Unit_Test_Case {
|
2016-02-08 19:57:48 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test get type returns the class name/type.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_get_type() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertEquals( 'stub', $token->get_type() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-18 18:24:06 +00:00
|
|
|
* Test set/get token to make sure it returns the passed token.
|
2016-03-01 17:09:29 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function test_wc_payment_token_token() {
|
2016-02-08 19:57:48 +00:00
|
|
|
$raw_token = time() . ' ' . __FUNCTION__;
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_token( $raw_token );
|
|
|
|
$this->assertEquals( $raw_token, $token->get_token() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-18 18:24:06 +00:00
|
|
|
* Test set/get user ID to make sure it passes the correct ID.
|
2016-03-01 17:09:29 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function test_wc_payment_user_id() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-03-18 18:24:06 +00:00
|
|
|
$token->set_user_id( 1 );
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertEquals( 1, $token->get_user_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test get user ID to make sure it returns 0 if there is no user ID.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_get_user_id_defaults_to_0() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertEquals( 0, $token->get_user_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-18 18:24:06 +00:00
|
|
|
* Test get/set the gateway ID.
|
2016-03-01 17:09:29 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function test_wc_payment_gateway_id() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_gateway_id( 'paypal' );
|
|
|
|
$this->assertEquals( 'paypal', $token->get_gateway_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-18 18:24:06 +00:00
|
|
|
* Test set/is a token as default.
|
2016-03-01 17:09:29 +00:00
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
2016-03-18 18:24:06 +00:00
|
|
|
public function test_wc_payment_token_is_default() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub( 1 );
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_default( true );
|
|
|
|
$this->assertTrue( $token->is_default() );
|
|
|
|
$token->set_default( false );
|
|
|
|
$this->assertFalse( $token->is_default() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test that get_data returns the correct internal representation for a token.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_get_data() {
|
|
|
|
$raw_token = time() . ' ' . __FUNCTION__;
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-03-18 18:24:06 +00:00
|
|
|
$token->set_token( $raw_token );
|
|
|
|
$token->set_gateway_id( 'paypal' );
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_extra( 'woocommerce' );
|
|
|
|
|
|
|
|
$data = $token->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( $raw_token, $data['token'] );
|
|
|
|
$this->assertEquals( 'paypal', $data['gateway_id'] );
|
|
|
|
$this->assertEquals( 'stub', $data['type'] );
|
2016-03-18 18:24:06 +00:00
|
|
|
$this->assertEquals( 'extra', $data['meta_data'][0]->key );
|
|
|
|
$this->assertEquals( 'woocommerce', $data['meta_data'][0]->value );
|
2016-02-08 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test token validation.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_validation() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_token( time() . ' ' . __FUNCTION__ );
|
|
|
|
$this->assertTrue( $token->validate() );
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertFalse( $token->validate() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test reading a token from the database.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_read() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ );
|
2016-02-08 19:57:48 +00:00
|
|
|
$token_id = $token->get_id();
|
|
|
|
|
2016-03-23 12:14:13 +00:00
|
|
|
$token_read = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token_read->read( $token_id );
|
|
|
|
|
|
|
|
$this->assertEquals( $token->get_token(), $token_read->get_token() );
|
|
|
|
$this->assertEquals( $token->get_extra(), $token_read->get_extra() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test updating a token.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_update() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ );
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertEquals( __FUNCTION__, $token->get_extra() );
|
|
|
|
$token->set_extra( ':)' );
|
|
|
|
$token->update();
|
|
|
|
$this->assertEquals( ':)', $token->get_extra() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test creating a new token.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_create() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$token->set_extra( __FUNCTION__ );
|
|
|
|
$token->set_token( time() );
|
|
|
|
$token->create();
|
|
|
|
|
|
|
|
$this->assertNotEmpty( $token->get_id() );
|
|
|
|
$this->assertEquals( __FUNCTION__, $token->get_extra() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-01 17:09:29 +00:00
|
|
|
* Test deleting a token.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_delete() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = WC_Helper_Payment_Token::create_stub_token( __FUNCTION__ );
|
2016-02-08 19:57:48 +00:00
|
|
|
$token_id = $token->get_id();
|
|
|
|
$token->delete();
|
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 a meta function (like CC's last4) doesn't work on the core abstract class.
|
|
|
|
* @since 2.6.0
|
2016-02-08 19:57:48 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_payment_token_last4_doesnt_work() {
|
2016-03-23 12:14:13 +00:00
|
|
|
$token = new WC_Payment_Token_Stub();
|
2016-02-08 19:57:48 +00:00
|
|
|
$this->assertFalse( is_callable( $token, 'get_last4' ) );
|
|
|
|
}
|
|
|
|
}
|