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