Add unit test that ultimately failed to hit the desired code but does add cover
authoreileen <emcnaughton@wikimedia.org>
Thu, 8 Oct 2020 01:48:23 +0000 (14:48 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 8 Oct 2020 01:48:23 +0000 (14:48 +1300)
tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php

index 8a8b26d31af4849de6d363dc7694a0f1f82f0ec0..b70bfc665f9d2652cf5b214cc23dd3b3766fbc57 100644 (file)
@@ -18,6 +18,7 @@ use Civi\Api4\Contact;
  * @group headless
  */
 class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
+  use CRMTraits_Custom_CustomDataTrait;
 
   protected $_individualId;
   protected $_contribution;
@@ -119,7 +120,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
         'civicrm_relationship',
         'civicrm_uf_match',
         'civicrm_address',
-      ]
+      ], TRUE
     );
     foreach ($this->ids['contact'] as $contactID) {
       $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
@@ -404,6 +405,9 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
    */
   public function testSubmitRecurCompleteInstantWithMail($thousandSeparator) {
     $this->setCurrencySeparators($thousandSeparator);
+    // Visibility is 'Public Pages and Listings' in order to try to get to a specific line
+    // of code to ensure it's tested - it might not be a 'real' use case.
+    $this->createCustomGroupWithFieldOfType(['extends' => 'Membership'], 'multi_country', NULL, ['visibility' => 'Public Pages and Listings']);
     $form = $this->getForm();
     $this->mut = new CiviMailUtils($this, TRUE);
     /** @var \CRM_Core_Payment_Dummy $processor */
@@ -424,13 +428,19 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $form->preProcess();
 
     $form->_contactID = $this->_individualId;
-    $form->_mode = 'test';
+    $form->_mode = 'live';
 
-    $form->testSubmit(array_merge($this->getBaseSubmitParams(), ['is_recur' => 1, 'send_receipt' => 1, 'auto_renew' => 1]));
+    $form->testSubmit(array_merge($this->getBaseSubmitParams(), [
+      'is_recur' => 1,
+      'send_receipt' => 1,
+      'auto_renew' => 1,
+      $this->getCustomFieldName('multi_country') => [1006, 1007],
+    ]));
     $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
     $this->assertEquals(1, $contributionRecur['is_email_receipt']);
     $this->mut->checkMailLog([
       '$ ' . $this->formatMoneyInput(7800.90),
+      'Country-multi : Angola, Anguilla',
     ]);
     $this->mut->stop();
     $this->setCurrencySeparators(',');
index 251326c7ce75a15b62dead82583bf4fea343e9eb..1d041f4f479b8b36068912472db9c57238400ec7 100644 (file)
@@ -86,14 +86,16 @@ trait CRMTraits_Custom_CustomDataTrait {
    *   Params for the group to be created.
    * @param string $customFieldType
    *
-   * @param string $identifier
+   * @param string|null $identifier
+   *
+   * @param array $fieldParams
    *
    * @throws \API_Exception
    * @throws \CRM_Core_Exception
    * @throws \Civi\API\Exception\UnauthorizedException
    */
-  public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
-    $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio'];
+  public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL, $fieldParams = []) {
+    $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio', 'multi_country'];
     if (!in_array($customFieldType, $supported, TRUE)) {
       throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
     }
@@ -101,7 +103,7 @@ trait CRMTraits_Custom_CustomDataTrait {
     $groupParams['name'] = $identifier ?? 'Custom Group';
     $this->createCustomGroup($groupParams);
     $reference = &$this->ids['CustomField'][$identifier . $customFieldType];
-    $fieldParams = ['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]];
+    $fieldParams = array_merge($fieldParams, ['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
     switch ($customFieldType) {
       case 'text':
         $reference = $this->createTextCustomField($fieldParams)['id'];
@@ -127,6 +129,10 @@ trait CRMTraits_Custom_CustomDataTrait {
         $reference = $this->createIntegerRadioCustomField($fieldParams)['id'];
         return;
 
+      case 'multi_country':
+        $reference = $this->createMultiCountryCustomField($fieldParams)['id'];
+        return;
+
     }
   }