Merge pull request #11368 from JMAConsulting/CRM-21499
[civicrm-core.git] / tests / phpunit / CRM / Utils / Mail / EmailProcessorTest.php
CommitLineData
cdc5c450 1<?php
2
3/**
4 * Class CRM_Utils_EmailProcessorTest
5 * @group headless
6 */
7
8class CRM_Utils_EmailProcessorTest extends CiviUnitTestCase {
9
10 /**
11 * Event queue record.
12 *
13 * @var array
14 */
15 protected $eventQueue = array();
16
17 /**
18 * ID of our sample contact.
19 *
20 * @var int
21 */
22 protected $contactID;
23
24 public function setUp() {
25 parent::setUp();
26 $this->callAPISuccess('MailSettings', 'get', array(
27 'api.MailSettings.create' => array(
28 'name' => 'local',
29 'protocol' => 'Localdir',
30 'source' => __DIR__ . '/data/mail',
31 'domain' => 'example.com',
32 ),
33 ));
34 }
35
36 /**
37 * Test the job processing function works and processes a bounce.
38 */
39 public function testBounceProcessing() {
40 $this->setUpMailing();
41
42 copy(__DIR__ . '/data/bounces/bounce_no_verp.txt', __DIR__ . '/data/mail/bounce_no_verp.txt');
43 $this->assertTrue(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
44 $this->callAPISuccess('job', 'fetch_bounces', array());
45 $this->assertFalse(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
46 $this->checkMailingBounces(1);
47 }
48
49 /**
50 * Test that a deleted email does not cause a hard fail.
51 *
52 * The civicrm_mailing_event_queue table tracks email ids to represent an
53 * email address. The id may not represent the same email by the time the bounce may
54 * come in - a weakness of storing the id not the email. Relevant here
55 * is that it might have been deleted altogether, in which case the bounce should be
56 * silently ignored. This ignoring is also at the expense of the contact
57 * having the same email address with a different id.
58 *
59 * Longer term it would make sense to track the email address & track bounces back to that
60 * rather than an id that may not reflect the email used. Issue logged CRM-20021.
61 *
62 * For not however, we are testing absence of mysql error in conjunction with CRM-20016.
63 */
64 public function testBounceProcessingDeletedEmail() {
65 $this->setUpMailing();
66 $this->callAPISuccess('Email', 'get', array(
67 'contact_id' => $this->contactID,
68 'api.email.delete' => 1,
69 ));
70
71 copy(__DIR__ . '/data/bounces/bounce_no_verp.txt', __DIR__ . '/data/mail/bounce_no_verp.txt');
72 $this->assertTrue(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
73 $this->callAPISuccess('job', 'fetch_bounces', array());
74 $this->assertFalse(file_exists(__DIR__ . '/data/mail/bounce_no_verp.txt'));
75 $this->checkMailingBounces(1);
76 }
77
78 /**
79 * Wrapper to check for mailing bounces.
80 *
81 * Normally we would call $this->callAPISuccessGetCount but there is not one & there is resistance to
82 * adding apis for 'convenience' so just adding a hacky function to get past the impasse.
83 *
84 * @param int $expectedCount
85 */
86 public function checkMailingBounces($expectedCount) {
87 $this->assertEquals($expectedCount, CRM_Core_DAO::singleValueQuery(
88 "SELECT count(*) FROM civicrm_mailing_event_bounce WHERE event_queue_id = " . $this->eventQueue['id']
89 ));
90 }
91
92 /**
93 * Set up a mailing.
94 */
95 public function setUpMailing() {
96 $this->contactID = $this->individualCreate(array('email' => 'undeliverable@example.com'));
d15a97f4 97 $groupID = $this->callAPISuccess('Group', 'create', array(
cdc5c450 98 'title' => 'Mailing group',
99 'api.GroupContact.create' => array(
100 'contact_id' => $this->contactID,
d15a97f4 101 ),
102 ));
cdc5c450 103 $this->createMailing(array('scheduled_date' => 'now', 'groups' => array('include' => array($groupID))));
104 $this->callAPISuccess('job', 'process_mailing', array());
105 $this->eventQueue = $this->callAPISuccess('MailingEventQueue', 'get', array('api.MailingEventQueue.create' => array('hash' => 'aaaaaaaaaaaaaaaa')));
106 }
107
108}