Merge pull request #4796 from jitendrapurohit/webtest4.6
[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 function setUp() {
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 $this->_apiversion = 3;
44 $this->_groupID = $this->groupCreate();
45 $this->_email = 'test@test.test';
46 }
47
48 //---------- civicrm_mailing_event_subscribe methods ---------
49
50 /**
51 * Test civicrm_mailing_group_event_subscribe with wrong params.
52 */
53 public function testMailerGroupSubscribeWrongParams() {
54 $params = array(
55 'email' => $this->_email,
56 'group_id' => 'Wrong Group ID',
57 'contact_id' => '2121',
58 'time_stamp' => '20111111010101',
59 'hash' => 'sasa',
60 );
61 $result = $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
62 if ($result['error_message'] != 'Subscription failed') {
63 $this->assertEquals($result['error_message'], 'Invalid Group id', 'In line ' . __LINE__);
64 }
65 else {
66 $this->assertEquals($result['error_message'], 'Subscription failed', 'In line ' . __LINE__);
67 }
68 }
69
70 /**
71 * Test civicrm_mailing_group_event_subscribe with given contact ID.
72 */
73 public function testMailerGroupSubscribeGivenContactId() {
74 $params = array(
75 'first_name' => 'Test',
76 'last_name' => 'Test',
77 'email' => $this->_email,
78 'contact_type' => 'Individual',
79 );
80 $contactID = $this->individualCreate($params);
81
82 $params = array(
83 'email' => $this->_email,
84 'group_id' => $this->_groupID,
85 'contact_id' => $contactID,
86 'hash' => 'b15de8b64e2cec34',
87 'time_stamp' => '20101212121212',
88 );
89 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
90 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
91
92 $this->contactDelete($contactID);
93 }
94
95 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
96
97 /**
98 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
99 */
100 public function testMailerGroupUnsubscribeWrongParams() {
101 $params = array(
102 'job_id' => 'Wrong ID',
103 'event_queue_id' => 'Wrong ID',
104 'hash' => 'Wrong Hash',
105 'time_stamp' => '20101212121212',
106 );
107
108 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
109 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
110 }
111
112 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
113
114 /**
115 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
116 */
117 public function testMailerGroupDomainUnsubscribeWrongParams() {
118 $params = array(
119 'job_id' => 'Wrong ID',
120 'event_queue_id' => 'Wrong ID',
121 'hash' => 'Wrong Hash',
122 'org_unsubscribe' => 1,
123 'time_stamp' => '20101212121212',
124 );
125
126 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
127 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
128 }
129
130 //----------- civicrm_mailing_group_event_resubscribe methods--------
131
132 /**
133 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
134 */
135
136 /**
137 * Test civicrm_mailing_group_event_resubscribe with wrong params.
138 */
139 public function testMailerGroupResubscribeWrongParams() {
140 $params = array(
141 'job_id' => 'Wrong ID',
142 'event_queue_id' => 'Wrong ID',
143 'hash' => 'Wrong Hash',
144 'org_unsubscribe' => 'test',
145 'time_stamp' => '20101212121212',
146 );
147 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
148 $this->assertEquals($result['error_message'], 'Queue event could not be found', 'In line ' . __LINE__);
149 }
150
151 //------------------------ success case ---------------------
152
153 /**
154 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
155 */
156 public function testMailerProcess() {
157 $params = array(
158 'first_name' => 'Test',
159 'last_name' => 'Test',
160 'email' => $this->_email,
161 'contact_type' => 'Individual',
162 );
163 $contactID = $this->individualCreate($params);
164
165 $params = array(
166 'email' => $this->_email,
167 'group_id' => $this->_groupID,
168 'contact_id' => $contactID,
169 'hash' => 'b15de8b64e2cec34',
170 'time_stamp' => '20101212121212',
171 );
172 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
173
174 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
175
176 $params = array(
177 'contact_id' => $result['values'][$result['id']]['contact_id'],
178 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
179 'hash' => $result['values'][$result['id']]['hash'],
180 'time_stamp' => '20101212121212',
181 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
182 );
183
184 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
185 $this->contactDelete($contactID);
186 }
187 }
188