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