fix membershiptypetest
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 *
e7112fa7 34 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
36 *
37 */
38require_once 'CiviTest/CiviUnitTestCase.php';
4cbe18b8
EM
39
40/**
41 * Class api_v3_JobTest
42 */
6a488035 43class api_v3_JobTest extends CiviUnitTestCase {
f39bacdf 44 protected $_apiversion = 3;
b7c9bc4c 45
6a488035
TO
46 public $DBResetRequired = FALSE;
47 public $_entity = 'Job';
ef1672da 48 public $_params = array();
6a488035 49
00be9182 50 public function setUp() {
6a488035 51 parent::setUp();
6510b20a 52 $this->useTransaction(TRUE);
ef1672da 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 );
6a488035
TO
63 }
64
6a488035 65 /**
6d6dc885 66 * Check with no name.
6a488035 67 */
00be9182 68 public function testCreateWithoutName() {
ef1672da 69 $params = array(
5896d037
TO
70 'is_active' => 1,
71 );
6d6dc885 72 $this->callAPIFailure('job', 'create', $params,
f39bacdf 73 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
74 );
75 }
6a488035
TO
76
77 /**
fe482240 78 * Create job with an invalid "run_frequency" value.
6a488035 79 */
00be9182 80 public function testCreateWithInvalidFrequency() {
6a488035 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 /**
6d6dc885 95 * Create job.
6a488035 96 */
00be9182 97 public function testCreate() {
ef1672da 98 $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
ba4a1892 99 $this->assertNotNull($result['values'][0]['id']);
6a488035
TO
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 /**
6d6dc885 108 * Check with empty array.
6a488035 109 */
00be9182 110 public function testDeleteEmpty() {
6a488035 111 $params = array();
d0e1eff2 112 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
113 }
114
115 /**
6d6dc885 116 * Check with No array.
6a488035 117 */
00be9182 118 public function testDeleteParamsNotArray() {
d0e1eff2 119 $result = $this->callAPIFailure('job', 'delete', 'string');
6a488035
TO
120 }
121
122 /**
6d6dc885 123 * Check if required fields are not passed.
6a488035 124 */
00be9182 125 public function testDeleteWithoutRequired() {
6a488035
TO
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 /**
6d6dc885 137 * Check with incorrect required fields.
6a488035 138 */
00be9182 139 public function testDeleteWithIncorrectData() {
6a488035 140 $params = array(
5896d037
TO
141 'id' => 'abcd',
142 );
d0e1eff2 143 $result = $this->callAPIFailure('job', 'delete', $params);
6a488035
TO
144 }
145
146 /**
6d6dc885 147 * Check job delete.
6a488035 148 */
00be9182 149 public function testDelete() {
ef1672da 150 $createResult = $this->callAPISuccess('job', 'create', $this->_params);
6c6e6187 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 /**
5896d037
TO
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 * /*
6c6e6187
TO
168 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
169 */
6a488035 170 public function testCallUpdateGreetingSuccess() {
5896d037 171 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array(
92915c55
TO
172 'gt' => 'postal_greeting',
173 'ct' => 'Individual',
174 ));
6c6e6187 175 }
6a488035
TO
176
177 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
178 $gt = 'postal_greeting,email_greeting,addressee';
179 $ct = 'Individual,Household';
f39bacdf 180 $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
6a488035 181 }
49f8272d 182
d946b676 183 /**
fe482240
EM
184 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited.
185 *
d946b676 186 * Note that this particular test sends the reminders to the additional recipients only
187 * as no real reminder person is configured
188 *
189 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
190 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
191 * 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
192 */
193 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
194 $membershipTypeID = $this->membershipTypeCreate();
0090e3d2 195 $this->membershipStatusCreate();
d946b676 196 $createTotal = 30;
5896d037 197 for ($i = 1; $i <= $createTotal; $i++) {
d946b676 198 $contactID = $this->individualCreate();
199 $groupID = $this->groupCreate(array('name' => $i, 'title' => $i));
200 $result = $this->callAPISuccess('action_schedule', 'create', array(
201 'title' => " job $i",
202 'subject' => "job $i",
203 'entity_value' => $membershipTypeID,
204 'mapping_id' => 4,
205 'start_action_date' => 'membership_join_date',
206 'start_action_offset' => 0,
207 'start_action_condition' => 'before',
208 'start_action_unit' => 'hour',
209 'group_id' => $groupID,
210 'limit_to' => FALSE,
211 ));
5896d037 212 $this->callAPISuccess('group_contact', 'create', array(
92915c55
TO
213 'contact_id' => $contactID,
214 'status' => 'Added',
215 'group_id' => $groupID,
216 ));
d946b676 217 }
218 $result = $this->callAPISuccess('job', 'send_reminder', array());
219 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
220 $this->assertEquals($successfulCronCount, $createTotal);
221 }
222
ff32c128 223 /**
fe482240
EM
224 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
225 *
ff32c128
EM
226 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
227 * & check that only the chosen one got the reminder
228 */
229 public function testCallSendReminderLimitTo() {
230 $membershipTypeID = $this->membershipTypeCreate();
231 $this->membershipStatusCreate();
232 $createTotal = 3;
233 $groupID = $this->groupCreate(array('name' => 'Texan drawlers', 'title' => 'a...'));
5896d037 234 for ($i = 1; $i <= $createTotal; $i++) {
ff32c128 235 $contactID = $this->individualCreate();
5896d037 236 if ($i == 2) {
ff32c128
EM
237 $theChosenOneID = $contactID;
238 }
5896d037
TO
239 if ($i < 3) {
240 $this->callAPISuccess('group_contact', 'create', array(
92915c55
TO
241 'contact_id' => $contactID,
242 'status' => 'Added',
243 'group_id' => $groupID,
244 ));
ff32c128 245 }
5896d037 246 if ($i > 1) {
ff32c128 247 $this->callAPISuccess('membership', 'create', array(
5896d037
TO
248 'contact_id' => $contactID,
249 'membership_type_id' => $membershipTypeID,
250 'join_date' => '+ 1 hour',
ff32c128
EM
251 )
252 );
253 }
254 }
255 $result = $this->callAPISuccess('action_schedule', 'create', array(
256 'title' => " remind all Texans",
257 'subject' => "drawling renewal",
258 'entity_value' => $membershipTypeID,
259 'mapping_id' => 4,
260 'start_action_date' => 'membership_join_date',
261 'start_action_offset' => 0,
262 'start_action_condition' => 'before',
263 'start_action_unit' => 'hour',
264 'group_id' => $groupID,
265 'limit_to' => TRUE,
266 ));
267 $result = $this->callAPISuccess('job', 'send_reminder', array());
268 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
269 $this->assertEquals($successfulCronCount, 1);
270 $sentToID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
271 $this->assertEquals($sentToID, $theChosenOneID);
272 }
273
49f8272d
E
274 public function testCallDisableExpiredRelationships() {
275 $individualID = $this->individualCreate();
276 $orgID = $this->organizationCreate();
277 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
5896d037 278 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array(
92915c55
TO
279 'return' => 'id',
280 'name_a_b' => 'Employee of',
281 ));
49f8272d
E
282 $result = $this->callAPISuccess('relationship', 'create', array(
283 'relationship_type_id' => $relationshipTypeID,
284 'contact_id_a' => $individualID,
285 'contact_id_b' => $orgID,
286 'is_active' => 1,
287 'end_date' => 'yesterday',
288 ));
289 $relationshipID = $result['id'];
290 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
291 $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array());
292 $result = $this->callAPISuccess('relationship', 'get', array());
293 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
294 $this->contactDelete($individualID);
295 $this->contactDelete($orgID);
296 }
297
4cbe18b8
EM
298 /**
299 * @param $op
100fef9d
CW
300 * @param string $objectName
301 * @param int $id
c490a46a 302 * @param array $params
4cbe18b8 303 */
6c6e6187 304 public function hookPreRelationship($op, $objectName, $id, &$params) {
5896d037 305 if ($op == 'delete') {
49f8272d
E
306 return;
307 }
5896d037 308 if ($params['is_active']) {
49f8272d
E
309 $params['description'] = 'Hooked';
310 }
311 else {
312 $params['description'] = 'Go Go you good thing';
313 }
314 }
96025800 315
6a488035 316}