Merge pull request #1216 from davecivicrm/CRM-13062
[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 public $_eNoticeCompliant = FALSE;
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', 'time_stamp' => '20111111010101',
69 'hash' => 'sasa',
70 );
71 $result = $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
72 if ($result['error_message'] != 'Subscription failed') {
73 $this->assertEquals($result['error_message'], 'Invalid Group id', 'In line ' . __LINE__);
74 }
75 else {
76 $this->assertEquals($result['error_message'], 'Subscription failed', 'In line ' . __LINE__);
77 }
78 }
79
80 /**
81 * Test civicrm_mailing_group_event_subscribe with given contact ID.
82 */
83 public function testMailerGroupSubscribeGivenContactId() {
84 $params = array(
85 'first_name' => 'Test',
86 'last_name' => 'Test',
87 'email' => $this->_email,
88 'contact_type' => 'Individual', );
89 $contactID = $this->individualCreate($params);
90
91 $params = array(
92 'email' => $this->_email,
93 'group_id' => $this->_groupID,
94 'contact_id' => $contactID, 'hash' => 'b15de8b64e2cec34',
95 'time_stamp' => '20101212121212',
96 );
97 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
98 $this->assertEquals($result['values']['contact_id'], $contactID);
99
100 $this->contactDelete($contactID);
101 }
102
103 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
104
105 /**
106 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
107 */
108 public function testMailerGroupUnsubscribeWrongParams() {
109 $params = array(
110 'job_id' => 'Wrong ID',
111 'event_queue_id' => 'Wrong ID',
112 'hash' => 'Wrong Hash', 'time_stamp' => '20101212121212',
113 );
114
115 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
116 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
117 }
118
119 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
120
121 /**
122 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
123 */
124 public function testMailerGroupDomainUnsubscribeWrongParams() {
125 $params = array(
126 'job_id' => 'Wrong ID',
127 'event_queue_id' => 'Wrong ID',
128 'hash' => 'Wrong Hash',
129 'org_unsubscribe' => 1, 'time_stamp' => '20101212121212',
130 );
131
132 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
133 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
134 }
135
136
137 //----------- civicrm_mailing_group_event_resubscribe methods--------
138
139 /**
140 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
141 */
142
143 /**
144 * Test civicrm_mailing_group_event_resubscribe with wrong params.
145 */
146 public function testMailerGroupResubscribeWrongParams() {
147 $params = array(
148 'job_id' => 'Wrong ID',
149 'event_queue_id' => 'Wrong ID',
150 'hash' => 'Wrong Hash',
151 'org_unsubscribe' => 'test', 'time_stamp' => '20101212121212',
152 );
153 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
154 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
155 }
156
157 //------------------------ success case ---------------------
158
159 /**
160 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
161 */
162 public function testMailerProcess() {
163 $params = array(
164 'first_name' => 'Test',
165 'last_name' => 'Test',
166 'email' => $this->_email,
167 'contact_type' => 'Individual', );
168 $contactID = $this->individualCreate($params);
169
170 $params = array(
171 'email' => $this->_email,
172 'group_id' => $this->_groupID,
173 'contact_id' => $contactID, 'hash' => 'b15de8b64e2cec34',
174 'time_stamp' => '20101212121212',
175 );
176 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
177
178 $this->assertAPISuccess($result, 'in line ' . __LINE__);
179 $this->assertEquals($result['values']['contact_id'], $contactID);
180
181 $params = array(
182 'contact_id' => $result['values']['contact_id'],
183 'subscribe_id' => $result['values']['subscribe_id'],
184 'hash' => $result['values']['hash'], 'time_stamp' => '20101212121212',
185 'event_subscribe_id' => $result['values']['subscribe_id'],
186 );
187
188
189 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
190
191 $this->assertAPISuccess($result, 'in line ' . __LINE__);
192 $this->contactDelete($contactID);
193 }
194 }
195