From 032b095c1c09c3c379a13c77f4de3a9ada5929c0 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 8 Oct 2020 14:48:23 +1300 Subject: [PATCH] Add unit test that ultimately failed to hit the desired code but does add cover --- .../CRM/Member/Form/MembershipRenewalTest.php | 16 +++++++++++++--- .../phpunit/CRMTraits/Custom/CustomDataTrait.php | 14 ++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php index 8a8b26d31a..b70bfc665f 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php @@ -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(','); diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 251326c7ce..1d041f4f47 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -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; + } } -- 2.25.1