From 543aa76e6bb3d57d21c0ad838c4abf02d484fbe0 Mon Sep 17 00:00:00 2001 From: Alok Patel Date: Mon, 23 Oct 2017 16:19:06 +0530 Subject: [PATCH] CRM-20787: Unit test for this issue. --- CRM/Core/BAO/RecurringEntity.php | 60 ++++++++++++++++++ CRM/Core/Page/AJAX/RecurringEntity.php | 42 +------------ .../CRM/Core/BAO/RecurringEntityTest.php | 63 +++++++++++++++++++ 3 files changed, 125 insertions(+), 40 deletions(-) diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index 2e4dd2471c..a41d9367a7 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -1179,4 +1179,64 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { return $result; } + /** + * Update mode in civicrm_recurring_entity table for event related data and price set in civicrm_price_set_entity. + * + * @param int $entityId + * Event id . + * @param string $entityTable + * @param string $mode + * @param string $linkedEntityTable + * Linked entity table name for this event . + * @param string $priceSet + * Price set of the event . + * + * @return array + */ + public static function updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet) { + $finalResult = array(); + + if (!empty($linkedEntityTable)) { + $result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $linkedEntityTable, $entityTable); + } + + $dao = new CRM_Core_DAO_RecurringEntity(); + if (!empty($result)) { + $dao->entity_id = $result['entityId']; + $dao->entity_table = $result['entityTable']; + } + else { + $dao->entity_id = $entityId; + $dao->entity_table = $entityTable; + } + + if ($dao->find(TRUE)) { + $dao->mode = $mode; + $dao->save(); + + //CRM-20787 Fix + //I am not sure about other fields, if mode = 3 apply for an event then other fields + //should be save for all other series events or not so applying for price set only for now here. + if (CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES === $mode) { + + //Step-1: Get all events of series + $seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable); + foreach ($seriesEventRecords as $event) { + //Step-3: Save price set in other series events + if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {//Remove existing priceset + CRM_Core_BAO_Discount::del($event['id'], $event['table']); + CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); //Add new price set + } + } + } + //CRM-20787 - Fix end + $finalResult['status'] = 'Done'; + } + else { + $finalResult['status'] = 'Error'; + } + + return $finalResult; + } + } diff --git a/CRM/Core/Page/AJAX/RecurringEntity.php b/CRM/Core/Page/AJAX/RecurringEntity.php index 9c1f3e9f89..9fe6b84f71 100644 --- a/CRM/Core/Page/AJAX/RecurringEntity.php +++ b/CRM/Core/Page/AJAX/RecurringEntity.php @@ -20,46 +20,8 @@ class CRM_Core_Page_AJAX_RecurringEntity { $entityId = CRM_Utils_Type::escape($_REQUEST['entityId'], 'Integer'); $entityTable = CRM_Utils_Type::escape($_REQUEST['entityTable'], 'String'); $priceSet = CRM_Utils_Type::escape($_REQUEST['priceSet'], 'String'); - - if (!empty($_REQUEST['linkedEntityTable'])) { - $result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $_REQUEST['linkedEntityTable'], $entityTable); - } - - $dao = new CRM_Core_DAO_RecurringEntity(); - if (!empty($result)) { - $dao->entity_id = $result['entityId']; - $dao->entity_table = $result['entityTable']; - } - else { - $dao->entity_id = $entityId; - $dao->entity_table = $entityTable; - } - - if ($dao->find(TRUE)) { - $dao->mode = $mode; - $dao->save(); - - //CRM-20787 Fix - //I am not sure about other fields, if mode = 3 apply for an event then other fields - //should be save for all other series events or not so applying for price set only for now here. - if (CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES === $mode) { - - //Step-1: Get all events of series - $seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable); - foreach ($seriesEventRecords as $event) { - //Step-3: Save price set in other series events - if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {//Remove existing priceset - CRM_Core_BAO_Discount::del($event['id'], $event['table']); - CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); //Add new price set - } - } - } - //CRM-20787 - Fix end - $finalResult['status'] = 'Done'; - } - else { - $finalResult['status'] = 'Error'; - } + $linkedEntityTable = $_REQUEST['linkedEntityTable']; + $finalResult = CRM_Core_BAO_RecurringEntity::updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet); } CRM_Utils_JSON::output($finalResult); } diff --git a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php index 9dd05181db..5f26ab18f0 100644 --- a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php +++ b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php @@ -105,6 +105,69 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase { } + /** + * Creating action schedule + */ + private function createActionSchedule($entity_id, $entity_table) { + $params = array( + "used_for" => $entity_table, + "entity_value" => $entity_id, + "start_action_date" => date("YmdHis"), + "repetition_frequency_unit" => "week", + "repetition_frequency_interval" => "3", + "start_action_condition" => "monday,tuesday,wednesday,thursday,friday,saturday", + "start_action_offset" => "2", + ); + $actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($params); + return $actionScheduleObj; + } + + /** + * Creating recurring entities + */ + private function createRecurringEntities($actionScheduleObj, $entity_id, $entity_table) { + $recursion = new CRM_Core_BAO_RecurringEntity(); + $recursion->dateColumns = array( + "start_date", + ); + $recursion->scheduleId = $actionScheduleObj->id; + $recursion->entity_id = $entity_id; + $recursion->entity_table = $entity_table; + $recursion->linkedEntities = array( + array( + "table" => "civicrm_price_set_entity", + "findCriteria" => array( + "entity_id" => $entity_id, + "entity_table" => $entity_table, + ), + "linkedColumns" => array( + "entity_id", + ), + "isRecurringEntityRecord" => FALSE, + ), + ); + return $recursion->generate(); + } + + /** + * Testing Event Generation through Entity Recursion. + */ + public function testRepeatEventCreation() { + $event = $this->eventCreate(); + $entity_table = "civicrm_event"; + $entity_id = $event["id"]; + CRM_Price_BAO_PriceSet::addTo($entity_table, $entity_id, 1); + $actionScheduleObj = $this->createActionSchedule($entity_id, $entity_table); + $recurringEntities = $this->createRecurringEntities($actionScheduleObj, $entity_id, $entity_table); + $finalResult = CRM_Core_BAO_RecurringEntity::updateModeAndPriceSet($entity_id, $entity_table, CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES, array(), 2); + $this->assertEquals(2, count($recurringEntities["civicrm_event"]), "Recurring events not created."); + $this->assertEquals(2, count($recurringEntities["civicrm_price_set_entity"]), "Recurring price sets not created."); + $priceSetOne = CRM_Price_BAO_PriceSet::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][0]); + $priceSetTwo = CRM_Price_BAO_PriceSet::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][1]); + $this->assertEquals(2, $priceSetOne, "Price set id of the recurring event is not updated."); + $this->assertEquals(2, $priceSetTwo, "Price set id of the recurring event is not updated."); + } + /** * Testing Event Generation through Entity Recursion. */ -- 2.25.1