Merge pull request #7244 from systopia/CRM-17589
[civicrm-core.git] / tests / phpunit / CRM / Group / Page / AjaxTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3
4 /**
5 * Class CRM_Group_Page_AjaxTest
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 'sEcho' => '1',
31 'page' => 1,
32 'rp' => 50,
33 'offset' => 0,
34 'rowCount' => 50,
35 'sort' => NULL,
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 CRM_Core_Config::singleton()->userPermissionClass->permissions = (array) $permission;
67 CRM_Contact_BAO_Group::getPermissionClause(TRUE);
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 CRM_Contact_BAO_Group::getPermissionClause(TRUE);
80 global $_REQUEST;
81 $_REQUEST = $this->_params;
82 }
83
84 /**
85 * Retrieve groups as 'view all contacts'
86 */
87 public function testGroupListViewAllContacts() {
88 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
89 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
90 $this->assertEquals(2, $total);
91 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
92 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
93 }
94
95 /**
96 * Check Group Edit w/o 'edit groups' permission.
97 */
98 public function testGroupEditWithAndWithoutPermission() {
99 $this->setPermissionAndRequest('view all contacts');
100 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
101 $this->assertEquals(2, $total);
102 $this->assertEquals('pick-me-active', $groups[2]['group_name']);
103 $this->assertEquals('not-me-active', $groups[4]['group_name']);
104
105 // as per changes made in PR-6822
106 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
107 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
108 $this->assertEquals(2, $total);
109 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
110 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
111 }
112
113 /**
114 * Retrieve groups as 'view all contacts' permissioned user
115 * Without setting params the default is both enabled & disabled
116 * (if you do set default it is enabled only)
117 */
118 public function testGroupListViewAllContactsFoundTitle() {
119 $this->_params['title'] = 'p';
120 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
121 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
122 $this->assertEquals(2, $total);
123 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
124 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
125 }
126
127 /**
128 * Retrieve groups as 'view all contacts'
129 */
130 public function testGroupListViewAllContactsNotFoundTitle() {
131 $this->_params['title'] = 'z';
132 $this->setPermissionAndRequest('view all contacts');
133 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
134 $this->assertEquals(0, $total);
135 }
136
137 /**
138 * Retrieve groups as 'edit all contacts'
139 */
140 public function testGroupListEditAllContacts() {
141 $this->setPermissionAndRequest(array('edit all contacts', 'edit groups'));
142 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
143 $this->assertEquals(2, $total);
144 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
145 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
146 }
147
148 /**
149 * Retrieve groups as 'view all contacts'
150 */
151 public function testGroupListViewAllContactsEnabled() {
152 $this->_params['status'] = 1;
153 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
154 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
155 $this->assertEquals(2, $total);
156 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
157 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
158 }
159
160 /**
161 * Retrieve groups as 'view all contacts'
162 */
163 public function testGroupListViewAllContactsDisabled() {
164 $this->_params['status'] = 2;
165 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
166 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
167 $this->assertEquals(2, $total);
168 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
169 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
170 }
171
172 /**
173 * Retrieve groups as 'view all contacts'
174 */
175 public function testGroupListViewAllContactsDisabledNotFoundTitle() {
176 $this->_params['status'] = 2;
177 $this->_params['title'] = 'n';
178 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
179 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
180 $this->assertEquals(1, $total);
181 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
182 }
183
184 /**
185 * Retrieve groups as 'view all contacts'
186 */
187 public function testGroupListViewAllContactsDisabledFoundTitle() {
188 $this->_params['status'] = 2;
189 $this->_params['title'] = 'p';
190 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
191 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
192 $this->assertEquals(1, $total);
193 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
194 }
195
196 /**
197 * Retrieve groups as 'view all contacts'
198 */
199 public function testGroupListViewAllContactsAll() {
200 $this->_params['status'] = 3;
201 $this->setPermissionAndRequest(array('view all contacts', 'edit groups'));
202 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
203 $this->assertEquals(4, $total);
204 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
205 $this->assertEquals('<span class="crm-editable crmf-title">not-me-disabled</span>', $groups[3]['group_name']);
206 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
207 $this->assertEquals('<span class="crm-editable crmf-title">not-me-active</span>', $groups[4]['group_name']);
208 }
209
210
211 /**
212 * Retrieve groups as 'view all contacts'
213 */
214 public function testGroupListAccessCiviCRM() {
215 $this->setPermissionAndRequest('access CiviCRM');
216 $permissionClause = CRM_Contact_BAO_Group::getPermissionClause(TRUE);
217 $this->assertEquals('1 = 0', $permissionClause);
218 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
219 $this->assertEquals(0, count($groups));
220 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
221 }
222
223 /**
224 * Retrieve groups as 'view all contacts'
225 */
226 public function testGroupListAccessCiviCRMEnabled() {
227 $this->_params['status'] = 1;
228 $this->setPermissionAndRequest('access CiviCRM');
229 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
230 $this->assertEquals(0, count($groups));
231 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
232 }
233
234 /**
235 * Retrieve groups as 'view all contacts'
236 */
237 public function testGroupListAccessCiviCRMDisabled() {
238 $this->_params['status'] = 2;
239 $this->setPermissionAndRequest('access CiviCRM');
240 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
241 $this->assertEquals(0, count($groups));
242 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
243 }
244
245 /**
246 * Retrieve groups as 'view all contacts'
247 */
248 public function testGroupListAccessCiviCRMAll() {
249 $this->_params['status'] = 2;
250 $this->setPermissionAndRequest('access CiviCRM');
251 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
252 $this->assertEquals(0, count($groups));
253 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
254 }
255
256 /**
257 * Retrieve groups as 'view all contacts'
258 */
259 public function testGroupListAccessCiviCRMFound() {
260 $this->_params['title'] = 'p';
261 $this->setPermissionAndRequest('access CiviCRM');
262 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
263 $this->assertEquals(0, count($groups));
264 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
265 }
266
267 /**
268 * Retrieve groups as 'view all contacts'
269 */
270 public function testGroupListAccessCiviCRMNotFound() {
271 $this->_params['title'] = 'z';
272 $this->setPermissionAndRequest('access CiviCRM');
273 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
274 $this->assertEquals(0, count($groups));
275 $this->assertEquals(0, $total, 'Total returned should be accurate based on permissions');
276 }
277
278 public function testTraditionalACL() {
279 $this->setupACL();
280 $this->setPermissionAndRequest('edit groups');
281 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
282 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
283 $this->assertEquals(1, $total, 'Total needs to be set correctly');
284 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
285 }
286
287 public function testTraditionalACLNotFoundTitle() {
288 $this->_params['title'] = 'n';
289 $this->setupACL();
290 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
291 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
292 $this->assertEquals(0, $total, 'Total needs to be set correctly');
293 }
294
295 public function testTraditionalACLFoundTitle() {
296 $this->_params['title'] = 'p';
297 $this->setupACL();
298 $this->setPermissionAndRequest('edit groups');
299 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
300 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
301 $this->assertEquals(2, $total, 'Total needs to be set correctly');
302 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
303 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
304 }
305
306 public function testTraditionalACLDisabled() {
307 $this->_params['status'] = 2;
308 $this->setupACL();
309 $this->setPermissionAndRequest('edit groups');
310 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
311 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
312 $this->assertEquals(1, $total, 'Total needs to be set correctly');
313 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
314 }
315
316 public function testTraditionalACLDisabledFoundTitle() {
317 $this->_params['status'] = 2;
318 $this->_params['title'] = 'p';
319 $this->setupACL();
320 $this->setPermissionAndRequest('edit groups');
321 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
322 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
323 $this->assertEquals(1, $total, 'Total needs to be set correctly');
324 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
325 }
326
327 public function testTraditionalACLDisabledNotFoundTitle() {
328 $this->_params['status'] = 2;
329 $this->_params['title'] = 'n';
330 $this->setupACL();
331 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
332 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
333 $this->assertEquals(0, $total, 'Total needs to be set correctly');
334 }
335
336 public function testTraditionalACLEnabled() {
337 $this->_params['status'] = 1;
338 $this->setupACL();
339 $this->setPermissionAndRequest('edit groups');
340 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
341 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
342 $this->assertEquals(1, $total, 'Total needs to be set correctly');
343 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
344 }
345
346 public function testTraditionalACLAll() {
347 $this->_params['status'] = 3;
348 $this->setupACL();
349 $this->setPermissionAndRequest('edit groups');
350 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
351 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
352 $this->assertEquals(2, $total, 'Total needs to be set correctly');
353 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
354 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
355 }
356
357 /**
358 * ACL Group hook.
359 */
360 public function testGroupListAclGroupHookDisabled() {
361 $this->_params['status'] = 2;
362 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
363 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
364 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
365 $this->assertEquals(1, $total, 'Total needs to be set correctly');
366 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
367 }
368
369 /**
370 * ACL Group hook.
371 */
372 public function testGroupListAclGroupHookDisabledFound() {
373 $this->_params['status'] = 2;
374 $this->_params['title'] = 'p';
375 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
376 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
377 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
378 $this->assertEquals(1, $total, 'Total needs to be set correctly');
379 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
380 }
381
382 /**
383 * ACL Group hook.
384 */
385 public function testGroupListAclGroupHookDisabledNotFound() {
386 $this->_params['status'] = 2;
387 $this->_params['title'] = 'n';
388 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
389 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
390 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
391 $this->assertEquals(0, $total, 'Total needs to be set correctly');
392 }
393
394
395 /**
396 * ACL Group hook.
397 */
398 public function testGroupListAclGroupHook() {
399 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
400 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
401 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
402 $this->assertEquals(1, $total, 'Total needs to be set correctly');
403 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
404 }
405
406 /**
407 * ACL Group hook.
408 */
409 public function testGroupListAclGroupHookTitleNotFound() {
410 $this->_params['title'] = 'n';
411 $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup');
412 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
413 $this->assertEquals(0, $total, 'Total needs to be set correctly');
414 $this->assertEquals(0, count($groups), 'Returned groups should exclude disabled by default');
415 }
416
417 /**
418 * ACL Group hook.
419 */
420 public function testGroupListAclGroupHookTitleFound() {
421 $this->_params['title'] = 'p';
422 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
423 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
424 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
425 $this->assertEquals(2, $total, 'Total needs to be set correctly');
426 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
427 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
428 }
429
430 /**
431 * ACL Group hook.
432 */
433 public function testGroupListAclGroupHookAll() {
434 $this->_params['status'] = 3;
435 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
436 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
437 $this->assertEquals(2, count($groups), 'Returned groups should exclude disabled by default');
438 $this->assertEquals(2, $total, 'Total needs to be set correctly');
439 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
440 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-disabled</span>', $groups[1]['group_name']);
441 }
442
443 /**
444 * ACL Group hook.
445 */
446 public function testGroupListAclGroupHookEnabled() {
447 $this->_params['status'] = 1;
448 $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup');
449 list($groups, $total) = CRM_Group_Page_AJAX::getGroupList();
450 $this->assertEquals(1, count($groups), 'Returned groups should exclude disabled by default');
451 $this->assertEquals(1, $total, 'Total needs to be set correctly');
452 $this->assertEquals('<span class="crm-editable crmf-title">pick-me-active</span>', $groups[2]['group_name']);
453 }
454
455 /**
456 * Implements ACLGroup hook.
457 * aclGroup function returns a list of permitted groups
458 * @param string $type
459 * @param int $contactID
460 * @param string $tableName
461 * @param array $allGroups
462 * @param array $currentGroups
463 */
464 public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) {
465 //don't use api - you will get a loop
466 $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'";
467 $groups = array();
468 $dao = CRM_Core_DAO::executeQuery($sql);
469 while ($dao->fetch()) {
470 $groups[] = $dao->id;
471 }
472
473 if (!empty($allGroups)) {
474 //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied
475 // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list
476 $currentGroups = array_intersect($groups, array_flip($allGroups));
477 }
478 else {
479 $currentGroups = $groups;
480 }
481 }
482
483 }