From 1c932c529f27206a970dbaad33a456c68bbd395a Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Wed, 25 May 2022 09:22:54 -0400 Subject: [PATCH] bring back deleted tests --- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index b80a1a0c77..e5b46aaf9a 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -1694,6 +1694,144 @@ $textValue } } + /** + * Same as testSendEmailWillReplaceTokensUniquelyForEachContact but with + * 3 recipients and an attachment. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testSendEmailWillReplaceTokensUniquelyForEachContact3(): void { + $contactId1 = $this->individualCreate(['last_name' => 'Red']); + $contactId2 = $this->individualCreate(['last_name' => 'Pink']); + $contactId3 = $this->individualCreate(['last_name' => 'Ochre']); + + // create a logged in USER since the code references it for sendEmail user. + $this->createLoggedInUser(); + $contact = $this->callAPISuccess('Contact', 'get', ['sequential' => 1, 'id' => ['IN' => [$contactId1, $contactId2, $contactId3]]]); + + // Add contact tokens in subject, html , text. + $subject = __FUNCTION__ . ' subject' . '{contact.display_name}'; + $html = __FUNCTION__ . ' html' . '{contact.display_name}'; + // Check the smarty doesn't mess stuff up. + $text = ' text' . '{contact.display_name} {$contact.first_name}'; + + $filepath = Civi::paths()->getPath('[civicrm.files]/custom'); + $fileName = 'test_email_create.txt'; + $fileUri = "{$filepath}/{$fileName}"; + // Create a file. + CRM_Utils_File::createFakeFile($filepath, 'aaaaaa', $fileName); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + 'subject' => $subject, + 'html_message' => $html, + 'text_message' => $text, + 'campaign_id' => $this->getCampaignID(), + 'from_email_address' => 'from@example.com', + 'to' => $contactId1 . '::' . $contact['values'][0]['email'] . ',' + . $contactId2 . '::' . $contact['values'][1]['email'] . ',' + . $contactId3 . '::' . $contact['values'][2]['email'], + 'attachFile_1' => [ + 'uri' => $fileUri, + 'type' => 'text/plain', + 'location' => $fileUri, + 'name' => $fileUri, + ], + 'attachDesc_1' => '', + ], [], []); + $form->set('cid', $contactId1 . ',' . $contactId2 . ',' . $contactId3); + $form->buildForm(); + $form->postProcess(); + + $result = $this->callAPISuccess('Activity', 'get', ['campaign_id' => $this->getCampaignID()]); + // An activity created for each of the two contacts + $this->assertEquals(3, $result['count']); + $id = 0; + foreach ($result['values'] as $activity) { + $htmlValue = str_replace('{contact.display_name}', $contact['values'][$id]['display_name'], $html); + $textValue = str_replace('{contact.display_name}', $contact['values'][$id]['display_name'], $text); + $subjectValue = str_replace('{contact.display_name}', $contact['values'][$id]['display_name'], $subject); + $details = "-ALTERNATIVE ITEM 0- +$htmlValue +-ALTERNATIVE ITEM 1- +$textValue +-ALTERNATIVE END- +"; + $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); + $this->assertEquals($activity['subject'], $subjectValue, 'Activity subject does not match.'); + $id++; + } + + unlink($fileUri); + } + + /** + * Checks that attachments are not duplicated for activities. + */ + public function testSendEmailDoesNotDuplicateAttachmentFileIdsForActivitiesCreated() { + $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. + $result = $this->callAPISuccess('Campaign', 'create', [ + 'version' => $this->_apiversion, + 'title' => __FUNCTION__ . ' campaign', + ]); + $campaign_id = $result['id']; + + $subject = __FUNCTION__ . ' subject'; + $html = __FUNCTION__ . ' html'; + $text = __FUNCTION__ . ' text'; + $userID = $loggedInUser; + + $filepath = Civi::paths()->getPath('[civicrm.files]/custom'); + $fileName = "test_email_create.txt"; + $fileUri = "{$filepath}/{$fileName}"; + // Create a file. + CRM_Utils_File::createFakeFile($filepath, 'Bananas do not bend themselves without a little help.', $fileName); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + 'subject' => $subject, + 'html_message' => $html, + 'text_message' => $text, + 'campaign_id' => $campaign_id, + 'from_email_address' => 'from@example.com', + 'to' => $contactId1 . '::' . $contact['values'][0]['email'] . ',' + . $contactId2 . '::' . $contact['values'][1]['email'], + 'attachFile_1' => [ + 'uri' => $fileUri, + 'type' => 'text/plain', + 'location' => $fileUri, + 'name' => $fileUri, + ], + 'attachDesc_1' => '', + ], [], []); + $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, i.e two activities. + $this->assertEquals(2, $result['count']); + $activityIds = array_column($result['values'], 'id'); + $result = $this->callAPISuccess('Activity', 'get', [ + 'return' => ['file_id'], + 'id' => ['IN' => $activityIds], + 'sequential' => 1, + ]); + + // Verify that the that both activities are linked to the same File Id. + $this->assertEquals($result['values'][0]['file_id'], $result['values'][1]['file_id']); + + unlink($fileUri); + } + /** * Adds a case with one activity. * -- 2.25.1