3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\Activity
;
15 * Test class for CRM_Contact_Form_Task_EmailCommon.
18 class CRM_Contact_Form_Task_EmailCommonTest
extends CiviUnitTestCase
{
23 * @throws \CRM_Core_Exception
25 protected function setUp(): void
{
27 $this->_contactIds
= [
28 $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']),
29 $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']),
31 $this->_optionValue
= $this->callAPISuccess('optionValue', 'create', [
32 'label' => '"Seamus Lee" <seamus@example.com>',
33 'option_group_id' => 'from_email_address',
38 * Cleanup after test class.
40 * Make sure the setting is returned to 'stock'.
42 public function tearDown(): void
{
43 Civi
::settings()->set('allow_mail_from_logged_in_contact', 0);
48 * Test generating domain emails
50 * @throws \CRM_Core_Exception
52 public function testDomainEmailGeneration() {
53 $emails = CRM_Core_BAO_Email
::domainEmails();
54 $this->assertNotEmpty($emails);
55 $optionValue = $this->callAPISuccess('OptionValue', 'Get', [
56 'id' => $this->_optionValue
['id'],
58 $this->assertTrue(array_key_exists('"Seamus Lee" <seamus@example.com>', $emails));
59 $this->assertEquals('"Seamus Lee" <seamus@example.com>', $optionValue['values'][$this->_optionValue
['id']]['label']);
63 * Test email uses signature.
65 * @throws \API_Exception
66 * @throws \CRM_Core_Exception
67 * @throws \CiviCRM_API3_Exception
68 * @throws \Civi\API\Exception\UnauthorizedException
70 public function testPostProcessWithSignature(): void
{
71 $mut = new CiviMailUtils($this, TRUE);
72 $bcc1 = $this->individualCreate(['email' => 'bcc1@example.com']);
73 $bcc2 = $this->individualCreate(['email' => 'bcc2@example.com']);
74 $emails = $this->callAPISuccess('Email', 'getlist', ['input' => 'bcc'])['values'];
76 foreach ($emails as $email) {
77 $bcc[] = $email['id'];
79 $bcc = implode(',', $bcc);
81 Civi
::settings()->set('allow_mail_from_logged_in_contact', 1);
82 $loggedInContactID = $this->createLoggedInUser();
83 $loggedInEmail = $this->callAPISuccess('Email', 'create', [
84 'email' => 'mickey@mouse.com',
85 'location_type_id' => 1,
87 'contact_id' => $loggedInContactID,
88 'signature_text' => 'This is a test Signature',
89 'signature_html' => '<p>This is a test Signature</p>',
92 $to = $form_contactIds = $form_toContactEmails = [];
93 for ($i = 0; $i < 27; $i++
) {
94 $email = 'spy' . $i . '@secretsquirrels.com';
95 $contactID = $this->individualCreate(['email' => $email]);
96 $form_contactIds[$contactID] = $contactID;
97 $to[] = $contactID . '::' . $email;
99 $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']);
100 $to[] = $deceasedContactID . '::' . 'email@example.com';
101 /* @var CRM_Contact_Form_Task_Email $form*/
102 $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [
103 'to' => implode(',', $to),
105 $form->_contactIds
= $form_contactIds;
106 $form->_contactIds
[$deceasedContactID] = $deceasedContactID;
108 $form->_allContactIds
= $form->_toContactIds
= $form->_contactIds
;
109 $form->_fromEmails
= [$loggedInEmail['id'] => 'mickey@mouse.com'];
110 // This rule somehow disappears if there's a form-related test before us,
111 // so register it again. See packages/HTML/QuickForm/file.php.
112 // update - actually - it's never registered. Even in form made
113 // I can see it missing - It's really weird.
114 $form->registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
115 $form->isSearchContext
= FALSE;
117 $form->submit(array_merge($form->_defaultValues
, [
118 // @todo - it's better to pass these into getForm
119 // and access them on the form using $this->getSubmittedValue().
120 'from_email_address' => $loggedInEmail['id'],
121 'subject' => 'Really interesting stuff',
126 'This is a test Signature',
129 $activity = Activity
::get(FALSE)->setSelect(['details'])->execute()->first();
130 $bccUrl1 = CRM_Utils_System
::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc1], TRUE);
131 $bccUrl2 = CRM_Utils_System
::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc2], TRUE);
132 $this->assertStringContainsString("bcc : <a href='" . $bccUrl1 . "'>Mr. Anthony Anderson II</a>, <a href='" . $bccUrl2 . "'>Mr. Anthony Anderson II</a>", $activity['details']);
133 $this->assertEquals([
135 'text' => '27 messages were sent successfully. ',
136 'title' => 'Messages Sent',
141 'text' => '(because no email address on file or communication preferences specify DO NOT EMAIL or Contact is deceased or Primary email address is On Hold)<ul><li><a href="/index.php?q=civicrm/contact/view&reset=1&cid=' . $deceasedContactID . '" title="dead@example.com">Mr. Anthony Anderson II</a></li></ul>',
142 'title' => 'One Message Not Sent',
146 ], CRM_Core_Session
::singleton()->getStatus());