From dafb4202de110d87f813f0b4f53a56a5d1419593 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 10 Nov 2017 13:15:48 +1300 Subject: [PATCH] Add unit test on financial items for pending payments. This test was supposed to demonstrate a failure but actually it demonstrates something working. Still, good to have an additional test --- CRM/Event/Form/Participant.php | 5 +- CRM/Financial/BAO/FinancialItem.php | 2 +- tests/phpunit/CRM/Event/BAO/CRM19273Test.php | 9 +- .../CRM/Event/Form/ParticipantTest.php | 108 ++++++++++++++++-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 43 +++++-- 5 files changed, 141 insertions(+), 26 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index f220463b4433..b9a1dec7cdcf 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -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] ); @@ -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']; diff --git a/CRM/Financial/BAO/FinancialItem.php b/CRM/Financial/BAO/FinancialItem.php index 81d6630261aa..32013dfe1be0 100644 --- a/CRM/Financial/BAO/FinancialItem.php +++ b/CRM/Financial/BAO/FinancialItem.php @@ -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(); diff --git a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php b/tests/phpunit/CRM/Event/BAO/CRM19273Test.php index 0675353f7c44..8726d83cb2c7 100644 --- a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php +++ b/tests/phpunit/CRM/Event/BAO/CRM19273Test.php @@ -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; diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 41acfe6e88e0..c1216626ed1c 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -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', @@ -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), ); @@ -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; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 324bb4ac2ebd..13a6a96f8b5e 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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. * @@ -3325,11 +3342,12 @@ 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'); @@ -3337,17 +3355,13 @@ protected function eventPriceSetCreate($feeTotal, $minAmt = 0) { $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), @@ -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']; } /**