5d451a6f506f8f3091aeca7074a4ae3a0f87e18e
[civicrm-core.git] / tests / phpunit / api / v3 / NoteTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class contains api test cases for "civicrm_note"
33 *
34 */
35 class api_v3_NoteTest extends CiviUnitTestCase {
36
37 protected $_apiversion;
38 protected $_contactID;
39 protected $_params;
40 protected $_noteID;
41 protected $_note;
42 public $_eNoticeCompliant = TRUE;
43
44 function __construct() {
45 parent::__construct();
46 }
47
48 function get_info() {
49 return array(
50 'name' => 'Note Create',
51 'description' => 'Test all Note Create API methods.',
52 'group' => 'CiviCRM API Tests',
53 );
54 }
55
56 function setUp() {
57
58 $this->_apiversion = 3;
59 // Connect to the database
60 parent::setUp();
61
62 $this->_contactID = $this->organizationCreate(NULL);
63
64 $this->_params = array(
65 'entity_table' => 'civicrm_contact',
66 'entity_id' => $this->_contactID,
67 'note' => 'Hello!!! m testing Note',
68 'contact_id' => $this->_contactID,
69 'modified_date' => '2011-01-31',
70 'subject' => 'Test Note',
71 'version' => $this->_apiversion,
72 );
73 $this->_note = $this->noteCreate($this->_contactID);
74 $this->_noteID = $this->_note['id'];
75 }
76
77 function tearDown() {
78 $tablesToTruncate = array(
79 'civicrm_note', 'civicrm_contact',
80 );
81 $this->quickCleanup($tablesToTruncate);
82 }
83
84 ///////////////// civicrm_note_get methods
85
86 /**
87 * check retrieve note with wrong params type
88 * Error Expected
89 */
90 function testGetWithWrongParamsType() {
91 $params = 'a string';
92 $result = civicrm_api('note', 'get', $params);
93 $this->assertAPIFailure($result,
94 "In line " . __LINE__
95 );
96 }
97
98 /**
99 * check retrieve note with empty parameter array
100 * Error expected
101 */
102 function testGetWithEmptyParams() {
103 $params = array();
104 $note = civicrm_api('note', 'get', $params);
105 $this->assertEquals($note['is_error'], 1);
106 }
107
108 /**
109 * check retrieve note with missing patrameters
110 * Error expected
111 */
112 function testGetWithoutEntityId() {
113 $params = array(
114 'entity_table' => 'civicrm_contact',
115 'version' => 3,
116 );
117 $note = civicrm_api('note', 'get', $params);
118 $this->assertEquals($note['is_error'], 0);
119 }
120
121 /**
122 * check civicrm_note_get
123 */
124 function testGet() {
125 $entityId = $this->_noteID;
126 $params = array(
127 'entity_table' => 'civicrm_contact',
128 'entity_id' => $entityId,
129 'version' => $this->_apiversion,
130 );
131 $result = civicrm_api3_note_get($params);
132 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
133 $this->assertAPISuccess($result, 'in line ' . __LINE__);
134 }
135
136
137 ///////////////// civicrm_note_create methods
138
139 /**
140 * Check create with wrong parameter
141 * Error expected
142 */
143 function testCreateWithWrongParamsType() {
144 $params = 'a string';
145 $result = civicrm_api('note', 'create', $params);
146 $this->assertAPIFailure($result,
147 "In line " . __LINE__
148 );
149 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
150 }
151
152 /**
153 * Check create with empty parameter array
154 * Error Expected
155 */
156 function testCreateWithEmptyNoteField() {
157 $this->_params['note'] = "";
158 $result = civicrm_api('note', 'create', $this->_params);
159 $this->assertAPIFailure($result);
160 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: note');
161 }
162
163 /**
164 * Check create with partial params
165 * Error expected
166 */
167 function testCreateWithoutEntityId() {
168 unset($this->_params['entity_id']);
169 $result = civicrm_api('note', 'create', $this->_params);
170 $this->assertAPIFailure($result);
171 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: entity_id');
172 }
173
174 /**
175 * Check create with partially empty params
176 * Error expected
177 */
178 function testCreateWithEmptyEntityId() {
179 $this->_params['entity_id'] = "";
180 $result = civicrm_api('note', 'create', $this->_params);
181 $this->assertAPIFailure($result);
182 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: entity_id');
183 }
184
185 /**
186 * Check civicrm_note_create
187 */
188 function testCreate() {
189
190 $result = civicrm_api('note', 'create', $this->_params);
191 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
192 $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note', 'in line ' . __LINE__);
193 $this->assertEquals(date('Y-m-d', strtotime($this->_params['modified_date'])), date('Y-m-d', strtotime($result['values'][$result['id']]['modified_date'])), 'in line ' . __LINE__);
194
195 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
196 $this->assertAPISuccess($result, 'in line ' . __LINE__);
197 $note = array(
198 'id' => $result['id'],
199 'version' => $this->_apiversion,
200 );
201 $this->noteDelete($note);
202 }
203
204 function testCreateWithApostropheInString() {
205 $params = array(
206 'entity_table' => 'civicrm_contact',
207 'entity_id' => $this->_contactID,
208 'note' => "Hello!!! ' testing Note",
209 'contact_id' => $this->_contactID,
210 'modified_date' => '2011-01-31',
211 'subject' => "With a '",
212 'sequential' => 1,
213 'version' => $this->_apiversion,
214 );
215 $result = civicrm_api('Note', 'Create', $params);
216 $this->assertAPISuccess($result, 'in line ' . __LINE__);
217 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note", 'in line ' . __LINE__);
218 $this->assertEquals($result['values'][0]['subject'], "With a '", 'in line ' . __LINE__);
219 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
220
221 //CleanUP
222 $note = array(
223 'id' => $result['id'],
224 'version' => $this->_apiversion,
225 );
226 $this->noteDelete($note);
227 }
228
229 /**
230 * Check civicrm_note_create - tests used of default set to now
231 */
232 function testCreateWithoutModifiedDate() {
233 unset($this->_params['modified_date']);
234 $apiResult = civicrm_api('note', 'create', $this->_params);
235 $this->assertAPISuccess($apiResult);
236 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
237 $this->noteDelete(array(
238 'id' => $apiResult['id'],
239 'version' => $this->_apiversion,
240 ));
241 }
242
243
244 ///////////////// civicrm_note_update methods
245
246 /**
247 * Check update note with wrong params type
248 * Error expected
249 */
250 function testUpdateWithWrongParamsType() {
251 $params = 'a string';
252 $result = civicrm_api('note', 'create', $params);
253 $this->assertAPIFailure($result,
254 "In line " . __LINE__
255 );
256 }
257
258 /**
259 * Check update with empty parameter array
260 * Error expected
261 */
262 function testUpdateWithEmptyParams() {
263 $params = array();
264 $note = civicrm_api('note', 'create', $params);
265 $this->assertEquals($note['is_error'], 1);
266 }
267
268 /**
269 * Check update with missing parameter (contact id)
270 * Error expected
271 */
272 function testUpdateWithoutContactId() {
273 $params = array(
274 'entity_id' => $this->_contactID,
275 'entity_table' => 'civicrm_contact',
276 'version' => $this->_apiversion,
277 );
278 $note = civicrm_api('note', 'create', $params);
279 $this->assertEquals($note['is_error'], 1);
280 $this->assertEquals($note['error_message'], 'Mandatory key(s) missing from params array: note');
281 }
282
283 /**
284 * Check civicrm_note_update
285 */
286 function testUpdate() {
287 $params = array(
288 'id' => $this->_noteID,
289 'contact_id' => $this->_contactID,
290 'note' => 'Note1',
291 'subject' => 'Hello World',
292 'version' => $this->_apiversion,
293 );
294
295 //Update Note
296 civicrm_api('note', 'create', $params);
297 $note = civicrm_api('Note', 'Get', array('version' => 3));
298 $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__);
299 $this->assertEquals($note['is_error'], 0, 'in line ' . __LINE__);
300 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID, 'in line ' . __LINE__);
301 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact', 'in line ' . __LINE__);
302 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject'], 'in line ' . __LINE__);
303 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note'], 'in line ' . __LINE__);
304 }
305
306 ///////////////// civicrm_note_delete methods
307
308 /**
309 * Check delete note with wrong params type
310 * Error expected
311 */
312 function testDeleteWithWrongParamsType() {
313 $params = 'a string';
314 $result = civicrm_api('note', 'delete', $params);
315 $this->assertAPIFailure($result,
316 "In line " . __LINE__
317 );
318 }
319
320 /**
321 * Check delete with empty parametes array
322 * Error expected
323 */
324 function testDeleteWithEmptyParams() {
325 $params = array();
326 $deleteNote = civicrm_api('note', 'delete', $params);
327 $this->assertEquals($deleteNote['is_error'], 1);
328 $this->assertEquals($deleteNote['error_message'], 'Mandatory key(s) missing from params array: version, id');
329 }
330
331 /**
332 * Check delete with wrong id
333 * Error expected
334 */
335 function testDeleteWithWrongID() {
336 $params = array(
337 'id' => 0,
338 'version' => $this->_apiversion,
339 );
340 $deleteNote = civicrm_api('note', 'delete', $params);
341 $this->assertEquals($deleteNote['is_error'], 1);
342 $this->assertEquals($deleteNote['error_message'], 'Mandatory key(s) missing from params array: id');
343 }
344
345 /**
346 * Check civicrm_note_delete
347 */
348 function testDelete() {
349 $additionalNote = $this->noteCreate($this->_contactID);
350
351 $params = array(
352 'id' => $additionalNote['id'],
353 'version' => $this->_apiversion,
354 );
355
356 $result = civicrm_api('note', 'delete', $params);
357
358 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
359 $this->assertAPISuccess($result, 'in line ' . __LINE__);
360 }
361 }
362
363 /**
364 * Test civicrm_activity_create() using example code
365 */
366 function testNoteCreateExample() {
367 require_once 'api/v3/examples/NoteCreate.php';
368 $result = UF_match_get_example();
369 $expectedResult = UF_match_get_expectedresult();
370 $this->assertEquals($result, $expectedResult);
371 }
372