4 * Class CRM_Utils_EmailProcessorTest
8 class CRM_Utils_EmailProcessorTest
extends CiviUnitTestCase
{
15 protected $eventQueue = array();
18 * ID of our sample contact.
24 public function setUp() {
26 $this->callAPISuccess('MailSettings', 'get', array(
27 'api.MailSettings.create' => array(
29 'protocol' => 'Localdir',
30 'source' => __DIR__
. '/data/mail',
31 'domain' => 'example.com',
37 * Test the job processing function works and processes a bounce.
39 public function testBounceProcessing() {
40 $this->setUpMailing();
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);
50 * Test that a deleted email does not cause a hard fail.
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.
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.
62 * For not however, we are testing absence of mysql error in conjunction with CRM-20016.
64 public function testBounceProcessingDeletedEmail() {
65 $this->setUpMailing();
66 $this->callAPISuccess('Email', 'get', array(
67 'contact_id' => $this->contactID
,
68 'api.email.delete' => 1,
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);
79 * Wrapper to check for mailing bounces.
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.
84 * @param int $expectedCount
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']
95 public function setUpMailing() {
96 $this->contactID
= $this->individualCreate(array('email' => 'undeliverable@example.com'));
97 $groupID = $this->callAPISuccess('Group', 'create', array(
98 'title' => 'Mailing group',
99 'api.GroupContact.create' => array(
100 'contact_id' => $this->contactID
,
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')));