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

Add unit test to event batch update (includes function extraction to support this) #12048

Merged
merged 2 commits into from
Apr 29, 2018
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
133 changes: 73 additions & 60 deletions CRM/Event/Form/Task/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -273,63 +271,7 @@ public function setDefaultValues() {
*/
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);
}

/**
Expand Down Expand Up @@ -519,4 +461,75 @@ public function assignToTemplate() {
}
}

/**
* @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'
);
foreach (array_keys($value) as $fieldName) {
// Unset the original custom field now that it has been formatting to the 'custom'
// array as it may not be in the right format for the api as is (notably for
// multiple checkbox values).
if (substr($fieldName, 0, 7) === 'custom_') {
unset($value[$fieldName]);
}
}

$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');
}
}

}
25 changes: 25 additions & 0 deletions tests/phpunit/CRM/Event/Form/Task/BatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Test CRM_Event_Form_Registration functions.
*
* @package CiviCRM
* @group headless
*/
class CRM_Event_Form_Task_BatchTest extends CiviUnitTestCase {

public function testSubmit() {
$group = $this->CustomGroupCreate(['extends' => 'Participant', 'title' => 'Participant']);
$field = $this->customFieldCreate(['custom_group_id' => $group['id'], 'html_type' => 'CheckBox', 'option_values' => ['two' => 'A couple', 'three' => 'A few', 'four' => 'Too Many']]);
$participantID = $this->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, 'custom_' . $field['id'] => ['two' => 1, 'four' => 1]]]]);

$participant = $this->callAPISuccessGetSingle('Participant', ['id' => $participantID]);
$this->assertEquals(1, $participant['participant_status_id']);
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ public function paymentProcessorAuthorizeNetCreate($params = array()) {
* @return int
* $id of participant created
*/
public function participantCreate($params) {
public function participantCreate($params = []) {
if (empty($params['contact_id'])) {
$params['contact_id'] = $this->individualCreate();
}
Expand Down