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