Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * File for the CiviCRM APIv3 job functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Job
33 *
34 * @copyright CiviCRM LLC (c) 2004-2013
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
36 *
37 */
38require_once 'CiviTest/CiviUnitTestCase.php';
39class api_v3_JobTest extends CiviUnitTestCase {
f39bacdf 40 protected $_apiversion = 3;
6a488035
TO
41 public $_eNoticeCompliant = TRUE;
42 public $DBResetRequired = FALSE;
43 public $_entity = 'Job';
ef1672da 44 public $_params = array();
6a488035
TO
45
46 function setUp() {
47 parent::setUp();
ef1672da 48 $this->_params = array(
49 'sequential' => 1,
50 'name' => 'API_Test_Job',
51 'description' => 'A long description written by hand in cursive',
52 'run_frequency' => 'Daily',
53 'api_entity' => 'ApiTestEntity',
54 'api_action' => 'apitestaction',
55 'parameters' => 'Semi-formal explanation of runtime job parameters',
56 'is_active' => 1,
57 );
6a488035
TO
58 }
59
60 function tearDown() {
61 $this->quickCleanup(array('civicrm_job'));
49f8272d 62 CRM_Utils_Hook::singleton()->reset();
6a488035
TO
63 parent::tearDown();
64 }
65
66 /**
67 * check with no name
68 */
69 function testCreateWithoutName() {
ef1672da 70 $params = array(
71 'is_active' => 1, );
f39bacdf 72 $result = $this->callAPIFailure('job', 'create', $params,
73 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
74 );
75 }
6a488035
TO
76
77 /**
78 * create job with an invalid "run_frequency" value
79 */
80 function testCreateWithInvalidFrequency() {
81 $params = array(
6a488035
TO
82 'sequential' => 1,
83 'name' => 'API_Test_Job',
84 'description' => 'A long description written by hand in cursive',
85 'run_frequency' => 'Fortnightly',
86 'api_entity' => 'ApiTestEntity',
87 'api_action' => 'apitestaction',
88 'parameters' => 'Semi-formal explanation of runtime job parameters',
89 'is_active' => 1,
90 );
d0e1eff2 91 $result = $this->callAPIFailure('job', 'create', $params);
6a488035
TO
92 }
93
94 /**
95 * create job
96 */
97 function testCreate() {
ef1672da 98 $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
6a488035
TO
99 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
100
101 // mutate $params to match expected return value
ef1672da 102 unset($this->_params['sequential']);
6a488035 103 //assertDBState compares expected values in $result to actual values in the DB
ef1672da 104 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params);
6a488035
TO
105 }
106
107 /**
108 * check with empty array
109 */
110 function testDeleteEmpty() {
111 $params = array();
d0e1eff2 112 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
113 }
114
115 /**
116 * check with No array
117 */
118 function testDeleteParamsNotArray() {
d0e1eff2 119 $result = $this->callAPIFailure('job', 'delete', 'string');
6a488035
TO
120 }
121
122 /**
123 * check if required fields are not passed
124 */
125 function testDeleteWithoutRequired() {
126 $params = array(
127 'name' => 'API_Test_PP',
128 'title' => 'API Test Payment Processor',
129 'class_name' => 'CRM_Core_Payment_APITest',
130 );
131
d0e1eff2
CW
132 $result = $this->callAPIFailure('job', 'delete', $params);
133 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
6a488035
TO
134 }
135
136 /**
137 * check with incorrect required fields
138 */
139 function testDeleteWithIncorrectData() {
140 $params = array(
ef1672da 141 'id' => 'abcd', );
d0e1eff2 142 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
143 }
144
145 /**
146 * check job delete
147 */
148 function testDelete() {
ef1672da 149 $createResult = $this->callAPISuccess('job', 'create', $this->_params);
150 $params = array('id' => $createResult['id'],);
f39bacdf 151 $result = $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__, __FILE__);
ef1672da 152 $this->assertAPIDeleted($this->_entity, $createResult['id']);
6a488035
TO
153 }
154
155 /**
156
157 public function testCallUpdateGreetingMissingParams() {
f39bacdf 158 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
6a488035
TO
159 $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
160 }
161
162 public function testCallUpdateGreetingIncorrectParams() {
f39bacdf 163 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
6a488035
TO
164 $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
165 }
166/*
167 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
168 */
169 public function testCallUpdateGreetingSuccess() {
f39bacdf 170 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual'));
6a488035
TO
171 }
172
173 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
174 $gt = 'postal_greeting,email_greeting,addressee';
175 $ct = 'Individual,Household';
f39bacdf 176 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
6a488035 177 }
49f8272d
E
178
179 public function testCallDisableExpiredRelationships() {
180 $individualID = $this->individualCreate();
181 $orgID = $this->organizationCreate();
182 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
183 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array('return' => 'id', 'name_a_b' => 'Employee of'));
184 $result = $this->callAPISuccess('relationship', 'create', array(
185 'relationship_type_id' => $relationshipTypeID,
186 'contact_id_a' => $individualID,
187 'contact_id_b' => $orgID,
188 'is_active' => 1,
189 'end_date' => 'yesterday',
190 ));
191 $relationshipID = $result['id'];
192 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
193 $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array());
194 $result = $this->callAPISuccess('relationship', 'get', array());
195 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
196 $this->contactDelete($individualID);
197 $this->contactDelete($orgID);
198 }
199
200 function hookPreRelationship($op, $objectName, $id, &$params ) {
201 if($op == 'delete') {
202 return;
203 }
204 if($params['is_active']) {
205 $params['description'] = 'Hooked';
206 }
207 else {
208 $params['description'] = 'Go Go you good thing';
209 }
210 }
6a488035
TO
211}
212