Merge pull request #7717 from yashodha/CRM-17908
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / EmailTest.php
1 <?php
2
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Core_BAO_EmailTest
7 */
8 class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
9 public function setUp() {
10 parent::setUp();
11
12 $this->quickCleanup(array('civicrm_contact', 'civicrm_email'));
13 }
14
15 /**
16 * Add() method (create and update modes)
17 */
18 public function testAdd() {
19 $contactId = Contact::createIndividual();
20
21 $params = array();
22 $params = array(
23 'email' => 'jane.doe@example.com',
24 'is_primary' => 1,
25 'location_type_id' => 1,
26 'contact_id' => $contactId,
27 );
28
29 CRM_Core_BAO_Email::add($params);
30
31 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
32 'Database check for created email address.'
33 );
34
35 // Now call add() to modify an existing email address
36
37 $params = array();
38 $params = array(
39 'id' => $emailId,
40 'contact_id' => $contactId,
41 'is_bulkmail' => 1,
42 'on_hold' => 1,
43 );
44
45 CRM_Core_BAO_Email::add($params);
46
47 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
48 'Database check on updated email record.'
49 );
50 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
51
52 Contact::delete($contactId);
53 }
54
55 /**
56 * HoldEmail() method (set and reset on_hold condition)
57 */
58 public function testHoldEmail() {
59 $contactId = Contact::createIndividual();
60
61 $params = array();
62 $params = array(
63 'email' => 'jane.doe@example.com',
64 'is_primary' => 1,
65 'location_type_id' => 1,
66 'contact_id' => $contactId,
67 );
68
69 CRM_Core_BAO_Email::add($params);
70
71 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
72 'Database check for created email address.'
73 );
74
75 // Now call add() to update on_hold=true and check record state
76 $params = array();
77 $params = array(
78 'id' => $emailId,
79 'contact_id' => $contactId,
80 'on_hold' => 1,
81 );
82
83 CRM_Core_BAO_Email::add($params);
84
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.'
89 );
90
91 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
92 'Compare hold_date (' . $holdDate . ') in DB to current year.'
93 );
94
95 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
96 'Check if on_hold=1 in updated email record.'
97 );
98
99 // Now call add() with on_hold=false and verify that reset_date is set.
100 $params = array();
101 $params = array(
102 'id' => $emailId,
103 'contact_id' => $contactId,
104 'on_hold' => 'null',
105 );
106
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.'
110 );
111 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
112 'Check if hold_date has been set to empty string.'
113 );
114
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.'
119 );
120
121 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
122 'Compare reset_date (' . $resetDate . ') in DB to current year.'
123 );
124
125 Contact::delete($contactId);
126 }
127
128 /**
129 * AllEmails() method - get all emails for our contact, with primary email first
130 */
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',
138 );
139
140 $contactId = Contact::createIndividual($contactParams);
141
142 $emails = CRM_Core_BAO_Email::allEmails($contactId);
143
144 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
145
146 $firstEmailValue = array_slice($emails, 0, 1);
147
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.');
150
151 Contact::delete($contactId);
152 }
153
154 }