Add test for failed payment
authoreileen <emcnaughton@wikimedia.org>
Thu, 1 Aug 2019 22:29:49 +0000 (10:29 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 2 Aug 2019 03:34:22 +0000 (15:34 +1200)
https://github.com/civicrm/civicrm-core/pull/14930

CRM/Core/Payment/Dummy.php
tests/phpunit/CRM/Event/Form/ParticipantTest.php

index 7c260074a2007c56f3a0006c4d36457e44cdbd20..717c4f3c6fb8a15bf5698cb08564e77fd7b693cb 100644 (file)
@@ -14,6 +14,8 @@
  * $Id: Dummy.php 45429 2013-02-06 22:11:18Z lobo $
  */
 
+use Civi\Payment\Exception\PaymentProcessorException;
+
 /**
  * Dummy payment processor
  */
@@ -73,6 +75,7 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
    *
    * @return array
    *   the result in a nice formatted array (or an error object)
+   * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public function doDirectPayment(&$params) {
     // Invoke hook_civicrm_paymentProcessor
@@ -99,6 +102,9 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
     //end of hook invocation
     if (!empty($this->_doDirectPaymentResult)) {
       $result = $this->_doDirectPaymentResult;
+      if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') {
+        throw new PaymentProcessorException($result['message'] ?? 'failed');
+      }
       $result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']);
       return $result;
     }
index 1b73d11180b72e7a8d61b0a712d49c862fa12ebf..cd57dbd88e029e2a96deadcd67fec0f2e57317ad 100644 (file)
@@ -100,7 +100,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     }
     $this->assertEquals(55, $sum);
 
