Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
index 3b73eaa5ce5fa6f12b5ff34fb53513dfc697858e..143cf3f788721595d215cc3b1457514ca31425f9 100644 (file)
@@ -1,8 +1,7 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
  */
 require_once 'CiviTest/CiviUnitTestCase.php';
 class api_v3_JobTest extends CiviUnitTestCase {
-  protected $_apiversion;
-
+  protected $_apiversion = 3;
   public $_eNoticeCompliant = TRUE;
   public $DBResetRequired = FALSE;
   public $_entity = 'Job';
-  public $_apiVersion = 3;
+  public $_params = array();
 
   function setUp() {
     parent::setUp();
-    $this->quickCleanup(array('civicrm_job'));
+    $this->_params = array(
+      'sequential' => 1,
+      'name' => 'API_Test_Job',
+      'description' => 'A long description written by hand in cursive',
+      'run_frequency' => 'Daily',
+      'api_entity' => 'ApiTestEntity',
+      'api_action' => 'apitestaction',
+      'parameters' => 'Semi-formal explanation of runtime job parameters',
+      'is_active' => 1,
+    );
   }
 
   function tearDown() {
     $this->quickCleanup(array('civicrm_job'));
+    CRM_Utils_Hook::singleton()->reset();
     parent::tearDown();
   }
 
@@ -60,13 +68,8 @@ class api_v3_JobTest extends CiviUnitTestCase {
    */
   function testCreateWithoutName() {
     $params = array(
-      'is_active' => 1,
-      'version' => $this->_apiVersion,
-    );
-    $result = civicrm_api('job', 'create', $params);
-
-    $this->assertEquals($result['is_error'], 1);
-    $this->assertEquals($result['error_message'],
+      'is_active' => 1,    );
+    $result = $this->callAPIFailure('job', 'create', $params,
       'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
     );
   }
@@ -76,7 +79,6 @@ class api_v3_JobTest extends CiviUnitTestCase {
    */
   function testCreateWithInvalidFrequency() {
     $params = array(
-      'version' => $this->_apiVersion,
       'sequential' => 1,
       'name' => 'API_Test_Job',
       'description' => 'A long description written by hand in cursive',
@@ -86,35 +88,20 @@ class api_v3_JobTest extends CiviUnitTestCase {
       'parameters' => 'Semi-formal explanation of runtime job parameters',
       'is_active' => 1,
     );
-    $result = civicrm_api('job', 'create', $params);
-    $this->assertEquals($result['is_error'], 1);
+    $result = $this->callAPIFailure('job', 'create', $params);
   }
 
   /**
    * create job
    */
   function testCreate() {
-    $params = array(
-      'version' => $this->_apiVersion,
-      'sequential' => 1,
-      'name' => 'API_Test_Job',
-      'description' => 'A long description written by hand in cursive',
-      'run_frequency' => 'Daily',
-      'api_entity' => 'ApiTestEntity',
-      'api_action' => 'apitestaction',
-      'parameters' => 'Semi-formal explanation of runtime job parameters',
-      'is_active' => 1,
-    );
-    $result = civicrm_api('job', 'create', $params);
-    $this->assertAPISuccess($result);
-    $this->documentMe($params, $result, __FUNCTION__, __FILE__);
+    $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
     $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
 
     // mutate $params to match expected return value
-    unset($params['version']);
-    unset($params['sequential']);
+    unset($this->_params['sequential']);
     //assertDBState compares expected values in $result to actual values in the DB
-    $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $params);
+    $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params);
   }
 
   /**
@@ -122,16 +109,14 @@ class api_v3_JobTest extends CiviUnitTestCase {
    */
   function testDeleteEmpty() {
     $params = array();
-    $result = civicrm_api('job', 'delete', $params);
-    $this->assertEquals($result['is_error'], 1);
+    $result = $this->callAPIFailure('job', 'delete', $params);
   }
 
   /**
    * check with No array
    */
   function testDeleteParamsNotArray() {
-    $result = civicrm_api('job', 'delete', 'string');
-    $this->assertEquals($result['is_error'], 1);
+    $result = $this->callAPIFailure('job', 'delete', 'string');
   }
 
   /**
@@ -144,9 +129,8 @@ class api_v3_JobTest extends CiviUnitTestCase {
       'class_name' => 'CRM_Core_Payment_APITest',
     );
 
-    $result = civicrm_api('job', 'delete', $params);
-    $this->assertEquals($result['is_error'], 1);
-    $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, id');
+    $result = $this->callAPIFailure('job', 'delete', $params);
+    $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
   }
 
   /**
@@ -154,67 +138,75 @@ class api_v3_JobTest extends CiviUnitTestCase {
    */
   function testDeleteWithIncorrectData() {
     $params = array(
-      'id' => 'abcd',
-      'version' => $this->_apiVersion,
-    );
-
-    $result = civicrm_api('job', 'delete', $params);
-
-    $this->assertEquals($result['is_error'], 1);
-    $this->assertEquals($result['error_message'], 'Invalid value for job ID');
+      'id' => 'abcd',    );
+    $result = $this->callAPIFailure('job', 'delete', $params);
   }
 
   /**
    * check job delete
    */
   function testDelete() {
-    $createParams = array(
-      'version' => $this->_apiVersion,
-      'sequential' => 1,
-      'name' => 'API_Test_Job',
-      'description' => 'A long description written by hand in cursive',
-      'run_frequency' => 'Daily',
-      'api_entity' => 'ApiTestEntity',
-      'api_action' => 'apitestaction',
-      'parameters' => 'Semi-formal explanation of runtime job parameters',
-      'is_active' => 1,
-    );
-    $createResult = civicrm_api('job', 'create', $createParams);
-    $this->assertAPISuccess($createResult);
-
-    $params = array(
-      'id' => $createResult['id'],
-      'version' => $this->_apiVersion,
-    );
-    $result = civicrm_api('job', 'delete', $params);
-    $this->documentMe($params, $result, __FUNCTION__, __FILE__);
-    $this->assertAPISuccess($result);
+    $createResult = $this->callAPISuccess('job', 'create', $this->_params);
+    $params = array('id' => $createResult['id'],);
+    $result = $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__, __FILE__);
+    $this->assertAPIDeleted($this->_entity, $createResult['id']);
   }
 
   /**
 
   public function testCallUpdateGreetingMissingParams() {
-    $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 1, 'version' => $this->_apiVersion));
+    $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
     $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
   }
 
   public function testCallUpdateGreetingIncorrectParams() {
-    $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds', 'version' => $this->_apiVersion));
+    $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
     $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
   }
 /*
  * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
  */
   public function testCallUpdateGreetingSuccess() {
-    $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual', 'version' => $this->_apiVersion));
-    $this->assertAPISuccess($result);
+    $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual'));
    }
 
   public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
     $gt = 'postal_greeting,email_greeting,addressee';
     $ct = 'Individual,Household';
-    $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct, 'version' => $this->_apiVersion));
-    $this->assertAPISuccess($result);
+    $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
+  }
+
+  public function testCallDisableExpiredRelationships() {
+    $individualID = $this->individualCreate();
+    $orgID = $this->organizationCreate();
+    CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
+    $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array('return' => 'id', 'name_a_b' => 'Employee of'));
+    $result = $this->callAPISuccess('relationship', 'create', array(
+      'relationship_type_id' => $relationshipTypeID,
+      'contact_id_a' => $individualID,
+      'contact_id_b' => $orgID,
+      'is_active' => 1,
+      'end_date' => 'yesterday',
+    ));
+    $relationshipID = $result['id'];
+    $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
+    $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array());
+    $result = $this->callAPISuccess('relationship', 'get', array());
+    $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
+    $this->contactDelete($individualID);
+    $this->contactDelete($orgID);
+  }
+
+  function hookPreRelationship($op, $objectName, $id, &$params ) {
+    if($op == 'delete') {
+      return;
+    }
+    if($params['is_active']) {
+      $params['description'] = 'Hooked';
+    }
+    else {
+      $params['description'] = 'Go Go you good thing';
+    }
   }
 }