Merge pull request #995 from dpradeep/CRM-10146-commit
[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 $this->callAPISuccess('note', 'get', array());
104 }
105
106 /**
107 * check retrieve note with missing patrameters
108 * Error expected
109 */
110 function testGetWithoutEntityId() {
111 $params = array(
112 'entity_table' => 'civicrm_contact',
113 'version' => 3,
114 );
115 $note = civicrm_api('note', 'get', $params);
116 $this->assertEquals($note['is_error'], 0);
117 }
118
119 /**
120 * check civicrm_note_get
121 */
122 function testGet() {
123 $entityId = $this->_noteID;
124 $params = array(
125 'entity_table' => 'civicrm_contact',
126 'entity_id' => $entityId,
127 'version' => $this->_apiversion,
128 );
129 $result = civicrm_api3_note_get($params);
130 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
131 $this->assertAPISuccess($result, 'in line ' . __LINE__);
132 }
133
134
135 ///////////////// civicrm_note_create methods
136
137 /**
138 * Check create with wrong parameter
139 * Error expected
140 */
141 function testCreateWithWrongParamsType() {
142 $params = 'a string';
143 $result = civicrm_api('note', 'create', $params);
144 $this->assertAPIFailure($result,
145 "In line " . __LINE__
146 );
147 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
148 }
149
150 /**
151 * Check create with empty parameter array
152 * Error Expected
153 */
154 function testCreateWithEmptyNoteField() {
155 $this->_params['note'] = "";
156 $result = civicrm_api('note', 'create', $this->_params);
157 $this->assertAPIFailure($result);
158 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: note');
159 }
160
161 /**
162 * Check create with partial params
163 * Error expected
164 */
165 function testCreateWithoutEntityId() {
166 unset($this->_params['entity_id']);
167 $result = civicrm_api('note', 'create', $this->_params);
168 $this->assertAPIFailure($result);
169 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: entity_id');
170 }
171
172 /**
173 * Check create with partially empty params
174 * Error expected
175 */
176 function testCreateWithEmptyEntityId() {
177 $this->_params['entity_id'] = "";
178 $result = civicrm_api('note', 'create', $this->_params);
179 $this->assertAPIFailure($result);
180 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: entity_id');
181 }
182
183 /**
184 * Check civicrm_note_create
185 */
186 function testCreate() {
187
188 $result = civicrm_api('note', 'create', $this->_params);
189 $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__);
190 $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note', 'in line ' . __LINE__);
191 $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__);
192
193 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
194 $this->assertAPISuccess($result, 'in line ' . __LINE__);
195 $note = array(
196 'id' => $result['id'],
197 'version' => $this->_apiversion,
198 );
199 $this->noteDelete($note);
200 }
201
202 function testCreateWithApostropheInString() {
203 $params = array(
204 'entity_table' => 'civicrm_contact',
205 'entity_id' => $this->_contactID,
206 'note' => "Hello!!! ' testing Note",
207 'contact_id' => $this->_contactID,
208 'modified_date' => '2011-01-31',
209 'subject' => "With a '",
210 'sequential' => 1,
211 'version' => $this->_apiversion,
212 );
213 $result = civicrm_api('Note', 'Create', $params);
214 $this->assertAPISuccess($result, 'in line ' . __LINE__);
215 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note", 'in line ' . __LINE__);
216 $this->assertEquals($result['values'][0]['subject'], "With a '", 'in line ' . __LINE__);
217 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
218
219 //CleanUP
220 $note = array(
221 'id' => $result['id'],
222 'version' => $this->_apiversion,
223 );
224 $this->noteDelete($note);
225 }
226
227 /**
228 * Check civicrm_note_create - tests used of default set to now
229 */
230 function testCreateWithoutModifiedDate() {
231 unset($this->_params['modified_date']);
232 $apiResult = civicrm_api('note', 'create', $this->_params);
233 $this->assertAPISuccess($apiResult);
234 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
235 $this->noteDelete(array(
236 'id' => $apiResult['id'],
237 'version' => $this->_apiversion,
238 ));
239 }
240
241
242 ///////////////// civicrm_note_update methods
243
244 /**
245 * Check update note with wrong params type
246 * Error expected
247 */
248 function testUpdateWithWrongParamsType() {
249 $params = 'a string';
250 $result = civicrm_api('note', 'create', $params);
251 $this->assertAPIFailure($result,
252 "In line " . __LINE__
253 );
254 }
255
256 /**
257 * Check update with empty parameter array
258 * Error expected
259 */
260 function testUpdateWithEmptyParams() {
261 $params = array();
262 $note = $this->callAPIFailure('note', 'create', $params);
263 }
264
265 /**
266 * Check update with missing parameter (contact id)
267 * Error expected
268 */
269 function testUpdateWithoutContactId() {
270 $params = array(
271 'entity_id' => $this->_contactID,
272 'entity_table' => 'civicrm_contact',
273 'version' => $this->_apiversion,
274 );
275 $note = $this->callAPIFailure('note', 'create', $params);
276 $this->assertEquals($note['error_message'], 'Mandatory key(s) missing from params array: note');
277 }
278
279 /**
280 * Check civicrm_note_update
281 */
282 function testUpdate() {
283 $params = array(
284 'id' => $this->_noteID,
285 'contact_id' => $this->_contactID,
286 'note' => 'Note1',
287 'subject' => 'Hello World',
288 'version' => $this->_apiversion,
289 );
290
291 //Update Note
292 civicrm_api('note', 'create', $params);
293 $note = civicrm_api('Note', 'Get', array('version' => 3));
294 $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__);
295 $this->assertEquals($note['is_error'], 0, 'in line ' . __LINE__);
296 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID, 'in line ' . __LINE__);
297 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact', 'in line ' . __LINE__);
298 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject'], 'in line ' . __LINE__);
299 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note'], 'in line ' . __LINE__);
300 }
301
302 ///////////////// civicrm_note_delete methods
303
304 /**
305 * Check delete note with wrong params type
306 * Error expected
307 */
308 function testDeleteWithWrongParamsType() {
309 $params = 'a string';
310 $result = civicrm_api('note', 'delete', $params);
311 $this->assertAPIFailure($result,
312 "In line " . __LINE__
313 );
314 }
315
316 /**
317 * Check delete with empty parametes array
318 * Error expected
319 */
320 function testDeleteWithEmptyParams() {
321 $params = array();
322 $deleteNote = $this->callAPIFailure('note', 'delete', $params);
323 $this->assertEquals($deleteNote['error_message'], 'Mandatory key(s) missing from params array: id');
324 }
325
326 /**
327 * Check delete with wrong id
328 * Error expected
329 */
330 function testDeleteWithWrongID() {
331 $params = array(
332 'id' => 0,
333 'version' => $this->_apiversion,
334 );
335 $deleteNote = $this->callAPIFailure('note', 'delete', $params);
336 $this->assertEquals($deleteNote['error_message'], 'Mandatory key(s) missing from params array: id');
337 }
338
339 /**
340 * Check civicrm_note_delete
341 */
342 function testDelete() {
343 $additionalNote = $this->noteCreate($this->_contactID);
344
345 $params = array(
346 'id' => $additionalNote['id'],
347 'version' => $this->_apiversion,
348 );
349
350 $result = civicrm_api('note', 'delete', $params);
351
352 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
353 $this->assertAPISuccess($result, 'in line ' . __LINE__);
354 }
355 }
356
357 /**
358 * Test civicrm_activity_create() using example code
359 */
360 function testNoteCreateExample() {
361 require_once 'api/v3/examples/NoteCreate.php';
362 $result = UF_match_get_example();
363 $expectedResult = UF_match_get_expectedresult();
364 $this->assertEquals($result, $expectedResult);
365 }
366