Merge pull request #3118 from yashodha/CRM-14134
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 *
06a1bc01 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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;
b7c9bc4c 41
6a488035
TO
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'));
0090e3d2 62 $this->quickCleanUpFinancialEntities();
49f8272d 63 CRM_Utils_Hook::singleton()->reset();
6a488035
TO
64 parent::tearDown();
65 }
66
67 /**
68 * check with no name
69 */
70 function testCreateWithoutName() {
ef1672da 71 $params = array(
72 'is_active' => 1, );
f39bacdf 73 $result = $this->callAPIFailure('job', 'create', $params,
74 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
75 );
76 }
6a488035
TO
77
78 /**
79 * create job with an invalid "run_frequency" value
80 */
81 function testCreateWithInvalidFrequency() {
82 $params = array(
6a488035
TO
83 'sequential' => 1,
84 'name' => 'API_Test_Job',
85 'description' => 'A long description written by hand in cursive',
86 'run_frequency' => 'Fortnightly',
87 'api_entity' => 'ApiTestEntity',
88 'api_action' => 'apitestaction',
89 'parameters' => 'Semi-formal explanation of runtime job parameters',
90 'is_active' => 1,
91 );
d0e1eff2 92 $result = $this->callAPIFailure('job', 'create', $params);
6a488035
TO
93 }
94
95 /**
96 * create job
97 */
98 function testCreate() {
ef1672da 99 $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
6a488035
TO
100 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
101
102 // mutate $params to match expected return value
ef1672da 103 unset($this->_params['sequential']);
6a488035 104 //assertDBState compares expected values in $result to actual values in the DB
ef1672da 105 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params);
6a488035
TO
106 }
107
108 /**
109 * check with empty array
110 */
111 function testDeleteEmpty() {
112 $params = array();
d0e1eff2 113 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
114 }
115
116 /**
117 * check with No array
118 */
119 function testDeleteParamsNotArray() {
d0e1eff2 120 $result = $this->callAPIFailure('job', 'delete', 'string');
6a488035
TO
121 }
122
123 /**
124 * check if required fields are not passed
125 */
126 function testDeleteWithoutRequired() {
127 $params = array(
128 'name' => 'API_Test_PP',
129 'title' => 'API Test Payment Processor',
130 'class_name' => 'CRM_Core_Payment_APITest',
131 );
132
d0e1eff2
CW
133 $result = $this->callAPIFailure('job', 'delete', $params);
134 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
6a488035
TO
135 }
136
137 /**
138 * check with incorrect required fields
139 */
140 function testDeleteWithIncorrectData() {
141 $params = array(
ef1672da 142 'id' => 'abcd', );
d0e1eff2 143 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
144 }
145
146 /**
147 * check job delete
148 */
149 function testDelete() {
ef1672da 150 $createResult = $this->callAPISuccess('job', 'create', $this->_params);
151 $params = array('id' => $createResult['id'],);
f39bacdf 152 $result = $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__, __FILE__);
ef1672da 153 $this->assertAPIDeleted($this->_entity, $createResult['id']);
6a488035
TO
154 }
155
156 /**
157
158 public function testCallUpdateGreetingMissingParams() {
f39bacdf 159 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
6a488035
TO
160 $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
161 }
162
163 public function testCallUpdateGreetingIncorrectParams() {
f39bacdf 164 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
6a488035
TO
165 $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
166 }
167/*
168 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
169 */
170 public function testCallUpdateGreetingSuccess() {
f39bacdf 171 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual'));
6a488035
TO
172 }
173
174 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
175 $gt = 'postal_greeting,email_greeting,addressee';
176 $ct = 'Individual,Household';
f39bacdf 177 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
6a488035 178 }
49f8272d 179
d946b676 180 /**
181 * test the call reminder success sends more than 25 reminders & is not incorrectly limited
182 * Note that this particular test sends the reminders to the additional recipients only
183 * as no real reminder person is configured
184 *
185 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
186 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
187 * that the api itself would need to be moved to the scheduled_reminder fn to do that with the job wrapper being respected for legacy functions
188 */
189 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
190 $membershipTypeID = $this->membershipTypeCreate();
0090e3d2 191 $this->membershipStatusCreate();
d946b676 192 $createTotal = 30;
193 for($i = 1; $i <= $createTotal; $i++) {
194 $contactID = $this->individualCreate();
195 $groupID = $this->groupCreate(array('name' => $i, 'title' => $i));
196 $result = $this->callAPISuccess('action_schedule', 'create', array(
197 'title' => " job $i",
198 'subject' => "job $i",
199 'entity_value' => $membershipTypeID,
200 'mapping_id' => 4,
201 'start_action_date' => 'membership_join_date',
202 'start_action_offset' => 0,
203 'start_action_condition' => 'before',
204 'start_action_unit' => 'hour',
205 'group_id' => $groupID,
206 'limit_to' => FALSE,
207 ));
208 $this->callAPISuccess('group_contact', 'create', array('contact_id' => $contactID, 'status' => 'Added', 'group_id' => $groupID));
209 }
210 $result = $this->callAPISuccess('job', 'send_reminder', array());
211 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
212 $this->assertEquals($successfulCronCount, $createTotal);
213 }
214
49f8272d
E
215 public function testCallDisableExpiredRelationships() {
216 $individualID = $this->individualCreate();
217 $orgID = $this->organizationCreate();
218 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
219 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array('return' => 'id', 'name_a_b' => 'Employee of'));
220 $result = $this->callAPISuccess('relationship', 'create', array(
221 'relationship_type_id' => $relationshipTypeID,
222 'contact_id_a' => $individualID,
223 'contact_id_b' => $orgID,
224 'is_active' => 1,
225 'end_date' => 'yesterday',
226 ));
227 $relationshipID = $result['id'];
228 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
229 $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array());
230 $result = $this->callAPISuccess('relationship', 'get', array());
231 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
232 $this->contactDelete($individualID);
233 $this->contactDelete($orgID);
234 }
235
236 function hookPreRelationship($op, $objectName, $id, &$params ) {
237 if($op == 'delete') {
238 return;
239 }
240 if($params['is_active']) {
241 $params['description'] = 'Hooked';
242 }
243 else {
244 $params['description'] = 'Go Go you good thing';
245 }
246 }
6a488035
TO
247}
248