Merge pull request #1216 from davecivicrm/CRM-13062
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29
30require_once 'CiviTest/CiviUnitTestCase.php';
31class api_v3_GroupContactTest extends CiviUnitTestCase {
32
33 protected $_contactId;
34 protected $_contactId1;
35 protected $_apiversion;
36 protected $_groupId1;
37 public $_eNoticeCompliant = True;
38
39 function get_info() {
40 return array(
41 'name' => 'Group Contact Create',
42 'description' => 'Test all Group Contact Create API methods.',
43 'group' => 'CiviCRM API Tests',
44 );
45 }
46
47 /*
48 * Set up for group contact tests
49 *
50 * @todo set up calls function that doesn't work @ the moment
51 */
52 function setUp() {
53 $this->_apiversion = 3;
54 parent::setUp();
55
56 $this->_contactId = $this->individualCreate(NULL);
57
58 $this->_groupId1 = $this->groupCreate(NULL);
59 $params = array(
60 'contact_id' => $this->_contactId,
61 'group_id' => $this->_groupId1,
62 'version' => $this->_apiversion,
63 );
64
65 $result = civicrm_api('group_contact', 'create', $params);
66
67 $group = array(
68 'name' => 'Test Group 2',
69 'domain_id' => 1,
70 'title' => 'New Test Group2 Created',
71 'description' => 'New Test Group2 Created',
72 'is_active' => 1,
73 'visibility' => 'User and User Admin Only',
74 'version' => $this->_apiversion,
75 );
76
77 $this->_groupId2 = $this->groupCreate($group, 3);
78 $params = array(
79 'contact_id.1' => $this->_contactId,
80 'group_id' => $this->_groupId2,
81 'version' => $this->_apiversion,
82 );
83 //@todo uncomment me when I work
84 //civicrm_group_contact_create( $params );
85
86 $this->_group = array(
87 $this->_groupId1 => array('title' => 'New Test Group Created',
88 'visibility' => 'Public Pages',
89 'in_method' => 'API',
90 ),
91 $this->_groupId2 => array(
92 'title' => 'New Test Group2 Created',
93 'visibility' => 'User and User Admin Only',
94 'in_method' => 'API',
95 ),
96 );
97 }
98
99 function tearDown() {
100 $tablesToTruncate = array(
101 'civicrm_contact',
102 'civicrm_group',
103 );
104 $this->quickCleanup($tablesToTruncate);
105 }
106
107 ///////////////// civicrm_group_contact_get methods
108 function testGet() {
109 $params = array(
110 'contact_id' => $this->_contactId,
111 'version' => $this->_apiversion,
112 );
113 $result = civicrm_api('group_contact', 'get', $params);
114 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
115 foreach ($result['values'] as $v) {
116 $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']);
117 $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']);
118 $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']);
119 }
120 }
121
122 function testGetGroupID() {
123 $description = "Get all from group and display contacts";
124 $subfile = "GetWithGroupID";
125 $params = array(
126 'group_id' => $this->_groupId1,
127 'version' => $this->_apiversion,
128 'api.group.get' => 1,
129 'sequential' => 1,
130 );
131 $result = civicrm_api('group_contact', 'get', $params);
132 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile);
133 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
134 $key = $values['id'];
135 $this->assertEquals($values['title'], $this->_group[$key]['title']);
136 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
137 }
138 }
139
140
141 ///////////////// civicrm_group_contact_add methods
142 function testCreateWithWrongParamsType() {
143 $params = 1;
d0e1eff2 144 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
145 $this->assertEquals($groups['error_message'], 'Input variable `params` is not an array');
146 }
147
148 function testCreateWithEmptyParams() {
149 $params = array();
d0e1eff2 150 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 151 $this->assertEquals($groups['error_message'],
d0e1eff2 152 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
153 );
154 }
155
156 function testCreateWithoutGroupIdParams() {
157 $params = array(
158 'contact_id' => $this->_contactId,
159 'version' => $this->_apiversion,
160 );
161
d0e1eff2 162 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
163 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
164 }
165
166 function testCreateWithoutContactIdParams() {
167 $params = array(
168 'group_id' => $this->_groupId1,
169 'version' => $this->_apiversion,
170 );
d0e1eff2 171 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
172 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
173 }
174
175 function testCreate() {
176 $cont = array(
177 'first_name' => 'Amiteshwar',
178 'middle_name' => 'L.',
179 'last_name' => 'Prasad',
180 'prefix_id' => 3,
181 'suffix_id' => 3,
182 'email' => 'amiteshwar.prasad@civicrm.org',
183 'contact_type' => 'Individual',
184 'version' => $this->_apiversion,
185 );
186
187 $this->_contactId1 = $this->individualCreate($cont);
188 $params = array(
189 'contact_id' => $this->_contactId,
190 'contact_id.2' => $this->_contactId1,
191 'group_id' => $this->_groupId1,
192 'version' => $this->_apiversion,
193 );
194
195 $result = civicrm_api('group_contact', 'create', $params);
196 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
791c263c 197 $this->assertAPISuccess($result, "in line " . __LINE__);
6a488035
TO
198 $this->assertEquals($result['not_added'], 1, "in line " . __LINE__);
199 $this->assertEquals($result['added'], 1, "in line " . __LINE__);
200 $this->assertEquals($result['total_count'], 2, "in line " . __LINE__);
201 }
202
203 ///////////////// civicrm_group_contact_remove methods
204 function testDelete() {
205 $params = array(
206 'contact_id' => $this->_contactId,
207 'group_id' => 1,
208 'version' => $this->_apiversion,
209 );
210
211 $result = civicrm_api('group_contact', 'delete', $params);
212 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
791c263c 213 $this->assertAPISuccess($result, "in line " . __LINE__);
6a488035
TO
214 $this->assertEquals($result['removed'], 1, "in line " . __LINE__);
215 $this->assertEquals($result['total_count'], 1, "in line " . __LINE__);
216 }
217}
218