ignore system_log in update single test as updating logs doesn't make sense
[civicrm-core.git] / tests / phpunit / api / v3 / NoteTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class contains api test cases for "civicrm_note"
32 *
33 */
34 class api_v3_NoteTest extends CiviUnitTestCase {
35
36 protected $_apiversion;
37 protected $_contactID;
38 protected $_params;
39 protected $_noteID;
40 protected $_note;
41
42
43 function __construct() {
44 parent::__construct();
45 }
46
47 function get_info() {
48 return array(
49 'name' => 'Note Create',
50 'description' => 'Test all Note Create API methods.',
51 'group' => 'CiviCRM API Tests',
52 );
53 }
54
55 function setUp() {
56
57 $this->_apiversion = 3;
58 // Connect to the database
59 parent::setUp();
60
61 $this->_contactID = $this->organizationCreate(NULL);
62
63 $this->_params = array(
64 'entity_table' => 'civicrm_contact',
65 'entity_id' => $this->_contactID,
66 'note' => 'Hello!!! m testing Note',
67 'contact_id' => $this->_contactID,
68 'modified_date' => '2011-01-31',
69 'subject' => 'Test Note', );
70 $this->_note = $this->noteCreate($this->_contactID);
71 $this->_noteID = $this->_note['id'];
72 }
73
74 function tearDown() {
75 $tablesToTruncate = array(
76 'civicrm_note', 'civicrm_contact',
77 );
78 $this->quickCleanup($tablesToTruncate);
79 }
80
81 ///////////////// civicrm_note_get methods
82
83 /**
84 * check retrieve note with empty parameter array
85 * Error expected
86 */
87 function testGetWithEmptyParams() {
88 $this->callAPISuccess('note', 'get', array());
89 }
90
91 /**
92 * check retrieve note with missing patrameters
93 * Error expected
94 */
95 function testGetWithoutEntityId() {
96 $params = array(
97 'entity_table' => 'civicrm_contact',
98 );
99 $note = $this->callAPISuccess('note', 'get', $params);
100 }
101
102 /**
103 * check civicrm_note_get
104 */
105 function testGet() {
106 $entityId = $this->_noteID;
107 $params = array(
108 'entity_table' => 'civicrm_contact',
109 'entity_id' => $entityId,
110 );
111 $result = $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__);
112 }
113
114
115 ///////////////// civicrm_note_create methods
116
117 /**
118 * Check create with empty parameter array
119 * Error Expected
120 */
121 function testCreateWithEmptyNoteField() {
122 $this->_params['note'] = "";
123 $result = $this->callAPIFailure('note', 'create', $this->_params,
124 'Mandatory key(s) missing from params array: note');
125 }
126
127 /**
128 * Check create with partial params
129 * Error expected
130 */
131 function testCreateWithoutEntityId() {
132 unset($this->_params['entity_id']);
133 $result = $this->callAPIFailure('note', 'create', $this->_params,
134 'Mandatory key(s) missing from params array: entity_id');
135 }
136
137 /**
138 * Check create with partially empty params
139 * Error expected
140 */
141 function testCreateWithEmptyEntityId() {
142 $this->_params['entity_id'] = "";
143 $result = $this->callAPIFailure('note', 'create', $this->_params,
144 'Mandatory key(s) missing from params array: entity_id');
145 }
146
147 /**
148 * Check civicrm_note_create
149 */
150 function testCreate() {
151
152 $result = $this->callAPIAndDocument('note', 'create', $this->_params, __FUNCTION__, __FILE__);
153 $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note', 'in line ' . __LINE__);
154 $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__);
155
156 $this->assertArrayHasKey('id', $result);
157 $note = array(
158 'id' => $result['id'], );
159 $this->noteDelete($note);
160 }
161
162 function testCreateWithApostropheInString() {
163 $params = array(
164 'entity_table' => 'civicrm_contact',
165 'entity_id' => $this->_contactID,
166 'note' => "Hello!!! ' testing Note",
167 'contact_id' => $this->_contactID,
168 'modified_date' => '2011-01-31',
169 'subject' => "With a '",
170 'sequential' => 1, );
171 $result = $this->callAPISuccess('Note', 'Create', $params);
172 $this->assertAPISuccess($result, 'in line ' . __LINE__);
173 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note", 'in line ' . __LINE__);
174 $this->assertEquals($result['values'][0]['subject'], "With a '", 'in line ' . __LINE__);
175 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
176
177 //CleanUP
178 $note = array(
179 'id' => $result['id'], );
180 $this->noteDelete($note);
181 }
182
183 /**
184 * Check civicrm_note_create - tests used of default set to now
185 */
186 function testCreateWithoutModifiedDate() {
187 unset($this->_params['modified_date']);
188 $apiResult = $this->callAPISuccess('note', 'create', $this->_params);
189 $this->assertAPISuccess($apiResult);
190 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
191 $this->noteDelete(array(
192 'id' => $apiResult['id'], ));
193 }
194
195
196 ///////////////// civicrm_note_update methods
197
198 /**
199 * Check update with empty parameter array
200 * Please don't copy & paste this - is of marginal value
201 * better to put time into the function on Syntax Conformance class that tests this
202 */
203 function testUpdateWithEmptyParams() {
204 $note = $this->callAPIFailure('note', 'create', array());
205 }
206
207 /**
208 * Check update with missing parameter (contact id)
209 * Error expected
210 */
211 function testUpdateWithoutContactId() {
212 $params = array(
213 'entity_id' => $this->_contactID,
214 'entity_table' => 'civicrm_contact', );
215 $note = $this->callAPIFailure('note', 'create', $params,
216 'Mandatory key(s) missing from params array: note'
217 );
218 }
219
220 /**
221 * Check civicrm_note_update
222 */
223 function testUpdate() {
224 $params = array(
225 'id' => $this->_noteID,
226 'contact_id' => $this->_contactID,
227 'note' => 'Note1',
228 'subject' => 'Hello World', );
229
230 //Update Note
231 $this->callAPISuccess('note', 'create', $params);
232 $note = $this->callAPISuccess('Note', 'Get', array());
233 $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__);
234 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID, 'in line ' . __LINE__);
235 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact', 'in line ' . __LINE__);
236 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject'], 'in line ' . __LINE__);
237 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note'], 'in line ' . __LINE__);
238 }
239
240 ///////////////// civicrm_note_delete methods
241
242
243 /**
244 * Check delete with empty parametes array
245 * Error expected
246 */
247 function testDeleteWithEmptyParams() {
248 $deleteNote = $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id');
249 }
250
251 /**
252 * Check delete with wrong id
253 * Error expected
254 */
255 function testDeleteWithWrongID() {
256 $params = array(
257 'id' => 0,
258 );
259 $deleteNote = $this->callAPIFailure('note', 'delete', $params
260 , 'Mandatory key(s) missing from params array: id');
261 }
262
263 /**
264 * Check civicrm_note_delete
265 */
266 function testDelete() {
267 $additionalNote = $this->noteCreate($this->_contactID);
268
269 $params = array(
270 'id' => $additionalNote['id'],
271 );
272
273 $result = $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
274 }
275 }
276
277 /**
278 * Test civicrm_activity_create() using example code
279 */
280 function testNoteCreateExample() {
281 require_once 'api/v3/examples/Note/Create.php';
282 $result = UF_match_get_example();
283 $expectedResult = UF_match_get_expectedresult();
284 $this->assertEquals($result, $expectedResult);
285 }
286