Test cleanup
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 26 Sep 2021 03:41:23 +0000 (16:41 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 26 Sep 2021 07:07:25 +0000 (20:07 +1300)
This switches us to a less hacky way to interact with the form in some of the tests.

Our helpers now support us interacting with the forms in a way that better mimics
the real flow & allows us to call 'postProcess'

CRM/Contribute/Form/Contribution.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php

index a21afe61d20ad9cf4582f08126d644bd066c5cb3..46925243104cc0695e5ae805529cf461bf50fdf9 100644 (file)
@@ -1493,7 +1493,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     }
     $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE);
 
-    $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id']));
+    $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'] ?? []));
     if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) {
       civicrm_api3('ContributionSoft', 'get', ['contribution_id' => $this->_id, 'pcp_id' => ['IS NULL' => 1], 'api.ContributionSoft.delete' => 1]);
     }
index 07e205dc64da27f9ae43dafbb091d15b9283149b..1e2985a5ae127b75a56ec20e31c0fb17969c30be 100644 (file)
@@ -632,21 +632,22 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
   /**
    * Test the submit function on the contribution page using numerical from email address.
    */
-  public function testSubmitEmailReceiptUserEmailFromAddress() {
-    $form = new CRM_Contribute_Form_Contribution();
-    $mut = new CiviMailUtils($this, TRUE);
+  public function testSubmitEmailReceiptUserEmailFromAddress(): void {
     $email = $this->callAPISuccess('Email', 'create', [
       'contact_id' => $this->_userId,
       'email' => 'testLoggedIn@example.com',
     ]);
-    $form->testSubmit([
+    $mut = new CiviMailUtils($this, TRUE);
+    $form = $this->getContributionForm([
+      'contribution_status_id' => 1,
       'total_amount' => 50,
       'financial_type_id' => 1,
       'contact_id' => $this->_individualId,
       'is_email_receipt' => TRUE,
       'from_email_address' => $email['id'],
-      'contribution_status_id' => 1,
-    ], CRM_Core_Action::ADD);
+    ]);
+
+    $form->postProcess();
     $this->callAPISuccessGetCount('Contribution', ['contact_id' => $this->_individualId], 1);
     $mut->checkMailLog([
       'Below you will find a receipt for this contribution.',
@@ -1034,20 +1035,18 @@ Price Field - Price Field 1        1   $ 100.00      $ 100.00
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testPartialPaymentWithCreditCard() {
+  public function testPartialPaymentWithCreditCard(): void {
     // create a partially paid contribution by using back-office form
-    $form = new CRM_Contribute_Form_Contribution();
-    $form->testSubmit(
-      [
-        'total_amount' => 50,
-        'financial_type_id' => 1,
-        'contact_id' => $this->_individualId,
-        'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
-        'check_number' => substr(sha1(rand()), 0, 7),
-        'billing_city-5' => 'Vancouver',
-        'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
-      ], CRM_Core_Action::ADD
-    );
+    $form = $this->getContributionForm([
+      'total_amount' => 50,
+      'financial_type_id' => 1,
+      'contact_id' => $this->_individualId,
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+      'check_number' => '7890',
+      'billing_city-5' => 'Vancouver',
+      'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
+    ]);
+    $form->postProcess();
 
     $contribution = $this->callAPISuccessGetSingle('Contribution', []);
     $this->callAPISuccess('Payment', 'create', ['contribution_id' => $contribution['id'], 'total_amount' => 10, 'payment_instrument_id' => 'Cash']);
@@ -1332,21 +1331,18 @@ Price Field - Price Field 1        1   $ 100.00      $ 100.00
   /**
    * function to test card_type and pan truncation.
    */
-  public function testCardTypeAndPanTruncation() {
-    $form = new CRM_Contribute_Form_Contribution();
-    $form->testSubmit(
-      [
-        'total_amount' => 100,
-        'financial_type_id' => 3,
-        'contact_id' => $this->_individualId,
-        'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
-        'contribution_status_id' => 1,
-        'credit_card_type' => 'Visa',
-        'pan_truncation' => 4567,
-        'price_set_id' => 0,
-      ],
-      CRM_Core_Action::ADD
-    );
+  public function testCardTypeAndPanTruncation(): void {
+    $form = $this->getContributionForm([
+      'total_amount' => 100,
+      'financial_type_id' => 3,
+      'contact_id' => $this->_individualId,
+      'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
+      'contribution_status_id' => 1,
+      'credit_card_type' => 'Visa',
+      'pan_truncation' => 4567,
+      'price_set_id' => 0,
+    ]);
+    $form->postProcess();
     $contribution = $this->callAPISuccessGetSingle('Contribution',
       [
         'contact_id' => $this->_individualId,
@@ -1361,8 +1357,8 @@ Price Field - Price Field 1        1   $ 100.00      $ 100.00
         'return' => ['card_type_id.label', 'pan_truncation'],
       ]
     );
-    $this->assertEquals(CRM_Utils_Array::value('card_type_id.label', $financialTrxn), 'Visa');
-    $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
+    $this->assertEquals('Visa', $financialTrxn['card_type_id.label']);
+    $this->assertEquals(4567, $financialTrxn['pan_truncation']);
   }
 
   /**
@@ -2181,4 +2177,18 @@ Price Field - Price Field 1        1   $ 100.00      $ 100.00
     $this->assertEquals(['trxn_id' => "Transaction ID's must be unique. Transaction '1234' already exists in your database."], $form->formRule($fields, [], $form));
   }
 
+  /**
+   * Get the contribution form object.
+   *
+   * @param array $formValues
+   *
+   * @return \CRM_Contribute_Form_Contribution
+   */
+  protected function getContributionForm(array $formValues): CRM_Contribute_Form_Contribution {
+    /* @var CRM_Contribute_Form_Contribution $form */
+    $form = $this->getFormObject('CRM_Contribute_Form_Contribution', $formValues);
+    $form->buildForm();
+    return $form;
+  }
+
 }