Merge pull request #20995 from ixiam/dev-import-phone-extension
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / EmailCommonTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\Activity;
13
14 /**
15 * Test class for CRM_Contact_Form_Task_EmailCommon.
16 * @group headless
17 */
18 class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase {
19
20 /**
21 * Set up for tests.
22 *
23 * @throws \CRM_Core_Exception
24 */
25 protected function setUp(): void {
26 parent::setUp();
27 $this->_contactIds = [
28 $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']),
29 $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']),
30 ];
31 $this->_optionValue = $this->callAPISuccess('optionValue', 'create', [
32 'label' => '"Seamus Lee" <seamus@example.com>',
33 'option_group_id' => 'from_email_address',
34 ]);
35 }
36
37 /**
38 * Cleanup after test class.
39 *
40 * Make sure the setting is returned to 'stock'.
41 */
42 public function tearDown(): void {
43 Civi::settings()->set('allow_mail_from_logged_in_contact', 0);
44 parent::tearDown();
45 }
46
47 /**
48 * Test generating domain emails
49 *
50 * @throws \CRM_Core_Exception
51 */
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'],
57 ]);
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']);
60 }
61
62 /**
63 * Test email uses signature.
64 *
65 * @throws \API_Exception
66 * @throws \CRM_Core_Exception
67 * @throws \CiviCRM_API3_Exception
68 * @throws \Civi\API\Exception\UnauthorizedException
69 */
70 public function testPostProcessWithSignature() {
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'];
75 $bcc = [];
76 foreach ($emails as $email) {
77 $bcc[] = $email['id'];
78 }
79 $bcc = implode(',', $bcc);
80
81 Civi::settings()->set('allow_mail_from_logged_in_contact', 1);
82 $loggedInContactID = $this->createLoggedInUser();
83 /* @var CRM_Contact_Form_Task_Email $form*/
84 $form = $this->getFormObject('CRM_Contact_Form_Task_Email');
85
86 for ($i = 0; $i < 27; $i++) {
87 $email = 'spy' . $i . '@secretsquirrels.com';
88 $contactID = $this->individualCreate(['email' => $email]);
89 $form->_contactIds[$contactID] = $contactID;
90 $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => $email])] = $email;
91 }
92 $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']);
93 $form->_contactIds[$deceasedContactID] = $deceasedContactID;
94 $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => 'dead@example.com'])] = 'dead@example.com';
95
96 $loggedInEmail = $this->callAPISuccess('Email', 'create', [
97 'email' => 'mickey@mouse.com',
98 'location_type_id' => 1,
99 'is_primary' => 1,
100 'contact_id' => $loggedInContactID,
101 'signature_text' => 'This is a test Signature',
102 'signature_html' => '<p>This is a test Signature</p>',
103 ]);
104 $form->_allContactIds = $form->_toContactIds = $form->_contactIds;
105 $form->_emails = [$loggedInEmail['id'] => 'mickey@mouse.com'];
106 $form->_fromEmails = [$loggedInEmail['id'] => 'mickey@mouse.com'];
107 // This rule somehow disappears if there's a form-related test before us,
108 // so register it again. See packages/HTML/QuickForm/file.php.
109 $form->registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
110 $form->isSearchContext = FALSE;
111 $form->buildForm();
112 $form->submit(array_merge($form->_defaultValues, [
113 'from_email_address' => $loggedInEmail['id'],
114 'subject' => 'Really interesting stuff',
115 'bcc_id' => $bcc,
116 'cc_id' => '',
117 ]));
118 $mut->checkMailLog([
119 'This is a test Signature',
120 ]);
121 $mut->stop();
122 $activity = Activity::get(FALSE)->setSelect(['details'])->execute()->first();
123 $bccUrl1 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc1], TRUE);
124 $bccUrl2 = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $bcc2], TRUE);
125 $this->assertStringContainsString("bcc : <a href='" . $bccUrl1 . "'>Mr. Anthony Anderson II</a>, <a href='" . $bccUrl2 . "'>Mr. Anthony Anderson II</a>", $activity['details']);
126 $this->assertEquals([
127 [
128 'text' => '27 messages were sent successfully. ',
129 'title' => 'Messages Sent',
130 'type' => 'success',
131 'options' => NULL,
132 ],
133 [
134 '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&amp;reset=1&amp;cid=' . $deceasedContactID . '" title="dead@example.com">Mr. Anthony Anderson II</a></li></ul>',
135 'title' => 'One Message Not Sent',
136 'type' => 'info',
137 'options' => NULL,
138 ],
139 ], CRM_Core_Session::singleton()->getStatus());
140 }
141
142 }