4 * Class CRM_Utils_Mail_EmailProcessorInboundTest
7 class CRM_Utils_Mail_EmailProcessorInboundTest
extends CiviUnitTestCase
{
10 * MailSettings record id.
14 protected $mailSettingsId;
16 public function 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', [
24 'protocol' => 'Localdir',
25 'source' => __DIR__
. '/data/mail',
26 'domain' => 'example.com',
27 // a little weird - is_default=0 means for inbound email processing
33 public function tearDown() {
34 CRM_Utils_File
::cleanDir(__DIR__
. '/data/mail');
35 $this->callAPISuccess('MailSettings', 'delete', [
36 'id' => $this->mailSettingsId
,
42 * Fetch activities with many attachments
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).
48 public function testFetchActivitiesWithManyAttachments() {
49 $mail = 'test_message_many_attachments.eml';
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);
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
);
61 // create some contacts
62 $senderContactId = $this->individualCreate([], 1);
63 $senderContact = $this->callAPISuccess('Contact', 'getsingle', [
64 'id' => $senderContactId,
66 $recipientContactId = $this->individualCreate([], 2);
67 $recipientContact = $this->callAPISuccess('Contact', 'getsingle', [
68 'id' => $recipientContactId,
72 'the_date' => date('r'),
73 'from_name' => $senderContact['display_name'],
74 'from_email' => $senderContact['email'],
75 'to_email' => $recipientContact['email'],
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);
83 // put it in the mail dir
84 file_put_contents(__DIR__
. '/data/mail/' . $mail, $file_contents);
87 $this->callAPISuccess('job', 'fetch_activities', []);
89 // check that file was removed from mail dir
90 $this->assertFalse(file_exists(__DIR__
. '/data/mail/' . $mail));
92 // get the filed activity, by sender contact id
93 $activities = $this->callAPISuccess('Activity', 'get', [
94 'source_contact_id' => $senderContact['id'],
96 $this->assertEquals(1, $activities['count']);
99 $activity = $activities['values'][$activities['id']];
100 $this->assertEquals('Testing 4 attachments', $activity['subject']);
102 // Check target is our recipient
103 $targets = $this->callAPISuccess('ActivityContact', 'get', [
104 'activity_id' => $activity['id'],
105 'record_type_id' => 'Activity Targets',
107 $this->assertEquals($recipientContact['id'], $targets['values'][$targets['id']]['contact_id']);
109 // Check we have 4 attachments
110 $attachments = $this->callAPISuccess('Attachment', 'get', [
111 'entity_id' => $activity['id'],
112 'entity_table' => 'civicrm_activity',
114 $this->assertEquals(4, $attachments['count']);
116 // reset in case it was different from defaults
117 Civi
::settings()->set('max_attachments', $currentUIMax);
118 Civi
::settings()->set('max_attachments_backend', $currentBackendMax);