From 546e90582fe217354969daa5347b829f7b9cdc74 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 7 Oct 2021 15:06:59 +1300 Subject: [PATCH] Clean up tests to call full postProcess for tasks --- CRM/Contact/Form/Task/EmailTrait.php | 59 +++- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 251 +++++++----------- .../CRM/Contact/Form/Task/EmailTest.php | 26 +- 3 files changed, 157 insertions(+), 179 deletions(-) diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index f79b53c40d..8d6bb3fd3a 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -304,7 +304,7 @@ trait CRM_Contact_Form_Task_EmailTrait { * @throws \CRM_Core_Exception */ public function setDefaultValues(): array { - $defaults = parent::setDefaultValues(); + $defaults = parent::setDefaultValues() ?: []; $fromEmails = $this->getFromEmails(); if (is_numeric(key($fromEmails))) { $emailID = (int) key($fromEmails); @@ -365,18 +365,16 @@ trait CRM_Contact_Form_Task_EmailTrait { // numerical key where as $from for use in the sendEmail in Activity needs to be of format of "To Name" $from = CRM_Utils_Mail::formatFromAddress($from); - $ccArray = $formValues['cc_id'] ? explode(',', $formValues['cc_id']) : []; - $cc = $this->getEmailString($ccArray); - $additionalDetails = empty($ccArray) ? '' : "\ncc : " . $this->getEmailUrlString($ccArray); + $cc = $this->getCc(); + $additionalDetails = empty($cc) ? '' : "\ncc : " . $this->getEmailUrlString($this->getCcArray()); - $bccArray = $formValues['bcc_id'] ? explode(',', $formValues['bcc_id']) : []; - $bcc = $this->getEmailString($bccArray); - $additionalDetails .= empty($bccArray) ? '' : "\nbcc : " . $this->getEmailUrlString($bccArray); + $bcc = $this->getBcc(); + $additionalDetails .= empty($bcc) ? '' : "\nbcc : " . $this->getEmailUrlString($this->getBccArray()); // send the mail [$sent, $activityIds] = $this->sendEmail( - $formValues['text_message'], - $formValues['html_message'], + $this->getSubmittedValue('text_message'), + $this->getSubmittedValue('html_message'), $from, $this->getAttachments($formValues), $cc, @@ -725,7 +723,7 @@ trait CRM_Contact_Form_Task_EmailTrait { * * Also insert a contact activity in each contacts record. */ - public function sendEmail( + protected function sendEmail( $text, $html, $from, @@ -829,7 +827,7 @@ trait CRM_Contact_Form_Task_EmailTrait { 'subject' => $subject, 'details' => $details, 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), - 'campaign_id' => $campaignID, + 'campaign_id' => $this->getSubmittedValue('campaign_id'), ]; if (!empty($caseID)) { $activityParams['case_id'] = $caseID; @@ -1013,4 +1011,43 @@ trait CRM_Contact_Form_Task_EmailTrait { return CRM_Utils_Token::getTokens($this->getSubject() . $this->getSubmittedValue('html_message') . $this->getSubmittedValue('text_message')); } + /** + * @return string + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + protected function getBcc(): string { + return $this->getEmailString($this->getBccArray()); + } + + /** + * @return string + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + protected function getCc(): string { + return $this->getEmailString($this->getCcArray()); + } + + /** + * @return array + */ + protected function getCcArray() { + if ($this->getSubmittedValue('cc_id')) { + return explode(',', $this->getSubmittedValue('cc_id')); + } + return []; + } + + /** + * @return array + */ + protected function getBccArray() { + $bccArray = []; + if ($this->getSubmittedValue('bcc_id')) { + $bccArray = explode(',', $this->getSubmittedValue('bcc_id')); + } + return $bccArray; + } + } diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 6545b496ed..ee01bcf283 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -1,6 +1,7 @@ quickCleanup($tablesToTruncate); $this->cleanUpAfterACLs(); - $this->setupForSmsTests(TRUE); + OptionValue::delete(FALSE)->addWhere('name', '=', 'CiviTestSMSProvider')->execute(); parent::tearDown(); } @@ -93,11 +93,8 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { * * getContactActivity() method get activities detail for given target contact * id. - * - * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception */ - public function testGetContactActivity() { + public function testGetContactActivity(): void { $contactId = $this->individualCreate(); $params = [ 'first_name' => 'liz', @@ -1221,6 +1218,7 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { } /** + * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ @@ -1242,46 +1240,22 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { ]; $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactId, 'return' => array_keys($contactDetailsIntersectKeys)]); - $contactDetailsIntersectKeys = [ - 'contact_id' => '', - 'sort_name' => '', - 'display_name' => '', - 'do_not_email' => '', - 'preferred_mail_format' => '', - 'is_deceased' => '', - 'email' => '', - 'on_hold' => '', - ]; - $contactDetails = [ - array_intersect_key($contact, $contactDetailsIntersectKeys), - ]; $subject = __FUNCTION__ . ' subject'; $html = __FUNCTION__ . ' html {contact.display_name} {case.case_type_id:label}'; $text = __FUNCTION__ . ' text {contact.display_name} {case.case_type_id:label}'; - - /* @var CRM_Contact_Form_Task_Email $form */ - $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + $form = $this->getCaseEmailTaskForm($contactId, [ 'subject' => $subject, 'html_message' => $html, 'text_message' => $text, ]); $mut = new CiviMailUtils($this, TRUE); - [$sent, $activity_ids] = $form->sendEmail( - $contactDetails, - $text, - $html, - __FUNCTION__ . '@example.com', - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - $this->getCaseID() - ); + $form->postProcess(); + $activity = Activity::get() + ->addSelect('activity_type_id:label', 'subject', 'details') + ->addWhere('activity_type_id:name', '=', 'Email') + ->execute()->first(); - $activity = $this->callAPISuccessGetSingle('Activity', ['id' => $activity_ids[0], 'return' => ['details', 'subject']]); $details = '-ALTERNATIVE ITEM 0- ' . __FUNCTION__ . ' html ' . $contact['display_name'] . ' Housing Support -ALTERNATIVE ITEM 1- @@ -1338,59 +1312,33 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { * This is different from SentEmailBasic to try to help prevent code that * assumes an email always has tokens in it. * + * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ public function testSendEmailBasicWithoutAnyTokens(): void { $contactId = $this->individualCreate(); // create a logged in USER since the code references it for sendEmail user. - $loggedInUser = $this->createLoggedInUser(); - - $contactDetailsIntersectKeys = [ - 'contact_id' => '', - 'sort_name' => '', - 'display_name' => '', - 'do_not_email' => '', - 'preferred_mail_format' => '', - 'is_deceased' => '', - 'email' => '', - 'on_hold' => '', - ]; - - $contact = $this->callAPISuccess('Contact', 'getsingle', ['id' => $contactId, 'return' => array_keys($contactDetailsIntersectKeys)]); - $contactDetailsIntersectKeys = [ - 'contact_id' => '', - 'sort_name' => '', - 'display_name' => '', - 'do_not_email' => '', - 'preferred_mail_format' => '', - 'is_deceased' => '', - 'email' => '', - 'on_hold' => '', - ]; - $contactDetails = [ - array_intersect_key($contact, $contactDetailsIntersectKeys), - ]; + $this->createLoggedInUser(); $subject = __FUNCTION__ . ' subject'; $html = __FUNCTION__ . ' html'; $text = __FUNCTION__ . ' text'; - /* @var CRM_Contact_Form_Task_Email $form */ - $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + $form = $this->getContactEmailTaskForm($contactId, [ 'subject' => $subject, 'html_message' => $html, 'text_message' => $text, ]); $mut = new CiviMailUtils($this, TRUE); - [$sent, $activity_ids] = $form->sendEmail( - $contactDetails, - $text, - $html, - $from = __FUNCTION__ . '@example.com' - ); + $form->postProcess(); + + $activity = Activity::get() + ->addSelect('activity_type_id:label', 'subject', 'details') + ->addWhere('activity_type_id:name', '=', 'Email') + ->execute()->first(); - $activity = $this->callAPISuccessGetSingle('Activity', ['id' => $activity_ids[0], 'return' => ['details', 'subject']]); $details = "-ALTERNATIVE ITEM 0- $html -ALTERNATIVE ITEM 1- @@ -1400,8 +1348,8 @@ $text $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); $this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.'); $mut->checkMailLog([ - "From: $from", - 'To: Anthony Anderson ', + 'From: from@example.com', + 'To: Anthony Anderson ', "Subject: $subject", $html, $text, @@ -1409,29 +1357,18 @@ $text $mut->stop(); } - public function testSendEmailWithCampaign() { + /** + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testSendEmailWithCampaign(): void { // Create a contact and contactDetails array. $contactId = $this->individualCreate(); // create a logged in USER since the code references it for sendEmail user. $this->createLoggedInUser(); - $session = CRM_Core_Session::singleton(); - $loggedInUser = $session->get('userID'); - - $contact = $this->civicrm_api('contact', 'getsingle', ['id' => $contactId, 'version' => $this->_apiversion]); - $contactDetailsIntersectKeys = [ - 'contact_id' => '', - 'sort_name' => '', - 'display_name' => '', - 'do_not_email' => '', - 'preferred_mail_format' => '', - 'is_deceased' => '', - 'email' => '', - 'on_hold' => '', - ]; - $contactDetails = [ - array_intersect_key($contact, $contactDetailsIntersectKeys), - ]; + $this->enableCiviCampaign(); // Create a campaign. $result = $this->civicrm_api('Campaign', 'create', [ @@ -1443,29 +1380,24 @@ $text $html = __FUNCTION__ . ' html'; $text = __FUNCTION__ . ' text'; /* @var CRM_Activity_Form_Task_Email $form */ - $form = $this->getFormObject('CRM_Activity_Form_Task_Email', [ - 'subject' => __FUNCTION__ . ' subject', + $form = $this->getCaseEmailTaskForm($contactId, [ + 'subject' => '', + 'html_message' => $html, + 'text_message' => $text, + 'campaign_id' => $campaign_id, ]); + $form->postProcess(); + $activity = Activity::get() + ->addSelect('activity_type_id:label', 'subject', 'details', 'campaign_id') + ->addWhere('activity_type_id:name', '=', 'Email') + ->execute()->first(); - [$sent, $activity_ids] = $email_result = $form->sendEmail( - $contactDetails, - $text, - $html, - __FUNCTION__ . '@example.com', - $attachments = NULL, - $cc = NULL, - $bcc = NULL, - $contactIds = NULL, - NULL, - $campaign_id - ); - $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activity_ids[0], 'version' => $this->_apiversion]); $this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.'); } /** */ - public function testSendSMSWithoutPermission() { + public function testSendSMSWithoutPermission(): void { $dummy = NULL; $session = CRM_Core_Session::singleton(); CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; @@ -1679,65 +1611,42 @@ $text $caseTest->setUp(); // Create a contact and contactDetails array. $contactId = $this->individualCreate(); - $contact = $this->callAPISuccess('Contact', 'get', ['id' => $contactId]); // create a logged in USER since the code references it for sendEmail user. $this->createLoggedInUser(); CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM', 'access all cases and activities', 'administer CiviCase']; - $session = CRM_Core_Session::singleton(); - $loggedInUser = $session->get('userID'); - - // create a case for this user - $result = $this->callAPISuccess('Case', 'create', [ - 'contact_id' => $contactId, - 'case_type_id' => 1, - 'subject' => "my case", - 'status_id' => "Open", - ]); - - $caseId = $result['id']; $subject = __FUNCTION__ . ' subject {case.subject}'; $html = __FUNCTION__ . ' html {case.subject}'; $text = __FUNCTION__ . ' text'; $mut = new CiviMailUtils($this, TRUE); - /* @var CRM_Contact_Form_Task_Email $form */ - $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + $form = $this->getCaseEmailTaskForm($contactId, [ 'subject' => $subject, 'html_message' => $html, 'text_message' => $text, + 'to' => $contactId . '::' . 'email@example.com', ]); - [$sent, $activity_ids] = $form->sendEmail( - $contact['values'], - $text, - $html, - $from = __FUNCTION__ . '@example.com', - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - $caseId - ); - $activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity_ids[0], 'return' => ['case_id']]); - $this->assertEquals($caseId, $activity['case_id'][0], 'Activity case_id does not match.'); - $mut->checkMailLog(['subject my case']); + $form->postProcess(); + $activity = Activity::get() + ->addSelect('id') + ->addWhere('activity_type_id:name', '=', 'Email') + ->execute()->first(); + $activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity['id'], 'return' => ['case_id']]); + $this->assertEquals($this->getCaseID(), $activity['case_id'][0], 'Activity case_id does not match.'); + $mut->checkMailLog(['subject Case Subject']); $mut->stop(); } /** * Checks that tokens are uniquely replaced for contacts. */ - public function testSendEmailWillReplaceTokensUniquelyForEachContact() { + public function testSendEmailWillReplaceTokensUniquelyForEachContact(): void { $contactId1 = $this->individualCreate(['last_name' => 'Red']); $contactId2 = $this->individualCreate(['last_name' => 'Pink']); // create a logged in USER since the code references it for sendEmail user. $this->createLoggedInUser(); - $session = CRM_Core_Session::singleton(); - $loggedInUser = $session->get('userID'); $contact = $this->callAPISuccess('Contact', 'get', ['sequential' => 1, 'id' => ['IN' => [$contactId1, $contactId2]]]); // Create a campaign. @@ -1757,19 +1666,14 @@ $text 'subject' => $subject, 'html_message' => $html, 'text_message' => $text, - ]); - $form->sendEmail( - $contact['values'], - $text, - $html, - __FUNCTION__ . '@example.com', - $attachments = NULL, - $cc = NULL, - $bcc = NULL, - $additionalDetails = NULL, - NULL, - $campaign_id - ); + 'campaign_id' => $campaign_id, + 'from_email_address' => 'from@example.com', + 'to' => $contactId1 . '::email@example.com,' . $contactId2 . '::email2@example.com', + ], [], []); + $form->set('cid', $contactId1 . ',' . $contactId2); + $form->buildForm(); + $form->postProcess(); + $result = $this->callAPISuccess('activity', 'get', ['campaign_id' => $campaign_id]); // An activity created for each of the two contacts $this->assertEquals(2, $result['count']); @@ -2837,4 +2741,39 @@ $textValue return $this->ids['Campaign'][0]; } + /** + * @param int $contactId + * @param array $submittedValues + * + * @return \CRM_Case_Form_Task_Email + */ + protected function getCaseEmailTaskForm(int $contactId, array $submittedValues): CRM_Case_Form_Task_Email { + $_REQUEST['cid'] = $contactId; + $_REQUEST['caseid'] = $this->getCaseID(); + /* @var CRM_Case_Form_Task_Email $form */ + $form = $this->getFormObject('CRM_Case_Form_Task_Email', array_merge([ + 'to' => $contactId . '::' . 'email@example.com', + 'from_email_address' => 'from@example.com', + ], $submittedValues)); + $form->buildForm(); + return $form; + } + + /** + * @param int $contactId + * @param array $submittedValues + * + * @return \CRM_Contact_Form_Task_Email + */ + protected function getContactEmailTaskForm(int $contactId, array $submittedValues): CRM_Contact_Form_Task_Email { + $_REQUEST['cid'] = $contactId; + /* @var CRM_Contact_Form_Task_Email $form */ + $form = $this->getFormObject('CRM_Contact_Form_Task_Email', array_merge([ + 'to' => $contactId . '::' . 'email@example.com', + 'from_email_address' => 'from@example.com', + ], $submittedValues)); + $form->buildForm(); + return $form; + } + } diff --git a/tests/phpunit/CRM/Contact/Form/Task/EmailTest.php b/tests/phpunit/CRM/Contact/Form/Task/EmailTest.php index 1f7d581e3d..4441228273 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/EmailTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/EmailTest.php @@ -98,6 +98,12 @@ class CRM_Contact_Form_Task_EmailTest extends CiviUnitTestCase { /* @var CRM_Contact_Form_Task_Email $form*/ $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ 'to' => implode(',', $to), + 'subject' => 'Really interesting stuff', + 'bcc_id' => $bcc, + 'cc_id' => '', + 'from_email_address' => $loggedInEmail['id'], + 'html_message' => 'blah', + 'text_message' => 'blah', ]); $form->_contactIds = $form_contactIds; $form->_contactIds[$deceasedContactID] = $deceasedContactID; @@ -106,18 +112,14 @@ class CRM_Contact_Form_Task_EmailTest extends CiviUnitTestCase { $form->_fromEmails = [$loggedInEmail['id'] => 'mickey@mouse.com']; $form->isSearchContext = FALSE; $form->buildForm(); - $form->submit(array_merge($form->_defaultValues, [ - // @todo - it's better to pass these into getForm - // and access them on the form using $this->getSubmittedValue(). - 'from_email_address' => $loggedInEmail['id'], - 'subject' => 'Really interesting stuff', - 'bcc_id' => $bcc, - 'cc_id' => '', - ])); - $mut->checkMailLog([ - 'This is a test Signature', - ]); - $mut->stop(); + $this->assertEquals([ + 'html_message' => '

--

This is a test Signature

', + 'text_message' => ' + +-- +This is a test Signature', + ], $form->_defaultValues); + $form->postProcess(); $activity = Activity::get(FALSE)->setSelect(['details'])->execute()->first(); $bccUrl1 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc1], TRUE); $bccUrl2 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc2], TRUE); -- 2.25.1