Commit | Line | Data |
---|---|---|
3aab90bd | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Group_Page_AjaxTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
3aab90bd | 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 | /** | |
eceb18cc | 15 | * AS disabled group the contact has permission to. |
33e61cb8 | 16 | * @var int |
3aab90bd | 17 | */ |
18 | protected $_permissionedDisabledGroup; | |
19 | ||
33e61cb8 CW |
20 | /** |
21 | * @var CRM_Utils_Hook_UnitTests | |
22 | */ | |
428f5595 | 23 | public $hookClass; |
5c2ad8fb | 24 | |
aaac0e0b EM |
25 | protected $_params = array(); |
26 | ||
00be9182 | 27 | public function setUp() { |
3aab90bd | 28 | parent::setUp(); |
29 | $this->_params = array( | |
3aab90bd | 30 | 'page' => 1, |
31 | 'rp' => 50, | |
32 | 'offset' => 0, | |
33 | 'rowCount' => 50, | |
34 | 'sort' => NULL, | |
d449ee28 | 35 | 'parentsOnly' => FALSE, |
3aab90bd | 36 | 'is_unit_test' => TRUE, |
37 | ); | |
38 | $this->hookClass = CRM_Utils_Hook::singleton(); | |
39 | $this->createLoggedInUser(); | |
92915c55 TO |
40 | $this->_permissionedDisabledGroup = $this->groupCreate(array( |
41 | 'title' => 'pick-me-disabled', | |
42 | 'is_active' => 0, | |
ae5ffbb7 | 43 | 'name' => 'pick-me-disabled', |
92915c55 TO |
44 | )); |
45 | $this->_permissionedGroup = $this->groupCreate(array( | |
46 | 'title' => 'pick-me-active', | |
47 | 'is_active' => 1, | |
ae5ffbb7 | 48 | 'name' => 'pick-me-active', |
92915c55 | 49 | )); |
3aab90bd | 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 | ||
00be9182 | 54 | public function tearDown() { |
3aab90bd | 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 | /** | |
aaac0e0b | 63 | * @param $permission |
3aab90bd | 64 | */ |
00be9182 | 65 | public function setPermissionAndRequest($permission) { |
bf15c191 | 66 | $this->setPermissions((array) $permission); |
f90c40b9 | 67 | global $_REQUEST; |
68 | $_REQUEST = $this->_params; | |
aaac0e0b EM |
69 | } |
70 | ||
71 | /** | |
72 | * @param $permission | |
73 | * @param $hook | |
74 | */ | |
00be9182 | 75 | public function setHookAndRequest($permission, $hook) { |
5c62295c | 76 | CRM_Core_Config::singleton()->userPermissionClass->permissions = (array) $permission; |
aaac0e0b | 77 | $this->hookClass->setHook('civicrm_aclGroup', array($this, $hook)); |
f90c40b9 | 78 | global $_REQUEST; |
79 | $_REQUEST = $this->_params; | |
aaac0e0b | 80 | } |
92915c55 | 81 | |
46d33c7a | 82 | /** |
83 | * CRM-18528 - Retrieve groups with filter | |
84 | */ | |
85 | public function testGroupListWithFilter() { | |
05ea397b | 86 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
87 | ||
88 | $_GET = $this->_params; | |
46d33c7a | 89 | $obj = new CRM_Group_Page_AJAX(); |
90 | ||
91 | //filter with title | |
05ea397b | 92 | $_GET['title'] = "not-me-active"; |
46d33c7a | 93 | $groups = $obj->getGroupList(); |
94 | $this->assertEquals(1, $groups['recordsTotal']); | |
05ea397b | 95 | $this->assertEquals('not-me-active', $groups['data'][0]['title']); |
46d33c7a | 96 | unset($_GET['title']); |
97 | ||
98 | // check on status | |
99 | $_GET['status'] = 2; | |
100 | $groups = $obj->getGroupList(); | |
338e8324 | 101 | foreach ($groups['data'] as $key => $val) { |
102 | $this->assertEquals('crm-entity disabled', $val['DT_RowClass']); | |
103 | } | |
46d33c7a | 104 | } |
105 | ||
aaac0e0b | 106 | /** |
100fef9d | 107 | * Retrieve groups as 'view all contacts' |
aaac0e0b | 108 | */ |
00be9182 | 109 | public function testGroupListViewAllContacts() { |
958e4503 | 110 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
5bf58d02 JL |
111 | $params = $this->_params; |
112 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
113 | $this->assertEquals(2, $groups['recordsTotal']); | |
00fb62ab JL |
114 | $this->assertEquals('not-me-active', $groups['data'][0]['title']); |
115 | $this->assertEquals('pick-me-active', $groups['data'][1]['title']); | |
958e4503 | 116 | } |
117 | ||
118 | /** | |
119 | * Check Group Edit w/o 'edit groups' permission. | |
00fb62ab JL |
120 | * |
121 | * FIXME permissions to edit groups can only be determined by the links, which is ridiculously long | |
958e4503 | 122 | */ |
123 | public function testGroupEditWithAndWithoutPermission() { | |
aaac0e0b | 124 | $this->setPermissionAndRequest('view all contacts'); |
f0577026 JL |
125 | $params = $this->_params; |
126 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
127 | $this->assertEquals(2, $groups['recordsTotal']); | |
fecb40f5 MW |
128 | $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=4&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][0]['links']); |
129 | $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=2&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a></span>', $groups['data'][1]['links']); | |
958e4503 | 130 | |
131 | // as per changes made in PR-6822 | |
132 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); | |
f0577026 JL |
133 | $params = $this->_params; |
134 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
135 | $this->assertEquals(2, $groups['recordsTotal']); | |
fecb40f5 MW |
136 | $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=4&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&reset=1&action=update&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&reset=1&action=delete&id=4" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][0]['links']); |
137 | $this->assertEquals('<span><a href="/index.php?q=civicrm/group/search&reset=1&force=1&context=smog&gid=2&component_mode=1" class="action-item crm-hover-button" title=\'Group Contacts\' >Contacts</a><a href="/index.php?q=civicrm/group&reset=1&action=update&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&reset=1&action=delete&id=2" class="action-item crm-hover-button small-popup" title=\'Delete Group\' >Delete</a></li></ul></span>', $groups['data'][1]['links']); | |
3aab90bd | 138 | } |
139 | ||
140 | /** | |
100fef9d | 141 | * Retrieve groups as 'view all contacts' permissioned user |
3aab90bd | 142 | * Without setting params the default is both enabled & disabled |
143 | * (if you do set default it is enabled only) | |
144 | */ | |
00be9182 | 145 | public function testGroupListViewAllContactsFoundTitle() { |
aaac0e0b | 146 | $this->_params['title'] = 'p'; |
958e4503 | 147 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
148 | $params = $this->_params; |
149 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
150 | $this->assertEquals(2, $groups['recordsTotal']); | |
00fb62ab JL |
151 | $this->assertEquals('pick-me-active', $groups['data'][0]['title']); |
152 | $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']); | |
3aab90bd | 153 | } |
154 | ||
155 | /** | |
100fef9d | 156 | * Retrieve groups as 'view all contacts' |
3aab90bd | 157 | */ |
00be9182 | 158 | public function testGroupListViewAllContactsNotFoundTitle() { |
aaac0e0b EM |
159 | $this->_params['title'] = 'z'; |
160 | $this->setPermissionAndRequest('view all contacts'); | |
f0577026 JL |
161 | $params = $this->_params; |
162 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
163 | $this->assertEquals(0, $groups['recordsTotal']); | |
3aab90bd | 164 | } |
92915c55 | 165 | |
3aab90bd | 166 | /** |
100fef9d | 167 | * Retrieve groups as 'edit all contacts' |
3aab90bd | 168 | */ |
00be9182 | 169 | public function testGroupListEditAllContacts() { |
958e4503 | 170 | $this->setPermissionAndRequest(array('edit all contacts', 'edit groups')); |
f0577026 JL |
171 | $params = $this->_params; |
172 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
173 | $this->assertEquals(2, $groups['recordsTotal']); | |
00fb62ab JL |
174 | $this->assertEquals('not-me-active', $groups['data'][0]['title']); |
175 | $this->assertEquals('pick-me-active', $groups['data'][1]['title']); | |
3aab90bd | 176 | } |
177 | ||
178 | /** | |
100fef9d | 179 | * Retrieve groups as 'view all contacts' |
3aab90bd | 180 | */ |
00be9182 | 181 | public function testGroupListViewAllContactsEnabled() { |
aaac0e0b | 182 | $this->_params['status'] = 1; |
958e4503 | 183 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
184 | $params = $this->_params; |
185 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
186 | $this->assertEquals(2, $groups['recordsTotal']); | |
00fb62ab JL |
187 | $this->assertEquals('not-me-active', $groups['data'][0]['title']); |
188 | $this->assertEquals('pick-me-active', $groups['data'][1]['title']); | |
3aab90bd | 189 | } |
190 | ||
191 | /** | |
100fef9d | 192 | * Retrieve groups as 'view all contacts' |
3aab90bd | 193 | */ |
00be9182 | 194 | public function testGroupListViewAllContactsDisabled() { |
aaac0e0b | 195 | $this->_params['status'] = 2; |
958e4503 | 196 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
197 | $params = $this->_params; |
198 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
199 | $this->assertEquals(2, $groups['recordsTotal']); | |
00fb62ab JL |
200 | $this->assertEquals('not-me-disabled', $groups['data'][0]['title']); |
201 | $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']); | |
3aab90bd | 202 | } |
203 | ||
204 | /** | |
100fef9d | 205 | * Retrieve groups as 'view all contacts' |
3aab90bd | 206 | */ |
00be9182 | 207 | public function testGroupListViewAllContactsDisabledNotFoundTitle() { |
aaac0e0b EM |
208 | $this->_params['status'] = 2; |
209 | $this->_params['title'] = 'n'; | |
958e4503 | 210 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
211 | $params = $this->_params; |
212 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
213 | $this->assertEquals(1, $groups['recordsTotal']); | |
00fb62ab | 214 | $this->assertEquals('not-me-disabled', $groups['data'][0]['title']); |
3aab90bd | 215 | } |
216 | ||
217 | /** | |
100fef9d | 218 | * Retrieve groups as 'view all contacts' |
3aab90bd | 219 | */ |
00be9182 | 220 | public function testGroupListViewAllContactsDisabledFoundTitle() { |
aaac0e0b EM |
221 | $this->_params['status'] = 2; |
222 | $this->_params['title'] = 'p'; | |
958e4503 | 223 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
224 | $params = $this->_params; |
225 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
226 | $this->assertEquals(1, $groups['recordsTotal']); | |
00fb62ab | 227 | $this->assertEquals('pick-me-disabled', $groups['data'][0]['title']); |
3aab90bd | 228 | } |
229 | ||
230 | /** | |
100fef9d | 231 | * Retrieve groups as 'view all contacts' |
3aab90bd | 232 | */ |
00be9182 | 233 | public function testGroupListViewAllContactsAll() { |
aaac0e0b | 234 | $this->_params['status'] = 3; |
958e4503 | 235 | $this->setPermissionAndRequest(array('view all contacts', 'edit groups')); |
f0577026 JL |
236 | $params = $this->_params; |
237 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
238 | $this->assertEquals(4, $groups['recordsTotal']); | |
00fb62ab JL |
239 | $this->assertEquals('not-me-active', $groups['data'][0]['title']); |
240 | $this->assertEquals('not-me-disabled', $groups['data'][1]['title']); | |
241 | $this->assertEquals('pick-me-active', $groups['data'][2]['title']); | |
242 | $this->assertEquals('pick-me-disabled', $groups['data'][3]['title']); | |
3aab90bd | 243 | } |
244 | ||
245 | ||
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; |
958e4503 | 411 | $this->setHookAndRequest(array('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'; | |
958e4503 | 425 | $this->setHookAndRequest(array('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 | ||
446 | ||
447 | /** | |
eceb18cc | 448 | * ACL Group hook. |
3aab90bd | 449 | */ |
00be9182 | 450 | public function testGroupListAclGroupHook() { |
958e4503 | 451 | $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup'); |
f0577026 JL |
452 | $params = $this->_params; |
453 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
00fb62ab | 454 | $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default'); |
f0577026 | 455 | $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly'); |
00fb62ab | 456 | $this->assertEquals('pick-me-active', $groups['data'][0]['title']); |
3aab90bd | 457 | } |
458 | ||
459 | /** | |
eceb18cc | 460 | * ACL Group hook. |
3aab90bd | 461 | */ |
00be9182 | 462 | public function testGroupListAclGroupHookTitleNotFound() { |
aaac0e0b EM |
463 | $this->_params['title'] = 'n'; |
464 | $this->setHookAndRequest('access CiviCRM', 'hook_civicrm_aclGroup'); | |
f0577026 | 465 | $params = $this->_params; |
00fb62ab JL |
466 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); |
467 | $this->assertEquals(0, count($groups['data']), 'Returned groups should exclude disabled by default'); | |
f0577026 | 468 | $this->assertEquals(0, $groups['recordsTotal'], 'Total needs to be set correctly'); |
3aab90bd | 469 | } |
470 | ||
471 | /** | |
eceb18cc | 472 | * ACL Group hook. |
3aab90bd | 473 | */ |
00be9182 | 474 | public function testGroupListAclGroupHookTitleFound() { |
aaac0e0b | 475 | $this->_params['title'] = 'p'; |
958e4503 | 476 | $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup'); |
00fb62ab | 477 | $params = $this->_params; |
f0577026 | 478 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); |
00fb62ab | 479 | $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default'); |
f0577026 | 480 | $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly'); |
00fb62ab JL |
481 | $this->assertEquals('pick-me-active', $groups['data'][0]['title']); |
482 | $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']); | |
3aab90bd | 483 | } |
484 | ||
485 | /** | |
eceb18cc | 486 | * ACL Group hook. |
3aab90bd | 487 | */ |
00be9182 | 488 | public function testGroupListAclGroupHookAll() { |
aaac0e0b | 489 | $this->_params['status'] = 3; |
958e4503 | 490 | $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup'); |
f0577026 JL |
491 | $params = $this->_params; |
492 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
00fb62ab | 493 | $this->assertEquals(2, count($groups['data']), 'Returned groups should exclude disabled by default'); |
f0577026 | 494 | $this->assertEquals(2, $groups['recordsTotal'], 'Total needs to be set correctly'); |
00fb62ab JL |
495 | $this->assertEquals('pick-me-active', $groups['data'][0]['title']); |
496 | $this->assertEquals('pick-me-disabled', $groups['data'][1]['title']); | |
3aab90bd | 497 | } |
498 | ||
499 | /** | |
eceb18cc | 500 | * ACL Group hook. |
3aab90bd | 501 | */ |
00be9182 | 502 | public function testGroupListAclGroupHookEnabled() { |
aaac0e0b | 503 | $this->_params['status'] = 1; |
958e4503 | 504 | $this->setHookAndRequest(array('access CiviCRM', 'edit groups'), 'hook_civicrm_aclGroup'); |
f0577026 JL |
505 | $params = $this->_params; |
506 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
00fb62ab | 507 | $this->assertEquals(1, count($groups['data']), 'Returned groups should exclude disabled by default'); |
f0577026 | 508 | $this->assertEquals(1, $groups['recordsTotal'], 'Total needs to be set correctly'); |
00fb62ab | 509 | $this->assertEquals('pick-me-active', $groups['data'][0]['title']); |
3aab90bd | 510 | } |
511 | ||
a19f5834 JM |
512 | /** |
513 | * Don't populate smart group cache when building Group list. | |
514 | * | |
515 | * It takes forever, especially if you have lots of smart groups. | |
516 | */ | |
517 | public function testGroupDontRegenerateSmartGroups() { | |
518 | // Create a contact. | |
519 | $firstName = 'Tweak'; | |
520 | $lastName = 'Octonaut'; | |
521 | $params = array( | |
522 | 'first_name' => $firstName, | |
523 | 'last_name' => $lastName, | |
524 | 'contact_type' => 'Individual', | |
525 | ); | |
526 | $contact = CRM_Contact_BAO_Contact::add($params); | |
527 | ||
528 | // Create a smart group. | |
529 | $searchParams = array( | |
530 | 'last_name' => $lastName, | |
531 | ); | |
532 | $groupParams = array('title' => 'Find all Octonauts', 'formValues' => $searchParams, 'is_active' => 1); | |
533 | $group = CRM_Contact_BAO_Group::createSmartGroup($groupParams); | |
534 | ||
535 | // Ensure the smart group is created. | |
536 | $this->assertTrue(is_int($group->id), "Smart group created successfully."); | |
537 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); | |
538 | ||
539 | // Ensure it is populating the cache when loaded. | |
540 | $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1'; | |
541 | $params = array(1 => array($group->id, 'Integer')); | |
542 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
543 | $this->assertEquals($dao->N, 1, '1 record should be found in smart group'); | |
544 | ||
b804448d JM |
545 | // Load the Manage Group page code and we should get a count from our |
546 | // group because the cache is fresh. | |
547 | $_GET = $this->_params; | |
548 | $obj = new CRM_Group_Page_AJAX(); | |
549 | $groups = $obj->getGroupList(); | |
550 | ||
551 | // Make sure we returned our smart group and ensure the count is accurate. | |
552 | $found = FALSE; | |
553 | $right_count = FALSE; | |
554 | foreach ($groups['data'] as $returned_group) { | |
555 | if ($returned_group['group_id'] == $group->id) { | |
556 | $found = TRUE; | |
557 | if ($returned_group['count'] == 1) { | |
558 | $right_count = TRUE; | |
559 | } | |
560 | } | |
561 | } | |
562 | $this->assertTrue($found, 'Smart group shows up on Manage Group page.'); | |
563 | $this->assertTrue($right_count, 'Smart group displays proper count when cache is loaded.'); | |
564 | ||
a19f5834 JM |
565 | // Purge the group contact cache. |
566 | CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($group->id); | |
567 | ||
568 | // Load the Manage Group page code. | |
569 | $_GET = $this->_params; | |
570 | $obj = new CRM_Group_Page_AJAX(); | |
571 | $groups = $obj->getGroupList(); | |
572 | ||
b804448d JM |
573 | // Make sure the smart group reports unknown count. |
574 | $count_is_unknown = FALSE; | |
a19f5834 JM |
575 | foreach ($groups['data'] as $returned_group) { |
576 | if ($returned_group['group_id'] == $group->id) { | |
b804448d JM |
577 | if ($returned_group['count'] == ts('unknown')) { |
578 | $count_is_unknown = TRUE; | |
579 | } | |
a19f5834 JM |
580 | } |
581 | } | |
b804448d | 582 | $this->assertTrue($count_is_unknown, 'Smart group shows up as unknown when cache is expired.'); |
a19f5834 JM |
583 | |
584 | // Ensure we did not populate the cache. | |
585 | $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1'; | |
586 | $params = array(1 => array($group->id, 'Integer')); | |
587 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
01ef4db9 JM |
588 | $test = 'Group contact cache should not be populated on Manage Groups ' . |
589 | 'when cache_date is null'; | |
590 | $this->assertEquals($dao->N, 0, $test); | |
591 | ||
592 | // Do it again, but this time don't clear group contact cache. Instead, | |
593 | // set it to expire. | |
594 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); | |
595 | $params['name'] = 'smartGroupCacheTimeout'; | |
596 | $timeout = civicrm_api3('Setting', 'getvalue', $params); | |
597 | $timeout = intval($timeout) * 60; | |
598 | // Reset the cache_date to $timeout seconds ago minus another 60 | |
599 | // seconds for good measure. | |
600 | $cache_date = date('YmdHis', time() - $timeout - 60); | |
601 | ||
602 | $sql = "UPDATE civicrm_group SET cache_date = %1 WHERE id = %2"; | |
603 | $update_params = array( | |
604 | 1 => array($cache_date, 'Timestamp'), | |
605 | 2 => array($group->id, 'Integer'), | |
606 | ); | |
607 | CRM_Core_DAO::executeQuery($sql, $update_params); | |
608 | ||
609 | // Load the Manage Group page code. | |
610 | $_GET = $this->_params; | |
611 | $obj = new CRM_Group_Page_AJAX(); | |
612 | $groups = $obj->getGroupList(); | |
613 | ||
614 | // Ensure we did not regenerate the cache. | |
615 | $sql = 'SELECT DATE_FORMAT(cache_date, "%Y%m%d%H%i%s") AS cache_date ' . | |
616 | 'FROM civicrm_group WHERE id = %1'; | |
617 | $params = array(1 => array($group->id, 'Integer')); | |
618 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
619 | $dao->fetch(); | |
620 | $test = 'Group contact cache should not be re-populated on Manage Groups ' . | |
621 | 'when cache_date has expired'; | |
622 | $this->assertEquals($dao->cache_date, $cache_date, $test); | |
a19f5834 JM |
623 | } |
624 | ||
3aab90bd | 625 | /** |
eceb18cc | 626 | * Implements ACLGroup hook. |
6c6e6187 TO |
627 | * aclGroup function returns a list of permitted groups |
628 | * @param string $type | |
629 | * @param int $contactID | |
630 | * @param string $tableName | |
631 | * @param array $allGroups | |
632 | * @param array $currentGroups | |
633 | */ | |
00be9182 | 634 | public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups, &$currentGroups) { |
aaac0e0b | 635 | //don't use api - you will get a loop |
3aab90bd | 636 | $sql = " SELECT * FROM civicrm_group WHERE name LIKE '%pick%'"; |
637 | $groups = array(); | |
638 | $dao = CRM_Core_DAO::executeQuery($sql); | |
639 | while ($dao->fetch()) { | |
640 | $groups[] = $dao->id; | |
641 | } | |
642 | ||
22e263ad | 643 | if (!empty($allGroups)) { |
3aab90bd | 644 | //all groups is empty if we really mean all groups but if a filter like 'is_disabled' is already applied |
645 | // it is populated, ajax calls from Manage Groups will leave empty but calls from New Mailing pass in a filtered list | |
646 | $currentGroups = array_intersect($groups, array_flip($allGroups)); | |
647 | } | |
648 | else { | |
649 | $currentGroups = $groups; | |
650 | } | |
651 | } | |
96025800 | 652 | |
3b777f60 JG |
653 | public function testEditAllGroupsACL() { |
654 | $this->setupEditAllGroupsACL(); | |
655 | $params = $this->_params; | |
656 | $groups = CRM_Contact_BAO_Group::getGroupListSelector($params); | |
3b777f60 JG |
657 | $this->assertNotEmpty($groups, 'If Edit All Groups is granted, at least one group should be visible'); |
658 | } | |
659 | ||
660 | /** | |
661 | * Set up an acl allowing Authenticated contacts to Edit All Groups | |
662 | * | |
663 | * You need to have pre-created these groups & created the user e.g | |
664 | * $this->createLoggedInUser(); | |
665 | * | |
666 | */ | |
667 | public function setupEditAllGroupsACL() { | |
668 | global $_REQUEST; | |
669 | $_REQUEST = $this->_params; | |
670 | ||
671 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
672 | $optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role')); | |
673 | $ov = new CRM_Core_DAO_OptionValue(); | |
674 | $ov->option_group_id = $optionGroupID; | |
675 | $ov->value = 55; | |
676 | if ($ov->find(TRUE)) { | |
677 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}"); | |
678 | } | |
679 | $optionValue = $this->callAPISuccess('option_value', 'create', array( | |
680 | 'option_group_id' => $optionGroupID, | |
681 | 'label' => 'groupmaster', | |
682 | 'value' => 55, | |
683 | )); | |
684 | $groupId = $this->groupCreate(['name' => 'groupmaster group']); | |
685 | // Assign groupmaster to groupmaster group in civicrm_acl_entity_role | |
686 | CRM_Core_DAO::executeQuery(" | |
687 | INSERT INTO civicrm_acl_entity_role ( | |
688 | `acl_role_id`, `entity_table`, `entity_id`, `is_active` | |
689 | ) VALUES (55, 'civicrm_group', $groupId, 1); | |
690 | "); | |
691 | // Put the user into this group | |
692 | $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID'); | |
693 | $this->callAPISuccess('group_contact', 'create', array( | |
694 | 'group_id' => $groupId, | |
695 | 'contact_id' => $this->_loggedInUser, | |
696 | )); | |
697 | // Add the ACL | |
698 | CRM_Core_DAO::executeQuery(" | |
699 | INSERT INTO civicrm_acl ( | |
700 | `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active` | |
701 | ) | |
702 | VALUES ( | |
703 | 'core-580', 'civicrm_acl_role', 55, 'Edit', 'civicrm_saved_search', 0, 1 | |
704 | ); | |
705 | "); | |
706 | ||
707 | } | |
708 | ||
3aab90bd | 709 | } |