<?php

/**
 * Class Payment_Token_CC.
 * @package ClassicCommerce\Tests\Payment_Tokens
 */
class WC_Tests_Payment_Token_CC extends WC_Unit_Test_Case {

	/**
	 * Test validation for empty/unset values.
	 * @since WC-2.6.0
	 */
	function test_wc_payment_token_cc_validate_empty() {
		$token = new WC_Payment_Token_CC();
		$token->set_token( time() . ' ' . __FUNCTION__ );
		$this->assertFalse( $token->validate() );
		$token->set_last4( '1111' );
		$token->set_expiry_year( '2016' );
		$token->set_expiry_month( '08' );
		$token->set_card_type( 'visa' );
		$this->assertTrue( $token->validate() );
	}

	/**
	 * Test validation for expiry length.
	 * @since WC-2.6.0
	 */
	function test_wc_payment_token_cc_validate_expiry_length() {
		$token = new WC_Payment_Token_CC();
		$token->set_token( time() . ' ' . __FUNCTION__ );
		$this->assertFalse( $token->validate() );

		$token->set_last4( '1111' );
		$token->set_expiry_year( '16' );
		$token->set_expiry_month( '08' );
		$token->set_card_type( 'visa' );

		$this->assertFalse( $token->validate() );

		$token->set_expiry_year( '2016' );

		$this->assertTrue( $token->validate() );

		$token->set_expiry_month( '888' );
		$this->assertFalse( $token->validate() );
	}

	/**
	 * Tes get/set card type.
	 * @since WC-2.6.0
	 */
	public function test_wc_payment_token_cc_card_type() {
		$token = new WC_Payment_Token_CC();
		$token->set_card_type( 'visa' );
		$this->assertEquals( 'visa', $token->get_card_type() );
	}

	/**
	 * Test get/set expiry year.
	 * @since WC-2.6.0
	 */
	public function test_wc_payment_token_cc_expiry_year() {
		$token = new WC_Payment_Token_CC();
		$token->set_expiry_year( '2016' );
		$this->assertEquals( '2016', $token->get_expiry_year() );
	}

	/**
	 * Test get/set expiry month.
	 * @since WC-2.6.0
	 */
	public function test_wc_payment_token_cc_expiry_month() {
		$token = new WC_Payment_Token_CC();
		$token->set_expiry_month( '08' );
		$this->assertEquals( '08', $token->get_expiry_month() );
	}

	/**
	 * Test get/set last4.
	 * @since WC-2.6.0
	 */
	public function test_wc_payment_token_cc_last4() {
		$token = new WC_Payment_Token_CC();
		$token->set_last4( '1111' );
		$this->assertEquals( '1111', $token->get_last4() );
	}

	/*
	 * Test reading/getting a token from DB correctly sets meta.
	 * @since WC-2.6.0
	 */
	public function test_wc_payment_token_cc_read_pulls_meta() {
		$token = WC_Helper_Payment_Token::create_cc_token();
		$token_id = $token->get_id();
		$token_read = new WC_Payment_Token_CC( $token_id );
		$this->assertEquals( '1234', $token_read->get_last4() );
	}

	/*
	 * Test saving a new value in a token after it has been created.
	 * @since WC-3.0.0
	 */
	public function test_wc_payment_token_cc_updates_after_create() {
		$token    = WC_Helper_Payment_Token::create_cc_token();
		$token_id = $token->get_id();
		$this->assertEquals( '1234', $token->get_last4() );

		$token->set_last4( '4321' );
		$token->set_user_id( 3 );
		$token->save();
		$this->assertEquals( '4321', $token->get_last4() );
		$this->assertEquals( 3, $token->get_user_id() );
	}
}
