Merge pull request #4185 from samuelsov/CRM-15330
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
36 *
37 */
38 require_once 'CiviTest/CiviUnitTestCase.php';
39
40 /**
41 * Class api_v3_JobTest
42 */
43 class api_v3_JobTest extends CiviUnitTestCase {
44 protected $_apiversion = 3;
45
46 public $DBResetRequired = FALSE;
47 public $_entity = 'Job';
48 public $_params = array();
49
50 public function setUp() {
51 parent::setUp();
52 $this->useTransaction(TRUE);
53 $this->_params = array(
54 'sequential' => 1,
55 'name' => 'API_Test_Job',
56 'description' => 'A long description written by hand in cursive',
57 'run_frequency' => 'Daily',
58 'api_entity' => 'ApiTestEntity',
59 'api_action' => 'apitestaction',
60 'parameters' => 'Semi-formal explanation of runtime job parameters',
61 'is_active' => 1,
62 );
63 }
64
65 /**
66 * Check with no name
67 */
68 public function testCreateWithoutName() {
69 $params = array(
70 'is_active' => 1,
71 );
72 $result = $this->callAPIFailure('job', 'create', $params,
73 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
74 );
75 }
76
77 /**
78 * Create job with an invalid "run_frequency" value
79 */
80 public function testCreateWithInvalidFrequency() {
81 $params = array(
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 );
91 $result = $this->callAPIFailure('job', 'create', $params);
92 }
93
94 /**
95 * Create job
96 */
97 public function testCreate() {
98 $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
99 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
100
101 // mutate $params to match expected return value
102 unset($this->_params['sequential']);
103 //assertDBState compares expected values in $result to actual values in the DB
104 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params);
105 }
106
107 /**
108 * Check with empty array
109 */
110 public function testDeleteEmpty() {
111 $params = array();
112 $result = $this->callAPIFailure('job', 'delete', $params);
113 }
114
115 /**
116 * Check with No array
117 */
118 public function testDeleteParamsNotArray() {
119 $result = $this->callAPIFailure('job', 'delete', 'string');
120 }
121
122 /**
123 * Check if required fields are not passed
124 */
125 public 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
132 $result = $this->callAPIFailure('job', 'delete', $params);
133 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
134 }
135
136 /**
137 * Check with incorrect required fields
138 */
139 public function testDeleteWithIncorrectData() {
140 $params = array(
141 'id' => 'abcd',
142 );
143 $result = $this->callAPIFailure('job', 'delete', $params);
144 }
145
146 /**
147 * Check job delete
148 */
149 public function testDelete() {
150 $createResult = $this->callAPISuccess('job', 'create', $this->_params);
151 $params = array('id' => $createResult['id']);
152 $result = $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__, __FILE__);
153 $this->assertAPIDeleted($this->_entity, $createResult['id']);
154 }
155
156 /**
157 *
158 * public function testCallUpdateGreetingMissingParams() {
159 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
160 * $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
161 * }
162 *
163 * public function testCallUpdateGreetingIncorrectParams() {
164 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
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() {
171 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array(
172 'gt' => 'postal_greeting',
173 'ct' => 'Individual',
174 ));
175 }
176
177 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
178 $gt = 'postal_greeting,email_greeting,addressee';
179 $ct = 'Individual,Household';
180 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
181 }
182
183 /**
184 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited
185 * Note that this particular test sends the reminders to the additional recipients only
186 * as no real reminder person is configured
187 *
188 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
189 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
190 * 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
191 */
192 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
193 $membershipTypeID = $this->membershipTypeCreate();
194 $this->membershipStatusCreate();
195 $createTotal = 30;
196 for ($i = 1; $i <= $createTotal; $i++) {
197 $contactID = $this->individualCreate();
198 $groupID = $this->groupCreate(array('name' => $i, 'title' => $i));
199 $result = $this->callAPISuccess('action_schedule', 'create', array(
200 'title' => " job $i",
201 'subject' => "job $i",
202 'entity_value' => $membershipTypeID,
203 'mapping_id' => 4,
204 'start_action_date' => 'membership_join_date',
205 'start_action_offset' => 0,
206 'start_action_condition' => 'before',
207 'start_action_unit' => 'hour',
208 'group_id' => $groupID,
209 'limit_to' => FALSE,
210 ));
211 $this->callAPISuccess('group_contact', 'create', array(
212 'contact_id' => $contactID,
213 'status' => 'Added',
214 'group_id' => $groupID,
215 ));
216 }
217 $result = $this->callAPISuccess('job', 'send_reminder', array());
218 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
219 $this->assertEquals($successfulCronCount, $createTotal);
220 }
221
222 /**
223 * Test scheduled reminders respect limit to (since above identified addition_to handling issue)
224 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
225 * & check that only the chosen one got the reminder
226 */
227 public function testCallSendReminderLimitTo() {
228 $membershipTypeID = $this->membershipTypeCreate();
229 $this->membershipStatusCreate();
230 $createTotal = 3;
231 $groupID = $this->groupCreate(array('name' => 'Texan drawlers', 'title' => 'a...'));
232 for ($i = 1; $i <= $createTotal; $i++) {
233 $contactID = $this->individualCreate();
234 if ($i == 2) {
235 $theChosenOneID = $contactID;
236 }
237 if ($i < 3) {
238 $this->callAPISuccess('group_contact', 'create', array(
239 'contact_id' => $contactID,
240 'status' => 'Added',
241 'group_id' => $groupID,
242 ));
243 }
244 if ($i > 1) {
245 $this->callAPISuccess('membership', 'create', array(
246 'contact_id' => $contactID,
247 'membership_type_id' => $membershipTypeID,
248 'join_date' => '+ 1 hour',
249 )
250 );
251 }
252 }
253 $result = $this->callAPISuccess('action_schedule', 'create', array(
254 'title' => " remind all Texans",
255 'subject' => "drawling renewal",
256 'entity_value' => $membershipTypeID,
257 'mapping_id' => 4,
258 'start_action_date' => 'membership_join_date',
259 'start_action_offset' => 0,
260 'start_action_condition' => 'before',
261 'start_action_unit' => 'hour',
262 'group_id' => $groupID,
263 'limit_to' => TRUE,
264 ));
265 $result = $this->callAPISuccess('job', 'send_reminder', array());
266 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
267 $this->assertEquals($successfulCronCount, 1);
268 $sentToID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
269 $this->assertEquals($sentToID, $theChosenOneID);
270 }
271
272 public function testCallDisableExpiredRelationships() {
273 $individualID = $this->individualCreate();
274 $orgID = $this->organizationCreate();
275 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
276 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array(
277 'return' => 'id',
278 'name_a_b' => 'Employee of',
279 ));
280 $result = $this->callAPISuccess('relationship', 'create', array(
281 'relationship_type_id' => $relationshipTypeID,
282 'contact_id_a' => $individualID,
283 'contact_id_b' => $orgID,
284 'is_active' => 1,
285 'end_date' => 'yesterday',
286 ));
287 $relationshipID = $result['id'];
288 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
289 $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array());
290 $result = $this->callAPISuccess('relationship', 'get', array());
291 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
292 $this->contactDelete($individualID);
293 $this->contactDelete($orgID);
294 }
295
296 /**
297 * @param $op
298 * @param string $objectName
299 * @param int $id
300 * @param array $params
301 */
302 public function hookPreRelationship($op, $objectName, $id, &$params) {
303 if ($op == 'delete') {
304 return;
305 }
306 if ($params['is_active']) {
307 $params['description'] = 'Hooked';
308 }
309 else {
310 $params['description'] = 'Go Go you good thing';
311 }
312 }
313
314 }