You are here

commerce_braintree.test in Commerce Braintree 7.3

Same filename and directory in other branches
  1. 7 tests/commerce_braintree.test
  2. 7.2 tests/commerce_braintree.test

Functional tests for the commerce payment module user interface.

File

tests/commerce_braintree.test
View source
<?php

/**
 * @file
 * Functional tests for the commerce payment module user interface.
 */

/**
 * Test payment user interface.
 */
class CommerceBraintreeTest extends CommerceBaseTestCase {

  /**
   * Order object.
   */
  protected $order;
  protected $price = 3;
  protected $randomName;

  /**
   * Set a random name.
   *
   * @param string $randomName
   */
  public function setRandomName($randomName) {
    $this->randomName = $randomName;
  }
  public function getRandomName() {
    return $this->randomName;
  }

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Braintree transaction test',
      'description' => 'Test Braintree service.',
      'group' => 'Drupal Commerce Braintree',
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    $modules = parent::setUpHelper('all');
    $modules[] = 'commerce_braintree';
    parent::setUp($modules);

    // User creation for different operations.
    $this->store_admin = $this
      ->createStoreAdmin();
    $this->web_user = $this
      ->drupalCreateUser(array(
      'manage own card data',
    ));

    // The rule that sends a mail after checkout completion should be disabled
    // as it returns an error caused by how mail messages are stored.
    $rules_config = rules_config_load('commerce_checkout_order_email');
    $rules_config->active = FALSE;
    $rules_config
      ->save();

    // Setup a default user name.
    $this
      ->setRandomName($this
      ->randomName());
  }
  protected function getCredentials() {

    // Include Braintree API.
    libraries_load('braintree_php');
    Braintree_Configuration::environment('sandbox');
    Braintree_Configuration::merchantId('y37hv7xmv9tqspcz');
    Braintree_Configuration::publicKey('25sz2yrhy4nr8vj7');
    Braintree_Configuration::privateKey('90907493a55d78ffdccaf415a3226f25');
  }

  /**
   * Return an array with credit card info.
   */
  protected function createTrDataInfo($order) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $this
      ->getCredentials();
    $trData = Braintree_TransparentRedirect::transactionData(array(
      // Add transaction related data.
      'transaction' => array(
        'type' => Braintree_Transaction::SALE,
        'amount' => $this->price,
        'orderId' => $order->order_id,
        'customer' => array(
          'firstName' => 'Test User',
          'email' => 'test@exemple.com',
        ),
        'billing' => array(
          'countryCodeAlpha2' => 'US',
        ),
        'options' => array(
          'submitForSettlement' => TRUE,
        ),
      ),
      'redirectUrl' => url('checkout/' . $order->order_id . '/payment/return/', array(
        'absolute' => TRUE,
      )),
    ));
    return $trData;
  }
  function testCommerceBraintreeRequest() {
    $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']);
    $result = Braintree_TransparentRedirect::confirm($feedback[1]);
  }

}

Classes

Namesort descending Description
CommerceBraintreeTest Test payment user interface.