Merge pull request #4627 from colemanw/docblocks
[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';
aba1cd8b
EM
4
5/**
6 * Class CRM_Core_BAO_EmailTest
7 */
6a488035 8class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
aba1cd8b
EM
9 /**
10 * @return array
11 */
6a488035
TO
12 function get_info() {
13 return array(
14 'name' => 'Email BAOs',
15 'description' => 'Test all Core_BAO_Email methods.',
16 'group' => 'CiviCRM BAO Tests',
17 );
18 }
19
20 function setUp() {
21 parent::setUp();
22
23 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_email' ) );
24 }
25
26 /**
27 * add() method (create and update modes)
28 */
29 function testAdd() {
30 $contactId = Contact::createIndividual();
31
32 $params = array();
33 $params = array(
34 'email' => 'jane.doe@example.com',
35 'is_primary' => 1,
36 'location_type_id' => 1,
37 'contact_id' => $contactId,
38 );
39
40 CRM_Core_BAO_Email::add($params);
41
42 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
43 'Database check for created email address.'
44 );
45
46 // Now call add() to modify an existing email address
47
48 $params = array();
49 $params = array(
50 'id' => $emailId,
51 'contact_id' => $contactId,
52 'is_bulkmail' => 1,
53 'on_hold' => 1,
54 );
55
56 CRM_Core_BAO_Email::add($params);
57
58 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
59 'Database check on updated email record.'
60 );
61 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
62
63 Contact::delete($contactId);
64 }
65
66 /**
67 * holdEmail() method (set and reset on_hold condition)
68 */
69 function testHoldEmail() {
70 $contactId = Contact::createIndividual();
71
72 $params = array();
73 $params = array(
74 'email' => 'jane.doe@example.com',
75 'is_primary' => 1,
76 'location_type_id' => 1,
77 'contact_id' => $contactId,
78 );
79
80 CRM_Core_BAO_Email::add($params);
81
82 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
83 'Database check for created email address.'
84 );
85
86 // Now call add() to update on_hold=true and check record state
87 $params = array();
88 $params = array(
89 'id' => $emailId,
90 'contact_id' => $contactId,
91 'on_hold' => 1,
92 );
93
94 CRM_Core_BAO_Email::add($params);
95
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.'
100 );
101
102 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
103 'Compare hold_date (' . $holdDate . ') in DB to current year.'
104 );
105
106 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
107 'Check if on_hold=1 in updated email record.'
108 );
109
110 // Now call add() with on_hold=false and verify that reset_date is set.
111 $params = array();
112 $params = array(
113 'id' => $emailId,
114 'contact_id' => $contactId,
115 'on_hold' => 'null',
116 );
117
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.'
121 );
122 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
123 'Check if hold_date has been set to empty string.'
124 );
125
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.'
130 );
131
132 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
133 'Compare reset_date (' . $resetDate . ') in DB to current year.'
134 );
135
136 Contact::delete($contactId);
137 }
138
139 /**
140 * allEmails() method - get all emails for our contact, with primary email first
141 */
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',
149 );
150
151 $contactId = Contact::createIndividual($contactParams);
152
153 $emails = CRM_Core_BAO_Email::allEmails($contactId);
154
155 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
156
157 $firstEmailValue = array_slice($emails, 0, 1);
158
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.');
161
162 Contact::delete($contactId);
163 }
164}
165