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