You are here

public function CommercePaymentUITest::testCommercePaymentAdministration in Commerce Core 7

Test the adding payments using administration pages.

File

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

Class

CommercePaymentUITest
Test payment user interface.

Code

public function testCommercePaymentAdministration() {

  // Order creation, in cart status.
  $this->order = $this
    ->createDummyOrder($this->store_customer->uid);

  // Log in as administrator user.
  $this
    ->drupalLogin($this->store_admin);

  // Go to payment administration page.
  $this
    ->drupalGet('admin/commerce/orders/' . $this->order->order_id . '/payment');
  $this
    ->assertText(t('Payment'), t('Payment text found on the page.'));

  // Check order balance.
  $balance = commerce_payment_order_balance($this->order);
  $this
    ->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format($balance['amount'], $balance['currency_code']) . '</td>', t('Order balance is equal to order amount'));

  // Add a payment for half of balance.
  $this
    ->drupalPostAJAX(NULL, array(
    'payment_method' => 'commerce_payment_example|commerce_payment_commerce_payment_example',
  ), array(
    'op' => t('Add payment'),
  ));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-amount')]", NULL, t('Amount field is present'));
  $this
    ->assertFieldByXPath("//select[starts-with(@id, 'edit-currency-code')]", NULL, t('Currency code field is present'));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-payment-details-credit-card-number')]", NULL, t('Credit card number field from payment example module is present'));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-submit')]", NULL, t('Save button is present'));
  $payment_amount = intval($balance['amount'] / 2);
  $post_data = array(
    'amount' => commerce_currency_amount_to_decimal($payment_amount, $balance['currency_code']),
    'currency_code' => 'USD',
  );
  $this
    ->drupalPost(NULL, $post_data, t('Save'));

  // Reload the order.
  $order = commerce_order_load_multiple(array(
    $this->order->order_id,
  ), array(), TRUE);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();

  // Check order balance, it should be half of total now.
  $new_balance = commerce_payment_order_balance(reset($order));
  $this
    ->assertEqual($new_balance['amount'], $balance['amount'] - $payment_amount, t('After half payment order balance is correct'));
  $this
    ->assertRaw('<td class="label">' . t('Total paid') . '</td><td class="total">' . commerce_currency_format($payment_amount, $post_data['currency_code']) . '</td>', t('Total paid reflects the payment'));
  $this
    ->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format($new_balance['amount'], $new_balance['currency_code']) . '</td>', t('Order balance is displayed correctly'));

  // Add a payment for the remainder.
  $this
    ->drupalPostAJAX(NULL, array(
    'payment_method' => 'commerce_payment_example|commerce_payment_commerce_payment_example',
  ), array(
    'op' => t('Add payment'),
  ));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-amount')]", NULL, t('Amount field is present'));
  $this
    ->assertFieldByXPath("//select[starts-with(@id, 'edit-currency-code')]", NULL, t('Currency code field is present'));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-payment-details-credit-card-number')]", NULL, t('Credit card number field from payment example module is present'));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-submit')]", NULL, t('Save button is present'));
  $post_data = array(
    'amount' => commerce_currency_amount_to_decimal($new_balance['amount'], $new_balance['currency_code']),
    'currency_code' => 'USD',
  );
  $this
    ->drupalPost(NULL, $post_data, t('Save'));

  // Reload the order.
  $order = commerce_order_load_multiple(array(
    $this->order->order_id,
  ), array(), TRUE);

  // Check order balance, it should be zero now.
  $new_balance = commerce_payment_order_balance(reset($order));
  $this
    ->assertEqual($new_balance['amount'], 0, t('Order balance is now zero'));
  $this
    ->assertRaw('<td class="label">' . t('Total paid') . '</td><td class="total">' . commerce_currency_format($balance['amount'], $balance['currency_code']) . '</td>', t('Total paid is now equal to order total'));
  $this
    ->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format(0, $new_balance['currency_code']) . '</td>', t('Balance is displayed as zero'));
}