Merge pull request #3057 from agh1/counties-as-extensions
[civicrm-core.git] / tests / phpunit / CRM / Group / Page / AjaxTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Group_Page_AjaxTest extends CiviUnitTestCase {
4 /**
5 * Permissioned group is used both as an active group the contact can see and as a group that allows
6 * logged in user to see contacts
7 * @var integer
8 */
9 protected $_permissionedGroup;
10 /**
11 * AS disabled group the contact has permission to
12 * @var unknown
13 */
14 protected $_permissionedDisabledGroup;
15
16 function get_info() {
17 return array(
18 'name' => 'Contact BAOs',
19 'description' => 'Test all Contact_BAO_Contact methods.',
20 'group' => 'CiviCRM BAO Tests',
21 );
22 }
23
24 function setUp() {
25 parent::setUp();
26 $this->_params = array(
27 'sEcho' => '1',
28 'page' => 1,
29 'rp' => 50,
30 'offset' => 0,
31 'rowCount' => 50,
32 'sort' => NULL,
33 'is_unit_test' => TRUE,
34 );
35 $this->hookClass = CRM_Utils_Hook::singleton();
36 $this->createLoggedInUser();
37 $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
38 $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
39 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
40 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
41 }
42
43 function tearDown() {
44 CRM_Utils_Hook::singleton()->reset();
45 $this->quickCleanup(array('civicrm_group'));
46 $config = CRM_Core_Config::singleton();
47 unset($config->userPermissionClass->permissions);
48 parent::tearDown();
49 }
50
51 /**
52 * retrieve groups as 'view all contacts'
53 */
54 function testGroupListViewAllContacts() {
55 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
56 global $_REQUEST;
57 $_REQUEST = $this->_params;
58 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
59 $this->assertEquals(2, $total);
60 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
61 $this->assertEquals('not-me-active', $groups[4]['group_name']);
62 }
63
64 /**
65 * retrieve groups as 'view all contacts' permissioned user
66 * Without setting params the default is both enabled & disabled
67 * (if you do set default it is enabled only)
68 */
69 function testGroupListViewAllContactsFoundTitle() {
70 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
71 global $_REQUEST;
72 $_REQUEST = $this->_params;
73 $_REQUEST['title'] = 'p';
74 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
75 $this->assertEquals(2, $total);
76 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
77 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
78 }
79
80 /**
81 * retrieve groups as 'view all contacts'
82 */
83 function testGroupListViewAllContactsNotFoundTitle() {
84 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
85 global $_REQUEST;
86 $_REQUEST = $this->_params;
87 $_REQUEST['title'] = 'z';
88 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
89 $this->assertEquals(0, $total);
90 }
91 /**
92 * retrieve groups as 'edit all contacts'
93 */
94 function testGroupListEditAllContacts() {
95 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
96 global $_REQUEST;
97 $_REQUEST = $this->_params;
98 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
99 $this->assertEquals(2, $total);
100 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
101 $this->assertEquals('not-me-active', $groups[4]['group_name']);
102 }
103
104 /**
105 * retrieve groups as 'view all contacts'
106 */
107 function testGroupListViewAllContactsEnabled() {
108 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
109 global $_REQUEST;
110 $_REQUEST = $this->_params;
111 $_REQUEST['status'] = 1;
112 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
113 $this->assertEquals(2, $total);
114 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
115 $this->assertEquals('not-me-active', $groups[4]['group_name']);
116 }
117
118 /**
119 * retrieve groups as 'view all contacts'
120 */
121 function testGroupListViewAllContactsDisabled() {
122 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
123 global $_REQUEST;
124 $_REQUEST = $this->_params;
125 $_REQUEST['status'] = 2;
126 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
127 $this->assertEquals(2, $total);
128 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
129 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
130 }
131
132 /**
133 * retrieve groups as 'view all contacts'
134 */
135 function testGroupListViewAllContactsDisabledNotFoundTitle() {
136 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
137 global $_REQUEST;
138 $_REQUEST = $this->_params;
139 $_REQUEST['status'] = 2;
140 $_REQUEST['title'] = 'n';
141 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
142 $this->assertEquals(1, $total);
143 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
144 }
145
146 /**
147 * retrieve groups as 'view all contacts'
148 */
149 function testGroupListViewAllContactsDisabledFoundTitle() {
150 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
151 global $_REQUEST;
152 $_REQUEST = $this->_params;
153 $_REQUEST['status'] = 2;
154 $_REQUEST['title'] = 'p';
155 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
156 $this->assertEquals(1, $total);
157 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
158 }
159
160 /**
161 * retrieve groups as 'view all contacts'
162 */
163 function testGroupListViewAllContactsAll() {
164 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts');
165 global $_REQUEST;
166 $_REQUEST = $this->_params;
167 $_REQUEST['status'] = 3;
168 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
169 $this->assertEquals(4, $total);
170 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
171 $this->assertEquals('not-me-disabled', $groups[3]['group_name']);
172 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
173 $this->assertEquals('not-me-active', $groups[4]['group_name']);
174 }
175
176
177 /**
178 * retrieve groups as 'view all contacts'
179 */
180 function testGroupListAccessCiviCRM() {
181 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
182 global $_REQUEST;
183 $_REQUEST = $this->_params;
184 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
185 $this->assertEquals(0, count($groups));
186 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
187 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
188 }
189 /**
190 * retrieve groups as 'view all contacts'
191 */
192 function testGroupListAccessCiviCRMEnabled() {
193 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
194 global $_REQUEST;
195 $_REQUEST = $this->_params;
196 $_REQUEST['status'] = 1;
197 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
198 $this->assertEquals(0, count($groups));
199 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
200 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
201 }
202 /**
203 * retrieve groups as 'view all contacts'
204 */
205 function testGroupListAccessCiviCRMDisabled() {
206 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
207 global $_REQUEST;
208 $_REQUEST = $this->_params;
209 $_REQUEST['status'] = 2;
210 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
211 $this->assertEquals(0, count($groups));
212 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
213 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
214 }
215
216 /**
217 * retrieve groups as 'view all contacts'
218 */
219 function testGroupListAccessCiviCRMAll() {
220 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
221 global $_REQUEST;
222 $_REQUEST = $this->_params;
223 $_REQUEST['status'] = 2;
224 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
225 $this->assertEquals(0, count($groups));
226 $this->markTestIncomplete('The AJAX function returns an incorrect result - needs fixing');
227 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
228 }
229
230 /**
231 * retrieve groups as 'view all contacts'
232 */
233 function testGroupListAccessCiviCRMFound() {
234 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
235 global $_REQUEST;
236 $_REQUEST = $this->_params;
237 $_REQUEST['title'] = 'p';
238 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
239 $this->assertEquals(0, count($groups));
240 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
241 }
242
243 /**
244 * retrieve groups as 'view all contacts'
245 */
246 function testGroupListAccessCiviCRMNotFound() {
247 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
248 global $_REQUEST;
249 $_REQUEST = $this->_params;
250 $_REQUEST['title'] = 'z';
251 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
252 $this->assertEquals(0, count($groups));
253 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
254 }
255
256 function testTraditionalACL () {
257 $this->setupACL();
258 global $_REQUEST;
259 $_REQUEST = $this->_params;
260 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
261 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
262 $this->assertEquals(1, $total, 'Total needs to be set correctly');
263 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
264 }
265
266 function testTraditionalACLNotFoundTitle () {
267 $this->setupACL();
268 global $_REQUEST;
269 $_REQUEST = $this->_params;
270 $_REQUEST['title'] = 'n';
271 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
272 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
273 $this->assertEquals(0, $total, 'Total needs to be set correctly');
274 }
275
276 function testTraditionalACLFoundTitle () {
277 $this->setupACL();
278 global $_REQUEST;
279 $_REQUEST = $this->_params;
280 $_REQUEST['title'] = 'p';
281 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
282 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
283 $this->assertEquals(2, $total, 'Total needs to be set correctly');
284 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
285 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
286 }
287
288 function testTraditionalACLDisabled () {
289 $this->setupACL();
290 global $_REQUEST;
291 $_REQUEST = $this->_params;
292 $_REQUEST['status'] = 2;
293 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
294 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
295 $this->assertEquals(1, $total, 'Total needs to be set correctly');
296 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
297 }
298
299 function testTraditionalACLDisabledFoundTitle () {
300 $this->setupACL();
301 global $_REQUEST;
302 $_REQUEST = $this->_params;
303 $_REQUEST['status'] = 2;
304 $_REQUEST['title'] = 'p';
305 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
306 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
307 $this->assertEquals(1, $total, 'Total needs to be set correctly');
308 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
309 }
310
311 function testTraditionalACLDisabledNotFoundTitle () {
312 $this->setupACL();
313 global $_REQUEST;
314 $_REQUEST = $this->_params;
315 $_REQUEST['status'] = 2;
316 $_REQUEST['title'] = 'n';
317 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
318 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
319 $this->assertEquals(0, $total, 'Total needs to be set correctly');
320 }
321
322 function testTraditionalACLEnabled () {
323 $this->setupACL();
324 global $_REQUEST;
325 $_REQUEST = $this->_params;
326 $_REQUEST['status'] = 1;
327 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
328 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
329 $this->assertEquals(1, $total, 'Total needs to be set correctly');
330 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
331 }
332
333 function testTraditionalACLAll () {
334 $this->setupACL();
335 global $_REQUEST;
336 $_REQUEST = $this->_params;
337 $_REQUEST['status'] = 3;
338 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
339 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
340 $this->assertEquals(2, $total, 'Total needs to be set correctly');
341 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
342 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
343 }
344
345 /**
346 * ACL Group hook
347 */
348 function testGroupListAclGroupHookDisabled() {
349 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
350 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
351 global $_REQUEST;
352 $_REQUEST = $this->_params;
353 $_REQUEST['status'] = 2;
354 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
355 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
356 $this->assertEquals(1, $total, 'Total needs to be set correctly');
357 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
358 }
359
360 /**
361 * ACL Group hook
362 */
363 function testGroupListAclGroupHookDisabledFound() {
364 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
365 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
366 global $_REQUEST;
367 $_REQUEST = $this->_params;
368 $_REQUEST['status'] = 2;
369 $_REQUEST['title'] = 'p';
370 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
371 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
372 $this->assertEquals(1, $total, 'Total needs to be set correctly');
373 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
374 }
375
376 /**
377 * ACL Group hook
378 */
379 function testGroupListAclGroupHookDisabledNotFound() {
380 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
381 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
382 global $_REQUEST;
383 $_REQUEST = $this->_params;
384 $_REQUEST['status'] = 2;
385 $_REQUEST['title'] = 'n';
386 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
387 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
388 $this->assertEquals(0, $total, 'Total needs to be set correctly');
389 }
390
391
392 /**
393 * ACL Group hook
394 */
395 function testGroupListAclGroupHook() {
396 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
397 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
398 global $_REQUEST;
399 $_REQUEST = $this->_params;
400 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
401 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
402 $this->assertEquals(1, $total, 'Total needs to be set correctly');
403 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
404 }
405
406 /**
407 * ACL Group hook
408 */
409 function testGroupListAclGroupHookTitleNotFound() {
410 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
411 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
412 global $_REQUEST;
413 $_REQUEST = $this->_params;
414 $_REQUEST['title'] = 'n';
415 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
416 $this->assertEquals(0, $total, 'Total needs to be set correctly');
417 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
418 }
419
420 /**
421 * ACL Group hook
422 */
423 function testGroupListAclGroupHookTitleFound() {
424 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
425 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
426 global $_REQUEST;
427 $_REQUEST = $this->_params;
428 $_REQUEST['title'] = 'p';
429 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
430 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
431 $this->assertEquals(2, $total, 'Total needs to be set correctly');
432 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
433 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
434 }
435
436 /**
437 * ACL Group hook
438 */
439 function testGroupListAclGroupHookAll() {
440 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
441 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
442 global $_REQUEST;
443 $_REQUEST = $this->_params;
444 $_REQUEST['status'] = 3;
445 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
446 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
447 $this->assertEquals(2, $total, 'Total needs to be set correctly');
448 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
449 $this->assertEquals('pick-me-disabled', $groups[1]['group_name']);
450 }
451
452 /**
453 * ACL Group hook
454 */
455 function testGroupListAclGroupHookEnabled() {
456 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
457 $this->hookClass->setHook('civicrm_aclGroup', array($this, 'hook_civicrm_aclGroup'));
458 global $_REQUEST;
459 $_REQUEST = $this->_params;
460 $_REQUEST['status'] = 1;
461 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
462 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
463 $this->assertEquals(1, $total, 'Total needs to be set correctly');
464 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
465 }
466
467 /**
468 * Implements ACLGroup hook
469 * aclGroup function returns a list of permitted groups
470 * @param string $type
471 * @param integer $contactID
472 * @param string $tableName
473 * @param array $allGroups
474 * @param array $currentGroups
475 */
476 function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
477 //dont use api - you will get a loop
478 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
479 $groups = array();
480 $dao = CRM_Core_DAO::executeQuery($sql);
481 while ($dao->fetch()) {
482 $groups[] = $dao->id;
483 }
484
485 if(!empty($allGroups)) {
486 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
487 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
488 $currentGroups = array_intersect($groups, array_flip($allGroups));
489 }
490 else {
491 $currentGroups = $groups;
492 }
493 }
494 }