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