Merge pull request #9598 from JMAConsulting/CRM-19585-7
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * Test class for Group API - civicrm_group_*
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_GroupTest extends CiviUnitTestCase {
35 protected $_apiversion;
36 protected $_groupID;
37
38 public function setUp() {
39 $this->_apiversion = 3;
40
41 parent::setUp();
42 $this->_groupID = $this->groupCreate();
43 }
44
45 public function tearDown() {
46
47 $this->groupDelete($this->_groupID);
48 }
49
50 public function testgroupCreateNoTitle() {
51 $params = array(
52 'name' => 'Test Group No title ',
53 'domain_id' => 1,
54 'description' => 'New Test Group Created',
55 'is_active' => 1,
56 'visibility' => 'Public Pages',
57 'group_type' => array(
58 '1' => 1,
59 '2' => 1,
60 ),
61 );
62
63 $group = $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title');
64 }
65
66
67 public function testGetGroupWithEmptyParams() {
68 $group = $this->callAPISuccess('group', 'get', $params = array());
69
70 $group = $group["values"];
71 $this->assertNotNull(count($group));
72 $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1");
73 $this->assertEquals($group[$this->_groupID]['is_active'], 1);
74 $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages');
75 }
76
77 public function testGetGroupParamsWithGroupId() {
78 $params = array('id' => $this->_groupID);
79 $group = $this->callAPISuccess('group', 'get', $params);
80
81 foreach ($group['values'] as $v) {
82 $this->assertEquals($v['name'], "Test Group 1");
83 $this->assertEquals($v['title'], 'New Test Group Created');
84 $this->assertEquals($v['description'], 'New Test Group Created');
85 $this->assertEquals($v['is_active'], 1);
86 $this->assertEquals($v['visibility'], 'Public Pages');
87 }
88 }
89
90 public function testGetGroupParamsWithGroupName() {
91 $params = array(
92 'name' => "Test Group 1",
93 );
94 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
95 $group = $group['values'];
96
97 foreach ($group as $v) {
98 $this->assertEquals($v['id'], $this->_groupID);
99 $this->assertEquals($v['title'], 'New Test Group Created');
100 $this->assertEquals($v['description'], 'New Test Group Created');
101 $this->assertEquals($v['is_active'], 1);
102 $this->assertEquals($v['visibility'], 'Public Pages');
103 }
104 }
105
106 public function testGetGroupParamsWithReturnName() {
107 $params = array();
108 $params['id'] = $this->_groupID;
109 $params['return.name'] = 1;
110 $group = $this->callAPISuccess('group', 'get', $params);
111 $this->assertEquals($group['values'][$this->_groupID]['name'],
112 "Test Group 1"
113 );
114 }
115
116 public function testGetGroupParamsWithGroupTitle() {
117 $params = array();
118 $params['title'] = 'New Test Group Created';
119 $group = $this->callAPISuccess('group', 'get', $params);
120
121 foreach ($group['values'] as $v) {
122 $this->assertEquals($v['id'], $this->_groupID);
123 $this->assertEquals($v['name'], "Test Group 1");
124 $this->assertEquals($v['description'], 'New Test Group Created');
125 $this->assertEquals($v['is_active'], 1);
126 $this->assertEquals($v['visibility'], 'Public Pages');
127 }
128 }
129
130 /**
131 * Test Group create with Group Type and Parent
132 */
133 public function testGroupCreateWithTypeAndParent() {
134 $params = array(
135 'name' => 'Test Group type',
136 'title' => 'Test Group Type',
137 'description' => 'Test Group with Group Type',
138 'is_active' => 1,
139 //check for empty parent
140 'parents' => "",
141 'visibility' => 'Public Pages',
142 'group_type' => array(1, 2),
143 );
144
145 $result = $this->callAPISuccess('Group', 'create', $params);
146 $group = $result['values'][$result['id']];
147 $this->assertEquals($group['name'], "Test Group type");
148 $this->assertEquals($group['is_active'], 1);
149 $this->assertEquals($group['parents'], "");
150 $this->assertEquals($group['group_type'], $params['group_type']);
151
152 //Pass group_type param in checkbox format.
153 $params = array_merge($params, array(
154 'name' => 'Test Checkbox Format',
155 'title' => 'Test Checkbox Format',
156 'group_type' => array(2 => 1),
157 )
158 );
159 $result = $this->callAPISuccess('Group', 'create', $params);
160 $group = $result['values'][$result['id']];
161 $this->assertEquals($group['name'], "Test Checkbox Format");
162 $this->assertEquals($group['group_type'], array_keys($params['group_type']));
163
164 //assert single value for group_type and parent
165 $params = array_merge($params, array(
166 'name' => 'Test Group 2',
167 'title' => 'Test Group 2',
168 'group_type' => 2,
169 'parents' => $result['id'],
170 )
171 );
172 $result = $this->callAPISuccess('Group', 'create', $params);
173 $group = $result["values"][$result['id']];
174 $this->assertEquals($group['group_type'], array($params['group_type']));
175 $this->assertEquals($group['parents'], $params['parents']);
176 }
177
178 public function testGetNonExistingGroup() {
179 $params = array();
180 $params['title'] = 'No such group Exist';
181 $group = $this->callAPISuccess('group', 'get', $params);
182 $this->assertEquals(0, $group['count']);
183 }
184
185 public function testgroupdeleteParamsnoId() {
186 $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id');
187 }
188
189 public function testgetfields() {
190 $description = "Demonstrate use of getfields to interrogate api.";
191 $params = array('action' => 'create');
192 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
193 $this->assertEquals(1, $result['values']['is_active']['api.default']);
194 }
195
196 public function testIllegalParentsParams() {
197 $params = array(
198 'title' => 'Test illegal Group',
199 'domain_id' => 1,
200 'description' => 'Testing illegal Parents params',
201 'is_active' => 1,
202 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
203 );
204 $this->callAPIFailure('group', 'create', $params);
205 unset($params['parents']);
206 $this->callAPISuccess('group', 'create', $params);
207 $group1 = $this->callAPISuccess('group', 'get', array(
208 'title' => 'Test illegal Group',
209 'parents' => array('IS NOT NULL' => 1),
210 ));
211 $this->assertEquals(0, $group1['count']);
212 $params['title'] = 'Test illegal Group 2';
213 $params['parents'] = array();
214 $params['parents'][$this->_groupID] = 'test Group';
215 $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
216 $group2 = $this->callAPIFailure('group', 'create', $params);
217 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
218 $group2 = $this->callAPISuccess('group', 'create', $params);
219 $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1);
220 }
221
222 }