Merge pull request #13966 from agileware/CIVICRM-1168
[civicrm-core.git] / tests / phpunit / CRM / Group / Page / AjaxTest.php
1 <?php
2
3 /**
4 * Class CRM_Group_Page_AjaxTest
5 * @group headless
6 */
7 class CRM_Group_Page_AjaxTest extends CiviUnitTestCase {
8 /**
9 * Permissioned group is used both as an active group the contact can see and as a group that allows
10 * logged in user to see contacts.
11 *
12 * @var int
13 */
14 protected $_permissionedGroup;
15 /**
16 * AS disabled group the contact has permission to.
17 * @var int
18 */
19 protected $_permissionedDisabledGroup;
20
21 /**
22 * @var CRM_Utils_Hook_UnitTests
23 */
24 public $hookClass;
25
26 protected $_params = array();
27
28 public function setUp() {
29 parent::setUp();
30 $this->_params = array(
31 'page' => 1,
32 'rp' => 50,
33 'offset' => 0,
34 'rowCount' => 50,
35 'sort' => NULL,
36 'parentsOnly' => FALSE,
37 'is_unit_test' => TRUE,
38 );
39 $this->hookClass = CRM_Utils_Hook::singleton();
40 $this->createLoggedInUser();
41 $this->_permissionedDisabledGroup = $this->groupCreate(array(
42 'title' => 'pick-me-disabled',
43 'is_active' => 0,
44 'name' => 'pick-me-disabled',
45 ));
46 $this->_permissionedGroup = $this->groupCreate(array(
47 'title' => 'pick-me-active',
48 'is_active' => 1,
49 'name' => 'pick-me-active',
50 ));
51 $this->groupCreate(array('title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled'));
52 $this->groupCreate(array('title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active'));
53 }
54
55 public function tearDown() {
56 CRM_Utils_Hook::singleton()->reset();
57 $this->quickCleanup(array('civicrm_group'));
58 $config = CRM_Core_Config::singleton();
59 unset($config->userPermissionClass->permissions);
60 parent::tearDown();
61 }
62
63 /**
64 * @param $permission
65 */
66 public function setPermissionAndRequest($permission) {
67 $this->setPermissions((array) $permission);
68 global $_REQUEST;
69 $_REQUEST = $this->_params;
70 }
71
72 /**
73 * @param $permission
74 * @param $hook
75 */
76 public function setHookAndRequest($permission, $hook) {
77 CRM_Core_Config::singleton()->userPermissionClass->permissions = (array) $permission;
78 $this->hookClass->setHook('civicrm_aclGroup', array($this, $hook));
79 global $_REQUEST;
80 $_REQUEST = $this->_params;
81 }
82
83 /**
84 * CRM-18528 - Retrieve groups with filter
85 */
86 public function testGroupListWithFilter() {
87 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
88
89 $_GET = $this->_params;
90 $obj = new CRM_Group_Page_AJAX();
91
92 //filter with title
93 $_GET['title'] = "not-me-active";
94 $groups = $obj->getGroupList();
95 $this->assertEquals(1, $groups['recordsTotal']);
96 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
97 unset($_GET['title']);
98
99 // check on status
100 $_GET['status'] = 2;
101 $groups = $obj->getGroupList();
102 foreach ($groups['data'] as $key => $val) {
103 $this->assertEquals('crm-entity disabled', $val['DT_RowClass']);
104 }
105 }
106
107 /**
108 * Retrieve groups as 'view all contacts'
109 */
110 public function testGroupListViewAllContacts() {
111 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
112 $params = $this->_params;
113 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
114 $this->assertEquals(2, $groups['recordsTotal']);
115 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
116 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
117 }
118
119 /**
120 * Check Group Edit w/o 'edit groups' permission.
121 *
122 * FIXME permissions to edit groups can only be determined by the links, which is ridiculously long
123 */
124 public function testGroupEditWithAndWithoutPermission() {
125 $this->setPermissionAndRequest('view all contacts');
126 $params = $this->_params;
127 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
128 $this->assertEquals(2, $groups['recordsTotal']);
129 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&amp;reset=1&amp;force=1&amp;context=smog&amp;gid=4&amp;component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][0]['links']);
130 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&amp;reset=1&amp;force=1&amp;context=smog&amp;gid=2&amp;component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][1]['links']);
131
132 // as per changes made in PR-6822
133 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
134 $params = $this->_params;
135 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
136 $this->assertEquals(2, $groups['recordsTotal']);
137 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&amp;reset=1&amp;force=1&amp;context=smog&amp;gid=4&amp;component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&amp;reset=1&amp;action=update&amp;id=4" class="action-item crm-hover-button" title=\'Edit Group\' >Settings</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Group\' >Disable</a></li><li><a href="/index.php?q=civicrm/group&amp;reset=1&amp;action=delete&amp;id=4" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][0]['links']);
138 $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&amp;reset=1&amp;force=1&amp;context=smog&amp;gid=2&amp;component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&amp;reset=1&amp;action=update&amp;id=2" class="action-item crm-hover-button" title=\'Edit Group\' >Settings</a></span><span class=\'btn-slide crm-hover-button\'>more<ul class=\'panel\'><li><a href="#" class="action-item crm-hover-button crm-enable-disable" title=\'Disable Group\' >Disable</a></li><li><a href="/index.php?q=civicrm/group&amp;reset=1&amp;action=delete&amp;id=2" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][1]['links']);
139 }
140
141 /**
142 * Retrieve groups as 'view all contacts' permissioned user
143 * Without setting params the default is both enabled & disabled
144 * (if you do set default it is enabled only)
145 */
146 public function testGroupListViewAllContactsFoundTitle() {
147 $this->_params['title'] = 'p';
148 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
149 $params = $this->_params;
150 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
151 $this->assertEquals(2, $groups['recordsTotal']);
152 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
153 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
154 }
155
156 /**
157 * Retrieve groups as 'view all contacts'
158 */
159 public function testGroupListViewAllContactsNotFoundTitle() {
160 $this->_params['title'] = 'z';
161 $this->setPermissionAndRequest('view all contacts');
162 $params = $this->_params;
163 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
164 $this->assertEquals(0, $groups['recordsTotal']);
165 }
166
167 /**
168 * Retrieve groups as 'edit all contacts'
169 */
170 public function testGroupListEditAllContacts() {
171 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
172 $params = $this->_params;
173 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
174 $this->assertEquals(2, $groups['recordsTotal']);
175 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
176 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
177 }
178
179 /**
180 * Retrieve groups as 'view all contacts'
181 */
182 public function testGroupListViewAllContactsEnabled() {
183 $this->_params['status'] = 1;
184 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
185 $params = $this->_params;
186 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
187 $this->assertEquals(2, $groups['recordsTotal']);
188 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
189 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
190 }
191
192 /**
193 * Retrieve groups as 'view all contacts'
194 */
195 public function testGroupListViewAllContactsDisabled() {
196 $this->_params['status'] = 2;
197 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
198 $params = $this->_params;
199 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
200 $this->assertEquals(2, $groups['recordsTotal']);
201 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
202 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
203 }
204
205 /**
206 * Retrieve groups as 'view all contacts'
207 */
208 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
209 $this->_params['status'] = 2;
210 $this->_params['title'] = 'n';
211 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
212 $params = $this->_params;
213 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
214 $this->assertEquals(1, $groups['recordsTotal']);
215 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
216 }
217
218 /**
219 * Retrieve groups as 'view all contacts'
220 */
221 public function testGroupListViewAllContactsDisabledFoundTitle() {
222 $this->_params['status'] = 2;
223 $this->_params['title'] = 'p';
224 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
225 $params = $this->_params;
226 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
227 $this->assertEquals(1, $groups['recordsTotal']);
228 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
229 }
230
231 /**
232 * Retrieve groups as 'view all contacts'
233 */
234 public function testGroupListViewAllContactsAll() {
235 $this->_params['status'] = 3;
236 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
237 $params = $this->_params;
238 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
239 $this->assertEquals(4, $groups['recordsTotal']);
240 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
241 $this->assertEquals('not-me-disabled', $groups['data'][1]['title']);
242 $this->assertEquals('pick-me-active', $groups['data'][2]['title']);
243 $this->assertEquals('pick-me-disabled', $groups['data'][3]['title']);
244 }
245
246 /**
247 * Retrieve groups as 'view all contacts'
248 */
249 public function testGroupListAccessCiviCRM() {
250 $this->setPermissionAndRequest('access CiviCRM');
251 $permissionClause = CRM_Contact_BAO_Group::getPermissionClause();
252 $this->assertEquals('1 = 0', $permissionClause);
253 $params = $this->_params;
254 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
255 $this->assertEquals(0, count($groups['data']));
256 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
257 }
258
259 /**
260 * Retrieve groups as 'view all contacts'
261 */
262 public function testGroupListAccessCiviCRMEnabled() {
263 $this->_params['status'] = 1;
264 $this->setPermissionAndRequest('access CiviCRM');
265 $params = $this->_params;
266 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
267 $this->assertEquals(0, count($groups['data']));
268 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
269 }
270
271 /**
272 * Retrieve groups as 'view all contacts'
273 */
274 public function testGroupListAccessCiviCRMDisabled() {
275 $this->_params['status'] = 2;
276 $this->setPermissionAndRequest('access CiviCRM');
277 $params = $this->_params;
278 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
279 $this->assertEquals(0, count($groups['data']));
280 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
281 }
282
283 /**
284 * Retrieve groups as 'view all contacts'
285 */
286 public function testGroupListAccessCiviCRMAll() {
287 $this->_params['status'] = 2;
288 $this->setPermissionAndRequest('access CiviCRM');
289 $params = $this->_params;
290 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
291 $this->assertEquals(0, count($groups['data']));
292 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
293 }
294
295 /**
296 * Retrieve groups as 'view all contacts'
297 */
298 public function testGroupListAccessCiviCRMFound() {
299 $this->_params['title'] = 'p';
300 $this->setPermissionAndRequest('access CiviCRM');
301 $params = $this->_params;
302 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
303 $this->assertEquals(0, count($groups['data']));
304 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
305 }
306
307 /**
308 * Retrieve groups as 'view all contacts'
309 */
310 public function testGroupListAccessCiviCRMNotFound() {
311 $this->_params['title'] = 'z';
312 $this->setPermissionAndRequest('access CiviCRM');
313 $params = $this->_params;
314 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
315 $this->assertEquals(0, count($groups['data']));
316 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
317 }
318
319 public function testTraditionalACL() {
320 $this->setupACL();
321 $this->setPermissionAndRequest('edit groups');
322 $params = $this->_params;
323 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
324 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
325 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
326 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
327 }
328
329 public function testTraditionalACLNotFoundTitle() {
330 $this->_params['title'] = 'n';
331 $this->setupACL();
332 $params = $this->_params;
333 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
334 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
335 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
336 }
337
338 public function testTraditionalACLFoundTitle() {
339 $this->_params['title'] = 'p';
340 $this->setupACL();
341 $this->setPermissionAndRequest('edit groups');
342 $params = $this->_params;
343 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
344 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
345 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
346 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
347 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
348 }
349
350 public function testTraditionalACLDisabled() {
351 $this->_params['status'] = 2;
352 $this->setupACL();
353 $this->setPermissionAndRequest('edit groups');
354 $params = $this->_params;
355 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
356 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
357 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
358 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
359 }
360
361 public function testTraditionalACLDisabledFoundTitle() {
362 $this->_params['status'] = 2;
363 $this->_params['title'] = 'p';
364 $this->setupACL();
365 $this->setPermissionAndRequest('edit groups');
366 $params = $this->_params;
367 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
368 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
369 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
370 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
371 }
372
373 public function testTraditionalACLDisabledNotFoundTitle() {
374 $this->_params['status'] = 2;
375 $this->_params['title'] = 'n';
376 $this->setupACL();
377 $params = $this->_params;
378 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
379 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
380 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
381 }
382
383 public function testTraditionalACLEnabled() {
384 $this->_params['status'] = 1;
385 $this->setupACL();
386 $this->setPermissionAndRequest('edit groups');
387 $params = $this->_params;
388 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
389 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
390 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
391 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
392 }
393
394 public function testTraditionalACLAll() {
395 $this->_params['status'] = 3;
396 $this->setupACL();
397 $this->setPermissionAndRequest('edit groups');
398 $params = $this->_params;
399 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
400 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
401 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
402 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
403 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
404 }
405
406 /**
407 * ACL Group hook.
408 */
409 public function testGroupListAclGroupHookDisabled() {
410 $this->_params['status'] = 2;
411 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
412 $params = $this->_params;
413 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
414 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
415 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
416 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
417 }
418
419 /**
420 * ACL Group hook.
421 */
422 public function testGroupListAclGroupHookDisabledFound() {
423 $this->_params['status'] = 2;
424 $this->_params['title'] = 'p';
425 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
426 $params = $this->_params;
427 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
428 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
429 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
430 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
431 }
432
433 /**
434 * ACL Group hook.
435 */
436 public function testGroupListAclGroupHookDisabledNotFound() {
437 $this->_params['status'] = 2;
438 $this->_params['title'] = 'n';
439 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
440 $params = $this->_params;
441 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
442 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
443 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
444 }
445
446 /**
447 * ACL Group hook.
448 */
449 public function testGroupListAclGroupHook() {
450 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
451 $params = $this->_params;
452 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
453 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
454 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
455 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
456 }
457
458 /**
459 * ACL Group hook.
460 */
461 public function testGroupListAclGroupHookTitleNotFound() {
462 $this->_params['title'] = 'n';
463 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
464 $params = $this->_params;
465 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
466 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
467 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
468 }
469
470 /**
471 * ACL Group hook.
472 */
473 public function testGroupListAclGroupHookTitleFound() {
474 $this->_params['title'] = 'p';
475 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
476 $params = $this->_params;
477 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
478 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
479 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
480 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
481 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
482 }
483
484 /**
485 * ACL Group hook.
486 */
487 public function testGroupListAclGroupHookAll() {
488 $this->_params['status'] = 3;
489 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
490 $params = $this->_params;
491 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
492 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
493 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
494 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
495 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
496 }
497
498 /**
499 * ACL Group hook.
500 */
501 public function testGroupListAclGroupHookEnabled() {
502 $this->_params['status'] = 1;
503 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
504 $params = $this->_params;
505 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
506 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
507 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
508 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
509 }
510
511 /**
512 * Don't populate smart group cache when building Group list.
513 *
514 * It takes forever, especially if you have lots of smart groups.
515 */
516 public function testGroupDontRegenerateSmartGroups() {
517 // Create a contact.
518 $firstName = 'Tweak';
519 $lastName = 'Octonaut';
520 $params = array(
521 'first_name' => $firstName,
522 'last_name' => $lastName,
523 'contact_type' => 'Individual',
524 );
525 $contact = CRM_Contact_BAO_Contact::add($params);
526
527 // Create a smart group.
528 $searchParams = array(
529 'last_name' => $lastName,
530 );
531 $groupParams = array('title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1);
532 $group = CRM_Contact_BAO_Group::createSmartGroup($groupParams);
533
534 // Ensure the smart group is created.
535 $this->assertTrue(is_int($group->id), "Smart group created successfully.");
536 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
537
538 // Ensure it is populating the cache when loaded.
539 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
540 $params = array(1 => array($group->id, 'Integer'));
541 $dao = CRM_Core_DAO::executeQuery($sql, $params);
542 $this->assertEquals($dao->N, 1, '1 record should be found in smart group');
543
544 // Load the Manage Group page code and we should get a count from our
545 // group because the cache is fresh.
546 $_GET = $this->_params;
547 $obj = new CRM_Group_Page_AJAX();
548 $groups = $obj->getGroupList();
549
550 // Make sure we returned our smart group and ensure the count is accurate.
551 $found = FALSE;
552 $right_count = FALSE;
553 foreach ($groups['data'] as $returned_group) {
554 if ($returned_group['group_id'] == $group->id) {
555 $found = TRUE;
556 if ($returned_group['count'] == 1) {
557 $right_count = TRUE;
558 }
559 }
560 }
561 $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
562 $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
563
564 // Purge the group contact cache.
565 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($group->id);
566
567 // Load the Manage Group page code.
568 $_GET = $this->_params;
569 $obj = new CRM_Group_Page_AJAX();
570 $groups = $obj->getGroupList();
571
572 // Make sure the smart group reports unknown count.
573 $count_is_unknown = FALSE;
574 foreach ($groups['data'] as $returned_group) {
575 if ($returned_group['group_id'] == $group->id) {
576 if ($returned_group['count'] == ts('unknown')) {
577 $count_is_unknown = TRUE;
578 }
579 }
580 }
581 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
582
583 // Ensure we did not populate the cache.
584 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
585 $params = array(1 => array($group->id, 'Integer'));
586 $dao = CRM_Core_DAO::executeQuery($sql, $params);
587 $test = 'Group contact cache should not be populated on Manage Groups ' .
588 'when cache_date is null';
589 $this->assertEquals($dao->N, 0, $test);
590
591 // Do it again, but this time don't clear group contact cache. Instead,
592 // set it to expire.
593 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
594 $params['name'] = 'smartGroupCacheTimeout';
595 $timeout = civicrm_api3('Setting', 'getvalue', $params);
596 $timeout = intval($timeout) * 60;
597 // Reset the cache_date to $timeout seconds ago minus another 60
598 // seconds for good measure.
599 $cache_date = date('YmdHis', time() - $timeout - 60);
600
601 $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
602 $update_params = array(
603 1 => array($cache_date, 'Timestamp'),
604 2 => array($group->id, 'Integer'),
605 );
606 CRM_Core_DAO::executeQuery($sql, $update_params);
607
608 // Load the Manage Group page code.
609 $_GET = $this->_params;
610 $obj = new CRM_Group_Page_AJAX();
611 $groups = $obj->getGroupList();
612
613 // Ensure we did not regenerate the cache.
614 $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
615 'FROM civicrm_group WHERE id = %1';
616 $params = array(1 => array($group->id, 'Integer'));
617 $dao = CRM_Core_DAO::executeQuery($sql, $params);
618 $dao->fetch();
619 $test = 'Group contact cache should not be re-populated on Manage Groups ' .
620 'when cache_date has expired';
621 $this->assertEquals($dao->cache_date, $cache_date, $test);
622 }
623
624 /**
625 * Implements ACLGroup hook.
626 * aclGroup function returns a list of permitted groups
627 * @param string $type
628 * @param int $contactID
629 * @param string $tableName
630 * @param array $allGroups
631 * @param array $currentGroups
632 */
633 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
634 //don't use api - you will get a loop
635 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
636 $groups = array();
637 $dao = CRM_Core_DAO::executeQuery($sql);
638 while ($dao->fetch()) {
639 $groups[] = $dao->id;
640 }
641
642 if (!empty($allGroups)) {
643 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
644 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
645 $currentGroups = array_intersect($groups, array_flip($allGroups));
646 }
647 else {
648 $currentGroups = $groups;
649 }
650 }
651
652 public function testEditAllGroupsACL() {
653 $this->setupEditAllGroupsACL();
654 $params = $this->_params;
655 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
656 $this->assertNotEmpty($groups, 'If Edit All Groups is granted, at least one group should be visible');
657 }
658
659 /**
660 * Set up an acl allowing Authenticated contacts to Edit All Groups
661 *
662 * You need to have pre-created these groups & created the user e.g
663 * $this->createLoggedInUser();
664 *
665 */
666 public function setupEditAllGroupsACL() {
667 global $_REQUEST;
668 $_REQUEST = $this->_params;
669
670 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
671 $optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role'));
672 $ov = new CRM_Core_DAO_OptionValue();
673 $ov->option_group_id = $optionGroupID;
674 $ov->value = 55;
675 if ($ov->find(TRUE)) {
676 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
677 }
678 $optionValue = $this->callAPISuccess('option_value', 'create', array(
679 'option_group_id' => $optionGroupID,
680 'label' => 'groupmaster',
681 'value' => 55,
682 ));
683 $groupId = $this->groupCreate(['name' => 'groupmaster group']);
684 // Assign groupmaster to groupmaster group in civicrm_acl_entity_role
685 CRM_Core_DAO::executeQuery("
686 INSERT INTO civicrm_acl_entity_role (
687 `acl_role_id`, `entity_table`, `entity_id`, `is_active`
688 ) VALUES (55, 'civicrm_group', $groupId, 1);
689 ");
690 // Put the user into this group
691 $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
692 $this->callAPISuccess('group_contact', 'create', array(
693 'group_id' => $groupId,
694 'contact_id' => $this->_loggedInUser,
695 ));
696 // Add the ACL
697 CRM_Core_DAO::executeQuery("
698 INSERT INTO civicrm_acl (
699 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
700 )
701 VALUES (
702 'core-580', 'civicrm_acl_role', 55, 'Edit', 'civicrm_saved_search', 0, 1
703 );
704 ");
705
706 }
707
708 }