From ec6d2df43974e8243449d37039a5e92fa89389a4 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Tue, 29 Aug 2017 20:43:22 -0500 Subject: [PATCH] Fix for CRM-21122: "Support selection of smart groups on Contact Dashboard" --- CRM/Contact/Form/GroupContact.php | 12 +- .../Page/View/UserDashBoard/GroupContact.php | 12 +- .../View/UserDashboard/GroupContactTest.php | 224 ++++++++++++++++++ 3 files changed, 243 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php diff --git a/CRM/Contact/Form/GroupContact.php b/CRM/Contact/Form/GroupContact.php index 27377e2c8e..a443d8115f 100644 --- a/CRM/Contact/Form/GroupContact.php +++ b/CRM/Contact/Form/GroupContact.php @@ -80,7 +80,17 @@ class CRM_Contact_Form_GroupContact extends CRM_Core_Form { // get the list of all the groups if ($this->_context == 'user') { $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE); - $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups); + $ids = CRM_Core_PseudoConstant::allGroup(); + $heirGroups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids); + + $allGroups = array(); + foreach ($heirGroups as $id => $group) { + // make sure that this group has public visibility + if ($onlyPublicGroups && $group['visibility'] == 'User and User Admin Only') { + continue; + } + $allGroups[$id] = $group; + } } else { $allGroups = CRM_Core_PseudoConstant::group(); diff --git a/CRM/Contact/Page/View/UserDashBoard/GroupContact.php b/CRM/Contact/Page/View/UserDashBoard/GroupContact.php index 247d5fe2f0..ed399bff16 100644 --- a/CRM/Contact/Page/View/UserDashBoard/GroupContact.php +++ b/CRM/Contact/Page/View/UserDashBoard/GroupContact.php @@ -40,28 +40,32 @@ class CRM_Contact_Page_View_UserDashBoard_GroupContact extends CRM_Contact_Page_ $this->_contactId, NULL, NULL, TRUE, TRUE, - $this->_onlyPublicGroups + $this->_onlyPublicGroups, + NULL, NULL, TRUE ); $in = CRM_Contact_BAO_GroupContact::getContactGroup( $this->_contactId, 'Added', NULL, FALSE, TRUE, - $this->_onlyPublicGroups + $this->_onlyPublicGroups, + NULL, NULL, TRUE ); $pending = CRM_Contact_BAO_GroupContact::getContactGroup( $this->_contactId, 'Pending', NULL, FALSE, TRUE, - $this->_onlyPublicGroups + $this->_onlyPublicGroups, + NULL, NULL, TRUE ); $out = CRM_Contact_BAO_GroupContact::getContactGroup( $this->_contactId, 'Removed', NULL, FALSE, TRUE, - $this->_onlyPublicGroups + $this->_onlyPublicGroups, + NULL, NULL, TRUE ); $this->assign('groupCount', $count); diff --git a/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php b/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php new file mode 100644 index 0000000000..0ed1cedbe2 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Page/View/UserDashboard/GroupContactTest.php @@ -0,0 +1,224 @@ +callAPISuccess('Group', 'create', array( + 'title' => $adminStdGroupTitle, + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + )); + // create public non-smart group + $publicStdGroupTitle = 'The Public Std Group'; + $publicStdGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $publicStdGroupTitle, + 'visibility' => 'Public Pages', + 'is_active' => 1, + )); + + // Prepare to create smart groups based on saved criteria Gender = Male. + // Start by creating the saved search. + $savedSearch = $this->callAPISuccess('SavedSearch', 'create', array( + 'form_values' => 'a:1:{i:0;a:5:{i:0;s:9:"gender_id";i:1;s:1:"=";i:2;i:2;i:3;i:0;i:4;i:0;}}', + )); + // Create contact with Gender - Male + $savedSearchContact = $this->individualCreate(array( + 'gender_id' => "Male", + 'first_name' => 'C', + ), 1); + // Create admin-only smart group for this saved search. + $adminSmartGroupTitle = 'The Admin-only Smart Group'; + $adminSmartGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $adminSmartGroupTitle, + 'visibility' => 'User and User Admin Only', + 'saved_search_id' => $savedSearch['id'], + 'is_active' => 1, + )); + // Create public smart group for this saved search. + $publicSmartGroupTitle = 'The Public Smart Group'; + $publicSmartGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $publicSmartGroupTitle, + 'visibility' => 'Public Pages', + 'saved_search_id' => $savedSearch['id'], + 'is_active' => 1, + )); + + // Get logged in user contact ID. + $user_id = $this->createLoggedInUser(); + $_REQUEST['id'] = $user_id; + + // Add current user to the test groups. + $publicSmartGroup = $this->callAPISuccess('Contact', 'create', array( + 'id' => $user_id, + 'group' => array( + $adminStdGroup['id'] => 1, + $adminSmartGroup['id'] => 1, + $publicStdGroup['id'] => 1, + $publicSmartGroup['id'] => 1, + ), + )); + + // Run the contact dashboard and assert that only the public groups appear + // in the variables. + $page = new CRM_Contact_Page_View_UserDashBoard_GroupContact(); + $page->run(); + + $groupIn = CRM_Core_Smarty::singleton()->get_template_vars('groupIn'); + $groupInTitles = CRM_Utils_Array::collect('title', $groupIn); + $this->assertContains($publicSmartGroupTitle, $groupInTitles, "Group '$publicSmartGroupTitle' should be in listed groups, but is not."); + $this->assertContains($publicStdGroupTitle, $groupInTitles, "Group '$publicStdGroupTitle' should be in listed groups, but is not."); + $this->assertNotContains($adminSmartGroupTitle, $groupInTitles, "Group '$adminSmartGroupTitle' should not be in listed groups, but is."); + $this->assertNotContains($adminStdGroupTitle, $groupInTitles, "Group '$adminStdGroupTitle' should not be in listed groups, but is."); + } + + /** + * Test that the select list of available groups, on the Contact Dashboard, + * contains the correct groups. + */ + public function testBrowseDisplaysCorrectListOfAVailableGroups() { + + // create admin-only non-smart group + $adminStdGroupTitle = 'The Admin-only Std Group' . uniqid(); + $adminStdGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $adminStdGroupTitle, + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + )); + // create public non-smart group + $publicStdGroupTitle = 'The Public Std Group' . uniqid(); + $publicStdGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $publicStdGroupTitle, + 'visibility' => 'Public Pages', + 'is_active' => 1, + )); + // create second public non-smart group + $publicStdGroupTitle2 = 'The 2nd Public Std Group' . uniqid(); + $publicStdGroup2 = $this->callAPISuccess('Group', 'create', array( + 'title' => $publicStdGroupTitle2, + 'visibility' => 'Public Pages', + 'is_active' => 1, + )); + + // Prepare to create smart groups based on saved criteria Gender = Male. + // Start by creating the saved search. + $savedSearch = $this->callAPISuccess('SavedSearch', 'create', array( + 'form_values' => 'a:1:{i:0;a:5:{i:0;s:9:"gender_id";i:1;s:1:"=";i:2;i:2;i:3;i:0;i:4;i:0;}}', + )); + // Create contact with Gender - Male + $savedSearchContact = $this->individualCreate(array( + 'gender_id' => "Male", + 'first_name' => 'C', + ), 1); + // Create admin-only smart group for this saved search. + $adminSmartGroupTitle = 'The Admin-only Smart Group' . uniqid(); + $adminSmartGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $adminSmartGroupTitle, + 'visibility' => 'User and User Admin Only', + 'saved_search_id' => $savedSearch['id'], + 'is_active' => 1, + )); + // Create public smart group for this saved search. + $publicSmartGroupTitle = 'The Public Smart Group' . uniqid(); + $publicSmartGroup = $this->callAPISuccess('Group', 'create', array( + 'title' => $publicSmartGroupTitle, + 'visibility' => 'Public Pages', + 'saved_search_id' => $savedSearch['id'], + 'is_active' => 1, + )); + + // Get logged in user contact ID. + $user_id = $this->createLoggedInUser(); + + // Run the contact dashboard and assert that only the public groups appear + // in select list of available groups. + $_REQUEST['id'] = $user_id; + $page = new CRM_Contact_Page_View_UserDashBoard_GroupContact(); + $page->run(); + + $form = CRM_Core_Smarty::singleton()->get_template_vars('form'); + $group_id_field_html = $form['group_id']['html']; + $this->assertContains($publicSmartGroupTitle, $group_id_field_html, "Group '$publicSmartGroupTitle' should be in listed available groups, but is not."); + $this->assertContains($publicStdGroupTitle, $group_id_field_html, "Group '$publicStdGroupTitle' should be in listed available groups, but is not."); + $this->assertNotContains($adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains($adminStdGroupTitle, $group_id_field_html, "Group '$adminStdGroupTitle' should not be in listed available groups, but is."); + + // Add current user to the test groups. + $publicSmartGroup = $this->callAPISuccess('Contact', 'create', array( + 'id' => $user_id, + 'group' => array( + $adminStdGroup['id'] => 1, + $adminSmartGroup['id'] => 1, + $publicStdGroup['id'] => 1, + $publicSmartGroup['id'] => 1, + ), + )); + + // Run the contact dashboard and assert that none of the groups appear + // in select list of available groups. + $_REQUEST['id'] = $user_id; + $page = new CRM_Contact_Page_View_UserDashBoard_GroupContact(); + $page->run(); + + $form = CRM_Core_Smarty::singleton()->get_template_vars('form'); + $group_id_field_html = $form['group_id']['html']; + $this->assertNotContains($publicSmartGroupTitle, $group_id_field_html, "Group '$publicSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains($publicStdGroupTitle, $group_id_field_html, "Group '$publicStdGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains($adminSmartGroupTitle, $group_id_field_html, "Group '$adminSmartGroupTitle' should not be in listed available groups, but is."); + $this->assertNotContains($adminStdGroupTitle, $group_id_field_html, "Group '$adminStdGroupTitle' should not be in listed available groups, but is."); + } + +} -- 2.25.1