generalize skip fields & testing linked-entities
authorpriyankakaran26 <priyanka.karan26@gmail.com>
Sat, 13 Sep 2014 18:31:42 +0000 (19:31 +0100)
committerdeepak-srivastava <deepak.srivastava.0303@gmail.com>
Fri, 3 Oct 2014 09:08:26 +0000 (10:08 +0100)
CRM/Core/BAO/RecurringEntity.php
CRM/Core/Page/AJAX/RecurringEntity.php
tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php

index 9d58f37c9a897ba6cbedf1b44d26eaee4c62e7ed..0c7253e901419174d7826bfafc72bf8b72a5ffb5 100644 (file)
 require_once 'packages/When/When.php'; 
 
 class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
-  public $linkedEntities = array();
+
   public $schedule = array();
   public $scheduleId = NULL;
-  public $scheduleDBParams = array();
+  public $scheduleFormValues = array();
 
   public $dateColumns = array();
   public $overwriteColumns = array();
   public $intervalDateColumns = array();
-
   public $excludeDates = array();
-
-  public $recursion = NULL;
-  public $recursionCounter = NULL;
+  
+  public $linkedEntities = array();
 
   public $isRecurringEntityRecord = TRUE;
 
+  protected $recursion = NULL;
+
   static $_tableDAOMapper = 
     array(
       'civicrm_event'       => 'CRM_Event_DAO_Event',
@@ -63,6 +63,15 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       'civicrm_activity_contact'  => 'CRM_Activity_DAO_ActivityContact',
     );
 
+
+  static $_updateSkipFields = 
+    array(
+      'civicrm_event'       => array('start_date', 'end_date'),
+      'civicrm_tell_friend' => array('entity_id'),
+      'civicrm_pcp_block'   => array('entity_id'),
+      'civicrm_activity'    => array('activity_date_time'),
+    );
+
   static function add(&$params) {
     if (CRM_Utils_Array::value('id', $params)) {
       CRM_Utils_Hook::pre('edit', 'RecurringEntity', $params['id'], $params);
@@ -121,13 +130,13 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
     if ($this->scheduleId) {
       // get params by ID
-      $this->recursion = $this->getRecursionFromReminder($this->scheduleId);
-    } else if (!empty($this->schedule)) {
-      $this->scheduleDBParams = $this->mapFormValuesToDB($this->schedule);//call using obj
+      $this->schedule = $this->getScheduleParams($this->scheduleId);
+    } else if (!empty($this->scheduleFormValues)) {
+      $this->schedule = $this->mapFormValuesToDB($this->scheduleFormValues);
     }
 
-    if (!empty($this->scheduleDBParams)) {
-      $this->recursion = $this->getRecursionFromReminderByDBParams($this->scheduleDBParams);
+    if (!empty($this->schedule)) {
+      $this->recursion = $this->getRecursionFromSchedule($this->schedule);
     }
     return $this->recursion;
   }
@@ -162,7 +171,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
           $this->isRecurringEntityRecord
         );
 
-        if ($obj->id) {
+        if (is_a($obj, 'CRM_Core_DAO') && $obj->id) {
           $newCriteria = array();
           $newEntities[$this->entity_table][$count] = $obj->id;
 
@@ -176,7 +185,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
               CRM_Utils_Array::value('isRecurringEntityRecord', $linkedInfo, TRUE)
             );
 
-            if ($linkedObj->id) {
+            if (is_a($linkedObj, 'CRM_Core_DAO') && $linkedObj->id) {
               $newEntities[$linkedInfo['table']][$count] = $linkedObj->id;
             }
           }
@@ -193,7 +202,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
 
     $recursionDates = array();
     if (is_a($this->recursion, 'When')) { 
-      $initialCount = CRM_Utils_Array::value('start_action_offset', $this->scheduleDBParams);
+      $initialCount = CRM_Utils_Array::value('start_action_offset', $this->scheduleFormValues);
 
       $exRangeStart = $exRangeEnd = NULL;
       if (!empty($this->excludeDateRangeColumns)) {
@@ -242,7 +251,6 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
             // lets increase the counter, so we get correct number of occurrences
             $initialCount++;
             $this->recursion->count($initialCount);
-            $this->recursionCounter = $initialCount;
           }
           continue;
         }
@@ -334,7 +342,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     }
     $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams);
 
