Move pear/mail from packages to composer.json.
[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 /**
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 /**
448 * ACL Group hook.
449 */
450 public function testGroupListAclGroupHook() {
451 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
452 $params = $this->_params;
453 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
454 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
455 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
456 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
457 }
458
459 /**
460 * ACL Group hook.
461 */
462 public function testGroupListAclGroupHookTitleNotFound() {
463 $this->_params['title'] = 'n';
464 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
465 $params = $this->_params;
466 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
467 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
468 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
469 }
470
471 /**
472 * ACL Group hook.
473 */
474 public function testGroupListAclGroupHookTitleFound() {
475 $this->_params['title'] = 'p';
476 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
477 $params = $this->_params;
478 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
479 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
480 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
481 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
482 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
483 }
484
485 /**
486 * ACL Group hook.
487 */
488 public function testGroupListAclGroupHookAll() {
489 $this->_params['status'] = 3;
490 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
491 $params = $this->_params;
492 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
493 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
494 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
495 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
496 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
497 }
498
499 /**
500 * ACL Group hook.
501 */
502 public function testGroupListAclGroupHookEnabled() {
503 $this->_params['status'] = 1;
504 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
505 $params = $this->_params;
506 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
507 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
508 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
509 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
510 }
511
512 /**
513 * Don't populate smart group cache when building Group list.
514 *
515 * It takes forever, especially if you have lots of smart groups.
516 */
517 public function testGroupDontRegenerateSmartGroups() {
518 // Create a contact.
519 $firstName = 'Tweak';
520 $lastName = 'Octonaut';
521 $params = array(
522 'first_name' => $firstName,
523 'last_name' => $lastName,
524 'contact_type' => 'Individual',
525 );
526 $contact = CRM_Contact_BAO_Contact::add($params);
527
528 // Create a smart group.
529 $searchParams = array(
530 'last_name' => $lastName,
531 );
532 $groupParams = array('title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1);
533 $group = CRM_Contact_BAO_Group::createSmartGroup($groupParams);
534
535 // Ensure the smart group is created.
536 $this->assertTrue(is_int($group->id), "Smart group created successfully.");
537 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
538
539 // Ensure it is populating the cache when loaded.
540 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
541 $params = array(1 => array($group->id, 'Integer'));
542 $dao = CRM_Core_DAO::executeQuery($sql, $params);
543 $this->assertEquals($dao->N, 1, '1 record should be found in smart group');
544
545 // Load the Manage Group page code and we should get a count from our
546 // group because the cache is fresh.
547 $_GET = $this->_params;
548 $obj = new CRM_Group_Page_AJAX();
549 $groups = $obj->getGroupList();
550
551 // Make sure we returned our smart group and ensure the count is accurate.
552 $found = FALSE;
553 $right_count = FALSE;
554 foreach ($groups['data'] as $returned_group) {
555 if ($returned_group['group_id'] == $group->id) {
556 $found = TRUE;
557 if ($returned_group['count'] == 1) {
558 $right_count = TRUE;
559 }
560 }
561 }
562 $this->assertTrue($found, 'Smart group shows up on Manage Group page.');
563 $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.');
564
565 // Purge the group contact cache.
566 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($group->id);
567
568 // Load the Manage Group page code.
569 $_GET = $this->_params;
570 $obj = new CRM_Group_Page_AJAX();
571 $groups = $obj->getGroupList();
572
573 // Make sure the smart group reports unknown count.
574 $count_is_unknown = FALSE;
575 foreach ($groups['data'] as $returned_group) {
576 if ($returned_group['group_id'] == $group->id) {
577 if ($returned_group['count'] == ts('unknown')) {
578 $count_is_unknown = TRUE;
579 }
580 }
581 }
582 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
583
584 // Ensure we did not populate the cache.
585 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
586 $params = array(1 => array($group->id, 'Integer'));
587 $dao = CRM_Core_DAO::executeQuery($sql, $params);
588 $test = 'Group contact cache should not be populated on Manage Groups ' .
589 'when cache_date is null';
590 $this->assertEquals($dao->N, 0, $test);
591
592 // Do it again, but this time don't clear group contact cache. Instead,
593 // set it to expire.
594 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
595 $params['name'] = 'smartGroupCacheTimeout';
596 $timeout = civicrm_api3('Setting', 'getvalue', $params);
597 $timeout = intval($timeout) * 60;
598 // Reset the cache_date to $timeout seconds ago minus another 60
599 // seconds for good measure.
600 $cache_date = date('YmdHis', time() - $timeout - 60);
601
602 $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2";
603 $update_params = array(
604 1 => array($cache_date, 'Timestamp'),
605 2 => array($group->id, 'Integer'),
606 );
607 CRM_Core_DAO::executeQuery($sql, $update_params);
608
609 // Load the Manage Group page code.
610 $_GET = $this->_params;
611 $obj = new CRM_Group_Page_AJAX();
612 $groups = $obj->getGroupList();
613
614 // Ensure we did not regenerate the cache.
615 $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' .
616 'FROM civicrm_group WHERE id = %1';
617 $params = array(1 => array($group->id, 'Integer'));
618 $dao = CRM_Core_DAO::executeQuery($sql, $params);
619 $dao->fetch();
620 $test = 'Group contact cache should not be re-populated on Manage Groups ' .
621 'when cache_date has expired';
622 $this->assertEquals($dao->cache_date, $cache_date, $test);
623 }
624
625 /**
626 * Implements ACLGroup hook.
627 * aclGroup function returns a list of permitted groups
628 * @param string $type
629 * @param int $contactID
630 * @param string $tableName
631 * @param array $allGroups
632 * @param array $currentGroups
633 */
634 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
635 //don't use api - you will get a loop
636 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
637 $groups = array();
638 $dao = CRM_Core_DAO::executeQuery($sql);
639 while ($dao->fetch()) {
640 $groups[] = $dao->id;
641 }
642
643 if (!empty($allGroups)) {
644 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
645 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
646 $currentGroups = array_intersect($groups, array_flip($allGroups));
647 }
648 else {
649 $currentGroups = $groups;
650 }
651 }
652
653 }