Skip to content

Commit

Permalink
Fix for dev/core#2885
Browse files Browse the repository at this point in the history
Added test

Added test
  • Loading branch information
jaapjansma committed Oct 6, 2021
1 parent b40adb2 commit fe4ed94
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CRM/Contact/Form/Task/AddToGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function preProcess() {

$this->_context = $this->get('context');
$this->_id = $this->get('amtgID');

CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id);
}

/**
Expand Down Expand Up @@ -119,6 +121,8 @@ public function buildQuickForm() {
}
else {
$this->setTitle(ts('Add Contacts to A Group'));
//build custom data
CRM_Custom_Form_CustomData::buildQuickForm($this);
}

$this->addDefaultButtons(ts('Add to Group'));
Expand All @@ -139,6 +143,7 @@ public function setDefaultValues() {
}

$defaults['group_option'] = 0;
$defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
return $defaults;
}

Expand Down Expand Up @@ -183,6 +188,7 @@ public function postProcess() {
$groupParams['visibility'] = "User and User Admin Only";
$groupParams['group_type'] = array_keys($params['group_type'] ?? []);
$groupParams['is_active'] = 1;
$groupParams['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, 'Group');

$createdGroup = CRM_Contact_BAO_Group::create($groupParams);
$groupID = $createdGroup->id;
Expand Down
3 changes: 3 additions & 0 deletions templates/CRM/Contact/Form/Task/AddToGroup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<td>{$form.group_type.html}</td>
</tr>
{/if}
<tr>
<td colspan=2>{include file="CRM/Custom/Form/CustomData.tpl"}</td>
</tr>
</table>
</td>
</tr>
Expand Down
104 changes: 104 additions & 0 deletions tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* @group headless
*/
class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {

protected function setUp(): void {
parent::setUp();
}

protected function getSearchTaskFormObject(array $formValues) {
$_POST = $formValues;
$_SERVER['REQUEST_METHOD'] = 'GET';
/* @var CRM_Core_Form $form */
$form = new CRM_Contact_Form_Task_AddToGroup();
$form->controller = new CRM_Contact_Controller_Search();
$form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
$_SESSION['_' . $form->controller->_name . '_container']['values']['Advanced'] = $formValues;
return $form;
}

/**
* Test delete to trash.
*/
public function testAddToGroup() {
$contact = $this->callAPISuccess('Contact', 'create', [
'contact_type' => 'Individual',
'first_name' => 'John',
'last_name' => 'Doe',
]);
$contactId = $contact['id'];
$existingGroupId = $this->groupCreate();
$form = $this->getSearchTaskFormObject(['cids' => $contactId, 'group_option' => 0, 'group_id' => $existingGroupId]);
$form->preProcess();
$form->_contactIds = [$contactId];
$form->set('_componentIds', [$contactId]);
$form->buildQuickForm();
$form->setDefaultValues();
$form->postProcess();

$groupCount = \Civi\Api4\GroupContact::get()
->addWhere('group_id', '=', $existingGroupId)
->addWhere('status', '=', 'Added')
->execute()
->count();
$this->assertEquals(1, $groupCount);
}

/**
* Test delete to trash.
*/
public function testAddToNewGroupWithCustomField() {
$contact = $this->callAPISuccess('Contact', 'create', [
'contact_type' => 'Individual',
'first_name' => 'Pete',
'last_name' => 'Johnson',
]);
$contactId = $contact['id'];
$customGroup = $this->customGroupCreate(['extends' => 'Group']);
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$customFieldId = $customField['id'];

$form = $this->getSearchTaskFormObject([
'cids' => $contactId,
'group_option' => 1,
'title' => 'Test Group With Custom Field',
'description' => '',
'custom_' . $customFieldId => 'Custom Value ABC',
]);
$form->preProcess();
$form->_contactIds = [$contactId];
$form->set('_componentIds', [$contactId]);
$form->buildQuickForm();
$form->setDefaultValues();
$form->postProcess();

$group = \Civi\Api4\Group::get()
->addSelect('custom.*', 'id')
->addWhere('title', '=', 'Test Group With Custom Field')
->execute();
$this->assertEquals(1, $group->count());
$group = $group->first();
$this->assertArrayKeyExists('new_custom_group.Custom_Field', $group);
$this->assertEquals('Custom Value ABC', $group['new_custom_group.Custom_Field']);

$groupCount = \Civi\Api4\GroupContact::get()
->addWhere('group_id', '=', $group['id'])
->addWhere('status', '=', 'Added')
->execute()
->count();
$this->assertEquals(1, $groupCount);
}

}

0 comments on commit fe4ed94

Please sign in to comment.