Merge pull request #4909 from pratikshad/broken-webtest
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
39de6fd5 4| CiviCRM version 4.6 |
b6708aeb 5+--------------------------------------------------------------------+
06a1bc01 6| Copyright CiviCRM LLC (c) 2004-2014 |
b6708aeb 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*/
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Test APIv3 civicrm_mailing_group_* functions
32 *
6c6e6187 33 * @package CiviCRM
6a488035
TO
34 */
35class api_v3_MailingGroupTest extends CiviUnitTestCase {
36 protected $_groupID;
37 protected $_email;
430ae6dd 38 protected $_apiversion;
b7c9bc4c 39
00be9182 40 public function setUp() {
6a488035 41 parent::setUp();
55941eb0 42 $this->useTransaction(TRUE);
6a488035 43 $this->_apiversion = 3;
fadb804f 44 $this->_groupID = $this->groupCreate();
ef170fe0 45 $this->_email = 'test@test.test';
6a488035
TO
46 }
47
6a488035
TO
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',
ef170fe0 57 'contact_id' => '2121',
58 'time_stamp' => '20111111010101',
6a488035
TO
59 'hash' => 'sasa',
60 );
d0e1eff2 61 $result = $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
6a488035
TO
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,
ef170fe0 78 'contact_type' => 'Individual',
79 );
6a488035
TO
80 $contactID = $this->individualCreate($params);
81
82 $params = array(
83 'email' => $this->_email,
84 'group_id' => $this->_groupID,
9f1b81e0 85 'contact_id' => $contactID,
86 'hash' => 'b15de8b64e2cec34',
6a488035
TO
87 'time_stamp' => '20101212121212',
88 );
7f0eb6cb 89 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
9e23eadb 90 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
6a488035
TO
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',
ef170fe0 104 'hash' => 'Wrong Hash',
105 'time_stamp' => '20101212121212',
6a488035
TO
106 );
107
d0e1eff2 108 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
6a488035
TO
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',
ef170fe0 122 'org_unsubscribe' => 1,
123 'time_stamp' => '20101212121212',
6a488035
TO
124 );
125
d0e1eff2 126 $result = $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
6a488035
TO
127 $this->assertEquals($result['error_message'], 'Domain Queue event could not be found', 'In line ' . __LINE__);
128 }
129
6a488035
TO
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',
ef170fe0 144 'org_unsubscribe' => 'test',
145 'time_stamp' => '20101212121212',
6a488035 146 );
d0e1eff2 147 $result = $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
6a488035
TO
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,
ef170fe0 161 'contact_type' => 'Individual',
162 );
6a488035
TO
163 $contactID = $this->individualCreate($params);
164
165 $params = array(
166 'email' => $this->_email,
167 'group_id' => $this->_groupID,
ef170fe0 168 'contact_id' => $contactID,
169 'hash' => 'b15de8b64e2cec34',
6a488035
TO
170 'time_stamp' => '20101212121212',
171 );
7f0eb6cb 172 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
6a488035 173
9e23eadb 174 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
6a488035
TO
175
176 $params = array(
9e23eadb 177 'contact_id' => $result['values'][$result['id']]['contact_id'],
178 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
ef170fe0 179 'hash' => $result['values'][$result['id']]['hash'],
180 'time_stamp' => '20101212121212',
9e23eadb 181 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
6a488035
TO
182 );
183
7f0eb6cb 184 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
6a488035
TO
185 $this->contactDelete($contactID);
186 }
187}