Merge pull request #19892 from colemanw/searchKitCreatedModified
[civicrm-core.git] / tests / phpunit / CRM / Utils / Mail / EmailProcessorInboundTest.php
CommitLineData
8913e915
D
1<?php
2
3/**
4 * Class CRM_Utils_Mail_EmailProcessorInboundTest
5 * @group headless
6 */
7class CRM_Utils_Mail_EmailProcessorInboundTest extends CiviUnitTestCase {
8
9 /**
10 * MailSettings record id.
11 *
12 * @var int
13 */
14 protected $mailSettingsId;
15
16 public function setUp() {
17 parent::setUp();
18 CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
19 mkdir(__DIR__ . '/data/mail');
20 // Note this is configured for Inbound Email Processing (not bounces)
21 // but otherwise is the same as bounces.
22 $this->mailSettingsId = $this->callAPISuccess('MailSettings', 'create', [
23 'name' => 'local',
24 'protocol' => 'Localdir',
25 'source' => __DIR__ . '/data/mail',
26 'domain' => 'example.com',
27 // a little weird - is_default=0 means for inbound email processing
28 'is_default' => '0',
29 'domain_id' => 1,
30 ])['id'];
31 }
32
33 public function tearDown() {
34 CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
35 $this->callAPISuccess('MailSettings', 'delete', [
36 'id' => $this->mailSettingsId,
37 ]);
38 parent::tearDown();
39 }
40
41 /**
42 * Fetch activities with many attachments
43 *
44 * In particular the default limit for the UI is 3, which is how this came up
45 * because it was also being used as a limit for backend processes. So we
46 * test 4, which is bigger than 3 (unless running on a 2-bit CPU).
47 */
48 public function testFetchActivitiesWithManyAttachments() {
49 $mail = 'test_message_many_attachments.eml';
50
51 // paranoid check that settings are the standard defaults
52 $currentUIMax = Civi::settings()->get('max_attachments');
53 $currentBackendMax = Civi::settings()->get('max_attachments_backend');
54 if ($currentUIMax > 3) {
55 Civi::settings()->set('max_attachments', 3);
56 }
57 if ($currentBackendMax < CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND) {
58 Civi::settings()->set('max_attachments_backend', CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND);
59 }
60
61 // create some contacts
62 $senderContactId = $this->individualCreate([], 1);
63 $senderContact = $this->callAPISuccess('Contact', 'getsingle', [
64 'id' => $senderContactId,
65 ]);
66 $recipientContactId = $this->individualCreate([], 2);
67 $recipientContact = $this->callAPISuccess('Contact', 'getsingle', [
68 'id' => $recipientContactId,
69 ]);
70
71 $templateFillData = [
72 'the_date' => date('r'),
73 'from_name' => $senderContact['display_name'],
74 'from_email' => $senderContact['email'],
75 'to_email' => $recipientContact['email'],
76 ];
77
78 // Retrieve the template and insert our data like current dates
79 $file_contents = file_get_contents(__DIR__ . '/data/inbound/' . $mail);
80 foreach ($templateFillData as $field => $value) {
81 $file_contents = str_replace("%%{$field}%%", $value, $file_contents);
82 }
83 // put it in the mail dir
84 file_put_contents(__DIR__ . '/data/mail/' . $mail, $file_contents);
85
86 // run the job
87 $this->callAPISuccess('job', 'fetch_activities', []);
88
89 // check that file was removed from mail dir
90 $this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
91
92 // get the filed activity, by sender contact id
93 $activities = $this->callAPISuccess('Activity', 'get', [
94 'source_contact_id' => $senderContact['id'],
95 ]);
96 $this->assertEquals(1, $activities['count']);
97
98 // check subject
99 $activity = $activities['values'][$activities['id']];
100 $this->assertEquals('Testing 4 attachments', $activity['subject']);
101
102 // Check target is our recipient
103 $targets = $this->callAPISuccess('ActivityContact', 'get', [
104 'activity_id' => $activity['id'],
105 'record_type_id' => 'Activity Targets',
106 ]);
107 $this->assertEquals($recipientContact['id'], $targets['values'][$targets['id']]['contact_id']);
108
109 // Check we have 4 attachments
110 $attachments = $this->callAPISuccess('Attachment', 'get', [
111 'entity_id' => $activity['id'],
112 'entity_table' => 'civicrm_activity',
113 ]);
114 $this->assertEquals(4, $attachments['count']);
115
116 // reset in case it was different from defaults
117 Civi::settings()->set('max_attachments', $currentUIMax);
118 Civi::settings()->set('max_attachments_backend', $currentBackendMax);
119 }
120
121}