[NFC] Test cleanup, prefer full form test flow
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 27 Mar 2023 02:36:37 +0000 (15:36 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 27 Mar 2023 02:59:32 +0000 (15:59 +1300)
tests/phpunit/CRM/Member/Form/MembershipTest.php

index c2968aa28a6b87335b9dfa913574df07805ffe61..341764ba25ad607363b8410471e7a1d8560e235e 100644 (file)
@@ -198,6 +198,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    * Test CRM_Member_Form_Membership::formRule() with a parameter
    * that has an start date before the join date and a rolling
    * membership type.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testFormRuleRollingEarlyStart(): void {
     $unixNow = time();
@@ -212,7 +214,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     $files = [];
     $obj = new CRM_Member_Form_Membership();
     $rc = CRM_Member_Form_Membership::formRule($params, $files, $obj);
-    $this->assertisArray($rc);
+    $this->assertIsArray($rc);
     $this->assertArrayHasKey('start_date', $rc);
   }
 
@@ -221,9 +223,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    *  that has an end date before the start date and a rolling
    *  membership type
    */
-  public function testFormRuleRollingEarlyEnd() {
-    $unixNow = time();
-    $unixYesterday = $unixNow - (24 * 60 * 60);
+  public function testFormRuleRollingEarlyEnd(): void {
+    $unixYesterday = time() - (24 * 60 * 60);
     $ymdYesterday = date('Y-m-d', $unixYesterday);
     $params = [
       'join_date' => date('Y-m-d'),
@@ -235,13 +236,16 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     $obj = new CRM_Member_Form_Membership();
     $rc = CRM_Member_Form_Membership::formRule($params, $files, $obj);
     $this->assertIsArray($rc);
-    $this->assertTrue(array_key_exists('end_date', $rc));
+    $this->assertArrayHasKey('end_date', $rc);
   }
 
   /**
-   *  Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type.
+   *  Test CRM_Member_Form_Membership::formRule() with end date but no start
+   * date and a rolling membership type.
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testFormRuleRollingEndNoStart() {
+  public function testFormRuleRollingEndNoStart(): void {
     $unixNow = time();
     $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
     $ymdYearFromNow = date('Y-m-d', $unixYearFromNow);
@@ -253,16 +257,18 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     ];
     $files = [];
     $obj = new CRM_Member_Form_Membership();
-    $rc = $obj::formRule($params, $files, $obj);
-    $this->assertIsArray($rc);
-    $this->assertTrue(array_key_exists('start_date', $rc));
+    $ruleResult = $obj::formRule($params, $files, $obj);
+    $this->assertIsArray($ruleResult);
+    $this->assertArrayHasKey('start_date', $ruleResult);
   }
 
   /**
    *  Test CRM_Member_Form_Membership::formRule() with a parameter
    *  that has an end date and a lifetime membership type
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testFormRuleRollingLifetimeEnd() {
+  public function testFormRuleRollingLifetimeEnd(): void {
     $unixNow = time();
     $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
     $params = [
@@ -277,7 +283,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     $obj = new CRM_Member_Form_Membership();
     $rc = $obj::formRule($params, $files, $obj);
     $this->assertIsArray($rc);
-    $this->assertTrue(array_key_exists('status_id', $rc));
+    $this->assertArrayHasKey('status_id', $rc);
   }
 
   /**
@@ -313,7 +319,10 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
     $this->assertTrue($validationResponse);
   }
 
-  public function testFormRuleUntilDateOverrideWithNoOverrideEndDate() {
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testFormRuleUntilDateOverrideWithNoOverrideEndDate(): void {
     $params = [
       'join_date' => date('Y-m-d'),
       'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']],
@@ -331,7 +340,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    *  Test CRM_Member_Form_Membership::formRule() with a join date
    *  of one month from now and a rolling membership type
    */
-  public function testFormRuleRollingJoin1MonthFromNow() {
+  public function testFormRuleRollingJoin1MonthFromNow(): void {
     $unixNow = time();
     $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
     $params = [
@@ -482,11 +491,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    * @dataProvider getThousandSeparators
    */
   public function testSubmit(string $thousandSeparator): void {
-    CRM_Core_Session::singleton()->getStatus(TRUE);
+    $_REQUEST['mode'] = 'test';
     $this->setCurrencySeparators($thousandSeparator);
-    $form = $this->getForm();
-    $this->mut = new CiviMailUtils($this, TRUE);
-    $form->_mode = 'test';
     $this->createLoggedInUser();
     $params = [
       'cid' => $this->_individualId,
@@ -522,11 +528,13 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
       'send_receipt' => TRUE,
       'receipt_text' => 'Receipt text',
     ];
-    $form->_contactID = $this->_individualId;
-    $form->testSubmit($params);
+    $form = $this->getForm($params);
+    $mailUtil = new CiviMailUtils($this, TRUE);
+    $form->buildForm();
+    $form->postProcess();
     $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
     $membershipEndYear = date('Y') + 1;
-    if (date('m-d') == '12-31') {
+    if (date('m-d') === '12-31') {
       // If you join on Dec 31, then the first term would end right away, so
       // add a year.
       $membershipEndYear++;
@@ -564,11 +572,10 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
         'return' => 'payment_instrument_id',
       ]),
     ], 'online');
-    $this->mut->checkMailLog([
+    $mailUtil->checkMailLog([
       Civi::format()->money('1234.56'),
       'Receipt text',
     ]);
-    $this->mut->stop();
     $this->assertEquals([
       [
         'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new Membership Expiration Date is December 31st, ' . $membershipEndYear . '. A membership confirmation and receipt has been sent to anthony_anderson@civicrm.org.',
@@ -884,7 +891,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
   }
 
   /**
-   * CRM-20946: Test the financial entires especially the reversed amount,
+   * CRM-20946: Test the financial entries especially the reversed amount,
    *  after related Contribution is cancelled
    *
    * @throws \CRM_Core_Exception
@@ -933,13 +940,12 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
 
   /**
    * Test membership with soft credits.
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testMembershipSoftCredit() {
-    $softIndividualId = $this->individualCreate();
-
-    $form = $this->getForm();
-    $form->preProcess();
+  public function testMembershipSoftCredit(): void {
     $this->createLoggedInUser();
+    $softIndividualID = $this->individualCreate();
     $params = $this->getBaseSubmitParams();
     unset($params['auto_renew'], $params['is_recur']);
     $params['record_contribution'] = TRUE;
@@ -948,16 +954,15 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
       'name' => "Gift",
       'option_group_id' => "soft_credit_type",
     ]);
-    $params['soft_credit_contact_id'] = $softIndividualId;
-    $form->_contactID = $this->_individualId;
-    // $form->_mode = 'test';
-    $form->testSubmit($params);
+    $params['soft_credit_contact_id'] = $softIndividualID;
+    $form = $this->getForm($params);
+    $form->postProcess();
     // Membership is created on main contact.
     $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
 
     // Verify is main contribution is created on soft contact.
     $contribution = $this->callAPISuccessGetSingle('Contribution', [
-      'contact_id' => $softIndividualId,
+      'contact_id' => $softIndividualID,
     ]);
     $this->assertEquals($contribution['soft_credit'][1]['contact_id'], $this->_individualId);
 
@@ -974,8 +979,6 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    */
   public function testSubmitPayLaterWithBilling(): void {
-    $form = $this->getForm();
-    $this->createLoggedInUser();
     $params = [
       'contact_id' => $this->_individualId,
       'join_date' => date('Y-m-d'),
@@ -1007,6 +1010,9 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
       'billing_postal_code-5' => '90210',
       'billing_country_id-5' => '1228',
     ];
+    $form = $this->getForm($params);
+    $this->createLoggedInUser();
+
     $form->_contactID = $this->_individualId;
 
     $form->testSubmit($params);
@@ -1284,7 +1290,6 @@ Expires: ',
    * @param array $formValues
    *
    * @return \CRM_Member_Form_Membership
-   * @throws \CRM_Core_Exception
    */
   protected function getForm(array $formValues = []): CRM_Member_Form_Membership {
     if (isset($_REQUEST['cid'])) {
@@ -1293,6 +1298,7 @@ Expires: ',
     /** @var CRM_Member_Form_Membership $form*/
     $form = $this->getFormObject('CRM_Member_Form_Membership', $formValues);
     $form->preProcess();
+    $form->buildForm();
     return $form;
   }
 
@@ -1353,13 +1359,10 @@ Expires: ',
    *
    * @throws \CRM_Core_Exception
    */
-  protected function createTwoMembershipsViaPriceSetInBackEnd(int $contactId, $isTaxEnabled = TRUE): void {
-    $form = $this->getForm();
-    $form->preProcess();
+  protected function createTwoMembershipsViaPriceSetInBackEnd(int $contactId, bool $isTaxEnabled = TRUE): void {
+    // register for both of these memberships via backoffice membership form submission
     $this->createLoggedInUser();
     $this->createMembershipPriceSet();
-
-    // register for both of these memberships via backoffice membership form submission
     $params = [
       'cid' => $contactId,
       'contact_id' => $contactId,
@@ -1398,7 +1401,8 @@ Expires: ',
       'billing_postal_code-5' => '90210',
       'billing_country_id-5' => '1228',
     ];
-    $form->testSubmit($params);
+    $form = $this->getForm($params);
+    $form->postProcess();
   }
 
   /**
@@ -1508,7 +1512,7 @@ Expires: ',
    * @throws \CRM_Core_Exception
    * @throws \Exception
    */
-  public function testCreatePendingWithMultipleTerms() {
+  public function testCreatePendingWithMultipleTerms(): void {
     CRM_Core_Session::singleton()->getStatus(TRUE);
     $this->mut = new CiviMailUtils($this, TRUE);
     $this->createLoggedInUser();
@@ -1581,8 +1585,10 @@ Expires: ',
   }
 
   /**
-   * Test Membership Payment owned by other contact, membership view should show all contribution records in listing.
-   * is other contact.
+   * Test Membership Payment owned by other contact, membership view should
+   * show all contribution records in listing. is other contact.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testMembershipViewContributionOwnerDifferent(): void {
     // Membership Owner