From 0f57ba7a4c092a4ea6513293de27860da7de307c Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 29 Apr 2018 17:47:07 +1200 Subject: [PATCH] Add unit test to event batch update (includes function extraction to support this) --- CRM/Event/Form/Task/Batch.php | 125 +++++++++--------- .../phpunit/CRM/Event/Form/Task/BatchTest.php | 22 +++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- 3 files changed, 88 insertions(+), 61 deletions(-) create mode 100644 tests/phpunit/CRM/Event/Form/Task/BatchTest.php diff --git a/CRM/Event/Form/Task/Batch.php b/CRM/Event/Form/Task/Batch.php index 442977aa82..fe71dc15e0 100644 --- a/CRM/Event/Form/Task/Batch.php +++ b/CRM/Event/Form/Task/Batch.php @@ -29,12 +29,10 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2018 - * $Id$ - * */ /** - * This class provides the functionality for batch profile update for events + * This class provides the functionality for batch profile update for events. */ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task { @@ -273,63 +271,7 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task { */ public function postProcess() { $params = $this->exportValues(); - $statusClasses = CRM_Event_PseudoConstant::participantStatusClass(); - if (isset($params['field'])) { - foreach ($params['field'] as $key => $value) { - - //check for custom data - $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, - $key, - 'Participant' - ); - - $value['id'] = $key; - - if (!empty($value['participant_role'])) { - if (is_array($value['participant_role'])) { - $value['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value['participant_role'])); - } - else { - $value['role_id'] = $value['participant_role']; - } - } - - //need to send mail when status change - $statusChange = FALSE; - $relatedStatusChange = FALSE; - if (!empty($value['participant_status'])) { - $value['status_id'] = $value['participant_status']; - $fromStatusId = CRM_Utils_Array::value($key, $this->_fromStatusIds); - if (!$fromStatusId) { - $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id'); - } - - if ($fromStatusId != $value['status_id']) { - $relatedStatusChange = TRUE; - } - if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) { - $statusChange = TRUE; - } - } - - unset($value['participant_status']); - - civicrm_api3('Participant', 'create', $value); - - //need to trigger mails when we change status - if ($statusChange) { - CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId); - } - if ($relatedStatusChange) { - //update related contribution status, CRM-4395 - self::updatePendingOnlineContribution($key, $value['status_id']); - } - } - CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success'); - } - else { - CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert'); - } + $this->submit($params); } /** @@ -519,4 +461,67 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task { } } + /** + * @param $params + */ + public function submit($params) { + $statusClasses = CRM_Event_PseudoConstant::participantStatusClass(); + if (isset($params['field'])) { + foreach ($params['field'] as $key => $value) { + + //check for custom data + $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, + $key, + 'Participant' + ); + + $value['id'] = $key; + + if (!empty($value['participant_role'])) { + if (is_array($value['participant_role'])) { + $value['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value['participant_role'])); + } + else { + $value['role_id'] = $value['participant_role']; + } + } + + //need to send mail when status change + $statusChange = FALSE; + $relatedStatusChange = FALSE; + if (!empty($value['participant_status'])) { + $value['status_id'] = $value['participant_status']; + $fromStatusId = CRM_Utils_Array::value($key, $this->_fromStatusIds); + if (!$fromStatusId) { + $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id'); + } + + if ($fromStatusId != $value['status_id']) { + $relatedStatusChange = TRUE; + } + if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) { + $statusChange = TRUE; + } + } + + unset($value['participant_status']); + + civicrm_api3('Participant', 'create', $value); + + //need to trigger mails when we change status + if ($statusChange) { + CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId); + } + if ($relatedStatusChange) { + //update related contribution status, CRM-4395 + self::updatePendingOnlineContribution($key, $value['status_id']); + } + } + CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success'); + } + else { + CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert'); + } + } + } diff --git a/tests/phpunit/CRM/Event/Form/Task/BatchTest.php b/tests/phpunit/CRM/Event/Form/Task/BatchTest.php new file mode 100644 index 0000000000..ad9b895a15 --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/Task/BatchTest.php @@ -0,0 +1,22 @@ +participantCreate(); + $participant = $this->callAPISuccessGetSingle('Participant', ['id' => $participantID]); + $this->assertEquals(2, $participant['participant_status_id']); + + $form = $this->getFormObject('CRM_Event_Form_Task_Batch'); + $form->submit(['field' => [$participantID => ['participant_status_id' => 1]]]); + + $participant = $this->callAPISuccessGetSingle('Participant', ['id' => $participantID]); + $this->assertEquals(1, $participant['participant_status_id']); + } +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 2e95aca070..3ace6bcf3c 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1090,7 +1090,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @return int * $id of participant created */ - public function participantCreate($params) { + public function participantCreate($params = []) { if (empty($params['contact_id'])) { $params['contact_id'] = $this->individualCreate(); } -- 2.25.1