CRM-20787: Unit test for this issue.
authorAlok Patel <alok@agileware.com.au>
Mon, 23 Oct 2017 10:49:06 +0000 (16:19 +0530)
committerAlok Patel <alok@agileware.com.au>
Mon, 23 Oct 2017 10:49:06 +0000 (16:19 +0530)
CRM/Core/BAO/RecurringEntity.php
CRM/Core/Page/AJAX/RecurringEntity.php
tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php

index 2e4dd2471cc7f25e1ed766907c140b33dc47b101..a41d9367a73f22bdbc91785b5f1665b8772f1297 100644 (file)
@@ -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;
+  }
+
 }
index 9c1f3e9f898ab2b295ac343195d2ffa775d56ed0..9fe6b84f713b93e7a4271ef14b45bee5812659c5 100644 (file)
@@ -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);
   }
index 9dd05181db58463b37de6ea88e436fa54429abf7..5f26ab18f0e552dc0a79ec536eedcabb32ec4e15 100644 (file)
@@ -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.
    */