Minor improvement in test set up function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 24 Jul 2023 05:51:48 +0000 (17:51 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 24 Jul 2023 19:43:28 +0000 (07:43 +1200)
tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 671a4b31a72ab4a268a649f1da3e6a9737e32464..e450e5ce40a727888fae0409e66113c28ffbd539 100644 (file)
  */
 class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
 
-  /**
-   * The id of the contribution page.
-   * @var int
-   */
-  private $contributionPageId;
-
   /**
    * The id of the contribution page's payment processor.
    * @var int
@@ -54,9 +48,9 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
   /**
    * Establish a standard list of submit params to more accurately test the submission.
    */
-  private function getSubmitParams() {
+  private function getSubmitParams(): array {
     return [
-      'id' => $this->contributionPageId,
+      'id' => $this->ids['ContributionPage']['test'],
       'amount' => 80,
       'first_name' => 'Billy',
       'last_name' => 'Gruff',
@@ -74,7 +68,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
   /**
    * Test that the membership is set to recurring if the membership type is always autorenew.
    */
-  public function testSetRecurFunction() {
+  public function testSetRecurFunction(): void {
     $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
     $form = $this->getContributionForm();
     $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
@@ -87,7 +81,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
   /**
    * Test that the membership is set to recurring if the membership type is optionally autorenew and is_recur is true.
    */
-  public function testSetRecurFunctionOptionalYes() {
+  public function testSetRecurFunctionOptionalYes(): void {
     $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
     $form = $this->getContributionForm();
     $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
@@ -100,7 +94,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
   /**
    * Test that the membership is not set to recurring if the membership type is optionally autorenew and is_recur is false.
    */
-  public function testSetRecurFunctionOptionalNo() {
+  public function testSetRecurFunctionOptionalNo(): void {
     $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
     $form = $this->getContributionForm();
     $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
@@ -127,42 +121,49 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
   /**
    * Get a contribution form object for testing.
    *
+   * @params array $submittedValues
+   * @params array $params
+   *
    * @return \CRM_Contribute_Form_Contribution_Main
    */
-  protected function getContributionForm($params = []) {
-    $this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
-      'name' => 'default_membership_type_amount',
-      'return' => 'id',
-    ]);
-
-    $paymentProcessor = $this->paymentProcessorCreate([
-      'payment_processor_type_id' => 'Dummy',
-      'is_test' => 0,
-    ]);
-
-    $contributionPageParams = (array_merge($params, [
-      'currency' => 'NZD',
-      'goal_amount' => 6000,
-      'is_pay_later' => 0,
-      'is_monetary' => 1,
-      'pay_later_text' => 'Front up',
-      'pay_later_receipt' => 'Ta',
-      'is_email_receipt' => 1,
-      'payment_processor' => $paymentProcessor,
-      'amount_block_is_active' => 1,
-    ]));
-
-    /** @var \CRM_Contribute_Form_Contribution_Main $form */
-    $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main');
-    $contributionPage = reset($this->contributionPageCreate($contributionPageParams)['values']);
-    $form->set('id', $contributionPage['id']);
-    CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId);
-    $form->preProcess();
-    $form->buildQuickForm();
-    // Need these values to create more realistic submit params (in getSubmitParams).
-    $this->paymentProcessorId = $paymentProcessor;
-    $this->contributionPageId = (int) $contributionPage['id'];
-    return $form;
+  protected function getContributionForm(array $submittedValues = [], $params = []): CRM_Contribute_Form_Contribution_Main {
+    try {
+      $this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
+        'name' => 'default_membership_type_amount',
+        'return' => 'id',
+      ]);
+
+      $paymentProcessor = $submittedValues['payment_processor_id'] = $this->paymentProcessorCreate([
+        'payment_processor_type_id' => 'Dummy',
+        'is_test' => 0,
+      ]);
+
+      $contributionPageParams = (array_merge($params, [
+        'currency' => 'NZD',
+        'goal_amount' => 6000,
+        'is_pay_later' => 0,
+        'is_monetary' => 1,
+        'pay_later_text' => 'Front up',
+        'pay_later_receipt' => 'Ta',
+        'is_email_receipt' => 1,
+        'payment_processor' => [$paymentProcessor],
+        'amount_block_is_active' => 1,
+      ]));
+      $contributionPage = $this->contributionPageCreate($contributionPageParams);
+      $submittedValues['id'] = $_REQUEST['id'] = (int) $contributionPage['id'];
+      /** @var \CRM_Contribute_Form_Contribution_Main $form */
+      $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main', $submittedValues);
+      $form->set('id', $contributionPage['id']);
+      CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId);
+      $form->preProcess();
+      $form->buildQuickForm();
+      // Need these values to create more realistic submit params (in getSubmitParams).
+      $this->paymentProcessorId = $paymentProcessor;
+      return $form;
+    }
+    catch (CRM_Core_Exception $e) {
+      $this->fail('Failed to prepare form' . $e->getMessage());
+    }
   }
 
   /**
@@ -219,7 +220,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
       $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams);
     }
 
-    $form = $this->getContributionForm(['priceSetID' => $priceSet['id']]);
+    $form = $this->getContributionForm([], ['priceSetID' => $priceSet['id']]);
     foreach ($form->_priceSet['fields'] as $pField) {
       foreach ($pField['options'] as $opId => $opValues) {
         $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
index 66197d36aa83404ea0fd4395d1dda7c88c3812f4..abc2e372c1ed5f1c1eaa80dfc4abb8205df0255d 100644 (file)
@@ -854,22 +854,22 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    * Create contribution page.
    *
    * @param array $params
+   * @param string $identifier
    *
    * @return array
    *   Array of contribution page
    */
-  public function contributionPageCreate($params = []): array {
-    $pageParams = array_merge([
+  public function contributionPageCreate(array $params = [], string $identifier = 'test'): array {
+    return $this->createTestEntity('ContributionPage', array_merge([
       'title' => 'Test Contribution Page',
-      'financial_type_id' => 1,
+      'financial_type_id:name' => 'Donation',
       'currency' => 'USD',
       'financial_account_id' => 1,
       'is_active' => 1,
       'is_allow_other_amount' => 1,
       'min_amount' => 10,
       'max_amount' => 1000,
-    ], $params);
-    return $this->callAPISuccess('ContributionPage', 'create', $pageParams);
+    ], $params), $identifier);
   }
 
   /**