test tweaks and updated examples
[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,
95 'hash' => 'b15de8b64e2cec34',
96 'time_stamp' => '20101212121212',
97 );
98 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
99 $this->assertEquals($result['values']['contact_id'], $contactID);
100
101 $this->contactDelete($contactID);
102 }
103
104 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
105
106 /**
107 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
108 */
109 public function testMailerGroupUnsubscribeWrongParams() {
110 $params = array(
111 'job_id' => 'Wrong ID',
112 'event_queue_id' => 'Wrong ID',
113 'hash' => 'Wrong Hash', 'time_stamp' => '20101212121212',
114 );
115
116 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
117 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
118 }
119
120 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
121
122 /**
123 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
124 */
125 public function testMailerGroupDomainUnsubscribeWrongParams() {
126 $params = array(
127 'job_id' => 'Wrong ID',
128 'event_queue_id' => 'Wrong ID',
129 'hash' => 'Wrong Hash',
130 'org_unsubscribe' => 1, 'time_stamp' => '20101212121212',
131 );
132
133 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
134 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
135 }
136
137
138 //----------- civicrm_mailing_group_event_resubscribe methods--------
139
140 /**
141 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
142 */
143
144 /**
145 * Test civicrm_mailing_group_event_resubscribe with wrong params.
146 */
147 public function testMailerGroupResubscribeWrongParams() {
148 $params = array(
149 'job_id' => 'Wrong ID',
150 'event_queue_id' => 'Wrong ID',
151 'hash' => 'Wrong Hash',
152 'org_unsubscribe' => 'test', 'time_stamp' => '20101212121212',
153 );
154 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
155 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
156 }
157
158 //------------------------ success case ---------------------
159
160 /**
161 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
162 */
163 public function testMailerProcess() {
164 $params = array(
165 'first_name' => 'Test',
166 'last_name' => 'Test',
167 'email' => $this->_email,
168 'contact_type' => 'Individual', );
169 $contactID = $this->individualCreate($params);
170
171 $params = array(
172 'email' => $this->_email,
173 'group_id' => $this->_groupID,
174 'contact_id' => $contactID, 'hash' => 'b15de8b64e2cec34',
175 'time_stamp' => '20101212121212',
176 );
177 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
178
179 $this->assertAPISuccess($result, 'in line ' . __LINE__);
180 $this->assertEquals($result['values']['contact_id'], $contactID);
181
182 $params = array(
183 'contact_id' => $result['values']['contact_id'],
184 'subscribe_id' => $result['values']['subscribe_id'],
185 'hash' => $result['values']['hash'], 'time_stamp' => '20101212121212',
186 'event_subscribe_id' => $result['values']['subscribe_id'],
187 );
188
189
190 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
191
192 $this->assertAPISuccess($result, 'in line ' . __LINE__);
193 $this->contactDelete($contactID);
194 }
195 }
196