Test tearDown fixes
[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
0b49aa04 16 public function setUp(): void {
8913e915
D
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
dd09ee0c 33 public function tearDown(): void {
8913e915
D
34 CRM_Utils_File::cleanDir(__DIR__ . '/data/mail');
35 $this->callAPISuccess('MailSettings', 'delete', [
36 'id' => $this->mailSettingsId,
37 ]);
d706c149 38 $this->quickCleanup(['civicrm_file', 'civicrm_entity_file']);
8913e915
D
39 parent::tearDown();
40 }
41
42 /**
43 * Fetch activities with many attachments
44 *
45 * In particular the default limit for the UI is 3, which is how this came up
46 * because it was also being used as a limit for backend processes. So we
47 * test 4, which is bigger than 3 (unless running on a 2-bit CPU).
48 */
49 public function testFetchActivitiesWithManyAttachments() {
50 $mail = 'test_message_many_attachments.eml';
51
52 // paranoid check that settings are the standard defaults
53 $currentUIMax = Civi::settings()->get('max_attachments');
54 $currentBackendMax = Civi::settings()->get('max_attachments_backend');
55 if ($currentUIMax > 3) {
56 Civi::settings()->set('max_attachments', 3);
57 }
58 if ($currentBackendMax < CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND) {
59 Civi::settings()->set('max_attachments_backend', CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND);
60 }
61
62 // create some contacts
63 $senderContactId = $this->individualCreate([], 1);
64 $senderContact = $this->callAPISuccess('Contact', 'getsingle', [
65 'id' => $senderContactId,
66 ]);
67 $recipientContactId = $this->individualCreate([], 2);
68 $recipientContact = $this->callAPISuccess('Contact', 'getsingle', [
69 'id' => $recipientContactId,
70 ]);
71
72 $templateFillData = [
73 'the_date' => date('r'),
74 'from_name' => $senderContact['display_name'],
75 'from_email' => $senderContact['email'],
76 'to_email' => $recipientContact['email'],
77 ];
78
79 // Retrieve the template and insert our data like current dates
80 $file_contents = file_get_contents(__DIR__ . '/data/inbound/' . $mail);
81 foreach ($templateFillData as $field => $value) {
82 $file_contents = str_replace("%%{$field}%%", $value, $file_contents);
83 }
84 // put it in the mail dir
85 file_put_contents(__DIR__ . '/data/mail/' . $mail, $file_contents);
86
87 // run the job
88 $this->callAPISuccess('job', 'fetch_activities', []);
89
90 // check that file was removed from mail dir
91 $this->assertFalse(file_exists(__DIR__ . '/data/mail/' . $mail));
92
93 // get the filed activity, by sender contact id
94 $activities = $this->callAPISuccess('Activity', 'get', [
95 'source_contact_id' => $senderContact['id'],
96 ]);
97 $this->assertEquals(1, $activities['count']);
98
99 // check subject
100 $activity = $activities['values'][$activities['id']];
101 $this->assertEquals('Testing 4 attachments', $activity['subject']);
102
103 // Check target is our recipient
104 $targets = $this->callAPISuccess('ActivityContact', 'get', [
105 'activity_id' => $activity['id'],
106 'record_type_id' => 'Activity Targets',
107 ]);
108 $this->assertEquals($recipientContact['id'], $targets['values'][$targets['id']]['contact_id']);
109
110 // Check we have 4 attachments
111 $attachments = $this->callAPISuccess('Attachment', 'get', [
112 'entity_id' => $activity['id'],
113 'entity_table' => 'civicrm_activity',
114 ]);
115 $this->assertEquals(4, $attachments['count']);
116
117 // reset in case it was different from defaults
118 Civi::settings()->set('max_attachments', $currentUIMax);
119 Civi::settings()->set('max_attachments_backend', $currentBackendMax);
120 }
121
122}