Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] extract member form batch update & add test #16151

Merged
merged 1 commit into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 61 additions & 53 deletions CRM/Member/Form/Task/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,78 +187,86 @@ public function setDefaultValues() {
/**
* Process the form after the input has been submitted and validated.
*
*
* @return void
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function postProcess() {
$params = $this->exportValues();
// @todo extract submit functions &
// extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
// handling of custom data, specifically checkbox fields.
if (isset($params['field'])) {
$this->submit($params);
CRM_Core_Session::setStatus(ts('Your updates have been saved.'), ts('Saved'), 'success');
}
else {
CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
}
}

/**
* @param array $params
*
* @return mixed
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function submit(array $params) {
$dates = [
'membership_join_date',
'membership_start_date',
'membership_end_date',
];
if (isset($params['field'])) {
$customFields = [];
foreach ($params['field'] as $key => $value) {
$params['id'] = $key;
if (!empty($value['membership_source'])) {
$value['source'] = $value['membership_source'];
}
$customFields = [];
foreach ($params['field'] as $key => $value) {
$value['id'] = $key;
if (!empty($value['membership_source'])) {
$value['source'] = $value['membership_source'];
}

if (!empty($value['membership_type'])) {
$membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
}
if (!empty($value['membership_type'])) {
$membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
}

unset($value['membership_source']);
unset($value['membership_type']);
unset($value['membership_source']);
unset($value['membership_type']);

//Get the membership status
$value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
unset($value['membership_status']);
foreach ($dates as $val) {
if (isset($value[$val])) {
$value[$val] = CRM_Utils_Date::processDate($value[$val]);
}
//Get the membership status
$value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
unset($value['membership_status']);
foreach ($dates as $val) {
if (isset($value[$val])) {
$value[$val] = CRM_Utils_Date::processDate($value[$val]);
}
}
if (empty($customFields)) {
if (empty($value['membership_type_id'])) {
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
}
if (empty($customFields)) {
if (empty($value['membership_type_id'])) {
$membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
}

// membership type custom data
$customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);
// membership type custom data
$customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);

$customFields = CRM_Utils_Array::crmArrayMerge($customFields,
CRM_Core_BAO_CustomField::getFields('Membership',
FALSE, FALSE, NULL, NULL, TRUE
)
);
}
//check for custom data
$value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
$key,
'Membership',
$membershipTypeId
$customFields = CRM_Utils_Array::crmArrayMerge($customFields,
CRM_Core_BAO_CustomField::getFields('Membership',
FALSE, FALSE, NULL, NULL, TRUE
)
);
}
//check for custom data
$value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
$key,
'Membership',
$membershipTypeId
);

$membership = CRM_Member_BAO_Membership::add($value);
$membership = CRM_Member_BAO_Membership::add($value);

// add custom field values
if (!empty($value['custom']) &&
is_array($value['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
}
// add custom field values
if (!empty($value['custom']) &&
is_array($value['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
}

CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
}
else {
CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
}
return $value;
}

}
63 changes: 63 additions & 0 deletions tests/phpunit/CRM/Member/Form/Task/BatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 |
+--------------------------------------------------------------------+
*/

/**
* @package CiviCRM_APIv3
* @subpackage API_Contribution
* @group headless
*/
class CRM_Member_Form_Task_BatchTest extends CiviUnitTestCase {

use CRMTraits_Custom_CustomDataTrait;

/**
* Clean up after each test.
*
* @throws \CRM_Core_Exception
*/
public function tearDown() {
$this->validateAllContributions();
$this->validateAllPayments();
$this->quickCleanUpFinancialEntities();
}

/**
* Test batch submission.
*
* @throws \CRM_Core_Exception
*/
public function testBatchSubmit() {
$form = $this->getFormObject('CRM_Member_Form_Task_Batch');
$membership1 = $this->contactMembershipCreate(['contact_id' => $this->individualCreate()]);
$membership2 = $this->contactMembershipCreate(['contact_id' => $this->individualCreate()]);
$this->createCustomGroupWithFieldOfType(['extends' => 'Membership'], 'text');
$form->submit([
'field' => [
$membership1 => [
$this->getCustomFieldName('text') => '80',
'membership_join_date' => '2019-12-26',
],
$membership2 => [
$this->getCustomFieldName('text') => '100',
'membership_join_date' => '2019-11-26',
'membership_source' => 'form',
],
],
]);
$memberships = $this->callAPISuccess('Membership', 'get', [])['values'];
$this->assertEquals('2019-12-26', $memberships[$membership1]['join_date']);
$this->assertEquals('2019-11-26', $memberships[$membership2]['join_date']);
$this->assertEquals('form', $memberships[$membership2]['source']);
$this->assertEquals(80, $memberships[$membership1][$this->getCustomFieldName('text')]);
$this->assertEquals(100, $memberships[$membership2][$this->getCustomFieldName('text')]);
}

}