Remove instances of fatal
[civicrm-core.git] / tests / phpunit / CRM / Group / Page / AjaxTest.php
CommitLineData
3aab90bd 1<?php
aba1cd8b
EM
2
3/**
4 * Class CRM_Group_Page_AjaxTest
acb109b7 5 * @group headless
aba1cd8b 6 */
3aab90bd 7class 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
9f266042 10 * logged in user to see contacts.
11 *
12 * @var int
3aab90bd 13 */
14 protected $_permissionedGroup;
15 /**
eceb18cc 16 * AS disabled group the contact has permission to.
33e61cb8 17 * @var int
3aab90bd 18 */
19 protected $_permissionedDisabledGroup;
20
33e61cb8
CW
21 /**
22 * @var CRM_Utils_Hook_UnitTests
23 */
428f5595 24 public $hookClass;
5c2ad8fb 25
9099cab3 26 protected $_params = [];
aaac0e0b 27
00be9182 28 public function setUp() {
3aab90bd 29 parent::setUp();
9099cab3 30 $this->_params = [
3aab90bd 31 'page' => 1,
32 'rp' => 50,
33 'offset' => 0,
34 'rowCount' => 50,
35 'sort' => NULL,
d449ee28 36 'parentsOnly' => FALSE,
3aab90bd 37 'is_unit_test' => TRUE,
9099cab3 38 ];
3aab90bd 39 $this->hookClass = CRM_Utils_Hook::singleton();
40 $this->createLoggedInUser();
9099cab3 41 $this->_permissionedDisabledGroup = $this->groupCreate([
39b959db
SL
42 'title' => 'pick-me-disabled',
43 'is_active' => 0,
44 'name' => 'pick-me-disabled',
9099cab3
CW
45 ]);
46 $this->_permissionedGroup = $this->groupCreate([
39b959db
SL
47 'title' => 'pick-me-active',
48 'is_active' => 1,
49 'name' => 'pick-me-active',
9099cab3
CW
50 ]);
51 $this->groupCreate(['title' => 'not-me-disabled', 'is_active' => 0, 'name' => 'not-me-disabled']);
52 $this->groupCreate(['title' => 'not-me-active', 'is_active' => 1, 'name' => 'not-me-active']);
3aab90bd 53 }
54
00be9182 55 public function tearDown() {
3aab90bd 56 CRM_Utils_Hook::singleton()->reset();
9099cab3 57 $this->quickCleanup(['civicrm_group']);
3aab90bd 58 $config = CRM_Core_Config::singleton();
59 unset($config->userPermissionClass->permissions);
60 parent::tearDown();
61 }
62
63 /**
aaac0e0b 64 * @param $permission
3aab90bd 65 */
00be9182 66 public function setPermissionAndRequest($permission) {
bf15c191 67 $this->setPermissions((array) $permission);
f90c40b9 68 global $_REQUEST;
69 $_REQUEST = $this->_params;
aaac0e0b
EM
70 }
71
72 /**
73 * @param $permission
74 * @param $hook
75 */
00be9182 76 public function setHookAndRequest($permission, $hook) {
5c62295c 77 CRM_Core_Config::singleton()->userPermissionClass->permissions = (array) $permission;
9099cab3 78 $this->hookClass->setHook('civicrm_aclGroup', [$this, $hook]);
f90c40b9 79 global $_REQUEST;
80 $_REQUEST = $this->_params;
aaac0e0b 81 }
92915c55 82
46d33c7a 83 /**
84 * CRM-18528 - Retrieve groups with filter
85 */
86 public function testGroupListWithFilter() {
9099cab3 87 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
05ea397b 88
89 $_GET = $this->_params;
46d33c7a 90 $obj = new CRM_Group_Page_AJAX();
91
92 //filter with title
05ea397b 93 $_GET['title'] = "not-me-active";
46d33c7a 94 $groups = $obj->getGroupList();
95 $this->assertEquals(1, $groups['recordsTotal']);
05ea397b 96 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
46d33c7a 97 unset($_GET['title']);
98
99 // check on status
100 $_GET['status'] = 2;
101 $groups = $obj->getGroupList();
338e8324 102 foreach ($groups['data'] as $key => $val) {
103 $this->assertEquals('crm-entity disabled', $val['DT_RowClass']);
104 }
46d33c7a 105 }
106
aaac0e0b 107 /**
100fef9d 108 * Retrieve groups as 'view all contacts'
aaac0e0b 109 */
00be9182 110 public function testGroupListViewAllContacts() {
9099cab3 111 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
5bf58d02
JL
112 $params = $this->_params;
113 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
114 $this->assertEquals(2, $groups['recordsTotal']);
00fb62ab
JL
115 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
116 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
958e4503 117 }
118
119 /**
120 * Check Group Edit w/o 'edit groups' permission.
00fb62ab
JL
121 *
122 * FIXME permissions to edit groups can only be determined by the links, which is ridiculously long
958e4503 123 */
124 public function testGroupEditWithAndWithoutPermission() {
aaac0e0b 125 $this->setPermissionAndRequest('view all contacts');
f0577026
JL
126 $params = $this->_params;
127 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
128 $this->assertEquals(2, $groups['recordsTotal']);
fecb40f5
MW
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']);
958e4503 131
132 // as per changes made in PR-6822
9099cab3 133 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
134 $params = $this->_params;
135 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
136 $this->assertEquals(2, $groups['recordsTotal']);
fecb40f5
MW
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']);
3aab90bd 139 }
140
141 /**
100fef9d 142 * Retrieve groups as 'view all contacts' permissioned user
3aab90bd 143 * Without setting params the default is both enabled & disabled
144 * (if you do set default it is enabled only)
145 */
00be9182 146 public function testGroupListViewAllContactsFoundTitle() {
aaac0e0b 147 $this->_params['title'] = 'p';
9099cab3 148 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
149 $params = $this->_params;
150 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
151 $this->assertEquals(2, $groups['recordsTotal']);
00fb62ab
JL
152 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
153 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 154 }
155
156 /**
100fef9d 157 * Retrieve groups as 'view all contacts'
3aab90bd 158 */
00be9182 159 public function testGroupListViewAllContactsNotFoundTitle() {
aaac0e0b
EM
160 $this->_params['title'] = 'z';
161 $this->setPermissionAndRequest('view all contacts');
f0577026
JL
162 $params = $this->_params;
163 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
164 $this->assertEquals(0, $groups['recordsTotal']);
3aab90bd 165 }
92915c55 166
3aab90bd 167 /**
100fef9d 168 * Retrieve groups as 'edit all contacts'
3aab90bd 169 */
00be9182 170 public function testGroupListEditAllContacts() {
9099cab3 171 $this->setPermissionAndRequest(['edit all contacts', 'edit groups']);
f0577026
JL
172 $params = $this->_params;
173 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
174 $this->assertEquals(2, $groups['recordsTotal']);
00fb62ab
JL
175 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
176 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
3aab90bd 177 }
178
179 /**
100fef9d 180 * Retrieve groups as 'view all contacts'
3aab90bd 181 */
00be9182 182 public function testGroupListViewAllContactsEnabled() {
aaac0e0b 183 $this->_params['status'] = 1;
9099cab3 184 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
185 $params = $this->_params;
186 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
187 $this->assertEquals(2, $groups['recordsTotal']);
00fb62ab
JL
188 $this->assertEquals('not-me-active', $groups['data'][0]['title']);
189 $this->assertEquals('pick-me-active', $groups['data'][1]['title']);
3aab90bd 190 }
191
192 /**
100fef9d 193 * Retrieve groups as 'view all contacts'
3aab90bd 194 */
00be9182 195 public function testGroupListViewAllContactsDisabled() {
aaac0e0b 196 $this->_params['status'] = 2;
9099cab3 197 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
198 $params = $this->_params;
199 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
200 $this->assertEquals(2, $groups['recordsTotal']);
00fb62ab
JL
201 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
202 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 203 }
204
205 /**
100fef9d 206 * Retrieve groups as 'view all contacts'
3aab90bd 207 */
00be9182 208 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
aaac0e0b
EM
209 $this->_params['status'] = 2;
210 $this->_params['title'] = 'n';
9099cab3 211 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
212 $params = $this->_params;
213 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
214 $this->assertEquals(1, $groups['recordsTotal']);
00fb62ab 215 $this->assertEquals('not-me-disabled', $groups['data'][0]['title']);
3aab90bd 216 }
217
218 /**
100fef9d 219 * Retrieve groups as 'view all contacts'
3aab90bd 220 */
00be9182 221 public function testGroupListViewAllContactsDisabledFoundTitle() {
aaac0e0b
EM
222 $this->_params['status'] = 2;
223 $this->_params['title'] = 'p';
9099cab3 224 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
225 $params = $this->_params;
226 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
227 $this->assertEquals(1, $groups['recordsTotal']);
00fb62ab 228 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
3aab90bd 229 }
230
231 /**
100fef9d 232 * Retrieve groups as 'view all contacts'
3aab90bd 233 */
00be9182 234 public function testGroupListViewAllContactsAll() {
aaac0e0b 235 $this->_params['status'] = 3;
9099cab3 236 $this->setPermissionAndRequest(['view all contacts', 'edit groups']);
f0577026
JL
237 $params = $this->_params;
238 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
239 $this->assertEquals(4, $groups['recordsTotal']);
00fb62ab
JL
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']);
3aab90bd 244 }
245
3aab90bd 246 /**
100fef9d 247 * Retrieve groups as 'view all contacts'
3aab90bd 248 */
00be9182 249 public function testGroupListAccessCiviCRM() {
aaac0e0b 250 $this->setPermissionAndRequest('access CiviCRM');
bc95ea0d 251 $permissionClause = CRM_Contact_BAO_Group::getPermissionClause();
aaac0e0b 252 $this->assertEquals('1 = 0', $permissionClause);
f0577026
JL
253 $params = $this->_params;
254 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 255 $this->assertEquals(0, count($groups['data']));
f0577026 256 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 257 }
92915c55 258
3aab90bd 259 /**
100fef9d 260 * Retrieve groups as 'view all contacts'
3aab90bd 261 */
00be9182 262 public function testGroupListAccessCiviCRMEnabled() {
aaac0e0b
EM
263 $this->_params['status'] = 1;
264 $this->setPermissionAndRequest('access CiviCRM');
f0577026
JL
265 $params = $this->_params;
266 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 267 $this->assertEquals(0, count($groups['data']));
f0577026 268 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 269 }
92915c55 270
3aab90bd 271 /**
100fef9d 272 * Retrieve groups as 'view all contacts'
3aab90bd 273 */
00be9182 274 public function testGroupListAccessCiviCRMDisabled() {
aaac0e0b
EM
275 $this->_params['status'] = 2;
276 $this->setPermissionAndRequest('access CiviCRM');
f0577026
JL
277 $params = $this->_params;
278 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 279 $this->assertEquals(0, count($groups['data']));
f0577026 280 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 281 }
282
283 /**
100fef9d 284 * Retrieve groups as 'view all contacts'
3aab90bd 285 */
00be9182 286 public function testGroupListAccessCiviCRMAll() {
aaac0e0b
EM
287 $this->_params['status'] = 2;
288 $this->setPermissionAndRequest('access CiviCRM');
f0577026
JL
289 $params = $this->_params;
290 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 291 $this->assertEquals(0, count($groups['data']));
f0577026 292 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 293 }
294
295 /**
100fef9d 296 * Retrieve groups as 'view all contacts'
3aab90bd 297 */
00be9182 298 public function testGroupListAccessCiviCRMFound() {
aaac0e0b
EM
299 $this->_params['title'] = 'p';
300 $this->setPermissionAndRequest('access CiviCRM');
f0577026
JL
301 $params = $this->_params;
302 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 303 $this->assertEquals(0, count($groups['data']));
f0577026 304 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 305 }
306
307 /**
100fef9d 308 * Retrieve groups as 'view all contacts'
3aab90bd 309 */
00be9182 310 public function testGroupListAccessCiviCRMNotFound() {
aaac0e0b
EM
311 $this->_params['title'] = 'z';
312 $this->setPermissionAndRequest('access CiviCRM');
f0577026
JL
313 $params = $this->_params;
314 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 315 $this->assertEquals(0, count($groups['data']));
f0577026 316 $this->assertEquals(0, $groups['recordsTotal'], 'Total returned should be accurate based on permissions');
3aab90bd 317 }
318
92915c55 319 public function testTraditionalACL() {
3aab90bd 320 $this->setupACL();
958e4503 321 $this->setPermissionAndRequest('edit groups');
f0577026
JL
322 $params = $this->_params;
323 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 324 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 325 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 326 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
3aab90bd 327 }
328
92915c55 329 public function testTraditionalACLNotFoundTitle() {
aaac0e0b 330 $this->_params['title'] = 'n';
3aab90bd 331 $this->setupACL();
f0577026
JL
332 $params = $this->_params;
333 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 334 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 335 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
3aab90bd 336 }
337
92915c55 338 public function testTraditionalACLFoundTitle() {
aaac0e0b 339 $this->_params['title'] = 'p';
3aab90bd 340 $this->setupACL();
958e4503 341 $this->setPermissionAndRequest('edit groups');
f0577026
JL
342 $params = $this->_params;
343 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 344 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 345 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab
JL
346 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
347 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 348 }
349
92915c55 350 public function testTraditionalACLDisabled() {
aaac0e0b 351 $this->_params['status'] = 2;
3aab90bd 352 $this->setupACL();
958e4503 353 $this->setPermissionAndRequest('edit groups');
f0577026
JL
354 $params = $this->_params;
355 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 356 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 357 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 358 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
3aab90bd 359 }
360
92915c55 361 public function testTraditionalACLDisabledFoundTitle() {
aaac0e0b
EM
362 $this->_params['status'] = 2;
363 $this->_params['title'] = 'p';
3aab90bd 364 $this->setupACL();
958e4503 365 $this->setPermissionAndRequest('edit groups');
f0577026
JL
366 $params = $this->_params;
367 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 368 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 369 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 370 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
3aab90bd 371 }
372
92915c55 373 public function testTraditionalACLDisabledNotFoundTitle() {
aaac0e0b
EM
374 $this->_params['status'] = 2;
375 $this->_params['title'] = 'n';
3aab90bd 376 $this->setupACL();
f0577026
JL
377 $params = $this->_params;
378 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 379 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 380 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
3aab90bd 381 }
382
92915c55 383 public function testTraditionalACLEnabled() {
aaac0e0b 384 $this->_params['status'] = 1;
3aab90bd 385 $this->setupACL();
958e4503 386 $this->setPermissionAndRequest('edit groups');
f0577026
JL
387 $params = $this->_params;
388 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 389 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 390 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 391 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
3aab90bd 392 }
393
92915c55 394 public function testTraditionalACLAll() {
aaac0e0b 395 $this->_params['status'] = 3;
3aab90bd 396 $this->setupACL();
958e4503 397 $this->setPermissionAndRequest('edit groups');
f0577026
JL
398 $params = $this->_params;
399 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 400 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 401 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab
JL
402 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
403 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 404 }
405
406 /**
eceb18cc 407 * ACL Group hook.
3aab90bd 408 */
00be9182 409 public function testGroupListAclGroupHookDisabled() {
aaac0e0b 410 $this->_params['status'] = 2;
9099cab3 411 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
f0577026
JL
412 $params = $this->_params;
413 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 414 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 415 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 416 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
3aab90bd 417 }
418
419 /**
eceb18cc 420 * ACL Group hook.
3aab90bd 421 */
00be9182 422 public function testGroupListAclGroupHookDisabledFound() {
aaac0e0b
EM
423 $this->_params['status'] = 2;
424 $this->_params['title'] = 'p';
9099cab3 425 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
f0577026
JL
426 $params = $this->_params;
427 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 428 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 429 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 430 $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']);
3aab90bd 431 }
432
433 /**
eceb18cc 434 * ACL Group hook.
3aab90bd 435 */
00be9182 436 public function testGroupListAclGroupHookDisabledNotFound() {
aaac0e0b
EM
437 $this->_params['status'] = 2;
438 $this->_params['title'] = 'n';
439 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
f0577026
JL
440 $params = $this->_params;
441 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
d449ee28 442 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 443 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
3aab90bd 444 }
445
3aab90bd 446 /**
eceb18cc 447 * ACL Group hook.
3aab90bd 448 */
00be9182 449 public function testGroupListAclGroupHook() {
9099cab3 450 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
f0577026
JL
451 $params = $this->_params;
452 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 453 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 454 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 455 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
3aab90bd 456 }
457
458 /**
eceb18cc 459 * ACL Group hook.
3aab90bd 460 */
00be9182 461 public function testGroupListAclGroupHookTitleNotFound() {
aaac0e0b
EM
462 $this->_params['title'] = 'n';
463 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
f0577026 464 $params = $this->_params;
00fb62ab
JL
465 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
466 $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 467 $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly');
3aab90bd 468 }
469
470 /**
eceb18cc 471 * ACL Group hook.
3aab90bd 472 */
00be9182 473 public function testGroupListAclGroupHookTitleFound() {
aaac0e0b 474 $this->_params['title'] = 'p';
9099cab3 475 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
00fb62ab 476 $params = $this->_params;
f0577026 477 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 478 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 479 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab
JL
480 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
481 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 482 }
483
484 /**
eceb18cc 485 * ACL Group hook.
3aab90bd 486 */
00be9182 487 public function testGroupListAclGroupHookAll() {
aaac0e0b 488 $this->_params['status'] = 3;
9099cab3 489 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
f0577026
JL
490 $params = $this->_params;
491 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 492 $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 493 $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab
JL
494 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
495 $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']);
3aab90bd 496 }
497
498 /**
eceb18cc 499 * ACL Group hook.
3aab90bd 500 */
00be9182 501 public function testGroupListAclGroupHookEnabled() {
aaac0e0b 502 $this->_params['status'] = 1;
9099cab3 503 $this->setHookAndRequest(['access CiviCRM', 'edit groups'], 'hook_civicrm_aclGroup');
f0577026
JL
504 $params = $this->_params;
505 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
00fb62ab 506 $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default');
f0577026 507 $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly');
00fb62ab 508 $this->assertEquals('pick-me-active', $groups['data'][0]['title']);
3aab90bd 509 }
510
a19f5834
JM
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';
9099cab3 520 $params = [
a19f5834
JM
521 'first_name' => $firstName,
522 'last_name' => $lastName,
523 'contact_type' => 'Individual',
9099cab3 524 ];
a19f5834
JM
525 $contact = CRM_Contact_BAO_Contact::add($params);
526
527 // Create a smart group.
9099cab3 528 $searchParams = [
a19f5834 529 'last_name' => $lastName,
9099cab3
CW
530 ];
531 $groupParams = ['title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1];
a19f5834
JM
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';
9099cab3 540 $params = [1 => [$group->id, 'Integer']];
a19f5834
JM
541 $dao = CRM_Core_DAO::executeQuery($sql, $params);
542 $this->assertEquals($dao->N, 1, '1 record should be found in smart group');
543
b804448d
JM
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
a19f5834
JM
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
b804448d
JM
572 // Make sure the smart group reports unknown count.
573 $count_is_unknown = FALSE;
a19f5834
JM
574 foreach ($groups['data'] as $returned_group) {
575 if ($returned_group['group_id'] == $group->id) {
b804448d
JM
576 if ($returned_group['count'] == ts('unknown')) {
577 $count_is_unknown = TRUE;
578 }
a19f5834
JM
579 }
580 }
b804448d 581 $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.');
a19f5834
JM
582
583 // Ensure we did not populate the cache.
584 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
9099cab3 585 $params = [1 => [$group->id, 'Integer']];
a19f5834 586 $dao = CRM_Core_DAO::executeQuery($sql, $params);
01ef4db9
JM
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";
9099cab3
CW
602 $update_params = [
603 1 => [$cache_date, 'Timestamp'],
604 2 => [$group->id, 'Integer'],
605 ];
01ef4db9
JM
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';
9099cab3 616 $params = [1 => [$group->id, 'Integer']];
01ef4db9
JM
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);
a19f5834
JM
622 }
623
3aab90bd 624 /**
eceb18cc 625 * Implements ACLGroup hook.
6c6e6187
TO
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 */
00be9182 633 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
aaac0e0b 634 //don't use api - you will get a loop
3aab90bd 635 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
9099cab3 636 $groups = [];
3aab90bd 637 $dao = CRM_Core_DAO::executeQuery($sql);
638 while ($dao->fetch()) {
639 $groups[] = $dao->id;
640 }
641
22e263ad 642 if (!empty($allGroups)) {
3aab90bd 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 }
96025800 651
3b777f60
JG
652 public function testEditAllGroupsACL() {
653 $this->setupEditAllGroupsACL();
654 $params = $this->_params;
655 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
3b777f60
JG
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
9099cab3
CW
670 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
671 $optionGroupID = $this->callAPISuccessGetValue('option_group', ['return' => 'id', 'name' => 'acl_role']);
3b777f60
JG
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 }
9099cab3 678 $optionValue = $this->callAPISuccess('option_value', 'create', [
3b777f60
JG
679 'option_group_id' => $optionGroupID,
680 'label' => 'groupmaster',
681 'value' => 55,
9099cab3 682 ]);
3b777f60
JG
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');
9099cab3 692 $this->callAPISuccess('group_contact', 'create', [
3b777f60
JG
693 'group_id' => $groupId,
694 'contact_id' => $this->_loggedInUser,
9099cab3 695 ]);
3b777f60
JG
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
3aab90bd 708}