Skip to content

Commit

Permalink
Add unit test on financial items for pending payments.
Browse files Browse the repository at this point in the history
This test was supposed to demonstrate a failure but actually it demonstrates something working.

Still, good to have an additional test
  • Loading branch information
eileenmcnaughton committed Nov 10, 2017
1 parent 182727a commit 3baa971
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 26 deletions.
5 changes: 4 additions & 1 deletion CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,9 @@ public function submit($params) {

//lets carry currency, CRM-4453
$params['fee_currency'] = $config->defaultCurrency;
if (!isset($lineItem[0])) {
$lineItem[0] = array();
}
CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'],
$params, $lineItem[0]
);
Expand Down Expand Up @@ -1666,7 +1669,7 @@ public function submit($params) {
//add dataArray in the receipts in ADD and UPDATE condition
$dataArray = array();
if ($this->_action & CRM_Core_Action::ADD) {
$line = $lineItem[0];
$line = isset($lineItem[0]) ? $lineItem[0] : array();
}
elseif ($this->_action & CRM_Core_Action::UPDATE) {
$line = $this->_values['line_items'];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/BAO/FinancialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct() {
* @param array $defaults
* (reference ) an assoc array to hold the flattened values.
*
* @return CRM_Contribute_BAO_FinancialItem
* @return CRM_Financial_BAO_FinancialItem
*/
public static function retrieve(&$params, &$defaults) {
$financialItem = new CRM_Financial_DAO_FinancialItem();
Expand Down
9 changes: 5 additions & 4 deletions tests/phpunit/CRM/Event/BAO/CRM19273Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ protected function cleanup() {
*
* @todo resolve this with parent function.
*
* @param int $amount
* @param int $min_fee
* @param int $feeTotal
* @param int $minAmt
* @param string $type
*
* @return int
*/
protected function eventPriceSetCreate($amount = 0, $min_fee = 0) {

protected function eventPriceSetCreate($feeTotal = 55, $minAmt = 0, $type = 'Text') {
$paramsSet['title'] = 'Two Options';
$paramsSet['name'] = CRM_Utils_String::titleToVar('Two Options');
$paramsSet['is_active'] = FALSE;
Expand Down
108 changes: 98 additions & 10 deletions tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function setUp() {
* @throws \Exception
*/
public function testSubmit() {
$form = $this->commonPrepare();
$form = $this->getForm();
$form->submit(array(
'register_date' => 'now',
'register_date_time' => '00:00:00',
Expand All @@ -31,18 +31,101 @@ public function testSubmit() {
$this->assertEquals(1, $participants['count']);
}

/**
* Test financial items pending transaction is later altered.
*
* @throws \Exception
*/
public function testSubmitUpaidPriceChangeWhileStillPending() {
$form = $this->getForm(array('is_monetary' => 1, 'financial_type_id' => 1));
$form->_quickConfig = TRUE;

$form->_lineItem = array(
0 => array(
13 => array(
'price_field_id' => $this->_ids['price_field'][0],
'price_field_value_id' => $this->_ids['price_field_value'][0],
'label' => 'Tiny-tots (ages 5-8)',
'field_title' => 'Tournament Fees',
'description' => NULL,
'qty' => 1,
'unit_price' => '800.000000000',
'line_total' => 800.0,
'participant_count' => 0,
'max_value' => NULL,
'membership_type_id' => NULL,
'membership_num_terms' => NULL,
'auto_renew' => NULL,
'html_type' => 'Radio',
'financial_type_id' => '4',
'tax_amount' => NULL,
'non_deductible_amount' => '0.00',
),
),
);
$form->setAction(CRM_Core_Action::ADD);
$form->_priceSetId = $this->_ids['price_set'];
$form->submit(array(
'register_date' => 'now',
'register_date_time' => '00:00:00',
'status_id' => 5,
'role_id' => 1,
'event_id' => $form->_eventId,
'priceSetId' => $this->_ids['price_set'],
'price_' . $this->_ids['price_field'][0] => array(
$this->_ids['price_field_value'][0] => 1,
),
'is_pay_later' => 1,
'amount_level' => 'Too much',
'fee_amount' => 55,
'total_amount' => 55,
'payment_processor_id' => 0,
'record_contribution' => TRUE,
'financial_type_id' => 1,
'contribution_status_id' => 2,
'payment_instrument_id' => 1,
));
$participants = $this->callAPISuccess('Participant', 'get', array());
$this->assertEquals(1, $participants['count']);
$contribution = $this->callAPISuccessGetSingle('Contribution', array());
$this->assertEquals(2, $contribution['contribution_status_id']);
$items = $this->callAPISuccess('FinancialItem', 'get', array());
$this->assertEquals(1, $items['count']);

$priceSetParams['price_' . $this->_ids['price_field'][0]] = $this->_ids['price_field_value'][1];
$lineItem = CRM_Price_BAO_LineItem::getLineItems($participants['id'], 'participant');
$this->assertEquals(55, $lineItem[1]['subTotal']);
$financialItems = $this->callAPISuccess('FinancialItem', 'get', array());
$sum = 0;
foreach ($financialItems['values'] as $financialItem) {
$sum += $financialItem['amount'];
}
$this->assertEquals(55, $sum);

CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participants['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem, 100);
$lineItem = CRM_Price_BAO_LineItem::getLineItems($participants['id'], 'participant');
// Participants is updated to 0 but line remains.
$this->assertEquals(0, $lineItem[1]['subTotal']);
$this->assertEquals(100, $lineItem[2]['subTotal']);
$financialItems = $this->callAPISuccess('FinancialItem', 'get', array());

$sum = 0;
foreach ($financialItems['values'] as $financialItem) {
$sum += $financialItem['amount'];
}
$this->assertEquals(100, $sum);
}

/**
* Initial test of submit function.
*
* @throws \Exception
*/
public function testSubmitWithPayment() {
$form = $this->commonPrepare(array('is_monetary' => 1, 'financial_type_id' => 1));
$paymentProcessorID = $this->processorCreate(array('is_test' => 0));
$form = $this->getForm(array('is_monetary' => 1, 'financial_type_id' => 1));
$form->_mode = 'Live';
$form->_values['fee'] = array();
$form->_isPaidEvent = TRUE;
$form->_quickConfig = TRUE;
$paymentProcessorID = $this->processorCreate(array('is_test' => 0));
$form->_fromEmails = array(
'from_email_id' => array('abc@gmail.com' => 1),
);
Expand Down Expand Up @@ -123,22 +206,27 @@ public function testParticipantOfflineReceipt() {
}

/**
* Shared preparation.
* Get prepared form object.
*
* @param array $eventParams
*
* @return CRM_Event_Form_Participant
*/
protected function commonPrepare($eventParams = array()) {
$event = $this->eventCreate($eventParams);
protected function getForm($eventParams = array()) {
if (!empty($eventParams['is_monetary'])) {
$event = $this->eventCreatePaid($eventParams);
}
else {
$event = $this->eventCreate($eventParams);
}

$contactID = $this->individualCreate();
$form = $this->getFormObject('CRM_Event_Form_Participant');
$form->_single = TRUE;
$form->_contactId = $contactID;
$form->_contactID = $form->_contactId = $contactID;
$form->setCustomDataTypes();
$form->_eventId = $event['id'];
if (!empty($eventParams['is_monetary'])) {
$form->_mode = 'Live';
$form->_bltID = 5;
$form->_values['fee'] = array();
$form->_isPaidEvent = TRUE;
Expand Down
43 changes: 33 additions & 10 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,23 @@ public function eventCreate($params = array()) {
return $this->callAPISuccess('Event', 'create', $params);
}

/**
* Create a paid event.
*
* @param array $params
*
* @return array
*/
protected function eventCreatePaid($params) {
$event = $this->eventCreate($params);
$this->priceSetID = $this->eventPriceSetCreate(55, 0, 'Radio');
CRM_Price_BAO_PriceSet::addTo('civicrm_event', $event['id'], $this->priceSetID);
$priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->priceSetID, TRUE, FALSE);
$priceSet = CRM_Utils_Array::value($this->priceSetID, $priceSet);
$this->eventFeeBlock = CRM_Utils_Array::value('fields', $priceSet);
return $event;
}

/**
* Delete event.
*
Expand Down Expand Up @@ -3325,29 +3342,26 @@ protected function assertAttachmentExistence($exists, $apiResult) {
*
* @param int $feeTotal
* @param int $minAmt
* @param string $type
*
* @return int
* Price Set ID.
*/
protected function eventPriceSetCreate($feeTotal, $minAmt = 0) {
protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text') {
// creating price set, price field
$paramsSet['title'] = 'Price Set';
$paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
$paramsSet['is_active'] = FALSE;
$paramsSet['extends'] = 1;
$paramsSet['min_amount'] = $minAmt;

$priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
$priceSetId = $priceset->id;
$priceSet = CRM_Price_BAO_PriceSet::create($paramsSet);
$this->_ids['price_set'] = $priceSet->id;

//Checking for priceset added in the table.
$this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
'id', $paramsSet['title'], 'Check DB for created priceset'
);
$paramsField = array(
'label' => 'Price Field',
'name' => CRM_Utils_String::titleToVar('Price Field'),
'html_type' => 'Text',
'html_type' => $type,
'price' => $feeTotal,
'option_label' => array('1' => 'Price Field'),
'option_value' => array('1' => $feeTotal),
Expand All @@ -3358,13 +3372,22 @@ protected function eventPriceSetCreate($feeTotal, $minAmt = 0) {
'weight' => 1,
'options_per_line' => 1,
'is_active' => array('1' => 1),
'price_set_id' => $priceset->id,
'price_set_id' => $this->_ids['price_set'],
'is_enter_qty' => 1,
'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
);
if ($type === 'Radio') {
$paramsField['is_enter_qty'] = 0;
$paramsField['option_value'][2] = $paramsField['option_weight'][2] = $paramsField['option_amount'][2] =100;
$paramsField['option_label'][2] = $paramsField['option_name'][2] = 'hundy';
}
CRM_Price_BAO_PriceField::create($paramsField);
$fields = $this->callAPISuccess('PriceField', 'get', array('price_set_id' => $this->_ids['price_set']));
$this->_ids['price_field'] = array_keys($fields['values']);
$fieldValues = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $this->_ids['price_field'][0]));
$this->_ids['price_field_value'] = array_keys($fieldValues['values']);

return $priceSetId;
return $this->_ids['price_set'];
}

/**
Expand Down

0 comments on commit 3baa971

Please sign in to comment.