Move Zero amount test
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 19 Nov 2023 22:32:02 +0000 (11:32 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Nov 2023 07:55:07 +0000 (20:55 +1300)
Civi/Test/FormTrait.php
tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php
tests/phpunit/api/v3/ContributionPageTest.php

index 56944e05c704cb30116bb84ade5e14b814064c16..47c602c9c70f7a45441234df72d9e1a2e2cbcb67 100644 (file)
@@ -57,6 +57,18 @@ trait FormTrait {
     }
   }
 
+  /**
+   * Assert that the sent mail included the supplied strings.
+   *
+   * @param array $strings
+   * @param int $mailIndex
+   */
+  protected function assertMailSentNotContainStrings(array $strings, int $mailIndex = 0): void {
+    foreach ($strings as $string) {
+      $this->assertMailSentNotContainingString($string, $mailIndex);
+    }
+  }
+
   /**
    * Assert that the sent mail included the supplied string.
    *
@@ -68,6 +80,17 @@ trait FormTrait {
     $this->assertStringContainsString(preg_replace('/\s+/', '', $string), preg_replace('/\s+/', '', $mail['body']));
   }
 
+  /**
+   * Assert that the sent mail included the supplied string.
+   *
+   * @param string $string
+   * @param int $mailIndex
+   */
+  protected function assertMailSentNotContainingString(string $string, int $mailIndex = 0): void {
+    $mail = $this->form->getMail()[$mailIndex];
+    $this->assertStringNotContainsString(preg_replace('/\s+/', '', $string), preg_replace('/\s+/', '', $mail['body']));
+  }
+
   /**
    * Assert that the sent mail included the supplied string.
    *
@@ -79,6 +102,15 @@ trait FormTrait {
     $this->assertStringContainsString($string, $mail['headers']);
   }
 
+  /**
+   * Assert the right number of mails were sent.
+   *
+   * @param int $count
+   */
+  protected function assertMailSentCount(int $count): void {
+    $this->assertCount($count, $this->form->getMail());
+  }
+
   /**
    * Assert that the sent mail included the supplied strings.
    *
index 2d1f0f9f26c85fcdc4e0e0b2aa26602d1203a6c1..97c3aca6065c15291262ed0466bc4759275cb28a 100644 (file)
@@ -529,4 +529,41 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase {
     $this->assertMailSentContainingHeaderString('Test Frontend title');
   }
 
+  /**
+   * Test a zero dollar membership (quick config, not separate payment).
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail(): void {
+    $this->createTestEntity('MembershipType', [
+      'min_amount' => 0,
+      'name' => 'Free',
+      'duration_unit' => 'year',
+      'duration_interval' => 1,
+      'period_type' => 'rolling',
+      'member_of_contact_id' => CRM_Core_BAO_Domain::getDomain()->contact_id,
+      'financial_type_id:name' => 'Member Dues',
+      'is_active' => 1,
+      'sequential' => 1,
+      'visibility' => 'Public',
+    ], 'free');
+    $this->contributionPageQuickConfigCreate([], [], FALSE, TRUE, TRUE, TRUE);
+    $this->submitOnlineContributionForm([
+      'payment_processor_id' => $this->ids['PaymentProcessor']['dummy'],
+      'price_' . $this->ids['PriceField']['contribution_amount'] => -1,
+      'price_' . $this->ids['PriceField']['membership_amount'] => $this->ids['PriceFieldValue']['membership_free'],
+      'id' => $this->getContributionPageID(),
+    ] + $this->getBillingSubmitValues(),
+    $this->getContributionPageID());
+
+    $contribution = $this->callAPISuccessGetSingle('Contribution', ['contribution_page_id' => $this->getContributionPageID()]);
+    $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id']]);
+    //Assert only one mail is being sent.
+    $this->assertMailSentCount(1);
+    $this->assertMailSentContainingStrings([
+      'Free',
+    ]);
+    $this->assertMailSentNotContainingString('Amount');
+  }
+
 }
index ae883719e4ecd659e5c6b23f5fff7a398629f7c8..ba7adb63853a3ff33845f6d88256a828bc894424 100644 (file)
@@ -306,42 +306,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals(date('Y-m-d', strtotime('+ 2 year - 1 day')), $membership['end_date']);
   }
 
-  /**
-   * Test submit with a membership block in place.
-   */
-  public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail(): void {
-    $mut = new CiviMailUtils($this, TRUE);
-    $this->setUpMembershipContributionPage(FALSE, FALSE, ['minimum_fee' => 0]);
-    $this->addProfile('supporter_profile', $this->getContributionPageID());
-
-    $submitParams = [
-      $this->getPriceFieldLabel('membership') => $this->getPriceFieldValue('general'),
-      'id' => $this->getContributionPageID(),
-      'billing_first_name' => 'Billy',
-      'billing_middle_name' => 'Goat',
-      'billing_last_name' => 'Gruffier',
-      'email-Primary' => 'billy-goat@the-new-bridge.net',
-      'payment_processor_id' => $this->params['payment_processor_id'],
-    ];
-
-    $this->callAPISuccess('ContributionPage', 'submit', $submitParams);
-    $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->getContributionPageID()]);
-    $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]);
-    //Assert only one mail is being sent.
-    $msgs = $mut->getAllMessages();
-    $this->assertCount(1, $msgs);
-
-    $mut->checkMailLog([
-      'Membership Type',
-      'General',
-      'Gruffier',
-    ], [
-      'Amount',
-    ]);
-    $mut->stop();
-    $mut->clearMessages();
-  }
-
   /**
    * Test submit with a pay later and check line item in mails.
    */