Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06a1bc01 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06a1bc01 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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';
e9479dcf
EM
31
32/**
33 * Class api_v3_GroupContactTest
34 */
6a488035
TO
35class api_v3_GroupContactTest extends CiviUnitTestCase {
36
37 protected $_contactId;
38 protected $_contactId1;
7fbb4198 39 protected $_apiversion = 3;
6a488035 40 protected $_groupId1;
6a488035 41
4cbe18b8
EM
42 /**
43 * @return array
44 */
6a488035
TO
45 function get_info() {
46 return array(
47 'name' => 'Group Contact Create',
48 'description' => 'Test all Group Contact Create API methods.',
49 'group' => 'CiviCRM API Tests',
50 );
51 }
52
c490a46a
CW
53 /**
54 * Set up for group contact tests
55 *
56 * @todo set up calls function that doesn't work @ the moment
57 */
6a488035 58 function setUp() {
6a488035
TO
59 parent::setUp();
60
e4d5f1e2 61 $this->_contactId = $this->individualCreate();
6a488035 62
72e41e3a 63 $this->_groupId1 = $this->groupCreate();
6a488035
TO
64 $params = array(
65 'contact_id' => $this->_contactId,
66 'group_id' => $this->_groupId1,
6a488035
TO
67 );
68
7fbb4198 69 $result = $this->callAPISuccess('group_contact', 'create', $params);
6a488035
TO
70
71 $group = array(
72 'name' => 'Test Group 2',
73 'domain_id' => 1,
74 'title' => 'New Test Group2 Created',
75 'description' => 'New Test Group2 Created',
76 'is_active' => 1,
77 'visibility' => 'User and User Admin Only',
6a488035
TO
78 );
79
d8e39cba 80 $this->_groupId2 = $this->groupCreate($group);
6a488035
TO
81
82 $this->_group = array(
83 $this->_groupId1 => array('title' => 'New Test Group Created',
84 'visibility' => 'Public Pages',
85 'in_method' => 'API',
86 ),
87 $this->_groupId2 => array(
88 'title' => 'New Test Group2 Created',
89 'visibility' => 'User and User Admin Only',
90 'in_method' => 'API',
91 ),
92 );
93 }
94
95 function tearDown() {
96 $tablesToTruncate = array(
97 'civicrm_contact',
98 'civicrm_group',
99 );
100 $this->quickCleanup($tablesToTruncate);
101 }
102
103 ///////////////// civicrm_group_contact_get methods
104 function testGet() {
105 $params = array(
106 'contact_id' => $this->_contactId,
6a488035 107 );
7fbb4198 108 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
109 foreach ($result['values'] as $v) {
110 $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']);
111 $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']);
112 $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']);
113 }
114 }
115
116 function testGetGroupID() {
117 $description = "Get all from group and display contacts";
118 $subfile = "GetWithGroupID";
119 $params = array(
120 'group_id' => $this->_groupId1,
6a488035
TO
121 'api.group.get' => 1,
122 'sequential' => 1,
123 );
7fbb4198 124 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
125 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
126 $key = $values['id'];
127 $this->assertEquals($values['title'], $this->_group[$key]['title']);
128 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
129 }
130 }
131
6a488035
TO
132 function testCreateWithEmptyParams() {
133 $params = array();
d0e1eff2 134 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 135 $this->assertEquals($groups['error_message'],
d0e1eff2 136 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
137 );
138 }
139
140 function testCreateWithoutGroupIdParams() {
141 $params = array(
7fbb4198 142 'contact_id' => $this->_contactId, );
6a488035 143
d0e1eff2 144 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
145 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
146 }
147
148 function testCreateWithoutContactIdParams() {
149 $params = array(
7fbb4198 150 'group_id' => $this->_groupId1, );
d0e1eff2 151 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
152 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
153 }
154
155 function testCreate() {
156 $cont = array(
157 'first_name' => 'Amiteshwar',
158 'middle_name' => 'L.',
159 'last_name' => 'Prasad',
160 'prefix_id' => 3,
161 'suffix_id' => 3,
162 'email' => 'amiteshwar.prasad@civicrm.org',
7fbb4198 163 'contact_type' => 'Individual', );
6a488035
TO
164
165 $this->_contactId1 = $this->individualCreate($cont);
166 $params = array(
167 'contact_id' => $this->_contactId,
168 'contact_id.2' => $this->_contactId1,
169 'group_id' => $this->_groupId1,
6a488035
TO
170 );
171
7fbb4198 172 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
173 $this->assertEquals($result['not_added'], 1, "in line " . __LINE__);
174 $this->assertEquals($result['added'], 1, "in line " . __LINE__);
175 $this->assertEquals($result['total_count'], 2, "in line " . __LINE__);
176 }
177
178 ///////////////// civicrm_group_contact_remove methods
179 function testDelete() {
180 $params = array(
181 'contact_id' => $this->_contactId,
182 'group_id' => 1,
6a488035
TO
183 );
184
7fbb4198 185 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
186 $this->assertEquals($result['removed'], 1, "in line " . __LINE__);
187 $this->assertEquals($result['total_count'], 1, "in line " . __LINE__);
188 }
3d700d00
CW
189
190 function testDeletePermanent() {
a9739e5d 191 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
3d700d00 192 $params = array(
a9739e5d 193 'id' => $result['id'],
3d700d00
CW
194 'skip_undelete' => TRUE,
195 );
196 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
197 $result = $this->callAPISuccess('group_contact', 'get', $params);
198 $this->assertEquals(0, $result['count'], "in line " . __LINE__);
a9739e5d 199 $this->assertArrayNotHasKey('id', $result, "in line " . __LINE__);
3d700d00 200 }
6a488035
TO
201}
202