You are here

function CommerceBraintreeCofTest::testCommerceBraintreeCofRequest in Commerce Braintree 7.2

Same name and namespace in other branches
  1. 7.3 tests/commerce_braintree_cof.test \CommerceBraintreeCofTest::testCommerceBraintreeCofRequest()
  2. 7 tests/commerce_braintree_cof.test \CommerceBraintreeCofTest::testCommerceBraintreeCofRequest()

Test a card on File request. Be sure we store the credit card.

File

tests/commerce_braintree_cof.test, line 116
Functional tests for the commerce payment module user interface.

Class

CommerceBraintreeCofTest
Test payment user interface.

Code

function testCommerceBraintreeCofRequest() {
  $this
    ->setBraintreeCredential();
  $user = $this->web_user;

  // Log in as normal user.
  $this
    ->drupalLogin($user);
  $product = $this
    ->createDummyProduct($this
    ->randomName(), $this
    ->randomName(), $this->price, 'USD', $this->store_admin->uid);
  $this->order = $this
    ->createDummyOrder($user->uid, array(
    $product->product_id => 20,
  ));
  $edit = array();
  $edit['transaction[credit_card][number]'] = '4111111111111111';
  $edit['transaction[credit_card][cardholder_name]'] = $this
    ->getRandomName();
  $edit['transaction[credit_card][expiration_month]'] = '10';
  $edit['transaction[credit_card][expiration_year]'] = '2012';
  $edit['transaction[credit_card][cvv]'] = '123';
  $edit['tr_data'] = $this
    ->createTrDataInfo($this->order);
  $option = array();
  $option['method'] = 'POST';
  $option['data'] = drupal_http_build_query($edit);
  $url = Braintree_TransparentRedirect::url();
  $request = drupal_http_request($url, $option);
  $feedback = explode('?', $request->headers['location']);
  $payment_method = commerce_payment_method_instance_load('braintree_cof|commerce_payment_braintree_cof');
  module_load_include('inc', 'commerce_braintree', 'commerce_braintree.commerce_braintree_cof');
  commerce_braintree_cof_process_transaction($this->order, $payment_method, $feedback[1]);

  // Now need to check is the credit card is really created.
  $query = db_select('commerce_card_data', 'ccd')
    ->fields('ccd', array(
    'remote_id',
  ))
    ->condition('card_name', $this
    ->getRandomName());
  $result = $query
    ->execute();

  // We should have one, and only one, result.
  $this
    ->assertEqual('1', $result
    ->rowCount(), 'Found 1 result.');

  // Then, try to re-use the created credit card to register a new
  // transaction.
  $query = db_select('commerce_card_data', 'ccd')
    ->fields('ccd', array(
    'remote_id',
  ))
    ->condition('card_name', $this
    ->getRandomName());
  $remote_id = $query
    ->execute()
    ->fetchField();
  $trData = Braintree_TransparentRedirect::transactionData(array(
    // Add transaction related data.
    'transaction' => array(
      'type' => Braintree_Transaction::SALE,
      'amount' => '42',
      'orderId' => $this->order->order_id,
      'paymentMethodToken' => $remote_id,
    ),
    'redirectUrl' => url('checkout/' . $this->order->order_id . '/payment/return/', array(
      'absolute' => TRUE,
    )),
  ));
  $edit = array();
  $edit['tr_data'] = $trData;
  $option = array();
  $option['method'] = 'POST';
  $option['data'] = drupal_http_build_query($edit);
  $url = Braintree_TransparentRedirect::url();
  $request = drupal_http_request($url, $option);
  $feedback = explode('?', $request->headers['location']);
  $result = Braintree_TransparentRedirect::confirm($feedback[1]);
  $this
    ->assertEqual($result->success, TRUE, 'COF - Transaction made by re-using a registered card confirmed.');
  $user = $this->web_user;

  // Log in as normal user.
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('user/' . $user->uid . '/stored-payment-methods');
  $this
    ->assertResponse(200, t('Stored payment methods page exists.'));
  $this
    ->assertText($this
    ->getRandomName(), t('Credit card found'));
  $this
    ->clickLink('Delete');
  $this
    ->GetCredentials();
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->drupalGet('user/' . $user->uid . '/stored-payment-methods');

  // The page should no exist anymore (no credit card).
  $this
    ->assertNoResponse(200, t('Stored payment methods page exists.'));
}