Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-21-13-15-18
[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; function get_info() {
39 return array(
40 'name' => 'Mailer Group',
41 'description' => 'Test all Mailer Group methods.',
42 'group' => 'CiviCRM API Tests',
43 );
44 }
45
46 function setUp() {
47 parent::setUp();
48 $this->_apiversion = 3;
49 $this->_groupID = $this->groupCreate(NULL);
50 $this->_email = 'test@test.test';
51 }
52
53 function tearDown() {
54 $this->groupDelete($this->_groupID);
55 }
56
57 //---------- civicrm_mailing_event_subscribe methods ---------
58
59 /**
60 * Test civicrm_mailing_group_event_subscribe with wrong params.
61 */
62 public function testMailerGroupSubscribeWrongParams() {
63 $params = array(
64 'email' => $this->_email,
65 'group_id' => 'Wrong Group ID',
66 'contact_id' => '2121',
67 'version' => $this->_apiversion,
68 'time_stamp' => '20111111010101',
69 'hash' => 'sasa',
70 );
71 $result = civicrm_api('mailing_event_subscribe', 'create', $params);
72 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
73 if ($result['error_message'] != 'Subscription failed') {
74 $this->assertEquals($result['error_message'], 'Invalid Group id', 'In line ' . __LINE__);
75 }
76 else {
77 $this->assertEquals($result['error_message'], 'Subscription failed', 'In line ' . __LINE__);
78 }
79 }
80
81 /**
82 * Test civicrm_mailing_group_event_subscribe with given contact ID.
83 */
84 public function testMailerGroupSubscribeGivenContactId() {
85 $params = array(
86 'first_name' => 'Test',
87 'last_name' => 'Test',
88 'email' => $this->_email,
89 'contact_type' => 'Individual',
90 'version' => $this->_apiversion,
91 );
92 $contactID = $this->individualCreate($params);
93
94 $params = array(
95 'email' => $this->_email,
96 'group_id' => $this->_groupID,
97 'contact_id' => $contactID,
98 'version' => $this->_apiversion,
99 'hash' => 'b15de8b64e2cec34',
100 'time_stamp' => '20101212121212',
101 );
102 $result = civicrm_api('mailing_event_subscribe', 'create', $params);
103 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
104 $this->assertAPISuccess($result, 'in line ' . __LINE__);
105 $this->assertEquals($result['values']['contact_id'], $contactID);
106
107 $this->contactDelete($contactID);
108 }
109
110 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
111
112 /**
113 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
114 */
115 public function testMailerGroupUnsubscribeWrongParams() {
116 $params = array(
117 'job_id' => 'Wrong ID',
118 'event_queue_id' => 'Wrong ID',
119 'hash' => 'Wrong Hash',
120 'version' => $this->_apiversion,
121 'time_stamp' => '20101212121212',
122 );
123
124 $result = civicrm_api('mailing_event_unsubscribe', 'create', $params);
125 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
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 = civicrm_api('mailing_event_unsubscribe', 'create', $params);
145 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
146 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
147 }
148
149
150 //----------- civicrm_mailing_group_event_resubscribe methods--------
151
152 /**
153 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
154 */
155
156 /**
157 * Test civicrm_mailing_group_event_resubscribe with wrong params.
158 */
159 public function testMailerGroupResubscribeWrongParams() {
160 $params = array(
161 'job_id' => 'Wrong ID',
162 'event_queue_id' => 'Wrong ID',
163 'hash' => 'Wrong Hash',
164 'org_unsubscribe' => 'test',
165 'version' => $this->_apiversion,
166 'time_stamp' => '20101212121212',
167 );
168 $result = civicrm_api('mailing_event_resubscribe', 'create', $params);
169 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
170 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
171 }
172
173 //------------------------ success case ---------------------
174
175 /**
176 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
177 */
178 public function testMailerProcess() {
179 $params = array(
180 'first_name' => 'Test',
181 'last_name' => 'Test',
182 'email' => $this->_email,
183 'contact_type' => 'Individual',
184 'version' => $this->_apiversion,
185 );
186 $contactID = $this->individualCreate($params);
187
188 $params = array(
189 'email' => $this->_email,
190 'group_id' => $this->_groupID,
191 'contact_id' => $contactID,
192 'version' => $this->_apiversion,
193 'hash' => 'b15de8b64e2cec34',
194 'time_stamp' => '20101212121212',
195 );
196 $result = civicrm_api('mailing_event_subscribe', 'create', $params);
197
198 $this->assertAPISuccess($result, 'in line ' . __LINE__);
199 $this->assertEquals($result['values']['contact_id'], $contactID);
200
201 $params = array(
202 'contact_id' => $result['values']['contact_id'],
203 'subscribe_id' => $result['values']['subscribe_id'],
204 'hash' => $result['values']['hash'],
205 'version' => $this->_apiversion,
206 'time_stamp' => '20101212121212',
207 'event_subscribe_id' => $result['values']['subscribe_id'],
208 );
209
210
211 $result = civicrm_api('mailing_event_confirm', 'create', $params);
212
213 $this->assertAPISuccess($result, 'in line ' . __LINE__);
214 $this->contactDelete($contactID);
215 }
216 }
217