fix version in header
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 /**
42 * @return array
43 */
44 function get_info() {
45 return array(
46 'name' => 'Mailer Group',
47 'description' => 'Test all Mailer Group methods.',
48 'group' => 'CiviCRM API Tests',
49 );
50 }
51
52 function setUp() {
53 parent::setUp();
54 $this->_apiversion = 3;
55 $this->_groupID = $this->groupCreate();
56 $this->_email = 'test@test.test';
57 }
58
59 function tearDown() {
60 $this->groupDelete($this->_groupID);
61 }
62
63 //---------- civicrm_mailing_event_subscribe methods ---------
64
65 /**
66 * Test civicrm_mailing_group_event_subscribe with wrong params.
67 */
68 public function testMailerGroupSubscribeWrongParams() {
69 $params = array(
70 'email' => $this->_email,
71 'group_id' => 'Wrong Group ID',
72 'contact_id' => '2121',
73 'time_stamp' => '20111111010101',
74 'hash' => 'sasa',
75 );
76 $result = $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
77 if ($result['error_message'] != 'Subscription failed') {
78 $this->assertEquals($result['error_message'], 'Invalid Group id', 'In line ' . __LINE__);
79 }
80 else {
81 $this->assertEquals($result['error_message'], 'Subscription failed', 'In line ' . __LINE__);
82 }
83 }
84
85 /**
86 * Test civicrm_mailing_group_event_subscribe with given contact ID.
87 */
88 public function testMailerGroupSubscribeGivenContactId() {
89 $params = array(
90 'first_name' => 'Test',
91 'last_name' => 'Test',
92 'email' => $this->_email,
93 'contact_type' => 'Individual',
94 );
95 $contactID = $this->individualCreate($params);
96
97 $params = array(
98 'email' => $this->_email,
99 'group_id' => $this->_groupID,
100 'contact_id' => $contactID,
101 'hash' => 'b15de8b64e2cec34',
102 'time_stamp' => '20101212121212',
103 );
104 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
105 $this->assertEquals($result['values'][$result['id']]['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 'time_stamp' => '20101212121212',
121 );
122
123 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
124 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
125 }
126
127 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
128
129 /**
130 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
131 */
132 public function testMailerGroupDomainUnsubscribeWrongParams() {
133 $params = array(
134 'job_id' => 'Wrong ID',
135 'event_queue_id' => 'Wrong ID',
136 'hash' => 'Wrong Hash',
137 'org_unsubscribe' => 1,
138 'time_stamp' => '20101212121212',
139 );
140
141 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
142 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
143 }
144
145 //----------- civicrm_mailing_group_event_resubscribe methods--------
146
147 /**
148 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
149 */
150
151 /**
152 * Test civicrm_mailing_group_event_resubscribe with wrong params.
153 */
154 public function testMailerGroupResubscribeWrongParams() {
155 $params = array(
156 'job_id' => 'Wrong ID',
157 'event_queue_id' => 'Wrong ID',
158 'hash' => 'Wrong Hash',
159 'org_unsubscribe' => 'test',
160 'time_stamp' => '20101212121212',
161 );
162 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
163 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
164 }
165
166 //------------------------ success case ---------------------
167
168 /**
169 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
170 */
171 public function testMailerProcess() {
172 $params = array(
173 'first_name' => 'Test',
174 'last_name' => 'Test',
175 'email' => $this->_email,
176 'contact_type' => 'Individual',
177 );
178 $contactID = $this->individualCreate($params);
179
180 $params = array(
181 'email' => $this->_email,
182 'group_id' => $this->_groupID,
183 'contact_id' => $contactID,
184 'hash' => 'b15de8b64e2cec34',
185 'time_stamp' => '20101212121212',
186 );
187 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
188
189 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
190
191 $params = array(
192 'contact_id' => $result['values'][$result['id']]['contact_id'],
193 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
194 'hash' => $result['values'][$result['id']]['hash'],
195 'time_stamp' => '20101212121212',
196 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
197 );
198
199 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
200 $this->contactDelete($contactID);
201 }
202 }
203