Merge pull request #6289 from yashodha/CRM-16879
[civicrm-core.git] / tests / phpunit / api / v3 / MailingGroupTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
39de6fd5 4| CiviCRM version 4.6 |
b6708aeb 5+--------------------------------------------------------------------+
e7112fa7 6| Copyright CiviCRM LLC (c) 2004-2015 |
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+--------------------------------------------------------------------+
e70a7fc0 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 );
7bd9cc5f 61 $this->callAPIFailure('mailing_event_subscribe', 'create', $params);
6a488035
TO
62 }
63
64 /**
65 * Test civicrm_mailing_group_event_subscribe with given contact ID.
66 */
67 public function testMailerGroupSubscribeGivenContactId() {
68 $params = array(
69 'first_name' => 'Test',
70 'last_name' => 'Test',
71 'email' => $this->_email,
ef170fe0 72 'contact_type' => 'Individual',
73 );
6a488035
TO
74 $contactID = $this->individualCreate($params);
75
76 $params = array(
77 'email' => $this->_email,
78 'group_id' => $this->_groupID,
9f1b81e0 79 'contact_id' => $contactID,
80 'hash' => 'b15de8b64e2cec34',
6a488035
TO
81 'time_stamp' => '20101212121212',
82 );
7f0eb6cb 83 $result = $this->callAPIAndDocument('mailing_event_subscribe', 'create', $params, __FUNCTION__, __FILE__);
9e23eadb 84 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
6a488035
TO
85
86 $this->contactDelete($contactID);
87 }
88
89 //-------- civicrm_mailing_group_event_unsubscribe methods-----------
90
91 /**
92 * Test civicrm_mailing_group_event_unsubscribe with wrong params.
93 */
94 public function testMailerGroupUnsubscribeWrongParams() {
95 $params = array(
96 'job_id' => 'Wrong ID',
97 'event_queue_id' => 'Wrong ID',
ef170fe0 98 'hash' => 'Wrong Hash',
99 'time_stamp' => '20101212121212',
6a488035
TO
100 );
101
7bd9cc5f 102 $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
6a488035
TO
103 }
104
105 //--------- civicrm_mailing_group_event_domain_unsubscribe methods -------
106
107 /**
108 * Test civicrm_mailing_group_event_domain_unsubscribe with wrong params.
109 */
110 public function testMailerGroupDomainUnsubscribeWrongParams() {
111 $params = array(
112 'job_id' => 'Wrong ID',
113 'event_queue_id' => 'Wrong ID',
114 'hash' => 'Wrong Hash',
ef170fe0 115 'org_unsubscribe' => 1,
116 'time_stamp' => '20101212121212',
6a488035
TO
117 );
118
7bd9cc5f 119 $this->callAPIFailure('mailing_event_unsubscribe', 'create', $params);
6a488035
TO
120 }
121
6a488035
TO
122 //----------- civicrm_mailing_group_event_resubscribe methods--------
123
124 /**
125 * Test civicrm_mailing_group_event_resubscribe with wrong params type.
126 */
127
128 /**
129 * Test civicrm_mailing_group_event_resubscribe with wrong params.
130 */
131 public function testMailerGroupResubscribeWrongParams() {
132 $params = array(
133 'job_id' => 'Wrong ID',
134 'event_queue_id' => 'Wrong ID',
135 'hash' => 'Wrong Hash',
ef170fe0 136 'org_unsubscribe' => 'test',
137 'time_stamp' => '20101212121212',
6a488035 138 );
7bd9cc5f 139 $this->callAPIFailure('mailing_event_resubscribe', 'create', $params);
6a488035
TO
140 }
141
142 //------------------------ success case ---------------------
143
144 /**
145 * Test civicrm_mailing_group_event_subscribe and civicrm_mailing_event_confirm functions - success expected.
146 */
147 public function testMailerProcess() {
148 $params = array(
149 'first_name' => 'Test',
150 'last_name' => 'Test',
151 'email' => $this->_email,
ef170fe0 152 'contact_type' => 'Individual',
153 );
6a488035
TO
154 $contactID = $this->individualCreate($params);
155
156 $params = array(
157 'email' => $this->_email,
158 'group_id' => $this->_groupID,
ef170fe0 159 'contact_id' => $contactID,
160 'hash' => 'b15de8b64e2cec34',
6a488035
TO
161 'time_stamp' => '20101212121212',
162 );
7f0eb6cb 163 $result = $this->callAPISuccess('mailing_event_subscribe', 'create', $params);
6a488035 164
9e23eadb 165 $this->assertEquals($result['values'][$result['id']]['contact_id'], $contactID);
6a488035
TO
166
167 $params = array(
9e23eadb 168 'contact_id' => $result['values'][$result['id']]['contact_id'],
169 'subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
ef170fe0 170 'hash' => $result['values'][$result['id']]['hash'],
171 'time_stamp' => '20101212121212',
9e23eadb 172 'event_subscribe_id' => $result['values'][$result['id']]['subscribe_id'],
6a488035
TO
173 );
174
7f0eb6cb 175 $result = $this->callAPISuccess('mailing_event_confirm', 'create', $params);
6a488035
TO
176 $this->contactDelete($contactID);
177 }
96025800 178
6a488035 179}