Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / EmailTest.php
CommitLineData
6a488035
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3require_once 'CiviTest/Contact.php';
4class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
5 function get_info() {
6 return array(
7 'name' => 'Email BAOs',
8 'description' => 'Test all Core_BAO_Email methods.',
9 'group' => 'CiviCRM BAO Tests',
10 );
11 }
12
13 function setUp() {
14 parent::setUp();
15
16 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_email' ) );
17 }
18
19 /**
20 * add() method (create and update modes)
21 */
22 function testAdd() {
23 $contactId = Contact::createIndividual();
24
25 $params = array();
26 $params = array(
27 'email' => 'jane.doe@example.com',
28 'is_primary' => 1,
29 'location_type_id' => 1,
30 'contact_id' => $contactId,
31 );
32
33 CRM_Core_BAO_Email::add($params);
34
35 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
36 'Database check for created email address.'
37 );
38
39 // Now call add() to modify an existing email address
40
41 $params = array();
42 $params = array(
43 'id' => $emailId,
44 'contact_id' => $contactId,
45 'is_bulkmail' => 1,
46 'on_hold' => 1,
47 );
48
49 CRM_Core_BAO_Email::add($params);
50
51 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
52 'Database check on updated email record.'
53 );
54 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
55
56 Contact::delete($contactId);
57 }
58
59 /**
60 * holdEmail() method (set and reset on_hold condition)
61 */
62 function testHoldEmail() {
63 $contactId = Contact::createIndividual();
64
65 $params = array();
66 $params = array(
67 'email' => 'jane.doe@example.com',
68 'is_primary' => 1,
69 'location_type_id' => 1,
70 'contact_id' => $contactId,
71 );
72
73 CRM_Core_BAO_Email::add($params);
74
75 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
76 'Database check for created email address.'
77 );
78
79 // Now call add() to update on_hold=true and check record state
80 $params = array();
81 $params = array(
82 'id' => $emailId,
83 'contact_id' => $contactId,
84 'on_hold' => 1,
85 );
86
87 CRM_Core_BAO_Email::add($params);
88
89 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
90 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
91 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
92 'Retrieve hold_date from the updated email record.'
93 );
94
95 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
96 'Compare hold_date (' . $holdDate . ') in DB to current year.'
97 );
98
99 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
100 'Check if on_hold=1 in updated email record.'
101 );
102
103 // Now call add() with on_hold=false and verify that reset_date is set.
104 $params = array();
105 $params = array(
106 'id' => $emailId,
107 'contact_id' => $contactId,
108 'on_hold' => 'null',
109 );
110
111 CRM_Core_BAO_Email::add($params);
112 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
113 'Check if on_hold=0 in updated email record.'
114 );
115 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
116 'Check if hold_date has been set to empty string.'
117 );
118
119 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
120 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
121 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
122 'Retrieve reset_date from the updated email record.'
123 );
124
125 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
126 'Compare reset_date (' . $resetDate . ') in DB to current year.'
127 );
128
129 Contact::delete($contactId);
130 }
131
132 /**
133 * allEmails() method - get all emails for our contact, with primary email first
134 */
135 function testAllEmails() {
136 $contactParams = array(
137 'first_name' => 'Alan',
138 'last_name' => 'Smith',
139 'email-1' => 'alan.smith1@example.com',
140 'email-2' => 'alan.smith2@example.com',
141 'email-3' => 'alan.smith3@example.com',
142 );
143
144 $contactId = Contact::createIndividual($contactParams);
145
146 $emails = CRM_Core_BAO_Email::allEmails($contactId);
147
148 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
149
150 $firstEmailValue = array_slice($emails, 0, 1);
151
152 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
153 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
154
155 Contact::delete($contactId);
156 }
157}
158