You are here

function PaymentTestPaymentValidationWebTestCase::testPaymentValidation in Payment 7

Test payment validation.

File

tests/payment_test/tests/PaymentTestPaymentValidationWebTestCase.test, line 41
Contains class PaymentTestPaymentValidationWebTestCase.

Class

PaymentTestPaymentValidationWebTestCase
Tests payment validation.

Code

function testPaymentValidation() {

  // Test that a valid payment validates.
  try {
    $this->payment_method
      ->validate($this
      ->paymentCreate(1, $this->payment_method));
    $valid = TRUE;
  } catch (PaymentValidationException $e) {
    $valid = FALSE;
  }
  $this
    ->assertTrue($valid, 'PaymentMethodController::validate() does not invalidate a valid payment.');

  // Test that a payment with a disabled payment method invalidates.
  try {
    $this->payment_method->enabled = FALSE;
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationPaymentMethodDisabledException $e) {
    $this->payment_method->enabled = TRUE;
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, 'PaymentMethodController::validate() invalidates a payment of which the payment method is disabled.');

  // Test that a payment without a currency code set invalidates.
  try {
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    $payment->currency_code = '';
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationMissingCurrencyException $e) {
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, 'PaymentMethodController::validate() invalidates a payment that has no currency set.');

  // Test that a payment without a description invalidates.
  try {
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    unset($payment->description);
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationDescriptionMissing $e) {
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, 'PaymentMethodController::validate() invalidates a payment that has no description.');

  // Test that a payment without a description invalidates.
  try {
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    $payment->description = str_repeat('a', 300);
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationDescriptionTooLong $e) {
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, 'PaymentMethodController::validate() invalidates a payment of which the description is too long.');

  // Test that a payment without a (callable) finish callback set
  // invalidates.
  try {
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    $payment->finish_callback = '';
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationMissingFinishCallback $e) {
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, 'PaymentMethodController::validate() invalidates a payment that has no (callable) finish callback set.');

  // Test that a payment with a total amount above its controller's maximum
  // invalidates.
  try {
    $payment = $this
      ->paymentCreate(1, $this->payment_method);
    $payment
      ->setLineItem(new PaymentLineItem(array(
      'name' => 'payment_test2',
      'amount' => 25,
    )));
    $this->payment_method
      ->validate($payment);
    $valid = TRUE;
  } catch (PaymentValidationAmountExceedsMaximumException $e) {
    $valid = FALSE;
  }
  $this
    ->assertFalse($valid, "PaymentMethodController::validate() invalidates a payment of which the amount exceeds its controller's supported maximum.");
}