Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-30-12-58-23
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
41 function get_info() {
42 return array(
43 'name' => 'Mailer Group',
44 'description' => 'Test all Mailer Group methods.',
45 'group' => 'CiviCRM API Tests',
46 );
47 }
48
49 function setUp() {
50 parent::setUp();
51 $this->_apiversion = 3;
52 $this->_groupID = $this->groupCreate();
53 $this->_email = 'test@test.test';
54 }
55
56 function tearDown() {
57 $this->groupDelete($this->_groupID);
58 }
59
60 //---------- civicrm_mailing_event_subscribe methods ---------
61
62 /**
63 * Test civicrm_mailing_group_event_subscribe with wrong params.
64 */
65 public function testMailerGroupSubscribeWrongParams() {
66 $params = array(
67 'email' => $this->_email,
68 'group_id' => 'Wrong Group ID',
69 'contact_id' => '2121',
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 );
92 $contactID = $this->individualCreate($params);
93
94 $params = array(
95 'email' => $this->_email,
96 'group_id' => $this->_groupID,
97 'contact_id' => $contactID,
98 'hash' => 'b15de8b64e2cec34',
99 'time_stamp' => '20101212121212',
100 );
101 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
102 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
103
104 $this->contactDelete($contactID);
105 }
106
107 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
108
109 /**
110 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
111 */
112 public function testMailerGroupUnsubscribeWrongParams() {
113 $params = array(
114 'job_id' => 'Wrong ID',
115 'event_queue_id' => 'Wrong ID',
116 'hash' => 'Wrong Hash',
117 'time_stamp' => '20101212121212',
118 );
119
120 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
121 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
122 }
123
124 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
125
126 /**
127 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
128 */
129 public function testMailerGroupDomainUnsubscribeWrongParams() {
130 $params = array(
131 'job_id' => 'Wrong ID',
132 'event_queue_id' => 'Wrong ID',
133 'hash' => 'Wrong Hash',
134 'org_unsubscribe' => 1,
135 'time_stamp' => '20101212121212',
136 );
137
138 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
139 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
140 }
141
142 //----------- civicrm_mailing_group_event_resubscribe methods--------
143
144 /**
145 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
146 */
147
148 /**
149 * Test civicrm_mailing_group_event_resubscribe with wrong params.
150 */
151 public function testMailerGroupResubscribeWrongParams() {
152 $params = array(
153 'job_id' => 'Wrong ID',
154 'event_queue_id' => 'Wrong ID',
155 'hash' => 'Wrong Hash',
156 'org_unsubscribe' => 'test',
157 'time_stamp' => '20101212121212',
158 );
159 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
160 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
161 }
162
163 //------------------------ success case ---------------------
164
165 /**
166 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
167 */
168 public function testMailerProcess() {
169 $params = array(
170 'first_name' => 'Test',
171 'last_name' => 'Test',
172 'email' => $this->_email,
173 'contact_type' => 'Individual',
174 );
175 $contactID = $this->individualCreate($params);
176
177 $params = array(
178 'email' => $this->_email,
179 'group_id' => $this->_groupID,
180 'contact_id' => $contactID,
181 'hash' => 'b15de8b64e2cec34',
182 'time_stamp' => '20101212121212',
183 );
184 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
185
186 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
187
188 $params = array(
189 'contact_id' => $result['values'][$result['id']]['contact_id'],
190 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
191 'hash' => $result['values'][$result['id']]['hash'],
192 'time_stamp' => '20101212121212',
193 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
194 );
195
196 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
197 $this->contactDelete($contactID);
198 }
199 }
200