-    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participants['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem, 100);
+    CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participants['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem);
     $lineItem = CRM_Price_BAO_LineItem::getLineItems($participants['id'], 'participant');
     // Participants is updated to 0 but line remains.
     $this->assertEquals(0, $lineItem[1]['subTotal']);
@@ -170,56 +170,21 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
    * Initial test of submit function.
    *
    * @param string $thousandSeparator
-   * @param array $fromEmails From Emails array to overwrite the default.
    *
    * @dataProvider getThousandSeparators
    *
    * @throws \Exception
    */
-  public function testSubmitWithPayment($thousandSeparator, $fromEmails = []) {
+  public function testSubmitWithPayment($thousandSeparator) {
     $this->setCurrencySeparators($thousandSeparator);
     $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
     $form->_mode = 'Live';
     $form->_quickConfig = TRUE;
+    $form->_fromEmails = [
+      'from_email_id' => ['abc@gmail.com' => 1],
+    ];
     $paymentProcessorID = $this->processorCreate(['is_test' => 0]);
-    if (empty($fromEmails)) {
-      $fromEmails = [
-        'from_email_id' => ['abc@gmail.com' => 1],
-      ];
-    }
-    $form->_fromEmails = $fromEmails;
-    $form->submit([
-      'register_date' => date('Ymd'),
-      'status_id' => 1,
-      'role_id' => 1,
-      'event_id' => $form->_eventId,
-      'credit_card_number' => 4444333322221111,
-      'cvv2' => 123,
-      'credit_card_exp_date' => [
-        'M' => 9,
-        'Y' => 2025,
-      ],
-      'credit_card_type' => 'Visa',
-      'billing_first_name' => 'Junko',
-      'billing_middle_name' => '',
-      'billing_last_name' => 'Adams',
-      'billing_street_address-5' => '790L Lincoln St S',
-      'billing_city-5' => 'Maryknoll',
-      'billing_state_province_id-5' => 1031,
-      'billing_postal_code-5' => 10545,
-      'billing_country_id-5' => 1228,
-      'payment_processor_id' => $paymentProcessorID,
-      'priceSetId' => '6',
-      'price_7' => [
-        13 => 1,
-      ],
-      'amount_level' => 'Too much',
-      'fee_amount' => $this->formatMoneyInput(1550.55),
-      'total_amount' => $this->formatMoneyInput(1550.55),
-      'from_email_address' => array_keys($form->_fromEmails['from_email_id'])[0],
-      'send_receipt' => 1,
-      'receipt_text' => '',
-    ]);
+    $form->submit($this->getSubmitParams($form->_eventId, $paymentProcessorID));
     $participants = $this->callAPISuccess('Participant', 'get', []);
     $this->assertEquals(1, $participants['count']);
     $contribution = $this->callAPISuccessGetSingle('Contribution', []);
@@ -227,12 +192,43 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     $this->assertEquals('Debit Card', $contribution['payment_instrument']);
   }
 
+  /**
+   * Initial test of submit function.
+   *
+   * @param string $thousandSeparator
+   * @param array $fromEmails From Emails array to overwrite the default.
+   *
+   * @dataProvider getThousandSeparators
+   *
+   * @throws \Exception
+   */
+  public function testSubmitWithFailedPayment($thousandSeparator, $fromEmails = []) {
+    $this->setCurrencySeparators($thousandSeparator);
+    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
+    $form->_mode = 'Live';
+    $form->_quickConfig = TRUE;
+    $paymentProcessorID = $this->processorCreate(['is_test' => 0]);
+    Civi\Payment\System::singleton()->getById($paymentProcessorID)->setDoDirectPaymentResult(['payment_status_id' => 'failed']);
+
+    $form->_fromEmails = [
+      'from_email_id' => ['abc@gmail.com' => 1],
+    ];
+    try {
+      $form->submit($this->getSubmitParams($form->_eventId, $paymentProcessorID));
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      return;
+    }
+    $this->fail('should have hit premature exit');
+  }
+
   /**
    * Test offline participant mail.
    *
    * @param string $thousandSeparator
    *
    * @dataProvider getThousandSeparators
+   * @throws \Exception
    */
   public function testParticipantOfflineReceipt($thousandSeparator) {
     $this->setCurrencySeparators($thousandSeparator);
@@ -269,10 +265,20 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     ]);
 
     // Use the email created as the from email ensuring we are passing a numeric from to test dev/core#1069
-    $this->testSubmitWithPayment($thousandSeparator, ['from_email_id' => [$email['id'] => 1]]);
+    $this->setCurrencySeparators($thousandSeparator);
+    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
+    $form->_mode = 'Live';
+    $form->_quickConfig = TRUE;
+    $form->_fromEmails = [
+      'from_email_id' => [$email['id'] => 1],
+    ];
+    $paymentProcessorID = $this->processorCreate(['is_test' => 0]);
+    $submitParams = $this->getSubmitParams($form->_eventId, $paymentProcessorID);
+    $submitParams['from_email_address'] = $email['id'];
+    $form->submit($submitParams);
     //Check if type is correctly populated in mails.
     //Also check the string email is present not numeric from.
-    $mail = $mut->checkMailLog([
+    $mut->checkMailLog([
       '<p>Test event type - 1</p>',
       'testloggedinreceiptemail@civicrm.org',
       $this->formatMoneyInput(1550.55),
@@ -286,6 +292,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
    * @param array $eventParams
    *
    * @return CRM_Event_Form_Participant
+   *
    * @throws \CRM_Core_Exception
    */
   protected function getForm($eventParams = []) {
@@ -409,4 +416,48 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     ]);
   }
 
+  /**
+   * Get params for submit function.
+   *
+   * @param int $eventID
+   * @param int $paymentProcessorID
+   *
+   * @return array
+   */
+  private function getSubmitParams(int $eventID, int $paymentProcessorID): array {
+    $submitParams = [
+      'register_date' => date('Ymd'),
+      'status_id' => 1,
+      'role_id' => 1,
+      'event_id' => $eventID,
+      'credit_card_number' => 4444333322221111,
+      'cvv2' => 123,
+      'credit_card_exp_date' => [
+        'M' => 9,
+        'Y' => 2025,
+      ],
+      'credit_card_type' => 'Visa',
+      'billing_first_name' => 'Junko',
+      'billing_middle_name' => '',
+      'billing_last_name' => 'Adams',
+      'billing_street_address-5' => '790L Lincoln St S',
+      'billing_city-5' => 'Maryknoll',
+      'billing_state_province_id-5' => 1031,
+      'billing_postal_code-5' => 10545,
+      'billing_country_id-5' => 1228,
+      'payment_processor_id' => $paymentProcessorID,
+      'priceSetId' => '6',
+      'price_7' => [
+        13 => 1,
+      ],
+      'amount_level' => 'Too much',
+      'fee_amount' => $this->formatMoneyInput(1550.55),
+      'total_amount' => $this->formatMoneyInput(1550.55),
+      'from_email_address' => 'abc@gmail.com',
+      'send_receipt' => 1,
+      'receipt_text' => '',
+    ];
+    return $submitParams;
+  }
+
 }