[Ref] Extend email trait test, process more sanely
[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(): 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'];
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 $loggedInEmail = $this->callAPISuccess('Email', 'create', [
84 'email' => 'mickey@mouse.com',
85 'location_type_id' => 1,
86 'is_primary' => 1,
87 'contact_id' => $loggedInContactID,
88 'signature_text' => 'This is a test Signature',
89 'signature_html' => '<p>This is a test Signature</p>',
90 ]);
91
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;
98 }
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),
104 ]);
105 $form->_contactIds = $form_contactIds;
106 $form->_contactIds[$deceasedContactID] = $deceasedContactID;
107
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;
116 $form->buildForm();
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',
122 'bcc_id' => $bcc,
123 'cc_id' => '',
124 ]));
125 $mut->checkMailLog([
126 'This is a test Signature',
127 ]);
128 $mut->stop();
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([
134 [
135 'text' => '27 messages were sent successfully. ',
136 'title' => 'Messages Sent',
137 'type' => 'success',
138 'options' => NULL,
139 ],
140 [
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&amp;reset=1&amp;cid=' . $deceasedContactID . '" title="dead@example.com">Mr. Anthony Anderson II</a></li></ul>',
142 'title' => 'One Message Not Sent',
143 'type' => 'info',
144 'options' => NULL,
145 ],
146 ], CRM_Core_Session::singleton()->getStatus());
147 }
148
149 }