Stop calling testSubmit(), quiet deprecation
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 13 Oct 2023 22:52:42 +0000 (11:52 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 14 Oct 2023 19:55:01 +0000 (08:55 +1300)
CRM/Contribute/Form/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index 5fbfb0e6d0cadd906c18f752c16ddf2be94d11a8..6ce3a35c2ed9010f09520c034d5c8f6a9a9775b7 100644 (file)
@@ -1558,8 +1558,20 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    *
    * @throws \CRM_Core_Exception
    * @throws \Civi\Payment\Exception\PaymentProcessorException
+   *
+   * @deprecated since 5.68 will be removed around 5.80.
+   *
+   * Try something like
+   *  use   use \Civi\Test\FormTrait;
+   *  $form = $this->getTestForm('CRM_Contribute_Form_Contribution', $submittedValues, [
+   *   'id' =>  4;
+   *   'action' => 'update',
+   * ]);
+   * $form->processForm();
    */
   public function testSubmit($params, $action, $creditCardMode = NULL) {
+    // Note that this is really used from tests - so adding noisy deprecations would make them
+    // fail straight away.
     $defaults = [
       'soft_credit_contact_id' => [],
       'receive_date' => date('Y-m-d H:i:s'),
@@ -1605,7 +1617,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     $this->_fields = [];
     return $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params));
-
   }
 
   /**
index 1887fdb729f4e32622c358bbc583877628f9da74..a03424cac92162a4538f97e43cc3be8cbfe49598 100644 (file)
@@ -16,6 +16,7 @@ use Civi\Api4\Pledge;
 use Civi\Api4\PriceField;
 use Civi\Api4\PriceFieldValue;
 use Civi\Api4\PriceSet;
+use Civi\Test\FormTrait;
 
 /**
  *  Test APIv3 civicrm_contribute_* functions
@@ -31,6 +32,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   use CRMTraits_Financial_OrderTrait;
   use CRMTraits_Financial_TaxTrait;
   use CRMTraits_Financial_PriceSetTrait;
+  use FormTrait;
 
   protected $individualID;
   protected $_contribution;
@@ -867,7 +869,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    *
    * @dataProvider getThousandSeparators
    */
-  public function testCreateDefaultNow($thousandSeparator) {
+  public function testCreateDefaultNow(string $thousandSeparator): void {
     $this->setCurrencySeparators($thousandSeparator);
     $params = $this->_params;
     unset($params['receive_date'], $params['net_amount']);
@@ -1159,9 +1161,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * Check handling of financial type.
    *
    * In the interests of removing financial type / contribution type checks from
-   * legacy format function lets test that the api is doing this for us
-   *
-   * @throws \CRM_Core_Exception
+   * legacy format function lets test that the api is doing this for us.
    */
   public function testValidNamedFinancialType(): void {
     $params = $this->_params;
@@ -3503,11 +3503,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'status_id' => 'Scheduled',
     ]);
     $this->assertEquals(1, $activity['count']);
-    $_REQUEST['id'] = $this->getContributionID();
-    $_REQUEST['action'] = 'update';
-    // change pending contribution to completed
-    /** @var CRM_Contribute_Form_Contribution $form */
-    $form = $this->getFormObject('CRM_Contribute_Form_Contribution', [
+    $submittedValues = [
       'total_amount' => 20,
       'net_amount' => 20,
       'fee_amount' => 0,
@@ -3531,9 +3527,13 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'currency' => 'USD',
       'contribution_page_id' => $this->_ids['contribution_page'],
       'source' => 'Membership Signup and Renewal',
+    ];
+    // Change pending contribution to completed.
+    $form = $this->getTestForm('CRM_Contribute_Form_Contribution', $submittedValues, [
+      'id' => $this->getContributionID(),
+      'action' => 'update',
     ]);
-    $form->buildForm();
-    $form->postProcess();
+    $form->processForm();
 
     // Case 2: After successful payment for Pending backoffice there are three activities created
     //  2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
@@ -3566,10 +3566,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->setUpPendingContribution($this->_ids['price_field_value'][0], 'second', [], ['entity_id' => $this->getMembershipID()], ['id' => $this->getMembershipID()]);
 
     //Update it to Failed.
-    $form->_params['id'] = $this->getContributionID('second');
-    $form->_params['contribution_status_id'] = 4;
-
-    $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
+    $submittedValues['contribution_status_id'] = 4;
+    $form = $this->getTestForm('CRM_Contribute_Form_Contribution', $submittedValues, [
+      'id' => $this->getContributionID('second'),
+      'action' => 'update',
+    ]);
+    $form->processForm();
     //Existing membership should not get updated to expired.
     $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $this->_ids['membership']]);
     $this->assertNotEquals(4, $membership['status_id']);
@@ -3974,7 +3976,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    *
    * @return null|string
    */
-  public function _getFinancialTrxnAmount($contId) {
+  public function _getFinancialTrxnAmount(int $contId): ?string {
     $query = "SELECT
      SUM( ft.total_amount ) AS total
      FROM civicrm_financial_trxn AS ft