CRM-13863 - Ajaxify contact groups tab
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
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
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 class api_v3_GroupContactTest extends CiviUnitTestCase {
32
33 protected $_contactId;
34 protected $_contactId1;
35 protected $_apiversion = 3;
36 protected $_groupId1;
37 protected $_groupContactId;
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 parent::setUp();
54
55 $this->_contactId = $this->individualCreate();
56
57 $this->_groupId1 = $this->groupCreate(NULL);
58 $params = array(
59 'contact_id' => $this->_contactId,
60 'group_id' => $this->_groupId1,
61 );
62
63 $result = $this->callAPISuccess('group_contact', 'create', $params);
64 $this->_groupContactId = $result['id'];
65
66 $group = array(
67 'name' => 'Test Group 2',
68 'domain_id' => 1,
69 'title' => 'New Test Group2 Created',
70 'description' => 'New Test Group2 Created',
71 'is_active' => 1,
72 'visibility' => 'User and User Admin Only',
73 );
74
75 $this->_groupId2 = $this->groupCreate($group, 3);
76
77 $this->_group = array(
78 $this->_groupId1 => array('title' => 'New Test Group Created',
79 'visibility' => 'Public Pages',
80 'in_method' => 'API',
81 ),
82 $this->_groupId2 => array(
83 'title' => 'New Test Group2 Created',
84 'visibility' => 'User and User Admin Only',
85 'in_method' => 'API',
86 ),
87 );
88 }
89
90 function tearDown() {
91 $tablesToTruncate = array(
92 'civicrm_contact',
93 'civicrm_group',
94 );
95 $this->quickCleanup($tablesToTruncate);
96 }
97
98 ///////////////// civicrm_group_contact_get methods
99 function testGet() {
100 $params = array(
101 'contact_id' => $this->_contactId,
102 );
103 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
104 foreach ($result['values'] as $v) {
105 $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']);
106 $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']);
107 $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']);
108 }
109 }
110
111 function testGetGroupID() {
112 $description = "Get all from group and display contacts";
113 $subfile = "GetWithGroupID";
114 $params = array(
115 'group_id' => $this->_groupId1,
116 'api.group.get' => 1,
117 'sequential' => 1,
118 );
119 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
120 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
121 $key = $values['id'];
122 $this->assertEquals($values['title'], $this->_group[$key]['title']);
123 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
124 }
125 }
126
127 function testCreateWithEmptyParams() {
128 $params = array();
129 $groups = $this->callAPIFailure('group_contact', 'create', $params);
130 $this->assertEquals($groups['error_message'],
131 'Mandatory key(s) missing from params array: group_id, contact_id'
132 );
133 }
134
135 function testCreateWithoutGroupIdParams() {
136 $params = array(
137 'contact_id' => $this->_contactId, );
138
139 $groups = $this->callAPIFailure('group_contact', 'create', $params);
140 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
141 }
142
143 function testCreateWithoutContactIdParams() {
144 $params = array(
145 'group_id' => $this->_groupId1, );
146 $groups = $this->callAPIFailure('group_contact', 'create', $params);
147 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
148 }
149
150 function testCreate() {
151 $cont = array(
152 'first_name' => 'Amiteshwar',
153 'middle_name' => 'L.',
154 'last_name' => 'Prasad',
155 'prefix_id' => 3,
156 'suffix_id' => 3,
157 'email' => 'amiteshwar.prasad@civicrm.org',
158 'contact_type' => 'Individual', );
159
160 $this->_contactId1 = $this->individualCreate($cont);
161 $params = array(
162 'contact_id' => $this->_contactId,
163 'contact_id.2' => $this->_contactId1,
164 'group_id' => $this->_groupId1,
165 );
166
167 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
168 $this->assertEquals($result['not_added'], 1, "in line " . __LINE__);
169 $this->assertEquals($result['added'], 1, "in line " . __LINE__);
170 $this->assertEquals($result['total_count'], 2, "in line " . __LINE__);
171 }
172
173 ///////////////// civicrm_group_contact_remove methods
174 function testDelete() {
175 $params = array(
176 'contact_id' => $this->_contactId,
177 'group_id' => 1,
178 );
179
180 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
181 $this->assertEquals($result['removed'], 1, "in line " . __LINE__);
182 $this->assertEquals($result['total_count'], 1, "in line " . __LINE__);
183 }
184
185 function testDeletePermanent() {
186 $params = array(
187 'id' => $this->_groupContactId,
188 'skip_undelete' => TRUE,
189 );
190 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
191 $result = $this->callAPISuccess('group_contact', 'get', $params);
192 $this->assertEquals(0, $result['count'], "in line " . __LINE__);
193 }
194 }
195