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