2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
6 * Class CRM_Core_BAO_EmailTest
8 class CRM_Core_BAO_EmailTest
extends CiviUnitTestCase
{
14 'name' => 'Email BAOs',
15 'description' => 'Test all Core_BAO_Email methods.',
16 'group' => 'CiviCRM BAO Tests',
23 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_email' ) );
27 * add() method (create and update modes)
30 $contactId = Contact
::createIndividual();
34 'email' => 'jane.doe@example.com',
36 'location_type_id' => 1,
37 'contact_id' => $contactId,
40 CRM_Core_BAO_Email
::add($params);
42 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
43 'Database check for created email address.'
46 // Now call add() to modify an existing email address
51 'contact_id' => $contactId,
56 CRM_Core_BAO_Email
::add($params);
58 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
59 'Database check on updated email record.'
61 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
63 Contact
::delete($contactId);
67 * holdEmail() method (set and reset on_hold condition)
69 function testHoldEmail() {
70 $contactId = Contact
::createIndividual();
74 'email' => 'jane.doe@example.com',
76 'location_type_id' => 1,
77 'contact_id' => $contactId,
80 CRM_Core_BAO_Email
::add($params);
82 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
83 'Database check for created email address.'
86 // Now call add() to update on_hold=true and check record state
90 'contact_id' => $contactId,
94 CRM_Core_BAO_Email
::add($params);
96 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
97 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
98 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
99 'Retrieve hold_date from the updated email record.'
102 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
103 'Compare hold_date (' . $holdDate . ') in DB to current year.'
106 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
107 'Check if on_hold=1 in updated email record.'
110 // Now call add() with on_hold=false and verify that reset_date is set.
114 'contact_id' => $contactId,
118 CRM_Core_BAO_Email
::add($params);
119 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
120 'Check if on_hold=0 in updated email record.'
122 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
123 'Check if hold_date has been set to empty string.'
126 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
127 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
128 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
129 'Retrieve reset_date from the updated email record.'
132 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
133 'Compare reset_date (' . $resetDate . ') in DB to current year.'
136 Contact
::delete($contactId);
140 * allEmails() method - get all emails for our contact, with primary email first
142 function testAllEmails() {
143 $contactParams = array(
144 'first_name' => 'Alan',
145 'last_name' => 'Smith',
146 'email-1' => 'alan.smith1@example.com',
147 'email-2' => 'alan.smith2@example.com',
148 'email-3' => 'alan.smith3@example.com',
151 $contactId = Contact
::createIndividual($contactParams);
153 $emails = CRM_Core_BAO_Email
::allEmails($contactId);
155 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
157 $firstEmailValue = array_slice($emails, 0, 1);
159 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
160 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
162 Contact
::delete($contactId);