Action schedule API modifications
authorvivekarora <vivek11112@gmail.com>
Tue, 3 Dec 2013 12:05:40 +0000 (17:35 +0530)
committervivekarora <vivek11112@gmail.com>
Tue, 3 Dec 2013 12:05:40 +0000 (17:35 +0530)
api/v3/ActionSchedule.php
tests/phpunit/api/v3/ActionScheduleTest.php

index 1e32f917d1602ad4e7edbf492fdc13b7b544c55d..d910f68ae41c9872eaa3aab5b0c53a81bf2f4138 100644 (file)
@@ -60,6 +60,16 @@ function civicrm_api3_action_schedule_get($params) {
  * {@getfields action_schedule_create}
  */
 function civicrm_api3_action_schedule_create($params) {
+  if (!CRM_Utils_Array::value('id', $params)) {
+    // an update does not require any mandatory parameters
+    civicrm_api3_verify_one_mandatory($params,
+      NULL,
+      array(
+        'title','mapping_id', 'entity_status', 'entity_value',
+      )
+    );
+  }
+  
   $ids = array();
   if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) {
     return civicrm_api3_create_error('Invalid value for ID');
@@ -103,4 +113,6 @@ function _civicrm_api3_action_schedule_create_spec(&$params) {
  */
 function civicrm_api3_action_schedule_delete($params) {
   return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
-}
\ No newline at end of file
+}
+
+
index aef5f44f4632a1509ae77558f4c2db60e9b99c2d..b7a483349a3cfc85602882cba0e17733468bc6bf 100644 (file)
@@ -39,6 +39,7 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase {
   protected $_apiversion = 3;
 
   public $_eNoticeCompliant = TRUE;
+  
   /**
    *  Test setup for every test
    *
@@ -65,21 +66,82 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase {
   }
 
   
-  function testActionScheduleCreate() {
-       
+  function testSimpleActionScheduleCreate() {
        $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
+       $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
+       $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
+       $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
+       $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
+       $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
+       $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
+       $title = "simpleActionSchedule".substr(sha1(rand()), 0, 7);
        $params = array(
-      'title' => 'simpleAction',
-      'entity_value' => '46',
+      'title' => $title,
+         'recipient' => $assigneeID,
+         'limit_to' => 1,
+         'entity_value' => $activityTypeId,
+         'entity_status' => $scheduledStatus,
+         'is_active' => 1,
+         'record_activity' => 1,
+         'mapping_id' => $mappingId,
     );
-       
        $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
        $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
        $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
        $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
        $this->assertEquals($oldCount+1, $newCount);
-
   }
-
+  
+  /**
+   * check if required fields are not passed
+   */
+  function testActionScheduleCreateWithoutRequired() {
+       $params = array(
+                       'subject' => 'this case should fail',
+                       'scheduled_date_time' => date('Ymd'),
+       );
+       $result = $this->callAPIFailure('activity', 'create', $params);
+  }
+  
+  function testActionScheduleWithScheduledDatesCreate() {
+       $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
+       $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
+       $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
+       $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
+       $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
+       $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
+       $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
+       $title = "simpleActionSchedule".substr(sha1(rand()), 0, 7);
+       $params = array(
+      'title' => $title,
+         'recipient' => $assigneeID,
+         'limit_to' => 1,
+         'entity_value' => $activityTypeId,
+         'entity_status' => $scheduledStatus,
+         'is_active' => 1,
+         'record_activity' => 1,
+         'mapping_id' => $mappingId,
+         'start_action_offset' => 3,
+         'start_action_unit' => 'day',
+         'start_action_condition' => 'before',
+         'start_action_date' => 'activity_date_time',
+         'is_repeat' => 1,
+         'repetition_frequency_unit'=> 'day',
+         'repetition_frequency_interval' => 3,
+         'end_frequency_unit' => 'hour',
+         'end_frequency_interval' => 0,
+         'end_action' => 'before',
+         'end_date' => 'activity_date_time',
+         'body_html' => 'Test description',
+         'subject' => 'Test subject'
+    );
+       $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
+       $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
+       $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
+       $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'][0], $params['start_action_offset']);
+       $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
+       $this->assertEquals($oldCount+1, $newCount);
+       
+  }
 
 }