Merge pull request #1031 from deepak-srivastava/hr
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test APIv3 civicrm_mailing_group_* functions
32 *
33 * @package CiviCRM
34 */
35 class api_v3_MailingGroupTest extends CiviUnitTestCase {
36 protected $_groupID;
37 protected $_email;
38 protected $_apiversion;
39
40 function get_info() {
41 return array(
42 'name' => 'Mailer Group',
43 'description' => 'Test all Mailer Group methods.',
44 'group' => 'CiviCRM API Tests',
45 );
46 }
47
48 function setUp() {
49 parent::setUp();
50 $this->_apiversion = 3;
51 $this->_groupID = $this->groupCreate(NULL);
52 $this->_email = 'test@test.test';
53 }
54
55 function tearDown() {
56 $this->groupDelete($this->_groupID);
57 }
58
59 //---------- civicrm_mailing_event_subscribe methods ---------
60
61 /**
62 * Test civicrm_mailing_group_event_subscribe with wrong params.
63 */
64 public function testMailerGroupSubscribeWrongParams() {
65 $params = array(
66 'email' => $this->_email,
67 'group_id' => 'Wrong Group ID',
68 'contact_id' => '2121',
69 'version' => $this->_apiversion,
70 'time_stamp' => '20111111010101',
71 'hash' => 'sasa',
72 );
73 $result = $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
74 if ($result['error_message'] != 'Subscription failed') {
75 $this->assertEquals($result['error_message'], 'Invalid Group id', 'In line ' . __LINE__);
76 }
77 else {
78 $this->assertEquals($result['error_message'], 'Subscription failed', 'In line ' . __LINE__);
79 }
80 }
81
82 /**
83 * Test civicrm_mailing_group_event_subscribe with given contact ID.
84 */
85 public function testMailerGroupSubscribeGivenContactId() {
86 $params = array(
87 'first_name' => 'Test',
88 'last_name' => 'Test',
89 'email' => $this->_email,
90 'contact_type' => 'Individual',
91 'version' => $this->_apiversion,
92 );
93 $contactID = $this->individualCreate($params);
94
95 $params = array(
96 'email' => $this->_email,
97 'group_id' => $this->_groupID,
98 'contact_id' => $contactID,
99 'version' => $this->_apiversion,
100 'hash' => 'b15de8b64e2cec34',
101 'time_stamp' => '20101212121212',
102 );
103 $result = civicrm_api('mailing_event_subscribe', 'create', $params);
104 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
105 $this->assertAPISuccess($result, 'in line ' . __LINE__);
106 $this->assertEquals($result['values']['contact_id'], $contactID);
107
108 $this->contactDelete($contactID);
109 }
110
111 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
112
113 /**
114 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
115 */
116 public function testMailerGroupUnsubscribeWrongParams() {
117 $params = array(
118 'job_id' => 'Wrong ID',
119 'event_queue_id' => 'Wrong ID',
120 'hash' => 'Wrong Hash',
121 'version' => $this->_apiversion,
122 'time_stamp' => '20101212121212',
123 );
124
125 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
126 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
127 }
128
129 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
130
131 /**
132 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
133 */
134 public function testMailerGroupDomainUnsubscribeWrongParams() {
135 $params = array(
136 'job_id' => 'Wrong ID',
137 'event_queue_id' => 'Wrong ID',
138 'hash' => 'Wrong Hash',
139 'org_unsubscribe' => 1,
140 'version' => $this->_apiversion,
141 'time_stamp' => '20101212121212',
142 );
143
144 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
145 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
146 }
147
148
149 //----------- civicrm_mailing_group_event_resubscribe methods--------
150
151 /**
152 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
153 */
154
155 /**
156 * Test civicrm_mailing_group_event_resubscribe with wrong params.
157 */
158 public function testMailerGroupResubscribeWrongParams() {
159 $params = array(
160 'job_id' => 'Wrong ID',
161 'event_queue_id' => 'Wrong ID',
162 'hash' => 'Wrong Hash',
163 'org_unsubscribe' => 'test',
164 'version' => $this->_apiversion,
165 'time_stamp' => '20101212121212',
166 );
167 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
168 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
169 }
170
171 //------------------------ success case ---------------------
172
173 /**
174 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
175 */
176 public function testMailerProcess() {
177 $params = array(
178 'first_name' => 'Test',
179 'last_name' => 'Test',
180 'email' => $this->_email,
181 'contact_type' => 'Individual',
182 'version' => $this->_apiversion,
183 );
184 $contactID = $this->individualCreate($params);
185
186 $params = array(
187 'email' => $this->_email,
188 'group_id' => $this->_groupID,
189 'contact_id' => $contactID,
190 'version' => $this->_apiversion,
191 'hash' => 'b15de8b64e2cec34',
192 'time_stamp' => '20101212121212',
193 );
194 $result = civicrm_api('mailing_event_subscribe', 'create', $params);
195
196 $this->assertAPISuccess($result, 'in line ' . __LINE__);
197 $this->assertEquals($result['values']['contact_id'], $contactID);
198
199 $params = array(
200 'contact_id' => $result['values']['contact_id'],
201 'subscribe_id' => $result['values']['subscribe_id'],
202 'hash' => $result['values']['hash'],
203 'version' => $this->_apiversion,
204 'time_stamp' => '20101212121212',
205 'event_subscribe_id' => $result['values']['subscribe_id'],
206 );
207
208
209 $result = civicrm_api('mailing_event_confirm', 'create', $params);
210
211 $this->assertAPISuccess($result, 'in line ' . __LINE__);
212 $this->contactDelete($contactID);
213 }
214 }
215