Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | */ | |
38 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
4cbe18b8 EM |
39 | |
40 | /** | |
41 | * Class api_v3_JobTest | |
42 | */ | |
6a488035 | 43 | class 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 TO |
49 | |
50 | function setUp() { | |
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 | /** |
100fef9d | 66 | * Check with no name |
6a488035 TO |
67 | */ |
68 | function testCreateWithoutName() { | |
ef1672da | 69 | $params = array( |
70 | 'is_active' => 1, ); | |
f39bacdf | 71 | $result = $this->callAPIFailure('job', 'create', $params, |
72 | 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action' | |
73 | ); | |
74 | } | |
6a488035 TO |
75 | |
76 | /** | |
100fef9d | 77 | * Create job with an invalid "run_frequency" value |
6a488035 TO |
78 | */ |
79 | function testCreateWithInvalidFrequency() { | |
80 | $params = array( | |
6a488035 TO |
81 | 'sequential' => 1, |
82 | 'name' => 'API_Test_Job', | |
83 | 'description' => 'A long description written by hand in cursive', | |
84 | 'run_frequency' => 'Fortnightly', | |
85 | 'api_entity' => 'ApiTestEntity', | |
86 | 'api_action' => 'apitestaction', | |
87 | 'parameters' => 'Semi-formal explanation of runtime job parameters', | |
88 | 'is_active' => 1, | |
89 | ); | |
d0e1eff2 | 90 | $result = $this->callAPIFailure('job', 'create', $params); |
6a488035 TO |
91 | } |
92 | ||
93 | /** | |
100fef9d | 94 | * Create job |
6a488035 TO |
95 | */ |
96 | function testCreate() { | |
ef1672da | 97 | $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__); |
6a488035 TO |
98 | $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__); |
99 | ||
100 | // mutate $params to match expected return value | |
ef1672da | 101 | unset($this->_params['sequential']); |
6a488035 | 102 | //assertDBState compares expected values in $result to actual values in the DB |
ef1672da | 103 | $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params); |
6a488035 TO |
104 | } |
105 | ||
106 | /** | |
100fef9d | 107 | * Check with empty array |
6a488035 TO |
108 | */ |
109 | function testDeleteEmpty() { | |
110 | $params = array(); | |
d0e1eff2 | 111 | $result = $this->callAPIFailure('job', 'delete', $params); |
6a488035 TO |
112 | } |
113 | ||
114 | /** | |
100fef9d | 115 | * Check with No array |
6a488035 TO |
116 | */ |
117 | function testDeleteParamsNotArray() { | |
d0e1eff2 | 118 | $result = $this->callAPIFailure('job', 'delete', 'string'); |
6a488035 TO |
119 | } |
120 | ||
121 | /** | |
100fef9d | 122 | * Check if required fields are not passed |
6a488035 TO |
123 | */ |
124 | function testDeleteWithoutRequired() { | |
125 | $params = array( | |
126 | 'name' => 'API_Test_PP', | |
127 | 'title' => 'API Test Payment Processor', | |
128 | 'class_name' => 'CRM_Core_Payment_APITest', | |
129 | ); | |
130 | ||
d0e1eff2 CW |
131 | $result = $this->callAPIFailure('job', 'delete', $params); |
132 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
133 | } |
134 | ||
135 | /** | |
100fef9d | 136 | * Check with incorrect required fields |
6a488035 TO |
137 | */ |
138 | function testDeleteWithIncorrectData() { | |
139 | $params = array( | |
ef1672da | 140 | 'id' => 'abcd', ); |
d0e1eff2 | 141 | $result = $this->callAPIFailure('job', 'delete', $params); |
6a488035 TO |
142 | } |
143 | ||
144 | /** | |
100fef9d | 145 | * Check job delete |
6a488035 TO |
146 | */ |
147 | function testDelete() { | |
ef1672da | 148 | $createResult = $this->callAPISuccess('job', 'create', $this->_params); |
149 | $params = array('id' => $createResult['id'],); | |
f39bacdf | 150 | $result = $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__, __FILE__); |
ef1672da | 151 | $this->assertAPIDeleted($this->_entity, $createResult['id']); |
6a488035 TO |
152 | } |
153 | ||
154 | /** | |
155 | ||
156 | public function testCallUpdateGreetingMissingParams() { | |
f39bacdf | 157 | $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1)); |
6a488035 TO |
158 | $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']); |
159 | } | |
160 | ||
161 | public function testCallUpdateGreetingIncorrectParams() { | |
f39bacdf | 162 | $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds')); |
6a488035 TO |
163 | $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']); |
164 | } | |
165 | /* | |
166 | * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function | |
167 | */ | |
168 | public function testCallUpdateGreetingSuccess() { | |
f39bacdf | 169 | $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual')); |
6a488035 TO |
170 | } |
171 | ||
172 | public function testCallUpdateGreetingCommaSeparatedParamsSuccess() { | |
173 | $gt = 'postal_greeting,email_greeting,addressee'; | |
174 | $ct = 'Individual,Household'; | |
f39bacdf | 175 | $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct)); |
6a488035 | 176 | } |
49f8272d | 177 | |
d946b676 | 178 | /** |
100fef9d | 179 | * Test the call reminder success sends more than 25 reminders & is not incorrectly limited |
d946b676 | 180 | * Note that this particular test sends the reminders to the additional recipients only |
181 | * as no real reminder person is configured | |
182 | * | |
183 | * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which | |
184 | * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems | |
185 | * 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 | |
186 | */ | |
187 | public function testCallSendReminderSuccessMoreThanDefaultLimit() { | |
188 | $membershipTypeID = $this->membershipTypeCreate(); | |
0090e3d2 | 189 | $this->membershipStatusCreate(); |
d946b676 | 190 | $createTotal = 30; |
191 | for($i = 1; $i <= $createTotal; $i++) { | |
192 | $contactID = $this->individualCreate(); | |
193 | $groupID = $this->groupCreate(array('name' => $i, 'title' => $i)); | |
194 | $result = $this->callAPISuccess('action_schedule', 'create', array( | |
195 | 'title' => " job $i", | |
196 | 'subject' => "job $i", | |
197 | 'entity_value' => $membershipTypeID, | |
198 | 'mapping_id' => 4, | |
199 | 'start_action_date' => 'membership_join_date', | |
200 | 'start_action_offset' => 0, | |
201 | 'start_action_condition' => 'before', | |
202 | 'start_action_unit' => 'hour', | |
203 | 'group_id' => $groupID, | |
204 | 'limit_to' => FALSE, | |
205 | )); | |
206 | $this->callAPISuccess('group_contact', 'create', array('contact_id' => $contactID, 'status' => 'Added', 'group_id' => $groupID)); | |
207 | } | |
208 | $result = $this->callAPISuccess('job', 'send_reminder', array()); | |
209 | $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log"); | |
210 | $this->assertEquals($successfulCronCount, $createTotal); | |
211 | } | |
212 | ||
ff32c128 | 213 | /** |
100fef9d | 214 | * Test scheduled reminders respect limit to (since above identified addition_to handling issue) |
ff32c128 EM |
215 | * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both |
216 | * & check that only the chosen one got the reminder | |
217 | */ | |
218 | public function testCallSendReminderLimitTo() { | |
219 | $membershipTypeID = $this->membershipTypeCreate(); | |
220 | $this->membershipStatusCreate(); | |
221 | $createTotal = 3; | |
222 | $groupID = $this->groupCreate(array('name' => 'Texan drawlers', 'title' => 'a...')); | |
223 | for($i = 1; $i <= $createTotal; $i++) { | |
224 | $contactID = $this->individualCreate(); | |
225 | if($i == 2) { | |
226 | $theChosenOneID = $contactID; | |
227 | } | |
228 | if($i < 3) { | |
229 | $this->callAPISuccess('group_contact', 'create', array('contact_id' => $contactID, 'status' => 'Added', 'group_id' => $groupID)); | |
230 | } | |
231 | if($i > 1) { | |
232 | $this->callAPISuccess('membership', 'create', array( | |
233 | 'contact_id' => $contactID, | |
234 | 'membership_type_id' => $membershipTypeID, | |
235 | 'join_date' => '+ 1 hour', | |
236 | ) | |
237 | ); | |
238 | } | |
239 | } | |
240 | $result = $this->callAPISuccess('action_schedule', 'create', array( | |
241 | 'title' => " remind all Texans", | |
242 | 'subject' => "drawling renewal", | |
243 | 'entity_value' => $membershipTypeID, | |
244 | 'mapping_id' => 4, | |
245 | 'start_action_date' => 'membership_join_date', | |
246 | 'start_action_offset' => 0, | |
247 | 'start_action_condition' => 'before', | |
248 | 'start_action_unit' => 'hour', | |
249 | 'group_id' => $groupID, | |
250 | 'limit_to' => TRUE, | |
251 | )); | |
252 | $result = $this->callAPISuccess('job', 'send_reminder', array()); | |
253 | $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log"); | |
254 | $this->assertEquals($successfulCronCount, 1); | |
255 | $sentToID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_action_log"); | |
256 | $this->assertEquals($sentToID, $theChosenOneID); | |
257 | } | |
258 | ||
49f8272d E |
259 | public function testCallDisableExpiredRelationships() { |
260 | $individualID = $this->individualCreate(); | |
261 | $orgID = $this->organizationCreate(); | |
262 | CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship')); | |
263 | $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array('return' => 'id', 'name_a_b' => 'Employee of')); | |
264 | $result = $this->callAPISuccess('relationship', 'create', array( | |
265 | 'relationship_type_id' => $relationshipTypeID, | |
266 | 'contact_id_a' => $individualID, | |
267 | 'contact_id_b' => $orgID, | |
268 | 'is_active' => 1, | |
269 | 'end_date' => 'yesterday', | |
270 | )); | |
271 | $relationshipID = $result['id']; | |
272 | $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']); | |
273 | $this->callAPISuccess($this->_entity, 'disable_expired_relationships', array()); | |
274 | $result = $this->callAPISuccess('relationship', 'get', array()); | |
275 | $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']); | |
276 | $this->contactDelete($individualID); | |
277 | $this->contactDelete($orgID); | |
278 | } | |
279 | ||
4cbe18b8 EM |
280 | /** |
281 | * @param $op | |
100fef9d CW |
282 | * @param string $objectName |
283 | * @param int $id | |
c490a46a | 284 | * @param array $params |
4cbe18b8 | 285 | */ |
49f8272d E |
286 | function hookPreRelationship($op, $objectName, $id, &$params ) { |
287 | if($op == 'delete') { | |
288 | return; | |
289 | } | |
290 | if($params['is_active']) { | |
291 | $params['description'] = 'Hooked'; | |
292 | } | |
293 | else { | |
294 | $params['description'] = 'Go Go you good thing'; | |
295 | } | |
296 | } | |
6a488035 TO |
297 | } |
298 |