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