-    if ($newObject && $newObject->id && $createRecurringEntity) {
+    if (is_a($newObject, 'CRM_Core_DAO') && $newObject->id && $createRecurringEntity) {
       $object = new $daoName( );
       foreach ($fromCriteria as $key => $value) {
         $object->$key = $value;
@@ -386,12 +394,15 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
       $processedEntities[$key] = 1;
 
       if (array_key_exists($entityTable, self::$_tableDAOMapper)) {
-        $daoName = self::$_tableDAOMapper[$entityTable];
+        $daoName  = self::$_tableDAOMapper[$entityTable];
 
-        // FIXME: generalize me
-        $skipData = array('start_date' => NULL, 
-          'end_date' => NULL,
-        );
+        $skipData = array();
+        if (array_key_exists($entityTable, self::$_updateSkipFields)) {
+          $skipFields = self::$_updateSkipFields[$entityTable];
+          foreach ($skipFields as $sfield) {
+            $skipData[$sfield] = NULL;
+          }
+        }
 
         $updateDAO = CRM_Core_DAO::cascadeUpdate($daoName, $obj->id, $entityID, $skipData);
         CRM_Core_DAO::freeResult();
@@ -485,17 +496,17 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     return $dao;
   }
 
-  function getRecursionFromReminder($scheduleReminderId){
-    if($scheduleReminderId){
+  function getScheduleParams($scheduleReminderId) {
+    $scheduleReminderDetails = array();
+    if ($scheduleReminderId){
       //Get all the details from schedule reminder table
       $scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId);
       $scheduleReminderDetails = (array) $scheduleReminderDetails;
-      $recursionDetails = self::getRecursionFromReminderByDBParams($scheduleReminderDetails);
     }
-    return $recursionDetails;
+    return $scheduleReminderDetails;
   }
 
-  function getRecursionFromReminderByDBParams($scheduleReminderDetails = array()){
+  function getRecursionFromSchedule($scheduleReminderDetails = array()){
     $r = new When();
     //If there is some data for this id
     if($scheduleReminderDetails['repetition_frequency_unit']){
index 160361b43312d7827f6a0cfe56ab7c6423107f91..db5cce29525a8d36b317042c0868fd5890a88191 100644 (file)
@@ -41,7 +41,7 @@ class CRM_Core_Page_AJAX_RecurringEntity {
     if(!empty($formValues)){
       $recursion = new CRM_Core_BAO_RecurringEntity();
       $recursion->dateColumns  = array('start_date');
-      $recursion->schedule     = $formValues;
+      $recursion->scheduleFormValues = $formValues;
       if (!empty($formValues['exclude_date_list'])) {
         $recursion->excludeDates = $formValues['exclude_date_list'];
         $recursion->excludeDateRangeColumns = array('start_date', 'end_date');
index 584a6fb3ed44c9309c5bf42eec27daafd19417a1..be20d54eca8ded34c7a42a1e171590a590eab3cc 100644 (file)
@@ -76,7 +76,7 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     $recursion->entity_id    = $daoActivity->id;
     $recursion->entity_table = 'civicrm_activity';
     $recursion->dateColumns  = array('activity_date_time');
-    $recursion->scheduleDBParams = array(
+    $recursion->schedule     = array(
       'entity_value'      => $daoActivity->id,
       'start_action_date'     => $daoActivity->activity_date_time,
       'entity_status' => 'fourth saturday',
@@ -87,7 +87,7 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     );
 
     $generatedEntities = $recursion->generate(); 
-    foreach ($generatedEntities as $entityID) {
+    foreach ($generatedEntities['civicrm_activity'] as $entityID) {
       $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id',
         'id', 'Check DB if repeating activities were created'
       );
@@ -102,7 +102,7 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     $daoActivity->save();
 
     // check if other activities were affected
-    foreach ($generatedEntities as $entityID) {
+    foreach ($generatedEntities['civicrm_activity'] as $entityID) {
       $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated');
     }
   }
@@ -146,7 +146,7 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     $recursion->entity_id    = $daoEvent->id;
     $recursion->entity_table = 'civicrm_event';
     $recursion->dateColumns  = array('start_date');
-    $recursion->scheduleDBParams = array (
+    $recursion->schedule     = array (
       'entity_value'                  => $daoEvent->id,
       'start_action_date'             => $daoEvent->start_date,
       'start_action_condition'        => 'wednesday',
@@ -239,7 +239,7 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     $daoEvent->save();
 
     // check if other events were affected
-    foreach ($generatedEntities as $entityID) {
+    foreach ($generatedEntities['civicrm_event'] as $entityID) {
       $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated');
     }
   }