From fe4ed94f3f2b1b1c187beac570ebfb591a4dab6d Mon Sep 17 00:00:00 2001 From: Jaap Jansma Date: Mon, 4 Oct 2021 13:21:45 +0200 Subject: [PATCH] Fix for dev/core#2885 Added test Added test --- CRM/Contact/Form/Task/AddToGroup.php | 6 + .../CRM/Contact/Form/Task/AddToGroup.tpl | 3 + .../CRM/Contact/Form/Task/AddToGroupTest.php | 104 ++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php diff --git a/CRM/Contact/Form/Task/AddToGroup.php b/CRM/Contact/Form/Task/AddToGroup.php index 0abbad8b901b..633985629bd9 100644 --- a/CRM/Contact/Form/Task/AddToGroup.php +++ b/CRM/Contact/Form/Task/AddToGroup.php @@ -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); } /** @@ -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')); @@ -139,6 +143,7 @@ public function setDefaultValues() { } $defaults['group_option'] = 0; + $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this); return $defaults; } @@ -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; diff --git a/templates/CRM/Contact/Form/Task/AddToGroup.tpl b/templates/CRM/Contact/Form/Task/AddToGroup.tpl index 24902aabd884..e5ff93e37252 100644 --- a/templates/CRM/Contact/Form/Task/AddToGroup.tpl +++ b/templates/CRM/Contact/Form/Task/AddToGroup.tpl @@ -39,6 +39,9 @@ {$form.group_type.html} {/if} + + {include file="CRM/Custom/Form/CustomData.tpl"} + diff --git a/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php b/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php new file mode 100644 index 000000000000..ff274e8982b9 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php @@ -0,0 +1,104 @@ +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); + } + +}