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
{
9 public function setUp() {
12 $this->quickCleanup(array('civicrm_contact', 'civicrm_email'));
16 * Add() method (create and update modes)
18 public function testAdd() {
19 $contactId = Contact
::createIndividual();
23 'email' => 'jane.doe@example.com',
25 'location_type_id' => 1,
26 'contact_id' => $contactId,
29 CRM_Core_BAO_Email
::add($params);
31 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
32 'Database check for created email address.'
35 // Now call add() to modify an existing email address
40 'contact_id' => $contactId,
45 CRM_Core_BAO_Email
::add($params);
47 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
48 'Database check on updated email record.'
50 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
52 Contact
::delete($contactId);
56 * HoldEmail() method (set and reset on_hold condition)
58 public function testHoldEmail() {
59 $contactId = Contact
::createIndividual();
63 'email' => 'jane.doe@example.com',
65 'location_type_id' => 1,
66 'contact_id' => $contactId,
69 CRM_Core_BAO_Email
::add($params);
71 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
72 'Database check for created email address.'
75 // Now call add() to update on_hold=true and check record state
79 'contact_id' => $contactId,
83 CRM_Core_BAO_Email
::add($params);
85 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
86 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
87 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
88 'Retrieve hold_date from the updated email record.'
91 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
92 'Compare hold_date (' . $holdDate . ') in DB to current year.'
95 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
96 'Check if on_hold=1 in updated email record.'
99 // Now call add() with on_hold=false and verify that reset_date is set.
103 'contact_id' => $contactId,
107 CRM_Core_BAO_Email
::add($params);
108 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
109 'Check if on_hold=0 in updated email record.'
111 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
112 'Check if hold_date has been set to empty string.'
115 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
116 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
117 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
118 'Retrieve reset_date from the updated email record.'
121 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
122 'Compare reset_date (' . $resetDate . ') in DB to current year.'
125 Contact
::delete($contactId);
129 * AllEmails() method - get all emails for our contact, with primary email first
131 public function testAllEmails() {
132 $contactParams = array(
133 'first_name' => 'Alan',
134 'last_name' => 'Smith',
135 'email-1' => 'alan.smith1@example.com',
136 'email-2' => 'alan.smith2@example.com',
137 'email-3' => 'alan.smith3@example.com',
140 $contactId = Contact
::createIndividual($contactParams);
142 $emails = CRM_Core_BAO_Email
::allEmails($contactId);
144 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
146 $firstEmailValue = array_slice($emails, 0, 1);
148 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
149 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
151 Contact
::delete($contactId);