3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_Group BAO
18 class CRM_Contact_BAO_GroupTest
extends CiviUnitTestCase
{
21 * Sets up the fixture, for example, opens a network connection.
23 * This method is called before a test is executed.
25 protected function setUp() {
30 * Tears down the fixture, for example, closes a network connection.
32 * This method is called after a test is executed.
34 protected function tearDown() {
35 $this->quickCleanup(['civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search']);
39 * Test case for add( ).
41 public function testAddSimple() {
43 $checkParams = $params = [
44 'title' => 'Group Uno',
45 'description' => 'Group One',
46 'visibility' => 'User and User Admin Only',
50 $group = CRM_Contact_BAO_Group
::create($params);
52 $this->assertDBCompareValues(
53 'CRM_Contact_DAO_Group',
60 * Test case to ensure child group is present in the hierarchy
61 * if it has multiple parent groups and not all are disabled.
63 public function testGroupHirearchy() {
65 // 1. Create two parent group A and B and disable B
66 // 2. Create a child group C
67 // 3. Ensure that Group C is present in the group hierarchy
70 'title' => 'Parent Group A',
71 'description' => 'Parent Group One',
72 'visibility' => 'User and User Admin Only',
75 $group1 = CRM_Contact_BAO_Group
::create($params);
77 $params = array_merge($params, [
79 'title' => 'Parent Group B',
80 'description' => 'Parent Group Two',
84 $group2 = CRM_Contact_BAO_Group
::create($params);
86 $params = array_merge($params, [
88 'title' => 'Child Group C',
89 'description' => 'Child Group C',
95 $group3 = CRM_Contact_BAO_Group
::create($params);
101 $groupsHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($params, NULL, ' ', TRUE);
102 // check if child group is present in the tree with formatted group title prepended with spacer ' '
103 $this->assertEquals(' Child Group C', $groupsHierarchy[$group3->id
]);
105 // Disable parent group A and ensure that child group C is not present as both of its parent groups are disabled
106 $group1->is_active
= 0;
108 $groupsHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($params, NULL, ' ', TRUE);
109 $this->assertFalse(array_key_exists($group3->id
, $groupsHierarchy));
113 * Test nestedGroup pseudoconstant
115 public function testNestedGroup() {
118 'title' => 'Parent Group A',
119 'description' => 'Parent Group One',
120 'visibility' => 'User and User Admin Only',
123 'group_type' => ['2' => 1],
125 $group1 = CRM_Contact_BAO_Group
::create($params);
129 'title' => 'Parent Group B',
130 'description' => 'Parent Group Two',
131 'visibility' => 'User and User Admin Only',
134 $group2 = CRM_Contact_BAO_Group
::create($params);
138 'title' => 'Child Group C',
139 'description' => 'Child Group C',
140 'visibility' => 'User and User Admin Only',
146 $group3 = CRM_Contact_BAO_Group
::create($params);
148 // Check with no group type restriction
149 $nestedGroup = CRM_Core_PseudoConstant
::nestedGroup();
150 $this->assertEquals([
151 $group1->id
=> 'Parent Group A',
152 $group2->id
=> 'Parent Group B',
153 $group3->id
=> ' Child Group C',
156 // Check restrict to mailing groups
157 $nestedGroup = CRM_Core_PseudoConstant
::nestedGroup(TRUE, 'Mailing');
158 $this->assertSame([$group1->id
=> 'Parent Group A'], $nestedGroup);
162 * Test adding a smart group.
164 public function testAddSmart() {
166 $checkParams = $params = [
167 'title' => 'Group Dos',
168 'description' => 'Group Two',
169 'visibility' => 'User and User Admin Only',
171 'formValues' => ['sort_name' => 'Adams'],
174 $group = CRM_Contact_BAO_Group
::createSmartGroup($params);
176 unset($checkParams['formValues']);
177 $this->assertDBCompareValues(
178 'CRM_Contact_DAO_Group',
179 ['id' => $group->id
],
185 * Load all sql data sets & return an array of saved searches.
189 public function dataProviderSavedSearch() {
191 $this->loadSavedSearches();
192 $results = CRM_Core_DAO
::singleValueQuery('SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE saved_search_id IS NOT NULL');
193 return [explode(',', $results)];
197 * Load saved search sql files into the DB.
199 public function loadSavedSearches() {
200 foreach (glob(dirname(__FILE__
) . "/SavedSearchDataSets/*.sql") as $file) {
201 CRM_Utils_File
::sourceSQLFile(NULL, $file);
206 * Check we can load smart groups based on config from 'real DBs' without fatal errors.
208 * Note that we are only testing lack of errors at this stage
209 * @todo - for some reason the data was getting truncated from the group table using dataprovider - would be preferable to get that working
210 * //@notdataProvider dataProviderSavedSearch
211 * //@notparam integer $groupID
213 * To add to this dataset do
216 * SELECT mapping_id FROM civicrm_group g LEFT JOIN civicrm_saved_search s ON saved_search_id = s.id WHERE g.id = @groupID INTO @mappingID;
217 * SELECT * FROM civicrm_mapping WHERE id = @mappingID;
218 * SELECT * FROM civicrm_mapping_field WHERE mapping_id = @mappingID;
219 * SELECT * FROM civicrm_saved_search WHERE mapping_id = @mappingID;
220 * SELECT g.* FROM civicrm_saved_search s LEFT JOIN civicrm_group g ON g.saved_search_id = s.id WHERE mapping_id = @mappingID;
222 * Copy the output to a single sql file and place in the SavedSearchDataSets folder - use the group number as the prefix.
223 * Try to keep as much of the real world irregular glory as you can! Don't change the table ids to be number 1 as this can hide errors
225 public function testGroupData() {
226 $groups = $this->dataProviderSavedSearch();
227 foreach ($groups[0] as $groupID) {
228 $group = new CRM_Contact_BAO_Group();
229 $group->id
= $groupID;
232 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
237 * Ensure that when updating a group with a linked organisation record even tho that record's id doesn't match the group id no db error is produced
239 public function testGroupUpdateWithOrganization() {
242 'title' => 'Group A',
243 'description' => 'Group One',
244 'visibility' => 'User and User Admin Only',
247 $group1 = CRM_Contact_BAO_Group
::create($params);
249 $domain1 = $this->callAPISuccess('Domain', 'get', ['id' => 1]);
252 'title' => 'Group B',
253 'description' => 'Group Two',
254 'visibility' => 'User and User Admin Only',
256 'organization_id' => $domain1['values'][1]['contact_id'],
258 $group2 = CRM_Contact_BAO_Group
::create($params2);
260 $domain2 = $this->callAPISuccess('Domain', 'get', ['id' => 2]);
263 'title' => 'Group C',
264 'description' => 'Group Three',
265 'visibility' => 'User and User Admin Only',
267 'organization_id' => $domain2['values'][2]['contact_id'],
269 $group3 = CRM_Contact_BAO_Group
::create($params3);
270 $params2['id'] = $group2->id
;
271 $testUpdate = CRM_Contact_BAO_Group
::create($params2);
275 * Ensure that when hidden smart group is created, wildcard string value is not ignored
277 public function testHiddenSmartGroup() {
278 $customGroup = $this->customGroupCreate();
280 'label' => 'testFld',
281 'data_type' => 'String',
282 'html_type' => 'Text',
283 'custom_group_id' => $customGroup['id'],
285 $customFieldID = CRM_Core_BAO_CustomField
::create($fields)->id
;
287 $contactID = $this->individualCreate(['custom_' . $customFieldID => 'abc']);
289 $hiddenSmartParams = [
290 'group_type' => ['2' => 1],
291 'form_values' => ['custom_' . $customFieldID => ['LIKE' => '%a%']],
292 'saved_search_id' => NULL,
293 'search_custom_id' => NULL,
294 'search_context' => 'advanced',
296 list($smartGroupID, $savedSearchID) = CRM_Contact_BAO_Group
::createHiddenSmartGroup($hiddenSmartParams);
298 $mailingID = $this->callAPISuccess('Mailing', 'create', [])['id'];
299 $this->callAPISuccess('MailingGroup', 'create', [
300 'mailing_id' => $mailingID,
301 'group_type' => 'Include',
302 'entity_table' => 'civicrm_group',
303 'entity_id' => $smartGroupID,
306 CRM_Mailing_BAO_Mailing
::getRecipients($mailingID);
307 $recipients = $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailingID]);
308 $this->assertEquals(1, $recipients['count'], 'Check recipient count');
312 * Test updating a group with just description and check the recent items
313 * list has the right title.
315 public function testGroupUpdateDescription() {
316 // Create a group. Copied from $this->testAddSimple().
317 // Note we need $checkParams because the function call changes $params.
318 $checkParams = $params = [
319 'title' => 'Group Uno',
320 'description' => 'Group One',
321 'visibility' => 'User and User Admin Only',
324 $group = CRM_Contact_BAO_Group
::create($params);
326 // Update the group with just id and description.
329 'description' => 'The first group',
331 CRM_Contact_BAO_Group
::create($newParams);
333 // Check it against original array, except description.
334 $result = $this->callAPISuccess('Group', 'getsingle', ['id' => $group->id
]);
335 foreach ($checkParams as $key => $value) {
336 if ($key === 'description') {
337 $this->assertEquals($newParams[$key], $result[$key], "$key doesn't match");
340 $this->assertEquals($checkParams[$key], $result[$key], "$key doesn't match");
344 // Check recent items list.
345 $recentItems = CRM_Utils_Recent
::get();
346 $this->assertEquals($checkParams['title'], $recentItems[0]['title']);